| |

Create Text on a Path – A Simple Fusion 360 Script

xkcd.com, “Code Quality

Background

A recent discussion about the quality of engraved text for 3D printing got me thinking about the best way to test various fonts and sizes. Creating text is fairly trivial in Fusion 360, but it quickly gets painful if you want create multiple lines of text in multiple sizes with different fonts.

I whipped up a quick design by putting several lines of test text on a series of 3-point arcs. It was just 5 lines of text with varying font heights from 2 – 10 mm. I used the standard Arial font that we all have on our system. It worked fine, but I wanted an easy way to edit the sketch and change the font, the test text string, and the font heights. It is not hard, but it is very putzy (that’s a technical term!) to go back and edit each string and change the font name and height.

Enter Fusion 360 Scripting

I have been messing with writing Python scripts and add-ins for Fusion 360 for a while and decided to whip up a quick-and-dirty script to quickly create a sketch with text on multiple paths with varying font heights. By using variables for the font name, text string, and a scaling factor, I can quickly generate a new sketch with any font on my system.

If you’d like to learn what’s involved in creating the script from scratch, keep reading. It’s not hard – you’ll just have to copy and paste my code. If you are familiar with scripting and how to create and manually install a script, just download the code and use it. The script is pretty well documented.

Download the Script

Install the Script

There are two methods to get my script installed. Both are well-documented by Autodesk.

The first method is pretty straightforward if you follow the instructions provided by Autodesk. It involves extracting the “Create Test Text on a Path” folder from the ZIP file to a specific place where Fusion 360 stores scripts. The location depends on whether you’re using a PC or a Mac. The instructions for how to do this can be found in the Fusion 360 Knowledge Network so I won’t go into detail here. Follow the instructions for adding a script manually in the link below.

Autodesk Knowledgebase – How to install an add-in or script in Fusion 360

The second method involves creating a script from scratch and then copying my code and saving the script. You may want to review the process first to get a good visual overview.
Autodesk Help – Creating a Script or Add-In

Creating a Script from Scratch

Open Fusion 360 and select the Utilities tab > Add-Ins to open the Scripts and Add-Ins dialog box.
You can also use Shift + S to open the Scripts dialog no matter which tab is active.

With the Scripts tab selected in the Scripts and Add-Ins dialog, click the Create button.

In the Create New Script dialog:

Create a New:Script
Programming LanguagePython
Script or Add-In NameCreate Test Text on a Path
(or something shorter if you prefer)
Description Simple script to quickly create test text on a path with increasing font heights
AuthorEd Johnson (that’s me!)
Target Operating SystemWindows and Mac
(there’s nothing that’s specific to either OS)
Folder Locationleave it as the default
Create New Script Dialog

Click Create.

The script will be added under the My Scripts section.
Click the Edit button to open Visual Studio Code editor.
You should see a basic “Hello script” placeholder code.
Delete all of this code.
Expand the script code block below, and click the Copy link to copy it to your clipboard.

Edit Script Dialog

