Traverse Components in an Assembly
Traverse Components in an Assembly
(OP)
I am trying to create a treeview of all the components and parts in an assembly. Listed below is the code I used (straight from SW help) and I am wodering why it will only go 2 levels deep. In other words, if you have a part in a subassembly it will return 'SubAssy1/Part1' rather than breaking it up into 2 model names. Any ideas? Thanks.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
TraverseComponent swRootComp, 1
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim i As Long
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
Debug.Print Mid(swChildComp.Name2, 1, Len(swChildComp.Name2) - 2)
Next i
End Sub
CODE
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
TraverseComponent swRootComp, 1
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim i As Long
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
Debug.Print Mid(swChildComp.Name2, 1, Len(swChildComp.Name2) - 2)
Next i
End Sub






RE: Traverse Components in an Assembly
RE: Traverse Components in an Assembly
RE: Traverse Components in an Assembly
RE: Traverse Components in an Assembly
I checked all components and I am still getting the xxx/yyy. I checked the nLevel and is it tunneling through correctly, but it is not displaying just the part as i would expect. Do you get the xxx/yyy as is goes past nLevel = 2 ?
Thanks
RE: Traverse Components in an Assembly
You seem to be confusing the text output of the component names with that of the filenames. There is no code above that references the component FileNames.
I think you are also confusing the text output (which contains 2 references) with a RECURSION level of 2.... in your debug statements, insert at the front a reference to the LEVEL (as passed in the function paremeter). This was originally in the SWX source.
The indentation level output in the DEBUG statements was to emulate a treeview, much like you are already looking for
Also, the code from SWX does NOT check to see if a component is suppressed; you may want to put your own code in there for that...
See my next post for a reworked version...
RE: Traverse Components in an Assembly
' with The FileName and config name of each
' part file. It ignores supressed components
' and envelopes. The indentation corresponds
' to the level of recursion.
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swConf As SldWorks.Configuration
Dim swRootComp As SldWorks.Component2
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swConf = swModel.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
TraverseComponent swRootComp, 1
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim swCompConfig As SldWorks.Configuration
Dim i As Long
Dim ChildCount As Long
Dim PathSpec As String
Dim CfgName As String
vChildComp = swComp.GetChildren
If ChildCount = 0 Then ' could be a part file...
If Not (swComp.IsEnvelope) Or (swComp.IsSuppressed) Then
CfgName = swComp.ReferencedConfiguration
PathSpec = swComp.GetPathName
If CfgName > "" Then
Debug.Print Space$(nLevel * 2), FileNameFromPathSpec(PathSpec & ""), "("; CfgName; ")"
End If
End If
End If
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
TraverseComponent swChildComp, nLevel + 1
Next i
End Sub
Function FileNameFromPathSpec(PathSpec As String) As String
' strips path from FileName
Dim SplitSpec As Variant
Dim NumSplits As Long
SplitSpec = Split(PathSpec, "\")
NumSplits = UBound(SplitSpec)
FileNameFromPathSpec = SplitSpec(NumSplits)
End Function