- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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!')
Solved! Go to Solution.