Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Traverse Components in an Assembly

Status
Not open for further replies.

Creigbm

Mechanical
Aug 1, 2003
161
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.

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
 
Replies continue below

Recommended for you

I would start by putting a "Stop" statement in the "i" loop right after you assign a new value for "swChildComp". Check to see if this is setting as expected.
 
I did a debug.print there and got the filename "xxx/yyy" rather than "xxx". It seems like the code is set up for an infinite number of levels but I guess not (unless SW posted buggy API code which could never happed :) )
 
I have used the code from swx, with additions, on a virtually daily basis for over a year. It recurses just fine. I would check to make sure that the assembly is loaded fully resolved; it wont 'tunnel' down thru lightweight or suppressed components.
 
Rocheey,

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
 
>> 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

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...
 
' Thos code will output an INDENTED tree,
' 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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor