Files
Traverse features in order using recursion
Code
Module: mMain
'Demonstrates how to traverse the feature tree of any SolidWorks document _
in order using a single recursive function.
'Preconditions: Document is open.
'Results: Names and types of all features are printed in the order they appear _
in the feature tree, with exceptions: _
a. Lightweight components or any other type of component not fully loaded _
into memory cannot have some or all features traversed. _
b. Smart fasteners may be listed in the tree after the component they are _
fastened to rather than after the Smart Fasteners folder. _
c. This macro only traverses true features. Not all items listed in the _
feature manager tree are true features. Use IFeature::GetSpecificFeature2 _
to get access to a feature's underlying items.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Debug.Print "Active model: " & swModel.GetTitle
TraverseModel swModel, 1
End Sub
Private Sub TraverseModel(model As Object, nLevel As Integer)
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swComp As SldWorks.Component2
Dim strPad As String
For i = 1 To nLevel
strPad = strPad & vbTab
Next i
Set swFeat = model.FirstFeature
Do While Not swFeat Is Nothing
Debug.Print strPad & swFeat.Name & " (" & swFeat.GetTypeName & ")"
If swFeat.GetTypeName = "Reference" Then
Set swComp = swFeat.GetSpecificFeature2
TraverseModel swComp, nLevel + 1
Else
Set swSubFeat = swFeat.GetFirstSubFeature
Do While Not swSubFeat Is Nothing
Debug.Print strPad & vbTab & swSubFeat.Name & " (" & swSubFeat.GetTypeName & ")"
Set swSubFeat = swSubFeat.GetNextSubFeature
Loop
End If
Set swFeat = swFeat.GetNextFeature
Loop
End Sub