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?
Solved! Go to 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.
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