Last active
July 21, 2022 21:33
-
-
Save nuada/e8c77827ce32ed7d348f7717909d98a3 to your computer and use it in GitHub Desktop.
Parts list script for Fusion 360 - components are aggregated by dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import adsk.core | |
import adsk.fusion | |
import csv | |
import os | |
import os.path | |
# Inspired by: https://forums.autodesk.com/t5/fusion-360-design-validate/parts-list-with-dimensions/m-p/9642058/highlight/true#M223047 | |
def run(context): | |
product = adsk.core.Application.get().activeProduct | |
design = adsk.fusion.Design.cast(product) | |
parts = {} | |
for occurrence in design.rootComponent.occurrences: | |
for body in occurrence.component.bRepBodies: | |
dimensions = body.boundingBox.minPoint.vectorTo(body.boundingBox.maxPoint).asPoint() | |
dimensions = tuple(sorted(dimensions.asArray())) | |
if dimensions not in parts: | |
parts[dimensions] = dict(quantity=0, names=set()) | |
parts[dimensions]['quantity'] += 1 | |
parts[dimensions]['names'].add(occurrence.component.name) | |
fname = os.path.join(os.environ['HOME'], '{}-parts.csv'.format(design.rootComponent.name)) | |
with open(fname, 'w', newline='') as output: | |
writer = csv.writer(output) | |
writer.writerow(('no', 'quantity', 'd1', 'd2', 'd3', 'components')) | |
for i, (dimensions, part) in enumerate(sorted(parts.items())): | |
dimensions = tuple(map(lambda d: product.unitsManager.formatInternalValue(d), dimensions)) | |
writer.writerow((i+1, part['quantity']) + dimensions + (';'.join(part['names']),)) | |
print('Parts list saved to file: {}\r'.format(fname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment