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: 

Updating Extrude when Sketch changes

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
zoe.ophelia.hammer
506 Views, 3 Replies

Updating Extrude when Sketch changes

zoe.ophelia.hammer
Explorer
Explorer

I'm trying to write a script that speeds up the process of making keyboard keycaps with legends sunk into the top. 

I found another script on this forum that worked similarly and i modified it to loop through a list of characters, each time updating the sketch that the extrude was based on, and then exporting the object as an STL. 

 

The script successfully iterates through the list, changes the text field, and exports the file with the correct name, however each file that it exports has the same letter extruded into the part.
It appears that the extrude feature is not updating after the sketch updates. I found some suggestions on the forums reccomending i roll back the timeline and back to the end to get it to update, but that didnt work. 

 

the extrude feature displays an error: "1 Reference  Failure. Cannot complete extrusion"

zoeopheliahammer_0-1648855174536.png

 

 

 

 

 

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)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
# Grab the sketch and first text entity 
        sk = rootComp.sketches.itemByName('ChangeText') 
        skText = sk.sketchTexts.item(0)
        for i in alphabet:
            # Change the text.
            legend = i
            skText.text = legend

            # Write in the path to the folder where you want to save STL‘s
            folder = r"X:\53.00 - 3d Printing\53.30 - Custom Models\Keycaps\Set 1.5\\"
            
            # 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)
            timeline = design.timeline
            adsk.doEvents()

            returnValue = timeline.moveToPreviousStep()
            adsk.doEvents()
            returnValue = timeline.moveToPreviousStep()
            adsk.doEvents()
            returnValue = timeline.moveToEnd()
            adsk.doEvents()

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

 

 

0 Likes

Updating Extrude when Sketch changes

I'm trying to write a script that speeds up the process of making keyboard keycaps with legends sunk into the top. 

I found another script on this forum that worked similarly and i modified it to loop through a list of characters, each time updating the sketch that the extrude was based on, and then exporting the object as an STL. 

 

The script successfully iterates through the list, changes the text field, and exports the file with the correct name, however each file that it exports has the same letter extruded into the part.
It appears that the extrude feature is not updating after the sketch updates. I found some suggestions on the forums reccomending i roll back the timeline and back to the end to get it to update, but that didnt work. 

 

the extrude feature displays an error: "1 Reference  Failure. Cannot complete extrusion"

zoeopheliahammer_0-1648855174536.png

 

 

 

 

 

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)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
# Grab the sketch and first text entity 
        sk = rootComp.sketches.itemByName('ChangeText') 
        skText = sk.sketchTexts.item(0)
        for i in alphabet:
            # Change the text.
            legend = i
            skText.text = legend

            # Write in the path to the folder where you want to save STL‘s
            folder = r"X:\53.00 - 3d Printing\53.30 - Custom Models\Keycaps\Set 1.5\\"
            
            # 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)
            timeline = design.timeline
            adsk.doEvents()

            returnValue = timeline.moveToPreviousStep()
            adsk.doEvents()
            returnValue = timeline.moveToPreviousStep()
            adsk.doEvents()
            returnValue = timeline.moveToEnd()
            adsk.doEvents()

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

 

 

3 REPLIES 3
Message 2 of 4

JeromeBriot
Mentor
Mentor
Accepted solution

Hello,

 

Try the script below. You have to delete the existing extrusion of the text before running the script.

 

import os
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)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

        # Write in the path to the folder where you want to save STL‘s
        folder = r"X:\53.00 - 3d Printing\53.30 - Custom Models\Keycaps\Set 1.5"

        distance = adsk.core.ValueInput.createByReal(4)

        extrudeFeatures = rootComp.features.extrudeFeatures
        extrude = None

        for i in alphabet:

            skText.text = i

            if extrude:
                extrude.deleteMe()

            extrudeInput = extrudeFeatures.createInput(skText, adsk.fusion.FeatureOperations.CutFeatureOperation)
            extent_distance = adsk.fusion.DistanceExtentDefinition.create(distance)
            extrudeInput.setOneSideExtent(extent_distance, adsk.fusion.ExtentDirections.NegativeExtentDirection)
            extrude = extrudeFeatures.add(extrudeInput)

            # Construct the output filename. Name will be the same as you‘ve changed    the text into.
            filename = os.path.join(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)

            adsk.doEvents()

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

 

1 Like

Hello,

 

Try the script below. You have to delete the existing extrusion of the text before running the script.

 

import os
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)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

        # Write in the path to the folder where you want to save STL‘s
        folder = r"X:\53.00 - 3d Printing\53.30 - Custom Models\Keycaps\Set 1.5"

        distance = adsk.core.ValueInput.createByReal(4)

        extrudeFeatures = rootComp.features.extrudeFeatures
        extrude = None

        for i in alphabet:

            skText.text = i

            if extrude:
                extrude.deleteMe()

            extrudeInput = extrudeFeatures.createInput(skText, adsk.fusion.FeatureOperations.CutFeatureOperation)
            extent_distance = adsk.fusion.DistanceExtentDefinition.create(distance)
            extrudeInput.setOneSideExtent(extent_distance, adsk.fusion.ExtentDirections.NegativeExtentDirection)
            extrude = extrudeFeatures.add(extrudeInput)

            # Construct the output filename. Name will be the same as you‘ve changed    the text into.
            filename = os.path.join(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)

            adsk.doEvents()

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

 

Message 3 of 4

zoe.ophelia.hammer
Explorer
Explorer

Thank you! I'll give it a try that way too. 

i ended up figuring this out myself a different way. i moved the timeline moving features around and it worked.

now what this one does is moves the timeline to before the extrude before it changes the text, and then moves it back to the end afterwards.

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')
        timeline = design.timeline
        # Get the first sketch text.
        skText = sk.sketchTexts.item(0)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', "( {\n[ <", ") }\n] >", ", ."]
# Grab the sketch and first text entity 
        sk = rootComp.sketches.itemByName('ChangeText') 
        skText = sk.sketchTexts.item(0)
        for i in alphabet:
            returnValue = timeline.moveToPreviousStep()
            returnValue = timeline.moveToPreviousStep()
            
            # Change the text.
            legend = i
            skText.text = legend

            adsk.doEvents()
            returnValue = timeline.moveToEnd()
            adsk.doEvents()

            # Write in the path to the folder where you want to save STL‘s
            folder = "C:\\Users\\Zoe\\Desktop\\Keycaps\\set 1.5"
            
            # 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()))
0 Likes

Thank you! I'll give it a try that way too. 

i ended up figuring this out myself a different way. i moved the timeline moving features around and it worked.

now what this one does is moves the timeline to before the extrude before it changes the text, and then moves it back to the end afterwards.

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')
        timeline = design.timeline
        # Get the first sketch text.
        skText = sk.sketchTexts.item(0)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', "( {\n[ <", ") }\n] >", ", ."]
# Grab the sketch and first text entity 
        sk = rootComp.sketches.itemByName('ChangeText') 
        skText = sk.sketchTexts.item(0)
        for i in alphabet:
            returnValue = timeline.moveToPreviousStep()
            returnValue = timeline.moveToPreviousStep()
            
            # Change the text.
            legend = i
            skText.text = legend

            adsk.doEvents()
            returnValue = timeline.moveToEnd()
            adsk.doEvents()

            # Write in the path to the folder where you want to save STL‘s
            folder = "C:\\Users\\Zoe\\Desktop\\Keycaps\\set 1.5"
            
            # 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()))
Message 4 of 4

JeromeBriot
Mentor
Mentor

@zoe.ophelia.hammer wrote:

i ended up figuring this out myself a different way. i moved the timeline moving features around and it worked.


I think that it is safer to roll the timeline back to a specific feature rather than counting how many steps to move the timeline marker.

 

Rename the letter extrusion as textExtrusion by right clicking on it in the timeline then run the script below:

import os
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

        timeline = design.timeline

        # Get the sketch named "ChangeText"
        sk = rootComp.sketches.itemByName('ChangeText')

        # Get the first sketch text.
        skText = sk.sketchTexts.item(0)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', "( {\n[ <", ") }\n] >", ", ."]

        # Write in the path to the folder where you want to save STL‘s
        folder = r"C:\Users\Zoe\Desktop\Keycaps\set 1.5"

        extrudes = rootComp.features.extrudeFeatures

        for txt in alphabet:

            textExtrusion = extrudes.itemByName('textExtrusion')
            textExtrusion.timelineObject.rollTo(True)
            returnValue = timeline.moveToPreviousStep()

            # Change the text.
            skText.text = txt

            returnValue = timeline.moveToEnd()
            adsk.doEvents()

            # Construct the output filename. Name will be the same as you‘ve changed    the text into.
            filename = os.path.join(folder, txt + '.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()))

 

1 Like


@zoe.ophelia.hammer wrote:

i ended up figuring this out myself a different way. i moved the timeline moving features around and it worked.


I think that it is safer to roll the timeline back to a specific feature rather than counting how many steps to move the timeline marker.

 

Rename the letter extrusion as textExtrusion by right clicking on it in the timeline then run the script below:

import os
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

        timeline = design.timeline

        # Get the sketch named "ChangeText"
        sk = rootComp.sketches.itemByName('ChangeText')

        # Get the first sketch text.
        skText = sk.sketchTexts.item(0)

        alphabet = ["A","B","C","D","E","F","G",'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', "( {\n[ <", ") }\n] >", ", ."]

        # Write in the path to the folder where you want to save STL‘s
        folder = r"C:\Users\Zoe\Desktop\Keycaps\set 1.5"

        extrudes = rootComp.features.extrudeFeatures

        for txt in alphabet:

            textExtrusion = extrudes.itemByName('textExtrusion')
            textExtrusion.timelineObject.rollTo(True)
            returnValue = timeline.moveToPreviousStep()

            # Change the text.
            skText.text = txt

            returnValue = timeline.moveToEnd()
            adsk.doEvents()

            # Construct the output filename. Name will be the same as you‘ve changed    the text into.
            filename = os.path.join(folder, txt + '.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()))

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report