forked from CADstudioCZ/Autodesk-Inventor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStandardAddInServer.cs
274 lines (228 loc) · 9.09 KB
/
StandardAddInServer.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Inventor;
namespace SelectionInfo
{
/// <summary>
/// This is the primary AddIn Server class that implements the ApplicationAddInServer interface
/// that all Inventor AddIns are required to implement. The communication between Inventor and
/// the AddIn is via the methods on this interface.
/// </summary>
[GuidAttribute("25119a9c-3557-4ed2-9bec-e184a99835f3")]
public class StandardAddInServer : ApplicationAddInServer
{
private ApplicationEvents applicationEvents;
private string ClientId = "{25119A9C-3557-4ED2-9BEC-E184A99835F3}";
// Inventor application object.
private Inventor.Application inventor;
private DockableWindow selectionInfoWnd;
private PropertyGrid selectionPropertyGrid;
private UserInputEvents userInputEvents;
public StandardAddInServer()
{
}
/// <summary>
/// Gets or sets the selected object for display properties.
/// </summary>
/// <value>
/// The selected object.
/// </value>
private object SelectedObject
{
get => selectionPropertyGrid?.SelectedObject;
set
{
if (selectionPropertyGrid != null)
selectionPropertyGrid.SelectedObject = value;
}
}
/// <summary>
/// Shows the ActiveDocument in the propertyGrid.
/// </summary>
private void ShowActiveDocument()
{
var entity = new DocumentInfo(inventor.ActiveDocument);
SelectedObject = entity;
}
/// <summary>
/// Clear the propertyGrid.
/// </summary>
private void ClearPalette()
{
SelectedObject = null;
}
/// <summary>
/// Shows the sent object in the propertyGrid, provided it is a valid [document type] entity.
/// </summary>
/// <param name="entity">Selected object.</param>
private void ShowEntity(object entity)
{
entity = SelectionInfoSelector.GetSelectionInfo(entity);
if (entity != null)
{
SelectedObject = entity;
}
{
//Leave unchanged
}
}
/// <summary>
/// Show the selected objects (if any) in the propertyGrid.
/// </summary>
private void ShowSelected()
{
var selectSet = inventor.ActiveDocument.SelectSet;
switch (selectSet.Count)
{
case 0:
{
ShowActiveDocument();
break;
}
case 1:
{
var entity = selectSet[1];
ShowEntity(entity);
break;
}
default:
{
ClearPalette();
break;
}
}
}
/// <summary>
/// Handle when a document is activated [opened].
/// </summary>
private void ApplicationEvents_OnActivateDocument(_Document DocumentObject, EventTimingEnum BeforeOrAfter,
NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
ShowSelected();
}
/// <summary>
///Handle when the current document is deactivated [closed].
/// </summary>
private void ApplicationEvents_OnDeactivateDocument(_Document DocumentObject, EventTimingEnum BeforeOrAfter,
NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
if (BeforeOrAfter != EventTimingEnum.kBefore)
return;
ClearPalette();
}
/// <summary>
/// Handle user-selections, choose best course based on how many objects are selected.
/// </summary>
/// <param name="JustSelectedEntities"></param>
/// <param name="MoreSelectedEntities"></param>
/// <param name="SelectionDevice"></param>
/// <param name="ModelPosition"></param>
/// <param name="ViewPosition"></param>
/// <param name="View"></param>
private void UserInputEvents_OnSelect(ObjectsEnumerator JustSelectedEntities,
ref ObjectCollection MoreSelectedEntities, SelectionDeviceEnum SelectionDevice, Point ModelPosition,
Point2d ViewPosition, Inventor.View View)
{
ShowSelected();
}
/// <summary>
/// Handle when the active document changes.
/// </summary>
private void ApplicationEvents_OnDocumentChange(_Document DocumentObject,
EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange,
NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
HandlingCode = HandlingCodeEnum.kEventNotHandled;
if (BeforeOrAfter != EventTimingEnum.kBefore)
return;
if (inventor.ActiveDocumentType == DocumentTypeEnum.kNoDocument)
{
ClearPalette();
}
else
{
ShowSelected();
}
}
/// <summary>
/// Handle when user deselects an item.
/// </summary>
private void UserInputEvents_OnUnSelect(ObjectsEnumerator UnSelectedEntities, SelectionDeviceEnum SelectionDevice, Point ModelPosition, Point2d ViewPosition, Inventor.View View)
{
ShowSelected();
}
#region ApplicationAddInServer Members
/// <summary>
///This method is called by Inventor when it loads the AddIn.
/// The AddInSiteObject provides access to the Inventor Application object.
/// The FirstTime flag indicates if the AddIn is loaded for the first time.
/// </summary>
/// <param name="addInSiteObject"></param>
/// <param name="firstTime"></param>
public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
{
// Initialize AddIn members.
inventor = addInSiteObject.Application;
//Create dockable window
selectionInfoWnd = inventor.UserInterfaceManager.DockableWindows.Add(ClientId,
"SelectionInfo.StandardAddInServer.selectionInfoWnd", "Selection");
selectionInfoWnd.ShowVisibilityCheckBox = true;
//Create propertyGrid control
selectionPropertyGrid = new PropertyGrid();
//Add propertyGrid to dockable window
selectionInfoWnd.AddChild(selectionPropertyGrid.Handle);
//Setup event handlers
userInputEvents = inventor.CommandManager.UserInputEvents;
userInputEvents.OnSelect += UserInputEvents_OnSelect;
userInputEvents.OnUnSelect += UserInputEvents_OnUnSelect;
applicationEvents = inventor.ApplicationEvents;
applicationEvents.OnActivateDocument += ApplicationEvents_OnActivateDocument;
applicationEvents.OnDeactivateDocument += ApplicationEvents_OnDeactivateDocument;
applicationEvents.OnDocumentChange += ApplicationEvents_OnDocumentChange;
}
/// <summary>
/// This method is called by Inventor when the AddIn in unloaded.
/// The AddIn will be unloaded either manually by the user or
/// when the Inventor session is terminated.
/// </summary>
public void Deactivate()
{
//Remove event handlers
userInputEvents.OnSelect -= UserInputEvents_OnSelect;
userInputEvents.OnUnSelect -= UserInputEvents_OnUnSelect;
applicationEvents.OnActivateDocument -= ApplicationEvents_OnActivateDocument;
applicationEvents.OnDeactivateDocument -= ApplicationEvents_OnDeactivateDocument;
applicationEvents.OnDocumentChange -= ApplicationEvents_OnDocumentChange;
//Cleanup selectionPropertyGrid
SelectedObject = null;
selectionPropertyGrid = null;
// Release objects.
applicationEvents = null;
userInputEvents = null;
inventor = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
public void ExecuteCommand(int commandID)
{
// Note:this method is now obsolete, you should use the
// ControlDefinition functionality for implementing commands.
}
public object Automation
{
// This property is provided to allow the AddIn to expose an API
// of its own to other programs. Typically, this would be done by
// implementing the AddIn's API interface in a class and returning
// that class object through this property.
get
{
// TODO: Add ApplicationAddInServer.Automation getter implementation
return null;
}
}
#endregion
}
}