-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathselectionClass.txt
356 lines (251 loc) · 12.5 KB
/
selectionClass.txt
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
********************************************
' MODULE / SUB CALLING CLASS
********************************************
Public Sub View_GetPoint()
' Create selection process instance
Dim oSC As New Select_ViewPosition
' Get the target point
Dim oTargetPoint As Point2d = oSC.Pick()
' Check to make sure something was selected
If oTargetPoint Is Nothing Then
MessageBox.Show("Target point not selected")
Exit Sub
End If
' Save point to settings
My.Settings.SourceView_X = oTargetPoint.X
My.Settings.SourceView_Y = oTargetPoint.Y
' Save to settings
My.Settings.Save()
' Clear objects
oSC = Nothing
oTargetPoint = Nothing
End Sub
********************************************
' CLASS BELOW HERE
********************************************
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Inventor
' You will need to change this to whatever you are using
Imports TD_DrawingAlign.TD_DrawingAlign.StandardAddInServer
Public Class Select_ViewPosition
Private WithEvents oInteraction As Inventor.InteractionEvents
Private WithEvents oSelectEvents As Inventor.SelectEvents
Private WithEvents oMouseEvents As Inventor.MouseEvents
Private bStillSelecting As Boolean
Public oHighlightedCurveSegment As DrawingCurveSegment ' This is public so calling sub can access directly + get the parent view
Private oIG As InteractionGraphics
Private oCG As ClientGraphics
Private selectedPoint As Inventor.Point2d
Public Sub New()
' Set event objects
oInteraction = ThisApplication.CommandManager.CreateInteractionEvents
oSelectEvents = oInteraction.SelectEvents
oMouseEvents = oInteraction.MouseEvents
' Set graphics objects
oIG = oInteraction.InteractionGraphics
oCG = oIG.OverlayClientGraphics
' Assign nothing to selected point so it at least doesnt return Null
selectedPoint = Nothing
End Sub
Public Function Pick() As Point2d
' Need to work out when the 'on terminated' function runs so it doesnt set the objects to nothing twice on 'cleanup' / escape
' Set selection flag
bStillSelecting = True
oSelectEvents.Enabled = True
oSelectEvents.SingleSelectEnabled = True
' Enable mouse movement events
oMouseEvents.MouseMoveEnabled = True
' Add selection filter
oSelectEvents.AddSelectionFilter(SelectionFilterEnum.kDrawingCurveSegmentFilter)
' Reset / assign objects
oHighlightedCurveSegment = Nothing
' Start interaction
oInteraction.Start()
' Run until selected
While bStillSelecting = True
ThisApplication.UserInterfaceManager.DoEvents()
End While
' Stop interaction + mouse move
oInteraction.Stop()
oMouseEvents.MouseMoveEnabled = False
' Clear objects
oMouseEvents = Nothing
oInteraction = Nothing
Return selectedPoint
End Function
Private Sub oSelectEvents_OnPreSelect(ByRef PreSelectEntity As DrawingCurveSegment, ByRef DoHighlight As Boolean, ByRef MorePreSelectEntities As Inventor.ObjectCollection, SelectionDevice As Inventor.SelectionDeviceEnum, ModelPosition As Inventor.Point, ViewPosition As Inventor.Point2d, View As Inventor.View) Handles oSelectEvents.OnPreSelect
oHighlightedCurveSegment = PreSelectEntity
' Preselect can prevent highlight
' DoHighlight = False
End Sub
Private Sub oSelectEvents_OnSelect(JustSelectedEntities As ObjectsEnumerator, SelectionDevice As SelectionDeviceEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View) Handles oSelectEvents.OnSelect
' Just selected entities is a collection
If JustSelectedEntities.Count = 0 Then
' No objects selected
oHighlightedCurveSegment = Nothing
Exit Sub
Else
' Get first selected item
oHighlightedCurveSegment = JustSelectedEntities.Item(1)
End If
' Exit if no highlughted curve object
If oHighlightedCurveSegment Is Nothing Then Exit Sub
' Circles have start + end point properties; but when the circle is fully closed, it is set to 'nothing'
Dim oTargetPoint As Inventor.Point2d = Nothing
' Check highlighted curve type
Select Case oHighlightedCurveSegment.GeometryType
Case Curve2dTypeEnum.kLineSegmentCurve2d, Curve2dTypeEnum.kBSplineCurve2d
' for some reason thi
oTargetPoint = ClosestPoint(ModelPosition, oHighlightedCurveSegment.StartPoint, oHighlightedCurveSegment.EndPoint)
Case Curve2dTypeEnum.kCircleCurve2d
' Complete circle curve
Dim oCircle As Inventor.Circle2d = oHighlightedCurveSegment.Geometry
'oTargetPoint = ClosestPoint(ModelPosition, oCircle.Center)
oTargetPoint = oCircle.Center
Case Curve2dTypeEnum.kCircularArcCurve2d
' Circular arc curve
Dim oArc As Inventor.Arc2d = oHighlightedCurveSegment.Geometry
oTargetPoint = ClosestPoint(ModelPosition, oArc.StartPoint, oArc.EndPoint, oArc.Center)
Case Else
'ThisApplication.StatusBarText = "Not matching edge type"
Exit Sub
End Select
' Check there is a valid target point
If oTargetPoint Is Nothing Then
MessageBox.Show("No point selected", "User selection")
bStillSelecting = False
Exit Sub
End If
' Assign point to target point
selectedPoint = oTargetPoint
' Set selection flag
bStillSelecting = False
End Sub
Private Sub oInteraction_OnTerminate() Handles oInteraction.OnTerminate
' Do nothing
bStillSelecting = False
' Clear references
oHighlightedCurveSegment = Nothing
' Turn off mouse move monitoring and clear reference just in case
oMouseEvents.MouseMoveEnabled = False
oMouseEvents = Nothing
' Remove any overlay graphics and clear objects
oCG.Delete()
oCG = Nothing
oIG = Nothing
' Set event objects
oInteraction.Stop()
oInteraction = Nothing
' Assign nothing to selected point so it at least doesnt return Null
selectedPoint = Nothing
End Sub
Private Sub oMouseEvents_OnMouseMove(ByVal Button As MouseButtonEnum, ByVal ShiftKeys As ShiftStateEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As Inventor.View) Handles oMouseEvents.OnMouseMove
' The last highlighted point will always be set to this object, so checking distance to point should be done otherwise it will not be de-selected until user hovers over another line
' Exit if no highlighted curve object
If oHighlightedCurveSegment Is Nothing Then Exit Sub
' Circles have start + end point properties; but when the circle is fully closed, it is set to 'nothing'
Dim oTargetPoint As Inventor.Point2d = Nothing
' Check highlighted curve type
Select Case oHighlightedCurveSegment.GeometryType
Case Curve2dTypeEnum.kLineSegmentCurve2d, Curve2dTypeEnum.kBSplineCurve2d
' for some reason thi
oTargetPoint = ClosestPoint(ModelPosition, oHighlightedCurveSegment.StartPoint, oHighlightedCurveSegment.EndPoint)
Case Curve2dTypeEnum.kCircleCurve2d
' Complete circle curve
Dim oCircle As Inventor.Circle2d = oHighlightedCurveSegment.Geometry
'oTargetPoint = ClosestPoint(ModelPosition, oCircle.Center)
oTargetPoint = oCircle.Center
Case Curve2dTypeEnum.kCircularArcCurve2d
' Circular arc curve
Dim oArc As Inventor.Arc2d = oHighlightedCurveSegment.Geometry
oTargetPoint = ClosestPoint(ModelPosition, oArc.StartPoint, oArc.EndPoint, oArc.Center)
Case Else
'ThisApplication.StatusBarText = "Not matching edge type"
Exit Sub
End Select
' Check there is a valid target point, clear it if not
If oTargetPoint Is Nothing Then
' Check there is a node in client graphics first
If oCG.Count = 1 Then
' Clear graphics node
oCG.ItemById(0).Delete()
End If
' Exit sub / no object
Exit Sub
End If
' Show circle on the point
ShowGreenCircle(oTargetPoint)
End Sub
Private Sub ShowGreenCircle(oPoint As Inventor.Point2d)
Dim oGN As Inventor.GraphicsNode
Dim oPG As Inventor.PointGraphics
' Check the client graphics count
If oCG.Count = 0 Then ' For new item
' Add a new graphics node to client grapics
oGN = oCG.AddNode(0)
' Add point graphics to graphics node
oPG = oGN.AddPointGraphics
Else
' Get existing graphics node
oGN = oCG.ItemById(0)
' Get existing point graphics
oPG = oGN.Item(1)
End If
' Create graphics coordinate set
Dim oGCS As Inventor.GraphicsCoordinateSet = oIG.GraphicsDataSets.CreateCoordinateSet(0)
' Create point array
Dim dPoints(3) As Double
dPoints(0) = oPoint.X ' X
dPoints(1) = oPoint.Y ' Y
dPoints(2) = 0 ' Z Always zero on a drawing
' Set graphics node points
oGCS.PutCoordinates(dPoints)
' Set point graphics coordinates
oPG.CoordinateSet = oGCS
' Set point display properties
oPG.PointRenderStyle = Inventor.PointRenderStyleEnum.kEndPointStyle
oPG.BurnThrough = True
' Update view / graphics (This may add to wrong view window only if user has multiple of the same document / split window open which is almost never used.
oIG.UpdateOverlayGraphics(ThisApplication.ActiveDocument.Views(1))
End Sub
Public Function ClosestPoint(ByVal SourcePoint As Inventor.Point, ByVal Point1 As Inventor.Point2d, Optional ByVal Point2 As Inventor.Point2d = Nothing, Optional ByVal Point3_Center As Inventor.Point2d = Nothing) As Inventor.Point2d
' Only add points that are not empty
Dim myList As New List(Of Inventor.Point2d)
' Populate with non empty objects
If Point1 IsNot Nothing Then myList.Add(Point1)
If Point2 IsNot Nothing Then myList.Add(Point2)
If Point3_Center IsNot Nothing Then myList.Add(Point3_Center) ' Only ever set this as center of arc
Dim dRange As Double = 0.5 ' Limiting range as distance in centimetres
' Create closest point object
Dim oClosestPoint As Inventor.Point2d = Nothing ' = myList.Item(0)
Dim dClosestDistance As Double = 99999 ' Use large number for safety
Dim dTempDistance As Double = 0
' Loop through three items
For Each item As Inventor.Point2d In myList
Try
' Calculate distance
dTempDistance = Math.Abs(Math.Sqrt((SourcePoint.X - item.X) ^ 2 + (SourcePoint.Y - item.Y) ^ 2))
' Check it is within range
If dTempDistance > dRange Then
Continue For
End If
' Check value is shorter
If dTempDistance < dClosestDistance Then
' Assign closest point
oClosestPoint = item
' Assign closest distance
dClosestDistance = dTempDistance
End If
Catch ex As Exception
MessageBox.Show("Error trying to calculate " & ControlChars.NewLine & ex.Message, "Closest Point Calculation")
End Try
Next
' Check to see if no closest point found, and if centerpoint is there, then use centerpoint as default
If oClosestPoint Is Nothing And Point3_Center IsNot Nothing Then
oClosestPoint = Point3_Center
End If
' Should be able to get here but return point1 if it happens
Return oClosestPoint
End Function
End Class