-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainWindowControlDef.vb
223 lines (172 loc) · 9.11 KB
/
MainWindowControlDef.vb
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
Imports System.Reflection
Imports System.Security.Principal
Imports System.Text
Imports System.Text.RegularExpressions
Imports $safeprojectname$.My
Imports Inventor
''' <summary>
''' This is a sample class that demonstrates creating a Control Definition by inheriting from the ControlDefBaseClass. <para/>
''' The ControlDefBaseClass handles the repetitive work of creating/accessing Tabs and Panels as well as adding Control Definitions to specified Panels.
''' You can modify this to fit your needs, or delete it.
''' </summary>
Public Class MainWindowControlDef : Inherits ControlDefBaseClass
'Inventor's button definition for this ControlDef.
Public WithEvents ButtonDef As ButtonDefinition
'The window that this control definition will display/interact with.
Public WithEvents MainWin As MainWindow
'Use Inventor's Transaction manager to set undo checkpoints.
Public TransMan As TransactionManager
Public AddinTransAction As Transaction
'Events to be used in the current ControlDef.
Public WithEvents AppEvents As ApplicationEvents
''' <summary>
''' [Private] Active Inventor Document reference object.
''' </summary>
Private _ActDoc As Document
''' <summary>
''' [Private] Document sub type such as "SolidPart" or "Sheetmetal".
''' </summary>
Private _ComponentType As InventorDocumentSubType
''' <summary>
''' [Private] AddinBaseClass reference object.
''' </summary>
Private _Addin As AddinBaseClass
''' <summary>
''' Creates the ComponentDefinition.
''' </summary>
''' <param name="Addin"></param>
Public Sub New(ByVal Addin As AddinBaseClass)
'Initialize the base class.
'This MUST be called before anything else.
MyBase.New(Addin)
'Populate to the private object.
_Addin = Addin
'Create the balloon tips that will be displayed to the user.
'This is less intrusive than constantly displaying message boxes.
_Addin.InvApp.UserInterfaceManager.BalloonTips.Add("$companysafename$_$addinsafename$_HeyNowBalloonTip", '<---- Company Name, Addin Name, Balloon Tip Name, No Spaces
"Hey Now", '<---- Balloon Tip Display Name, Spaces OK
"Hey Now, This is a balloon tip!"
)
'Hookup the event objects needed for this control.
AppEvents = _Addin.InvApp.ApplicationEvents
'Define the button name & internal name. The Button Name can be anything but the Internal Name must be unique and cannot contain spaces.
'Because there are nearly 2000 button definition already in Inventor I typically create Internal Names by using this format...
'MyCompanyName_MyAddinSafeName_MyButtonName
ButtonName = "$addinname$ Button 1"
LargeIcon = My.Resources.Settings
SmallIcon = My.Resources.Settings_32x32
ButtonInternalName = "$companysafename$_$addinsafename$_MainWindow"
ShowButtonText = True
'General hover text to be displayed when user hovers over the button.
'Note that this text will ignored if you decide to use the progressive tool tip.
ToolTip = "Launch $addinname$."
'This feature is optional. If set to True then your button will display a more elaborate tool tip with a
'title, space for a longer button description, and the option to add an image or video string.
Progressive = False
ProgressiveTitle = Nothing
ProgressiveDesc = Nothing
ProgressiveImage = Nothing
ProgressiveVideo = Nothing
'Create a new list of the ribbons you want to add the button to.
IncludeInRibbons = New List(Of RibbonType)({RibbonType.Part, RibbonType.Assembly})
'Specify the Internal Name of the Ribbon Tab that the button should be included in.
'If Left blank then a control definition will be created but NOT added to UI.
'If a Ribbon Tab with the specified InternalName is found then the existing object is used.
'If a Ribbon Tab with the specified InternalName is not fount then it is created.
'Use the same nomenclature as explained for the ButtonInternalName above.
'<Required if adding button to UI.>
TabInternalName = "$companysafename$_$addinsafename$_Tab"
'Friendly Name of Tab to create if InteranlName is not found. This name can contain spaces.
TabName = "$companyname$"
'Specify the Internal Name of the Ribbon Panel that the button should be included in.
'If Left blank then a control definition will be created but NOT added to UI.
'If a Ribbon Panel with the specified InternalName is found then the existing object is used.
'If a Ribbon Panel with the specified InternalName is not fount then it is created.
'Use the same nomenclature as explained for the ButtonInternalName above.
'<Required if adding button to UI.>
PanelInternalName = "$companysafename$_$addinsafename$_Panel1"
'Friendly Name of Tab to create if InteranlName is not found. This name can contain spaces.
PanelName = "$addinname$"
'Create the control button definition and link it to your Global Private WithEvents button def above.
ButtonDef = CreateControlDefButton()
'Make the call to add the button to the User Interface.
Call AddButtonToUI()
End Sub
''' <summary>
''' Launch the main window for the $addinname$ add-in.
''' </summary>
Private Sub ButtonDefinition_OnExecute(Context As NameValueMap) Handles ButtonDef.OnExecute
Try
'Don't allow multiple instances of the window.
If Not MainWin Is Nothing Then Exit Sub
'Set the Active Document & Active RangeBox
_ActDoc = _Addin.InvApp.ActiveDocument
'Get the active document type.
_ComponentType = _Addin.InvApp.ActiveDocument.GetDocumentSubType
'Get the Active Document.
Select Case True
Case _ComponentType = InventorDocumentSubType.Assembly
_ActDoc = DirectCast(_ActDoc, AssemblyDocument)
Case _ComponentType = InventorDocumentSubType.SolidPart
_ActDoc = DirectCast(_ActDoc, PartDocument)
Case _ComponentType = InventorDocumentSubType.SheetMetal
_ActDoc = DirectCast(_ActDoc, PartDocument)
Case Else
'We don't have a valid document type.
Exit Sub
End Select
'Create the transaction manager and start a transaction.
TransMan = _Addin.InvApp.TransactionManager
AddinTransAction = TransMan.StartTransaction(_ActDoc, "$addinname$")
'Create the window object.
MainWin = New MainWindow(_Addin)
'You could create events in your MainWindow and add handlers here to handle user interactions.
'It would look something like this...
AddHandler MainWin.MyEvent, AddressOf SomeMethodInThisClass
'Now show the window.
MainWin.ShowDialog()
'End the transaction so it is committed.
'We won't get here until the MainWin is closed.
If Not AddinTransAction Is Nothing Then
AddinTransAction.End()
AddinTransAction = Nothing
End If
Catch ex As Exception
'On error we need to abort the transaction if it is uncommitted.
If Not AddinTransAction Is Nothing Then
AddinTransAction.Abort()
AddinTransAction = Nothing
End If
'Log the error.
_Addin.Logging.WriteToErrorLog("There was an error opening the form!", _Addin, ex)
End Try
End Sub
#Region "Inventor Events"
'An example of where you can handle events that this control def has hooked into.
Private Sub AppEvents_OnActivateDocument(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum,
Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles AppEvents.OnActivateDocument
Select Case BeforeOrAfter
Case EventTimingEnum.kBefore
Case EventTimingEnum.kAfter
Case EventTimingEnum.kAbort
End Select
End Sub
Private Sub AppEvents_OnSaveDocument(DocumentObject As _Document, BeforeOrAfter As EventTimingEnum,
Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles AppEvents.OnSaveDocument
Select Case BeforeOrAfter
Case EventTimingEnum.kBefore
Case EventTimingEnum.kAfter
Case EventTimingEnum.kAbort
End Select
End Sub
#End Region
#Region "Sample Event Handler"
''' <summary>
''' Just a sample event handler to show how to handle events from your MainWindow. <para/>
''' You can modify this to fit your needs, or delete it.
''' </summary>
Public Sub SomeMethodInThisClass()
MsgBox("My Main Window Fired An Event!")
End Sub
#End Region
End Class