<?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: Level of a part in the assembly in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13093585#M636</link>
    <description>&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;I tested your code but it give me to many rows&lt;/P&gt;&lt;P&gt;I want something more compact like the image on the right&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joao_mirandaEFM7E_0-1729262567956.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1423321iCF0E5CBDE4D48D27/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joao_mirandaEFM7E_0-1729262567956.png" alt="joao_mirandaEFM7E_0-1729262567956.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Oct 2024 14:47:06 GMT</pubDate>
    <dc:creator>joao_mirandaEFM7E</dc:creator>
    <dc:date>2024-10-18T14:47:06Z</dc:date>
    <item>
      <title>Level of a part in the assembly</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13090385#M634</link>
      <description>&lt;P&gt;I am developing an API Script to extract a persolalized BOM&lt;/P&gt;&lt;P&gt;But I couth not understand yet how to get Level of a part in the assembly.&lt;/P&gt;&lt;P&gt;Like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joao_mirandaEFM7E_0-1729152945504.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1422651iF72E416C1B525207/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joao_mirandaEFM7E_0-1729152945504.png" alt="joao_mirandaEFM7E_0-1729152945504.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I've tried this link &lt;A href="https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-5a4fa3a6-fa21-11e4-b610-f8b156d7cd97" target="_blank"&gt;https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-5a4fa3a6-fa21-11e4-b610-f8b156d7cd97 , but it's not the same.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 08:36:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13090385#M634</guid>
      <dc:creator>joao_mirandaEFM7E</dc:creator>
      <dc:date>2024-10-17T08:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Level of a part in the assembly</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13091007#M635</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here you have an updated version of the script which replaces the spaces by the levels as you need them:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import adsk.core, adsk.fusion, traceback

# Performs a recursive traversal of an entire assembly structure.
def traverseAssembly(occurrences: adsk.fusion.Occurrences, parentLevel: str, inputString: str) -&amp;gt; str:
    for i in range(0, occurrences.count):
        occ = occurrences.item(i)
        currentLevel = f'{parentLevel}{"." if parentLevel else ""}{i+1}'
        inputString += f'{currentLevel} {occ.name}\n'

        if occ.childOccurrences:
            inputString = traverseAssembly(occ.childOccurrences, currentLevel, inputString)
    return inputString


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get the root component of the active design.
        rootComp = design.rootComponent
        

        # Create the title for the output.
        resultString = 'Root (' + design.parentDocument.name + ')\n'
        
        # Call the recursive function to traverse the assembly and build the output string.
        resultString = traverseAssembly(rootComp.occurrences.asList, "", resultString)

        # Display the result.
        # Write the results to the TEXT COMMANDS window.
        textPalette = ui.palettes.itemById('TextCommands')
        if not textPalette.isVisible:
            textPalette.isVisible = True
        textPalette.writeText(resultString)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My test produces the following result:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Root (Untitled)
1 Component1:1
1.1 Component3:1
1.1.1 Component4:1
1.1.2 Component5:1
2 Component2:1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jorge Jaramillo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Oct 2024 12:38:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13091007#M635</guid>
      <dc:creator>Jorge_Jaramillo</dc:creator>
      <dc:date>2024-10-17T12:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Level of a part in the assembly</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13093585#M636</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;I tested your code but it give me to many rows&lt;/P&gt;&lt;P&gt;I want something more compact like the image on the right&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="joao_mirandaEFM7E_0-1729262567956.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1423321iCF0E5CBDE4D48D27/image-size/medium?v=v2&amp;amp;px=400" role="button" title="joao_mirandaEFM7E_0-1729262567956.png" alt="joao_mirandaEFM7E_0-1729262567956.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 14:47:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13093585#M636</guid>
      <dc:creator>joao_mirandaEFM7E</dc:creator>
      <dc:date>2024-10-18T14:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Level of a part in the assembly</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13093683#M637</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This new version of the function prints single components per level with occurrences count:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def traverseAssembly(occurrences: adsk.fusion.Occurrences, parentLevel: str, inputString: str) -&amp;gt; str:
    unique_comps = {}
    for occu in occurrences:
        unique_comps[occu.component.entityToken] = unique_comps.get(occu.component.entityToken, 0) + 1
    for i in range(0, occurrences.count):
        occ = occurrences.item(i)
        if occ.component.entityToken in unique_comps:
            currentLevel = f'{parentLevel}{"." if parentLevel else ""}{i+1}'
            inputString += f'{currentLevel} {occ.name} {unique_comps[occ.component.entityToken]}\n'

            if occ.childOccurrences:
                inputString = traverseAssembly(occ.childOccurrences, currentLevel, inputString)
            unique_comps.pop(occ.component.entityToken)
    return inputString&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jorge Jaramillo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Oct 2024 15:35:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/level-of-a-part-in-the-assembly/m-p/13093683#M637</guid>
      <dc:creator>Jorge_Jaramillo</dc:creator>
      <dc:date>2024-10-18T15:35:37Z</dc:date>
    </item>
  </channel>
</rss>

