import adsk.core
import adsk.fusion
from collections import defaultdict
# Fusion 360 parts list with dimensions
# API manual https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-C1545D80-D804-4CF3-886D-9B5C54B2D7A2
def run(context):
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
units = design.unitsManager.defaultLengthUnits
# Get the root component of the active design.
rootComp = design.rootComponent
# Get occurences from root
components = [rootComp.occurrences[i].component for i in range(
rootComp.occurrences.count)]
components = [rootComp] + components
total_lengths = defaultdict(float)
total_counts = defaultdict(int)
dialog_str = ''
dialog_str += '
'
dialog_str += ''
dialog_str += 'Component | '
dialog_str += 'Body | '
dialog_str += f'x ({units}) | '
dialog_str += f'y ({units}) | '
dialog_str += f'z ({units}) | '
dialog_str += '
'
not_visible_count = 0
for subcomp in components:
for bidx in range(0, subcomp.bRepBodies.count):
body = subcomp.bRepBodies.item(bidx)
if not body.isVisible:
not_visible_count += 1
continue
dimvector = body.boundingBox.minPoint.vectorTo(
body.boundingBox.maxPoint).asPoint()
dims = sorted(dimvector.asArray())
formx = product.unitsManager.formatInternalValue(
dims[0], units, False)
formy = product.unitsManager.formatInternalValue(
dims[1], units, False)
formz = product.unitsManager.formatInternalValue(
dims[2], units, False)
dialog_str += ''
dialog_str += f'{subcomp.name} | '
dialog_str += f'{body.name} | '
dialog_str += f'{formx} | '
dialog_str += f'{formy} | '
dialog_str += f'{formz} | '
dialog_str += '
'
key_length = f'{formx} x {formy}'
total_lengths[key_length] += float(formz)
key_count = f'{formx} x {formy} x {formz}'
total_counts[key_count] += 1
dialog_str += '
'
dialog_str += 'Total counts
'
dialog_str += ''
dialog_str += ''
dialog_str += 'Dimensions | '
dialog_str += 'Count |
'
for key_count in sorted(total_counts.keys()):
dialog_str += f''
dialog_str += f'{key_count} | '
dialog_str += f'{total_counts[key_count]} | '
dialog_str += '
'
dialog_str += '
'
dialog_str += 'Total lengths
'
dialog_str += ''
dialog_str += ''
dialog_str += f'Dimensions ({units}) | '
dialog_str += f'Total length ({units}) | '
dialog_str += '
'
for key_length in sorted(total_lengths.keys()):
dialog_str += f''
dialog_str += f'{key_length} | '
dialog_str += f'{total_lengths[key_length]} | '
dialog_str += '
'
dialog_str += '
'
if not_visible_count:
dialog_str += f'
Note: {not_visible_count} hidden {"body" if not_visible_count == 1 else "bodies"} excluded from the lists'
ui.messageBox(dialog_str, 'Total materials used', adsk.core.MessageBoxButtonTypes.OKButtonType,
adsk.core.MessageBoxIconTypes.InformationIconType)
print(dialog_str)