Here is my code. It is my first attempt at a script. It's even still named Hello World. Please be gentle and don't hurt yourself laughing too hard.
#Author- Chip Cox
#Description- My first script
import adsk.core, adsk.fusion, adsk.cam, traceback, string,os
from itertools import chain
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
#Replace this section with a drop down font selection later
[ans,canc]=ui.inputBox('Font Family','Font')
if canc==True :
ui.messageBox('cancled')
return
else:
font=ans
[ans,canc]=ui.inputBox('Font Size','Font')
if canc==True :
ui.messageBox('Cancled')
return
else:
fontsize=int(ans)/10
# we have our font now - it may not be valid but it's a string
#ui.messageBox('Font ' + font + ' - ' + str(int(fontsize*10)))
# Lets get the design mode and get started
design=adsk.fusion.Design.cast(app.activeProduct)
if not design:
ui.messageBox('No active Fusion 360 Design', 'No Design')
return
else:
rc=design.rootComponent # rc = Root Component
#ui.messageBox('root component name = ' + rc.name)
trans=None
for tc in rc.occurrences:
if tc.name=='Template:1':
trans=tc.transform
break
if trans==None:
ui.messageBox('Template component not found')
return
# create new occurance of component and name it after the font we are creating.
nc=rc.occurrences.addNewComponentCopy(tc.component,trans)
nc.component.name=font + str(int(fontsize*10))
nc.activate()
nc.isLightBulbOn=True
tc.isLightBulbOn=False
#find the sketch named template in the component occurance we are working with
tempsk=nc.component.sketches.itemByName('Template')
# get the text box on the sketch
tempsktextitem=tempsk.sketchTexts.item(0)
# set its font to the new font entered above
tempsktextitem.fontName=font
#set the user parameter for font size to the new font size. This effects ALL components.
design.userParameters.itemByName('FontSize').value=fontsize
# Write in the path to the folder where you want to save STL‘s
fdir=ui.createFolderDialog()
fdir.title = 'Folder to save STL file in'
fdirResults=fdir.showDialog()
# Check fdirResults for ok/cancel/etc
folder = fdir.folder + '/'
#ui.messageBox(folder)
ext=rc.features.extrudeFeatures
#for c in chain(numbers,letters,upperletters,numbers):
for c in string.printable:
tempsktextitem.text=c
print(c)
tempsk.redefine(rc.xZConstructionPlane)
# Construct the output filename. Name will be the same as you‘ve changed the text into.
filename = ''.join([folder,font, '-', str(int(fontsize)), '-ch-',c if c!='/' else 'slash'])
#ui.messageBox(filename)
# Save the file as STL.
exportMgr = adsk.fusion.ExportManager.cast(design.exportManager)
stlOptions = exportMgr.createSTLExportOptions(nc.bRepBodies.item(0),filename)
if stlOptions == None:
ui.messageBox('stlOptions is null')
return
stlOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementMedium
#stlOptions.filename = filename
stlOptions.isIncludingInvisibleBodies=True
stlOptions.isIncludingInvisibleComponents=True
if not exportMgr.execute(stlOptions):
ui.messageBox('error executing export')
ui.messageBox('Done')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))