Hide Table of Contents

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

 

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Create Multiple Undo Command Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2010 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.