Hello,
Please try the following codes and see if they are what you want.
Thanks,
Marshal
import adsk.core, adsk.fusion, traceback
def spacePadRight(value, length):
pad = ''
if type(value) is str:
paddingLength = length - len(value) + 1
else:
paddingLength = length - value + 1
while paddingLength > 0:
pad += ' '
paddingLength -= 1
return str(value) + pad
def walkThrough(bom):
mStr = ''
for item in bom:
mStr += spacePadRight(item['name'], 25) + str(spacePadRight(item['instances'], 15)) + str(spacePadRight(item['material'], 25)) + str(item['description']) + '\n'
return mStr
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
title = 'Extract BOM'
if not design:
ui.messageBox('No active design', title)
return
# Get all occurrences in the root component of the active design
root = design.rootComponent
occs = root.allOccurrences
# Gather information about each unique component
bom = []
for occ in occs:
comp = occ.component
jj = 0
for bomI in bom:
if bomI['component'] == comp:
# Increment the instance count of the existing row.
bomI['instances'] += 1
break
jj += 1
if jj == len(bom):
# Add this component to the BOM
bom.append({
'component': comp,
'name': comp.name,
'instances': 1,
'material': comp.material.name if comp.material else '',
'description': comp.description
})
# Display the BOM
title = spacePadRight('Name', 25) + spacePadRight('Instances', 15) + spacePadRight('Material', 25) + 'Description\n'
msg = title + '\n' + walkThrough(bom)
ui.messageBox(msg, 'Bill Of Materials')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Marshal Tu
Fusion Developer
>