autocad.net使用Ribbon runtime API在实体选择时显示上下文选项卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//by 鸟哥 qq1833183060
//qq群 720924083
//2020-11-10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Geometry;
namespace AcadDemo
{
public class RibbonDemo
{
//参考 https://adndevblog.typepad.com/autocad/2012/04/displaying-a-contextual-tab-upon-entity-selection-using-ribbon-runtime-api.html
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Use: Displays a contextual tab upon entity selection,
// using Ribbon runtime API.
//
// Author: Philippe Leefsma, April 2012
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Autodesk.Windows.RibbonTab _ctxTab = null;
[CommandMethod("CtxTabUponSelect")]
public void CtxTabUponSelect()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
//Set up event for selection changed

doc.ImpliedSelectionChanged +=new EventHandler(doc_ImpliedSelectionChanged);
}

void doc_ImpliedSelectionChanged(object sender, EventArgs e)
{
Document doc = Application.DocumentManager.MdiActiveDocument;

PromptSelectionResult psr = doc.Editor.SelectImplied();
//if no entities are selected, we hide our context tab

if (psr.Value == null)
{
HideTab();
return;
}
//In this example we only display the tab if only circles are

// selected. You may want to change this condition of course.
foreach (SelectedObject selObj in psr.Value)
{
if (selObj.ObjectId.ObjectClass.DxfName.ToLower()!= "circle")
{
HideTab();
return;
}
}
//We will use the Application.Idle event to safely display our tab
if (_ctxTab == null || !_ctxTab.IsVisible)
{
Autodesk.AutoCAD.ApplicationServices.Application.Idle +=new EventHandler(OnIdle);
}
}

void OnIdle(object sender, EventArgs e)
{
//Make sure ribbon manager is available

if (Autodesk.Windows.ComponentManager.Ribbon != null)
{
//Create tab if it doesn't exist

if (_ctxTab == null)
CreateCtxTab();

//Otherwise make it visible

if (!_ctxTab.IsVisible)
{
Autodesk.Windows.RibbonControl ribbonCtrl =Autodesk.Windows.ComponentManager.Ribbon;

ribbonCtrl.ShowContextualTab(_ctxTab, false, true);

_ctxTab.IsActive = true;
}

if (!_ctxTab.IsActive)
_ctxTab.IsActive = true;
}
}

//Tab creation method

void CreateCtxTab()
{
Autodesk.Windows.RibbonControl ribbonCtrl =Autodesk.Windows.ComponentManager.Ribbon;

_ctxTab = new Autodesk.Windows.RibbonTab();

_ctxTab.Name = "MyTab";

_ctxTab.Id = "MY_CTX_TAB_ID";

_ctxTab.IsVisible = true;

_ctxTab.Title = _ctxTab.Name;

_ctxTab.IsContextualTab = true;
ribbonCtrl.Tabs.Add(_ctxTab);

}

void HideTab()
{
Autodesk.AutoCAD.ApplicationServices.Application.Idle-= new EventHandler(OnIdle);

if (_ctxTab != null)
{
Autodesk.Windows.RibbonControl ribbonCtrl=Autodesk.Windows.ComponentManager.Ribbon;

ribbonCtrl.HideContextualTab(_ctxTab);

_ctxTab.IsVisible = false;

_ctxTab = null;

}
}
}
}