Announcements

Announcement: We’re aware of an issue affecting starting a new topic from category pages and creating new blog posts. New topics can still be started directly from the relevant board. Learn more here.

List of all subcomponents in a component similar to design.allComponents

List of all subcomponents in a component similar to design.allComponents

Anonymous
Not applicable
1,594 Views
2 Replies
Message 1 of 3

List of all subcomponents in a component similar to design.allComponents

Anonymous
Not applicable

Is there a command to get a list of all subcomponents of a component in a design, similar to design.allComponents, but for components instead?

0 Likes
Accepted solutions (1)
1,595 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

There's a subtlety first in that a component is distinct from an occurrence which is, essentially, a component instance. They are used interchangeably sometimes!

 

Anyway. If you start with the rootComponent say (or any component but root is most obvious), and look at its occurrences property - this is a collection of the Occurrences associated with that component (yes, it's mildly headwrecking initially). You will see it has a "asList" property which yields the collection as a list - with the usual count property and item() method. If you traverse that list that will give you a component tree:

 

for i in range(occurrencelist.count):
        occ = occurrencelist.item(i)

 

So each occ.component is the component that occurrence references. If occ.childOccurrences is non Null, the occurrence has a subtree of occurrences which gives you another list of occurrences and so on down the tree. You can do a classic recursive action to traverse the tree:

 

def traverse(occurrences):
    for i in range(occurrences.count):
        occ = occurrences.item(i)

.... do stuff with occ like look at occ.component ....
if occ.childOccurrences: traverse(occ.childOccurrences)

 

Something like that anyway 🙂

 

Conor.

0 Likes
Message 3 of 3

ekinsb
Alumni
Alumni

I just want to add a couple of more things to this discussion.  First, I agree that the term "Component" is overused within Fusion.  The user-interface only uses the term "Component" and uses it for both components and occurrences but they are very different.  The API has to differentiate between the two.  I recommend reading the topic below in the API help.  Hopefully it will make it much clearer in your mind.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-88A4DB43-CFDD-4CFF-B124-7EE67915A07A

 

Second, here's an existing sample program that uses the ability that Conor mentioned and traverses an entire assembly.

 

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-5a4fa3a6-fa21-11e4-b610-f8b156d7cd97


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes