Create Multiple Undo Command Example (VBA)
This example shows how to create a SolidWorks Undo command to undo multiple
SolidWorks API operations.
'---------------------------------------------------
'
' Preconditions:
' (1)
A part named Cylinder.SLDPRT exists in
' the
specified folder.
' (2)
The part contains four sketches.
'
' Postconditions:
' (1)
Two of sketches are extruded and two
' of
the sketches are cut-extruded.
' (2)
The following items appear in the
' SolidWorks
Undo list in this order:
'
' Boss
' API Undo
' Boss
'
' NOTE: If you select API
Undo in the SolidWorks Undo list:
' (1)
The boss created after the recording of the SolidWorks Undo
' object
ended is undone.
' (2)
The two cut-extrudes created during the recording
' of
the SolidWorks Undo object are undone.
' (3)
The boss created before recording of the SolidWorks Undo
' object
remains.
'
' Just
like in SolidWorks user-interface, all actions above the
' selected
item in the SolidWorks Undo list are reversed.
'
'-------------------------------------------------------
Option Explicit
Sub main()
On Error GoTo ErrorHandler
Dim
swApp As SldWorks.SldWorks
Dim
swModel As SldWorks.ModelDoc2
Dim
swSelMgr As SldWorks.SelectionMgr
Dim
swModelDocExt As SldWorks.ModelDocExtension
Dim
swFeatMgr As SldWorks.FeatureManager
Dim
swModelView As SldWorks.ModelView
Dim
fileerror As Long
Dim
filewarning As Long
Dim
boolstatus As Boolean
Set
swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
swApp.UserControl = True
Set
swModel = swApp.OpenDoc6("E:\samples\undo\Cylinder.SLDPRT",
1, swOpenDocOptions_Silent, "", fileerror, filewarning)
Set
swSelMgr = swModel.SelectionManager
Set
swModelDocExt = swModel.Extension
Set
swFeatMgr = swModel.FeatureManager
Set
swModelView = swModel.ActiveView
swModelView.FrameState = 1
'
Select the sketch before starting recording the SolidWorks Undo object
and extrude it
boolstatus
= swModelDocExt.SelectByID2("Line2@Sketch2",
"EXTSKETCHSEGMENT", -0.0153467762926, 0.006762545597155, 0,
False, 0, Nothing, 0)
swFeatMgr.FeatureExtrusion2 True, False, False,
0, 0, 0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994,
False, False, False, False, 1, 1, 1, 0, 0, False
'
Start recording the SolidWorks Undo object
swModelDocExt.StartRecordingUndoObject
'
Select a sketch and cut-extrude it
boolstatus
= swModelDocExt.SelectByID2("Arc1@Sketch5",
"EXTSKETCHSEGMENT", -0.008417867506009, -0.01009141061654, 0,
False, 0, Nothing, 0)
swFeatMgr.FeatureCut True, False, False, 0, 0,
0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994,
False, False, False, False, 0, 1, 1
'
Select another sketch and cut-extrude it
boolstatus
= swModelDocExt.SelectByID2("Arc1@Sketch3",
"EXTSKETCHSEGMENT", 0.007968395164302, -0.005611927229324, 0,
False, 0, Nothing, 0)
swFeatMgr.FeatureCut True, False, False, 0, 0,
0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994,
False, False, False, False, 0, 1, 1
'
End recording the SolidWorks Undo object
swModelDocExt.FinishRecordingUndoObject ("API
Undo")
'
Select a sketch and extrude it
boolstatus
= swModelDocExt.SelectByID2("Line4@Sketch4",
"EXTSKETCHSEGMENT", 0.001185029149606, 0.01014982014618, 0,
False, 0, Nothing, 0)
swFeatMgr.FeatureExtrusion2 True, False, False,
0, 0, 0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994,
False, False, False, False, 1, 1, 1, 0, 0, False
Exit
Sub
ErrorHandler:
MsgBox
"Error: " & Err.Description
End
End Sub