×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Traverse Components in an Assembly

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.

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

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.

RE: Traverse Components in an Assembly

(OP)
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 :) )

RE: Traverse Components in an Assembly

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.

RE: Traverse Components in an Assembly

(OP)
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

RE: Traverse Components in an Assembly

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

RE: Traverse Components in an Assembly

' 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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources