Rebuild and Rollback Event Handler Example (VB.NET)
This example shows how to handle the pre-notification
that is fired when an assembly document is rebuilt. It also shows how to handle the post-notification that
is fired when an assembly document is either rebuilt or rolled back.
'
----------------------------------------------------------------------------
' Preconditions: Open:
' <install_dir>\samples\tutorial\cosmosfloxpress\ball valve\ball_valve.sldasm.
'
' Postconditions:
' 1. A dialog pops up with the pre-notification of the rebuild
' (check the taskbar
for the minimized dialog).
' 2. Click OK to close the dialog.
' 3. The assembly is rebuilt.
' 4. A dialog pops up with the post-notification of the rebuild
' (check the
taskbar for the minimized dialog).
' 5. Click OK to close the dialog.
' 6. Move rollback bar to just above Sketch1.
' 7. A dialog pops up with the post-notification of the rebuild
' (check the
taskbar for the minimized dialog).
' 8. Click OK to close the dialog.
' 9. Observe the Immediate Window.
' 10.
After moving the rollback bar several times, click Reset in the IDE
' to
disable the event handler.
' 11.
Check Stop VSTA debugger on macro exit
in
' Tools > Options > System Options
> General.
'
' NOTE:
Do not
save the assembly document,
' as it is used in a SolidWorks tutorial.
'
----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Dim
swModel As ModelDoc2
Dim
swModelDocExt As ModelDocExtension
Public
WithEvents swAssembly As AssemblyDoc
Dim
boolstatus As Boolean
Sub
main()
swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swStopDebuggingVstaOnExit,
False)
swModel
= swApp.ActiveDoc
swModelDocExt
= swModel.Extension
'
Event notification
swAssembly
= swModel
AttachEventHandlers()
'
Rebuild the model
swModel.ForceRebuild3(True)
End
Sub
Sub
AttachEventHandlers()
AttachSWEvents()
End
Sub
Sub
AttachSWEvents()
AddHandler
swAssembly.RegenNotify, AddressOf
Me.swAssembly_RegenNotify
AddHandler
swAssembly.RegenPostNotify2, AddressOf
Me.swAssembly_RegenPostNotify2
End
Sub
Private
Function swAssembly_RegenNotify() As Integer
'Display
message before a rebuild
MsgBox("A
rebuild pre-notification event has been fired.")
End
Function
Private
Function swAssembly_RegenPostNotify2(ByVal stopFeature As Object) As Integer
'Display
message after a rebuild or a roll back
If
Not stopFeature Is Nothing Then
MsgBox("A
rollback post-notification event has been fired.")
Dim
feature As Feature
feature
= stopFeature
Debug.Print("The
rollback bar is just above " & feature.Name & " in the
FeatureManager design tree.")
Else
MsgBox("A
rebuild post-notification event has been fired.")
End
If
End
Function
Public
swApp As SldWorks
End Class