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: Part or assembly is open.
'
' Postconditions: None
'
' NOTES: The delay between steps is set to 1 second.
'
'-----------------------------------
Option Explicit
Public Enum swDocumentTypes_e
swDocNONE
= 0
swDocPART
= 1
swDocASSEMBLY
= 2
swDocDRAWING
= 3
End Enum
Public Enum swMoveRollbackBarTo_e
swMoveRollbackBarToEnd
= 1
swMoveRollbackBarToPreviousPosition
= 2
swMoveRollbackBarToBeforeFeature
= 3
swMoveRollbackBarToAfterFeature
= 4
End Enum
Sub main()
'
Delay in seconds
Const
DELAY As
Single = 1#
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swPart As
SldWorks.PartDoc
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swFeatMgr As
SldWorks.FeatureManager
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
SldWorks.feature
Dim
vFeatFace As
Variant
Dim
swFace As
SldWorks.face2
Dim
sFeatName() As
String
Dim
sNow As
Single
Dim
nDocType As
Long
Dim
i As
Long
Dim
j As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swFeatMgr = swModel.FeatureManager
Set
swFeat = swModel.FirstFeature
nDocType
= swModel.GetType
Select
Case nDocType
Case
swDocPART
Set
swPart = swModel
Case
swDocASSEMBLY
Set
swAssy = swModel
End
Select
ReDim
sFeatName(0)
Do
While Not swFeat Is Nothing
sFeatName(UBound(sFeatName))
= swFeat.Name
ReDim
Preserve sFeatName(UBound(sFeatName) + 1)
Set
swFeat = swFeat.GetNextFeature
Loop
'
Loop over-allocates array by 1, so remove last (empty) entry
ReDim
Preserve sFeatName(UBound(sFeatName) - 1)
'
Playback each feature in the FeatureManager design tree
For
i = 0 To UBound(sFeatName)
Debug.Print
sFeatName(i)
bRet
= swFeatMgr.EditRollback(swMoveRollbackBarToAfterFeature,
sFeatName(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
bRet
'
Remove any previous highlights
swModel.GraphicsRedraw2
'
Highlight feature if it has any geometry
Select
Case nDocType
Case
swDocPART
Set
swFeat = swPart.FeatureByName(sFeatName(i))
Case
swDocASSEMBLY
Set
swFeat = swAssy.FeatureByName(sFeatName(i))
End
Select
vFeatFace
= swFeat.GetFaces
If
Not IsEmpty(vFeatFace) Then
For
j = 0 To UBound(vFeatFace)
Set
swFace = vFeatFace(j)
swFace.Highlight True
Next
j
End
If
'
Only pause if rollback is successful
If
bRet Then
sNow
= Timer
While
sNow + DELAY > Timer
'
Allow SolidWorks to refresh screen
DoEvents
Wend
End
If
Next
i
'
Remove highlight from last feature
swModel.GraphicsRedraw2
End Sub
'-----------------------------------