Traverse assembly and count subassemblies

Traverse assembly and count subassemblies

fakeru
Advocate Advocate
1,424 Views
3 Replies
Message 1 of 4

Traverse assembly and count subassemblies

fakeru
Advocate
Advocate

Hi,

Is there a way to traverse the top assembly and count all the unique subassemblies for each subassembly at Level 1 only?

assembly tree.png

Thanks

Autodesk Inventor 2015 Certified Professional
0 Likes
1,425 Views
3 Replies
Replies (3)
Message 2 of 4

ianteneth
Advocate
Advocate

Hi! There are a lot of good posts about this in the forum. There is also a good sample program from Autodesk that traverses an assembly and counts all of the parts (See link below). It uses recursion to traverse the assembly. With this code you should be able to build the program that you need. If you aren't quite sure how to modify the Autodesk code then I can assist with that.

 

As far as only traversing one level deep, I haven't tested this but my first approach would be to create a variable called, for example, depth. And I would update it as I traverse the assembly. When the depth reaches the level you want then you would stop traversing. You would have to pass this variable into the recursive function also I think.

 

And about getting the unique assemblies. I don't think there is one line of code to do that. My approach would be to create a List and as I traverse the assembly and add each subassembly. But before you add it to the list I would check to see if the list already contains a subassembly with that name. Hope this helps!

 

https://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-71992135-2633-4DE3-ACA7-60738CD204E7

Message 3 of 4

fakeru
Advocate
Advocate

Hi, 

Thanks for your feedback.

I have something put together, but there is one major problem. I'm using this code as a base:

Public Sub TraverseAssemblySample() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    Set oAsmDoc = ThisApplication.ActiveDocument 
    Debug.Print oAsmDoc.DisplayName 

    ' Call the function that does the recursion. 
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1) 
End Sub 

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, _ 
                             Level As Integer) 
    ' Iterate through all of the occurrence in this collection.  This 
    ' represents the occurrences at the top level of an assembly. 
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In Occurrences 
        ' Print the name of the current occurrence. 
        Debug.Print Space(Level * 3) & oOcc.Name 

        ' Check to see if this occurrence represents a subassembly 
        ' and recursively call this function to traverse through it. 
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1) 
        End If 
    Next 
End Sub

The problem is that once it reaches an assembly occurrence, it starts traversing that assembly.

What I need is the code to traverse one entire assembly, count the subassemblies and they traverse them one by one and so on... 

Autodesk Inventor 2015 Certified Professional
0 Likes
Message 4 of 4

Yadow
Enthusiast
Enthusiast

Maybe something like this? (I just made a re-make of the code posted above and I don´t know how to make it look as good..).

 

Public Sub TraverseAssemblySample()
' Get the active assembly.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
Debug.Print oAsmDoc.DisplayName

' Call the function that does the recursion.
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)
End Sub

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)

Dim oOcc As ComponentOccurrence
'Prints names of components in the top-layer of an assembly.
For Each oOcc In Occurrences
' Print the name of the current occurrence.
Debug.Print Space(Level * 3) & oOcc.Name
Next

'Restart from the top and traverses Assemblies if they are found.
For Each oOcc In Occurrences
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)
End If
Next
End Sub

0 Likes