Change Text Script

Change Text Script

sdi1400294UX4P6
Observer Observer
901 Views
2 Replies
Message 1 of 3

Change Text Script

sdi1400294UX4P6
Observer
Observer

Hello, 

i am new in scripting and i am facing a problem i am runnign this script that another member of the forum wrote but the thing is i am managing to change the text but it doesnt change the model unless i run compute all i tried adding adsk.doEvent() but still no change in the model... any idea why? the script i am running is: 

 

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

        for i in range(100):
            time.sleep(0.01)
            adsk.doEvents()

        # Write in the path to the folder where you want to save STL‘s
        folder = 'C:\stl\zz'

        for i in range(100):
            time.sleep(0.01)
            adsk.doEvents()
        
        # 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
902 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @sdi1400294UX4P6 .

 

Using the recently added Design.computeAll Method, the model changes were reflected and the STL file was exported correctly.

 

・・・
        # Change the text.
        skText.text = returnValue

        # for i in range(100):
        #     time.sleep(0.01)
        #     adsk.doEvents()

        # Write in the path to the folder where you want to save STL‘s
        folder = 'C:\stl\zz'

        # for i in range(100):
        #     time.sleep(0.01)
        #     adsk.doEvents()
        
        design.computeAll()
        
        # Construct the output filename. Name will be the same as you‘ve changed    the text into.
        filename = folder + skText.text + '.stl'

・・・
0 Likes
Message 3 of 3

sdi1400294UX4P6
Observer
Observer

omg! it works like a charm! 

Thank you you really saved me from a lot of frustration !!

0 Likes