<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Traverse assembly and count subassemblies in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8788953#M76010</link>
    <description>&lt;P&gt;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..).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Public Sub TraverseAssemblySample()&lt;BR /&gt;' Get the active assembly.&lt;BR /&gt;Dim oAsmDoc As AssemblyDocument&lt;BR /&gt;Set oAsmDoc = ThisApplication.ActiveDocument&lt;BR /&gt;Debug.Print oAsmDoc.DisplayName&lt;/P&gt;
&lt;P&gt;' Call the function that does the recursion.&lt;BR /&gt;Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)&lt;BR /&gt;End Sub&lt;/P&gt;
&lt;P&gt;Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)&lt;/P&gt;
&lt;P&gt;Dim oOcc As ComponentOccurrence&lt;BR /&gt;'Prints names of components in the top-layer of an assembly.&lt;BR /&gt;For Each oOcc In Occurrences&lt;BR /&gt;' Print the name of the current occurrence.&lt;BR /&gt;Debug.Print Space(Level * 3) &amp;amp; oOcc.Name&lt;BR /&gt;Next&lt;BR /&gt;&lt;BR /&gt;'Restart from the top and traverses Assemblies if they are found.&lt;BR /&gt;For Each oOcc In Occurrences&lt;BR /&gt;If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then&lt;BR /&gt;Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)&lt;BR /&gt;End If&lt;BR /&gt;Next&lt;BR /&gt;End Sub&lt;/P&gt;</description>
    <pubDate>Tue, 14 May 2019 04:48:54 GMT</pubDate>
    <dc:creator>Yadow</dc:creator>
    <dc:date>2019-05-14T04:48:54Z</dc:date>
    <item>
      <title>Traverse assembly and count subassemblies</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8785657#M76007</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Is there a way to traverse the top assembly and count all the unique subassemblies for each subassembly at Level 1 only?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="assembly tree.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/635545i50C31EFC57987E15/image-size/large?v=v2&amp;amp;px=999" role="button" title="assembly tree.png" alt="assembly tree.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2019 09:52:24 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8785657#M76007</guid>
      <dc:creator>fakeru</dc:creator>
      <dc:date>2019-05-12T09:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: Traverse assembly and count subassemblies</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8785766#M76008</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-71992135-2633-4DE3-ACA7-60738CD204E7" target="_blank" rel="noopener"&gt;https://help.autodesk.com/view/INVNTOR/2020/ENU/?guid=GUID-71992135-2633-4DE3-ACA7-60738CD204E7&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 12 May 2019 14:01:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8785766#M76008</guid>
      <dc:creator>ianteneth</dc:creator>
      <dc:date>2019-05-12T14:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Traverse assembly and count subassemblies</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8787288#M76009</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your feedback.&lt;/P&gt;
&lt;P&gt;I have something put together, but there is one major problem. I'm using this code as a base:&lt;/P&gt;
&lt;PRE&gt;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) &amp;amp; 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&lt;/PRE&gt;
&lt;P&gt;The problem is that once it reaches an assembly occurrence, it starts traversing that assembly.&lt;/P&gt;
&lt;P&gt;What I need is the code to traverse one entire assembly, count the subassemblies and they traverse them one by one and so on...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2019 13:07:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8787288#M76009</guid>
      <dc:creator>fakeru</dc:creator>
      <dc:date>2019-05-13T13:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: Traverse assembly and count subassemblies</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8788953#M76010</link>
      <description>&lt;P&gt;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..).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Public Sub TraverseAssemblySample()&lt;BR /&gt;' Get the active assembly.&lt;BR /&gt;Dim oAsmDoc As AssemblyDocument&lt;BR /&gt;Set oAsmDoc = ThisApplication.ActiveDocument&lt;BR /&gt;Debug.Print oAsmDoc.DisplayName&lt;/P&gt;
&lt;P&gt;' Call the function that does the recursion.&lt;BR /&gt;Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1)&lt;BR /&gt;End Sub&lt;/P&gt;
&lt;P&gt;Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer)&lt;/P&gt;
&lt;P&gt;Dim oOcc As ComponentOccurrence&lt;BR /&gt;'Prints names of components in the top-layer of an assembly.&lt;BR /&gt;For Each oOcc In Occurrences&lt;BR /&gt;' Print the name of the current occurrence.&lt;BR /&gt;Debug.Print Space(Level * 3) &amp;amp; oOcc.Name&lt;BR /&gt;Next&lt;BR /&gt;&lt;BR /&gt;'Restart from the top and traverses Assemblies if they are found.&lt;BR /&gt;For Each oOcc In Occurrences&lt;BR /&gt;If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then&lt;BR /&gt;Call TraverseAssembly(oOcc.SubOccurrences, Level + 1)&lt;BR /&gt;End If&lt;BR /&gt;Next&lt;BR /&gt;End Sub&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2019 04:48:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/traverse-assembly-and-count-subassemblies/m-p/8788953#M76010</guid>
      <dc:creator>Yadow</dc:creator>
      <dc:date>2019-05-14T04:48:54Z</dc:date>
    </item>
  </channel>
</rss>

