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: 

Validating Font Names

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
edjohnson100
178 Views, 2 Replies

Validating Font Names

Greetings,

I'm new to API and Python, but making progress due in large part to the great content here. 

 

I've been struggling with trying to figure out a way to confirm that a font name that I prompt the user for is valid. I have searched the forum and found an older solution that requires the user to start a Drawing, which from what I can tell still can not be done via the API. 

 

I'd simply like to test the user's font name input against fonts that are available to Fusion.

 

My solution is to try to create a sketch with some text using the font name entered by the user. If it doesn't throw an error, I delete the sketch and move on. If it fails, I display an error message and prompt the user again.

 

My solution works, but it seems there should be a better, simpler way. Any ideas on how to do this in a less heavy way are appreciated.

 

Thanks!

 

 

        ui = None
        try:
            app = adsk.core.Application.get()
            ui = app.userInterface
                    
            #doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
            design = app.activeProduct

            # Get the root component of the active design.
            rootComp = design.rootComponent
            unitsMgr = design.unitsManager

            # get some values from the user   
            textFontName = 'Arial Black'            
            isValid = False
            while not isValid:
                (textFontName, cancelled) = ui.inputBox('Font Name (must match system font name): ', 'Enter the Font Name', textFontName)
                if cancelled:
                    return
                try:
                    # Create a temporary sketch with the font to make sure it's valid
                    # There is probably a more elegant way to get this done, but this works
                    sk = rootComp.sketches.add(rootComp.xYConstructionPlane)
                    texts = sk.sketchTexts
                    input = texts.createInput2('Validate Font Name', 1)
                    input.fontName = textFontName
                    input.setAsMultiLine(adsk.core.Point3D.create(10, 10, 0),
                        adsk.core.Point3D.create(0, 0, 0),
                        adsk.core.HorizontalAlignments.CenterHorizontalAlignment,
                        adsk.core.VerticalAlignments.MiddleVerticalAlignment, 0)
                    sketchPart = texts.add(input)
                    # Delete the temporary sketch
                    sk.deleteMe()
                    #ui.messageBox(textFontName, 'Font Name')
                    isValid = True
                except:
                    ui.messageBox(textFontName + ' does not appear to be installed.', 'Invalid Font Name', 
                        adsk.core.MessageBoxButtonTypes.OKButtonType, 
                        adsk.core.MessageBoxIconTypes.CriticalIconType)
                    isValid = False

        except:
            if ui:
                ui.messageBox('I\'m a doctor, not an Python expert!\n\n{}'.format(traceback.format_exc()),'Dammit, Jim!')

 

  

Tags (3)
2 REPLIES 2
Message 2 of 3
kandennti
in reply to: edjohnson100

Hi @edjohnson100 .

 

For a list of font names that can be used, this is a good place to start.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/api-access-to-list-of-available-supported-... 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-to-edit-font-across-multiple-sketch... 

 

I have created a function to create a list of fonts that can be used directly from the dialog using the text command.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

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

        fontNames = getSketchFonts()
        [print(f) for f in fontNames]

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Get a list of fonts that can be used in a sketch.
import re
def getSketchFonts() -> list:
    app :adsk.fusion.Application = adsk.core.Application.get()

    # dammy doc
    doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    des :adsk.fusion.Design = app.activeProduct
    root :adsk.fusion.Component = des.rootComponent

    # dammy skt text
    skt :adsk.fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
    txts :adsk.fusion.SketchTexts = skt.sketchTexts
    txtIpt :adsk.fusion.SketchTextInput = txts.createInput2('dammy', 1.0)
    txtIpt.setAsMultiLine(
        adsk.core.Point3D.create(0, 0, 0),
        adsk.core.Point3D.create(1, 1, 0),
        adsk.core.HorizontalAlignments.CenterHorizontalAlignment,
        adsk.core.VerticalAlignments.MiddleVerticalAlignment, 0)
    dmyTxt :adsk.fusion.SketchText = txts.add(txtIpt)

    # select text
    ui = app.userInterface
    sels :adsk.core.Selections = ui.activeSelections
    sels.clear()
    sels.add(dmyTxt)

    # exec textCommands
    app.executeTextCommand(u'Commands.Start EditMTextCmd')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')

    # close dammy doc
    doc.close(False)

    # get font names
    fontDatas =[]
    fontDateFG = False
    for tmpInfo in txtCmdRes.split('MenuItems:\n')[:-1]:
        if fontDateFG:
            fontDatas = [re.sub(r"^\s+", "", s.split(',')[0]) for s in tmpInfo.split('\n')][0:-2]
            break

        stateInfo = tmpInfo.split('\n')[-2]
        if 'TextFont' in stateInfo:
            fontDateFG = True

    return fontDatas

 

Message 3 of 3
edjohnson100
in reply to: kandennti

Hi @kandennti,

Thanks for sharing your solution. I did look at the thread you mentioned but it seemed that there wasn't a clear, simple  solution. I like your approach in creating the temporary document to get the list of fonts. Not too far off from my approach with a temporary sketch, but building a dropdown list for selecting the font will be a lot cleaner for my application.

 

Thanks!

Ed

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