Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why is this so slow, and how can I make it faster?

3 REPLIES 3
Reply
Message 1 of 4
Julie_7
190 Views, 3 Replies

Why is this so slow, and how can I make it faster?

I am creating labels for my toolbox drawers. I 3D print them in two colors and they slide into slots in the drawer handles.

 

Julie_7_0-1687876786572.png

 

In order to make the process easier I wrote a script that reads a list of label names and for each label changes the embossed text, resizes the backing plate, then saves the body as STL.

 

I am wondering if I am doing something wrong. The script is very slow to run. There is also a noticeable delay when manually saving the sketch after changing the text and after changing the parameter value, but not near as bad as when running the script.

 

I ran the script without the STL conversion and file saving. It took 208 seconds to perform the two steps 21 times, which is about 10 seconds for each change.

Julie_7_1-1687879230274.png

The design is extremely simple. I have attached a zip file with the design and the script.

 

Thanks for the help!

3 REPLIES 3
Message 2 of 4
BrianEkins
in reply to: Julie_7

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 Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4
felix85SA3
in reply to: Julie_7

I got curious and did some testing based on @BrianEkins approach. I changed the design to use an Extrude instead of a Boss and set the last Extrudes Extent type to Distance (label_base_length). That reduced the time by another 30%

Message 4 of 4
Julie_7
in reply to: felix85SA3

Using extrude for the text is an interesting idea. Given that I am extruding after embossing to cut the text surface at an angle, maybe I can reduce it to a single extrude to the cut plane.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report