/SensorenUndAktoren/EventHandling.vb

https://github.com/derralle/SensorenUndAktoren · Visual Basic · 289 lines · 194 code · 86 blank · 9 comment · 0 complexity · 1eccb063d5439d7dd8b4af511c4e9260 MD5 · raw file

  1. Imports SolidWorks.Interop.sldworks
  2. Imports SolidWorks.Interop.swconst
  3. 'Base class for model event handlers
  4. Public Class DocumentEventHandler
  5. Protected openModelViews As New Hashtable()
  6. Protected userAddin As SwAddin
  7. Protected iDocument As ModelDoc2
  8. Protected iSwApp As SldWorks
  9. Overridable Function Init(ByVal sw As SldWorks, ByVal addin As SwAddin, ByVal model As ModelDoc2) As Boolean
  10. End Function
  11. Overridable Function AttachEventHandlers() As Boolean
  12. End Function
  13. Overridable Function DetachEventHandlers() As Boolean
  14. End Function
  15. Function ConnectModelViews() As Boolean
  16. Dim iModelView As ModelView
  17. iModelView = iDocument.GetFirstModelView()
  18. While (Not iModelView Is Nothing)
  19. If Not openModelViews.Contains(iModelView) Then
  20. Dim mView As New DocView()
  21. mView.Init(userAddin, iModelView, Me)
  22. mView.AttachEventHandlers()
  23. openModelViews.Add(iModelView, mView)
  24. End If
  25. iModelView = iModelView.GetNext
  26. End While
  27. End Function
  28. Function DisconnectModelViews() As Boolean
  29. 'Close events on all currently open docs
  30. Dim mView As DocView
  31. Dim key As ModelView
  32. Dim numKeys As Integer
  33. numKeys = openModelViews.Count
  34. Dim keys() As Object = New Object(numKeys - 1) {}
  35. 'Remove all ModelView event handlers
  36. openModelViews.Keys.CopyTo(keys, 0)
  37. For Each key In keys
  38. mView = openModelViews.Item(key)
  39. mView.DetachEventHandlers()
  40. openModelViews.Remove(key)
  41. mView = Nothing
  42. key = Nothing
  43. Next
  44. End Function
  45. Sub DetachModelViewEventHandler(ByVal mView As ModelView)
  46. Dim docView As DocView
  47. If openModelViews.Contains(mView) Then
  48. docView = openModelViews.Item(mView)
  49. openModelViews.Remove(mView)
  50. mView = Nothing
  51. docView = Nothing
  52. End If
  53. End Sub
  54. End Class
  55. 'Class to listen for Part Events
  56. Public Class PartEventHandler
  57. Inherits DocumentEventHandler
  58. Dim WithEvents iPart As PartDoc
  59. Overrides Function Init(ByVal sw As SldWorks, ByVal addin As SwAddin, ByVal model As ModelDoc2) As Boolean
  60. userAddin = addin
  61. iPart = model
  62. iDocument = iPart
  63. iSwApp = sw
  64. End Function
  65. Overrides Function AttachEventHandlers() As Boolean
  66. AddHandler iPart.DestroyNotify, AddressOf Me.PartDoc_DestroyNotify
  67. AddHandler iPart.NewSelectionNotify, AddressOf Me.PartDoc_NewSelectionNotify
  68. ConnectModelViews()
  69. End Function
  70. Overrides Function DetachEventHandlers() As Boolean
  71. RemoveHandler iPart.DestroyNotify, AddressOf Me.PartDoc_DestroyNotify
  72. RemoveHandler iPart.NewSelectionNotify, AddressOf Me.PartDoc_NewSelectionNotify
  73. DisconnectModelViews()
  74. userAddin.DetachModelEventHandler(iDocument)
  75. End Function
  76. Function PartDoc_DestroyNotify() As Integer
  77. DetachEventHandlers()
  78. End Function
  79. Function PartDoc_NewSelectionNotify() As Integer
  80. End Function
  81. End Class
  82. 'Class to listen for Assembly Events
  83. Public Class AssemblyEventHandler
  84. Inherits DocumentEventHandler
  85. Dim WithEvents iAssembly As AssemblyDoc
  86. Dim swAddin As SwAddin
  87. Overrides Function Init(ByVal sw As SldWorks, ByVal addin As SwAddin, ByVal model As ModelDoc2) As Boolean
  88. userAddin = addin
  89. iAssembly = model
  90. iDocument = iAssembly
  91. iSwApp = sw
  92. swAddin = addin
  93. End Function
  94. Overrides Function AttachEventHandlers() As Boolean
  95. AddHandler iAssembly.DestroyNotify, AddressOf Me.AssemblyDoc_DestroyNotify
  96. AddHandler iAssembly.NewSelectionNotify, AddressOf Me.AssemblyDoc_NewSelectionNotify
  97. AddHandler iAssembly.ComponentStateChangeNotify, AddressOf Me.AssemblyDoc_ComponentStateChangeNotify
  98. AddHandler iAssembly.ComponentStateChangeNotify2, AddressOf Me.AssemblyDoc_ComponentStateChangeNotify2
  99. AddHandler iAssembly.ComponentVisualPropertiesChangeNotify, AddressOf Me.AssemblyDoc_ComponentVisiblePropertiesChangeNotify
  100. AddHandler iAssembly.ComponentDisplayStateChangeNotify, AddressOf Me.AssemblyDoc_ComponentDisplayStateChangeNotify
  101. ConnectModelViews()
  102. End Function
  103. Overrides Function DetachEventHandlers() As Boolean
  104. RemoveHandler iAssembly.DestroyNotify, AddressOf Me.AssemblyDoc_DestroyNotify
  105. RemoveHandler iAssembly.NewSelectionNotify, AddressOf Me.AssemblyDoc_NewSelectionNotify
  106. RemoveHandler iAssembly.ComponentStateChangeNotify, AddressOf Me.AssemblyDoc_ComponentStateChangeNotify
  107. RemoveHandler iAssembly.ComponentStateChangeNotify2, AddressOf Me.AssemblyDoc_ComponentStateChangeNotify2
  108. RemoveHandler iAssembly.ComponentVisualPropertiesChangeNotify, AddressOf Me.AssemblyDoc_ComponentVisiblePropertiesChangeNotify
  109. RemoveHandler iAssembly.ComponentDisplayStateChangeNotify, AddressOf Me.AssemblyDoc_ComponentDisplayStateChangeNotify
  110. DisconnectModelViews()
  111. userAddin.DetachModelEventHandler(iDocument)
  112. End Function
  113. Function AssemblyDoc_DestroyNotify() As Integer
  114. DetachEventHandlers()
  115. End Function
  116. Function AssemblyDoc_NewSelectionNotify() As Integer
  117. End Function
  118. Protected Function ComponentStateChange(ByVal componentModel As Object, Optional ByVal newCompState As Short = swComponentSuppressionState_e.swComponentResolved) As Integer
  119. Dim modDoc As ModelDoc2 = componentModel
  120. Dim newState As swComponentSuppressionState_e = newCompState
  121. Select Case newState
  122. Case swComponentSuppressionState_e.swComponentFullyResolved, swComponentSuppressionState_e.swComponentResolved
  123. If ((Not modDoc Is Nothing) AndAlso Not Me.swAddin.OpenDocumentsTable.Contains(modDoc)) Then
  124. Me.swAddin.AttachModelDocEventHandler(modDoc)
  125. End If
  126. Exit Select
  127. End Select
  128. End Function
  129. 'attach events to a component if it becomes resolved
  130. Public Function AssemblyDoc_ComponentStateChangeNotify(ByVal componentModel As Object, ByVal oldCompState As Short, ByVal newCompState As Short) As Integer
  131. Return ComponentStateChange(componentModel, newCompState)
  132. End Function
  133. 'attach events to a component if it becomes resolved
  134. Public Function AssemblyDoc_ComponentStateChangeNotify2(ByVal componentModel As Object, ByVal CompName As String, ByVal oldCompState As Short, ByVal newCompState As Short) As Integer
  135. Return ComponentStateChange(componentModel, newCompState)
  136. End Function
  137. Public Function AssemblyDoc_ComponentVisiblePropertiesChangeNotify(ByVal swObject As Object) As Integer
  138. Dim component As Component2
  139. Dim modDoc As ModelDoc2
  140. component = swObject
  141. modDoc = component.GetModelDoc
  142. Return ComponentStateChange(modDoc)
  143. End Function
  144. Public Function AssemblyDoc_ComponentDisplayStateChangeNotify(ByVal swObject As Object) As Integer
  145. Dim component As Component2
  146. Dim modDoc As ModelDoc2
  147. component = swObject
  148. modDoc = component.GetModelDoc
  149. Return ComponentStateChange(modDoc)
  150. End Function
  151. End Class
  152. 'Class to listen for Drawing Events
  153. Public Class DrawingEventHandler
  154. Inherits DocumentEventHandler
  155. Dim WithEvents iDrawing As DrawingDoc
  156. Overrides Function Init(ByVal sw As SldWorks, ByVal addin As SwAddin, ByVal model As ModelDoc2) As Boolean
  157. userAddin = addin
  158. iDrawing = model
  159. iDocument = iDrawing
  160. iSwApp = sw
  161. End Function
  162. Overrides Function AttachEventHandlers() As Boolean
  163. AddHandler iDrawing.DestroyNotify, AddressOf Me.DrawingDoc_DestroyNotify
  164. AddHandler iDrawing.NewSelectionNotify, AddressOf Me.DrawingDoc_NewSelectionNotify
  165. ConnectModelViews()
  166. End Function
  167. Overrides Function DetachEventHandlers() As Boolean
  168. RemoveHandler iDrawing.DestroyNotify, AddressOf Me.DrawingDoc_DestroyNotify
  169. RemoveHandler iDrawing.NewSelectionNotify, AddressOf Me.DrawingDoc_NewSelectionNotify
  170. DisconnectModelViews()
  171. userAddin.DetachModelEventHandler(iDocument)
  172. End Function
  173. Function DrawingDoc_DestroyNotify() As Integer
  174. DetachEventHandlers()
  175. End Function
  176. Function DrawingDoc_NewSelectionNotify() As Integer
  177. End Function
  178. End Class
  179. 'Class for handling ModelView events
  180. Public Class DocView
  181. Dim WithEvents iModelView As ModelView
  182. Dim userAddin As SwAddin
  183. Dim parentDoc As DocumentEventHandler
  184. Function Init(ByVal addin As SwAddin, ByVal mView As ModelView, ByVal parent As DocumentEventHandler) As Boolean
  185. userAddin = addin
  186. iModelView = mView
  187. parentDoc = parent
  188. End Function
  189. Function AttachEventHandlers() As Boolean
  190. AddHandler iModelView.DestroyNotify2, AddressOf Me.ModelView_DestroyNotify2
  191. AddHandler iModelView.RepaintNotify, AddressOf Me.ModelView_RepaintNotify
  192. End Function
  193. Function DetachEventHandlers() As Boolean
  194. RemoveHandler iModelView.DestroyNotify2, AddressOf Me.ModelView_DestroyNotify2
  195. RemoveHandler iModelView.RepaintNotify, AddressOf Me.ModelView_RepaintNotify
  196. parentDoc.DetachModelViewEventHandler(iModelView)
  197. End Function
  198. Function ModelView_DestroyNotify2(ByVal destroyTYpe As Integer) As Integer
  199. DetachEventHandlers()
  200. End Function
  201. Function ModelView_RepaintNotify(ByVal repaintTYpe As Integer) As Integer
  202. End Function
  203. End Class