I did some testing, and it seems like it's just how long Fusion takes to compute it. However, I was able to do some optimization. When I ran your program as-is (which doesn't export the STL files), it took me 122 seconds. I changed it to move the timeline marker to immediately after the sketch and then moved the marker back to the end after the text had been edited and the parameter changed. This results in a single compute to account for the changes. This took 80 seconds. Most of the time is spent when the timeline marker is moved back and is a result of Fusion doing the modeling operations. The final extrude, where you trim the text, is also expensive. When I suppressed it, the time to run was 30 seconds. Here's my modified code that prints out the times for each step. You'll see that setting the text and changing the parameter are very fast.
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design: adsk.fusion.Design = app.activeProduct
component = design.allComponents.itemByName('Label Plate')
body = component.bRepBodies.itemByName('label plate')
sketch = component.sketches.itemByName('label plate')
text = sketch.sketchTexts[0]
base_length_param = design.allParameters.itemByName('label_base_length')
# load all label text from file
labels = [label.strip() for label in open(LABEL_FILE).readlines() if label.strip()]
ui.messageBox('Exporting %d labels to %s' % (len(labels), PROJECT_DIR))
start = time.time()
timelinePstn = design.timeline.markerPosition
for i, label in enumerate(labels):
labelStart = time.time()
last = time.time()
sketch.timelineObject.rollTo(False)
app.log(label)
app.log(f'Roll back timeline: {time.time() - last}')
last = time.time()
text.text = label
max_text = text.boundingBox.maxPoint.x
max_text = 2 * float(max_text) + 1 # 5mm padding on each end
app.log(f'Set text: {time.time() - last}')
last = time.time()
base_length_param.value = max_text
app.log(f'Set parameter value: {time.time() - last}')
last = time.time()
design.timeline.markerPosition = timelinePstn
app.log(f'Timeline marker move: {time.time() - last}')
filepath = PROJECT_DIR / label
# ui.messageBox('saving label to %s' % filepath)
# Let the view have a chance to paint just so you can watch the progress.
# adsk.doEvents()
# export_body(design, body, filepath)
app.log(f'Full label: {time.time() - labelStart}')
adsk.doEvents()
finish = time.time()
ui.messageBox('Saved %d label files to %s in %s seconds' % (i, PROJECT_DIR, int(finish-start)))
app.log(f'Total time: {finish-start}')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian EkinsInventor and Fusion 360 API Expert
Website/Blog:
https://EkinsSolutions.com