Hide Table of Contents

Roll Back Model Example (VBA)

This example shows how to step through the FeatureManager design tree of a model by rolling back to each feature in reverse sequence. Running an example like this can provide insight into the design intent of the user.

' Preconditions:
' 1. Open a part or assembly document.
' 2. Open the Immediate window.
' 3. Run the macro.
' Postconditions:
' 1. Observe the FeatureManager design tree
'    while the macro executes.
' 2. Examine the Immediate window to see the
'    names of the features rolled back and forward.
' NOTE: The delay between steps is set to 1 second.
Option Explicit
Sub Main()
    ' Delay in seconds
    Const DELAY                     As Single = 1#    
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.ModelDoc2
    Dim swModelView                 As SldWorks.ModelView
    Dim swPart                      As SldWorks.PartDoc
    Dim swAssy                      As SldWorks.AssemblyDoc
    Dim swFeatMgr                   As SldWorks.FeatureManager
    Dim swFeat                      As SldWorks.Feature
    Dim featFace                    As Variant
    Dim swFace                      As SldWorks.Face2
    Dim rect                        As Variant
    Dim featName()                  As String
    Dim timeNow                     As Single
    Dim docType                     As Long
    Dim i                           As Long
    Dim j                           As Long
    Dim status                      As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swModelView = swModel.ActiveView
    Set rect = Nothing
    Set swFeatMgr = swModel.FeatureManager
    Set swFeat = swModel.FirstFeature
    docType = swModel.GetType
    Select Case docType
        Case swDocPART
            Set swPart = swModel
        Case swDocASSEMBLY
            Set swAssy = swModel
        Case Else
            Debug.Print "Open a part or assembly document, then rerun this macro."
            Exit Sub
    End Select
    ReDim featName(0)
    Do While Not swFeat Is Nothing
        featName(UBound(featName)) = swFeat.Name
        ReDim Preserve featName(UBound(featName) + 1)
        Set swFeat = swFeat.GetNextFeature
    ' Loop over-allocates array by 1, so remove last (empty) entry
    ReDim Preserve featName(UBound(featName) - 1)
    ' Playback each feature in the FeatureManager design tree
    For i = 0 To UBound(featName)
        Debug.Print featName(i)
        status = swFeatMgr.EditRollback(swMoveRollbackBarToAfterFeature, featName(i))
        ' Do not assert because you may be trying to roll back or roll forward
        ' to a feature that cannot be rolled back or forward to; for example,
        ' the Lighting or Annotations folder        
        'Debug.Assert status
        ' Remove any previous highlights
        swModelView.GraphicsRedraw (rect)
        ' Highlight feature if it has any geometry
        Select Case docType
            Case swDocPART
                Set swFeat = swPart.FeatureByName(featName(i))
            Case swDocASSEMBLY
                Set swFeat = swAssy.FeatureByName(featName(i))
        End Select
        featFace = swFeat.GetFaces
        If Not IsEmpty(featFace) Then
            For j = 0 To UBound(featFace)
                Set swFace = featFace(j)
                swFace.Highlight True
            Next j
        End If
        ' Only pause if rollback is successful
        If status Then
            timeNow = Timer
            While timeNow + DELAY > Timer
                ' Allow SOLIDWORKS to refresh screen
        End If
    Next i
    ' Remove highlight from last feature
    swModelView.GraphicsRedraw (rect)
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

Subject:   Feedback on Help Topics
Page:   Roll Back Model Example (VBA)
*   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:


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

Web Help Content Version: API Help (English only) 2016 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.