If you don't care about where a part exists within the assembly and don't need the structure, here's a simple way to get a list of components and their count.
import adsk.core, adsk.fusion, traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
des: adsk.fusion.Design = app.activeProduct
root = des.rootComponent
# Create a list of all components and their count.
partInfos = []
for comp in des.allComponents:
refOccs = root.allOccurrencesByComponent(comp)
partInfos.append([comp.name, comp.description, refOccs.count])
# Show the results
for partInfo in partInfos:
app.log(f'{partInfo[0]}, {partInfo[1]}, {partInfo[2]}')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
I tested it on an assembly of mine with multiple levels and got this result. Most of the components don't have a description, so that's why the second field is empty for most of them. It also shows the assemblies, but you could filter assemblies out by checking if the component has any child occurrences and skip that component if it does.
Biscut Joiner, , 3
BackRail, , 12
Large Shelf-Main, , 9
Mid-Large Shelf, , 9
LeftSide, , 6
Jigsaw, , 3
Bag, , 6
22665T31, Vulcan Caster, Swivel, 4" x 2" Phenolic Wheel, 800 lb Capacity, 18
Shelf-Edge, , 30
CabinetMutilLevelTest v1, , 0
PegBoard, , 6
BackStile, , 18
Router, , 3
Pocket Hole Jig, , 3
RightSide, , 6
Tool Cabinet v32, Tool Cabinet, 3
SmallShelf, , 12
Mid-ShelfDivider, , 3
Door, , 6
Back, , 6
Mid-Top, , 3
Skeleton, Reference, 3
TopBottom, , 12
Shelf, , 12
Brad Nailer, , 3
Mid-Divider, , 3
Mid-Left, , 3
Mid-Right, , 3
Mid-Bottom, , 3
Mid, , 3
Orbital Sander, , 3
Finish Nailer, , 3
Cabinet Skeleton, , 3
SmallShelfAssembly, , 12
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com