Files
Code
Module: mMain
'Demonstrates how to get the components of the active assembly or a
' specified sub-assembly in order without running into the bug
' involving IFeature::GetSpecificFeature2 that is described here:
' http://www.evernote.com/l/APCL8bOUV35A1Kl7seZYoVVsA5QTOO1IWeY/
'Preconditions:
'1. Assembly is open
'2. Select nothing if you want the children from the active assembly.
' Select a sub-assembly if you want the children from that sub-
' assembly only.
'Notes:
'-For the first parameter in GetComponents, you must pass in the
' the pointer to a model that is active, otherwise the aforementioned
' bug can occur.
'-When implementing this in your final program, it would be wise to test
' the number of components returned by GetComponenents against the
' number returned by IAssemblyDoc::GetComponentsCount (which mirrors
' the number given in AssemblyXpert / Performance Evalulation.
Const TOP_LEVEL_ONLY = False
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim vComps As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swComp = swSelMgr.GetSelectedObject6(1, -1)
vComps = mComponentUtility.GetComponents(swModel, swComp, TOP_LEVEL_ONLY)
If swComp Is Nothing Then
Set swAssy = swModel
Else
Set swCompModel = swComp.GetModelDoc2
Set swAssy = swCompModel
End If
PrintResults swAssy, vComps
End Sub
Private Sub PrintResults(swAssy As SldWorks.AssemblyDoc, vComps As Variant)
If IsEmpty(vComps) Then
Debug.Print "Empty"
Exit Sub
End If
Debug.Print "Assembly component count: " & swAssy.GetComponentCount(TOP_LEVEL_ONLY)
Debug.Print "Returned component count: " & CStr(UBound(vComps) + 1)
Debug.Print "Names of returned components:"
Dim i As Integer
For i = 0 To UBound(vComps)
Debug.Print vbTab & vComps(i).Name2
Next i
End Sub
Module: mComponentUtility
Option Compare Text
Function GetComponents(model As SldWorks.ModelDoc2, targetAssembly As SldWorks.Component2, _
topLevelOnly As Boolean) As Variant
Dim intTargetAssemblyLevel As Integer
Dim strComponentName As String
If targetAssembly Is Nothing Then
intTargetAssemblyLevel = 0
strComponentName = Empty
Else
intTargetAssemblyLevel = GetLevel(targetAssembly)
strComponentName = targetAssembly.Name2
End If
Dim swComps() As SldWorks.Component2
ReDim swComps(0)
TraverseModel strComponentName, swComps, model, intTargetAssemblyLevel, topLevelOnly
If swComps(0) Is Nothing Then
GetComponents = Empty
Else
GetComponents = swComps
End If
End Function
Private Sub TraverseModel(strComponentName As String, ByRef swComps() As SldWorks.Component2, _
model As Object, intTargetAssemblyLevel As Integer, topLevelOnly As Boolean)
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swComp As SldWorks.Component2
Set swFeat = model.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName = "Reference" Then
ComponentHandler swComp, swFeat, strComponentName, swComps, intTargetAssemblyLevel, _
topLevelOnly
Else
Set swSubFeat = swFeat.GetFirstSubFeature
Do While Not swSubFeat Is Nothing
If swSubFeat.GetTypeName = "ReferencePattern" Then _
ComponentHandler swComp, swSubFeat, strComponentName, swComps, _
intTargetAssemblyLevel, topLevelOnly
Set swSubFeat = swSubFeat.GetNextSubFeature
Loop
End If
Set swFeat = swFeat.GetNextFeature
Loop
End Sub
Private Sub ComponentHandler(swComp As SldWorks.Component2, swFeat As SldWorks.Feature, _
strComponentName As String, ByRef swComps() As SldWorks.Component2, _
intTargetAssemblyLevel As Integer, topLevelOnly As Boolean)
Set swComp = swFeat.GetSpecificFeature2
If swComp Is Nothing Then Exit Sub
If IsChild(strComponentName, swComp) Then
If Not swComps(0) Is Nothing Then ReDim Preserve swComps(UBound(swComps) + 1)
Set swComps(UBound(swComps)) = swComp
End If
If GetLevel(swComp) - intTargetAssemblyLevel < 1 Or topLevelOnly = False Then _
TraverseModel strComponentName, swComps, swComp, intTargetAssemblyLevel, topLevelOnly
End Sub
Private Function IsChild(ByVal strComponentName As String, swComp As SldWorks.Component2) As Boolean
If strComponentName <> Empty Then strComponentName = strComponentName & "/"
If InStr(1, swComp.Name2, strComponentName) = 1 Then
IsChild = True
Else
IsChild = False
End If
End Function
Private Function GetLevel(swComp As SldWorks.Component2) As Integer
GetLevel = UBound(Split(swComp.Name2, "/")) + 1
End Function