"""Create Test Text on a Path

# Author-Ed Johnson
# Description-Simple script to quickly create test text on a path with increasing font heights
# Date-2022-02-24
# This should be perfectly safe, but use at your own risk
#
# Feel free to use and modify as you like
# 
"""
import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try: 
        app = adsk.core.Application.get()
        ui = app.userInterface

        # text to use for testing - don't get too long-winded or the text will exceed the arc
        testString = 'Text - 1a 2b 3j 4Q 5G 6*'
        
        # font to test - must be a valid font name on your system
        testFont = 'Arial'
        
        # test designed for Arial font
        # increase or decrease scale factor to adjust spacing and arc size (i.e. 1.1 = 110%)
        # i.e, with default text string, Arial Black needs a factor of 1.25 to scale up the arcs so the text fits
        scaleFactor = 1.0

        # Create a new document and get the Design.
        #doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

        # Get the active component of the active design.
        component = design.activeComponent

        # Create a new sketch on the XY construction plane.
        sk = component.sketches.add(component.xYConstructionPlane)

        # Get the SketchTexts collection object.
        texts = sk.sketchTexts

        # it would be more elegant and efficient to create the arcs and place the text in a loop,
        # but this is meant to be quick and dirty and this is easier to read, understand, and edit
        
        # Draw the arcs to create paths for the text on a path.
        # point coordinates are X,Y,Z and expressed in centimeters
        # the 3 points are start point, any point along arc, end point

        arc0 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-2 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0, 2 * scaleFactor, 0),
                                                        adsk.core.Point3D.create(2 * scaleFactor, 0, 0))
        arc1 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-2.5 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0, 2.5 * scaleFactor, 0),
                                                        adsk.core.Point3D.create(2.5 * scaleFactor, 0, 0))
        arc2 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-3.25 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0,3.25 * scaleFactor,0),
                                                        adsk.core.Point3D.create(3.25 * scaleFactor, 0, 0))
        arc3 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-4.25 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0,4.25 * scaleFactor,0),
                                                        adsk.core.Point3D.create(4.25 * scaleFactor, 0, 0))
        arc4 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-5.25 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0,5.25 * scaleFactor,0),
                                                        adsk.core.Point3D.create(5.25 * scaleFactor, 0, 0))
        arc5 = sk.sketchCurves.sketchArcs.addByThreePoints(adsk.core.Point3D.create(-6.5 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(0,6.5 * scaleFactor,0),
                                                        adsk.core.Point3D.create(6.5 * scaleFactor, 0, 0))
        # set inner arcs as construction lines
        # arc0 and arc5 define the outer boundaries of the profile, so they are not construction lines.
        arc0.isConstruction = False
        arc1.isConstruction = True
        arc2.isConstruction = True
        arc3.isConstruction = True
        arc4.isConstruction = True
        arc5.isConstruction = False

        # draw line along the bottom of the largest arc so we have a closed profile to extrude
        line1 = sk.sketchCurves.sketchLines.addByTwoPoints(adsk.core.Point3D.create(-6.5 * scaleFactor, 0, 0),
                                                        adsk.core.Point3D.create(6.5 * scaleFactor, 0, 0))

        # Define text inputs to place along the arcs
        # input values are the text string and the font height (expressed in centimeters)
        # only 4 inputs to define since text is only placed on arcs 1-4
		
        input1 = texts.createInput2('4 mm ' + testString, 0.4)
        input1.setAsAlongPath(arc1, True, adsk.core.HorizontalAlignments.CenterHorizontalAlignment, 0)
        input1.isHorizontalFlip = False
        input1.isVerticalFlip = False
        input1.fontName = testFont

        input2 = texts.createInput2('5 mm ' + testString, 0.5)
        input2.setAsAlongPath(arc2, True, adsk.core.HorizontalAlignments.CenterHorizontalAlignment, 0)
        input2.isHorizontalFlip = False
        input2.isVerticalFlip = False
        input2.fontName = testFont

        input3 = texts.createInput2('6 mm ' + testString, 0.6)
        input3.setAsAlongPath(arc3, True, adsk.core.HorizontalAlignments.CenterHorizontalAlignment, 0)
        input3.isHorizontalFlip = False
        input3.isVerticalFlip = False
        input3.fontName = testFont

        input4 = texts.createInput2('8 mm ' + testString, 0.8)
        input4.setAsAlongPath(arc4, True, adsk.core.HorizontalAlignments.CenterHorizontalAlignment, 0)
        input4.isHorizontalFlip = False
        input4.isVerticalFlip = False
        input4.fontName = testFont

        # place the text on the arc paths
        texts.add(input1)        
        texts.add(input2)        
        texts.add(input3)        
        texts.add(input4)        
        
    # traceback error handler
	except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Code language: PHP (php)

If you downloaded the script, you can also copy the code from the Create Test Text on a Path.txt file.
Switch back to the VS Code editor and paste in the code you just copied.

Note
Unlike most other programming languages, Python is very picky about white space and indented code.

The copied code should be formatted OK, but I have seen an issue once in a while where after pasting the code, you may see a bunch of squiggly red lines and errors and warning on the status bar along the bottom of the editor window (VS Code has error indicators all over the place!) Most of these errors are “inconsistent use of tabs and spaces”, which then create the other errors and warnings you see.

A quick way I found to reformat the code so Python is happy is to select all of the code (Ctrl+A) and hit TAB followed by Shift+TAB. This should fix all of the code indenting and clear up all of the errors.

VS Code Python Errors

After cleaning up any errors, save the script (File > Save or Ctrl+S). At this point you can close the VS Code editor, but I recommend leaving it open. This will allow you to make tweaks to the variables and re-run the script. There is no need to restart Fusion between edits of the script. Just remember to save your current code changes before re-running the script.

Running the Script

Once the script is created and saved, open the Scripts and Add-Ins dialog (Utilities > Add-Ins > Scripts and Add-Ins or use the shortcut Shift+S) and run the Create Test Text on a Path script in the My Scripts section.

Use Shift+S to quickly open this dialog

Barring any errors, a new sketch with the text on a path will be created in the active component (or root level if you don’t have any components created).

The script has a few easy-to-edit variables near the top of the script (highlighted in the code block above). Try changing one or more and see the results. I took a “brute force” approach on purpose to make it easy for anyone to decipher the code. It makes editing easier (but quite repetitive!)

You are now on your own to continue editing. Go ahead and extrude the profiles and then engrave or emboss your text to finish creating your model for 3D printing.

One thing that can be improved is adding variables for the font height and prefix strings for each size. I’ll leave that as a challenge for you to explore.

Script Errors

If you tweak the code and then run into error messages, they will show up in a couple of places. The first is in the Text Command window on the bottom the the Fusion 360 window. If the window is collapsed, click the small + symbol to expand it. Errors in here are thrown before the script tries to run. These are usually caused by white space or other formatting errors in the code. The message looks cryptic, but you should be able to get a clue to what’s wrong and which line number is causing the problem.

The other error message you might see is a runtime error that is thrown during the execution of the script. These are displayed in the form of a modal dialog box in Fusion’s main window. If you use the generic error handling found in many Fusion 360 sample scripts as I have here, the message looks almost as cryptic as a Windows blue screen of death. Again, if you take a breath and scan the message, you will usually find some helpful information so you can fix your error(s).

Fusion 360 Modal Error Message

Companion YouTube Video

That’s A Wrap!

I hope you found this useful and that it was enough to take some of the mystery out of creating and running your own scripts. The Autodesk Fusion 360 Knowledgebase and Support Forum are great resources to help you out when you are ready to roll up your sleeves and tackle Fusion 360 scripting.

Fusion 360 API Documentation

Fusion 360 Support Forums

Feel free to leave any comments or questions before you leave. I’d love to know what you think.

“The more we learn, the more we discover how much we do not know.”

– Yoda, Star Wars: Episode III Revenge of the Sith (novelization)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *