Hello,
I’ve came accross the same idea as yours that I wanted to achieve. After few days of reading, tinkering and “learning” how python works I’ve found really cool solution that I want to share with you guys. It lets me to change the text in the design in just two clicks and save that as an stl automatically – ready for 3D printing.
First, let’s start from the credits which goes to:
- ekinsb and rwillardphil from this forum had a discussion and did some Fusion API coding that I‘ve mixed together. (https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-to-select-sketchtext-and-have-a-dia...)
- Once again ekinsb made another awesome answer with the code on this forum thread that I‘ve mixed in (https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-api-to-change-parameter-and-then-ex...)
So to get started, first you need to create anew script (code below):
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct
rootComp = design.rootComponent
# Get the sketch named "ChangeText"
sk = rootComp.sketches.itemByName('ChangeText')
# Get the first sketch text.
skText = sk.sketchTexts.item(0)
#Prompts the user for the new Text
(returnValue, cancelled) = ui.inputBox('What text?', 'New text:', )
# Grab the sketch and first text entity
sk = rootComp.sketches.itemByName('ChangeText')
skText = sk.sketchTexts.item(0)
# Change the text.
skText.text = returnValue
# Write in the path to the folder where you want to save STL‘s
folder = 'C:/Users/Desktop/etc...'
# Construct the output filename. Name will be the same as you‘ve changed the text into.
filename = folder + skText.text + '.stl'
# Save the file as STL.
exportMgr = adsk.fusion.ExportManager.cast(design.exportManager)
stlOptions = exportMgr.createSTLExportOptions(rootComp)
stlOptions.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementMedium
stlOptions.filename = filename
exportMgr.execute(stlOptions)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
You need to create design and add new sketch where the text that you want to change will be.
IMPORTANT: name that sketch „ChangeText“ so that script could grab it and work on.
Next thing that was very important for me was to find a solution to align text so that when you change it it still stays aligned. It is done by a little trick as Fusion doesn‘t have much text options. It is really simple when you find it.
I‘ve made a screencast how everything works and how to align text so it stays „parametric“. Attached below.
Note that when you choose coincident constraint for the text you should hold SHIFT+find center of the line and click it.
Hope that will help as it helped me a lot 🙂
Happy 3D printing!
P.S. Maybe someone could help me to add some code so that if you change the text into the same one that is already in the STL export folder, it automatically makes file name "filename+2"?