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: 

Rotated and constrained SketchText can't be updated

1 REPLY 1
Reply
Message 1 of 2
RedHatter15
104 Views, 1 Reply

Rotated and constrained SketchText can't be updated

Hello,

 

Once a SketchText has been rotated and constrained I can't update any of the properties through the API. For example when the script below is ran on the attached file I get "RuntimeError: 2 : InternalValidationError : res".

 

Am I doing something wrong? Is this a bug? Is there a work around?

 

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        sketches = app.activeProduct.rootComponent.sketches
        sketch = sketches.itemByName('Sketch1')
        text = sketch.sketchTexts[0]
        text.fontName = 'Arial'
        text.text = 'B'

    except:
        if ui:
            ui.messageBox(f'Failed:\n{traceback.format_exc()}')
1 REPLY 1
Message 2 of 2
kandennti
in reply to: RedHatter15

Hi @RedHatter15 .

 

I tried various things, and it seems that the green arrow line is having an effect.

1.png

However, simply deleting the line caused an error.

 

Then I changed the size of the text box a little bit, and when I changed it back to the original size and executed it, there was no error.

2.png

This is not an Undo/Redo operation, and the resizing needs to be determined each time.

 

If I delete one line and move the text box, I can see that the vertex of the text box matches the endpoint of the first line I deleted, so I think this is the cause.

3.png

 

However, I am able to change the line in the GUI without any problem.
If this is possible in the GUI, it may be possible to change it using TextCommands, although this is not the preferred method.
I was able to change it with the one I created here.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        txt: adsk.fusion.SketchText = root.sketches[0].sketchTexts[0]

        changeSketchTextProp(txt,'Arial','B')

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


def changeSketchTextProp(
    SktTxt: adsk.fusion.SketchText,
    fontName: str = '',
    text: str = ''):

    app: adsk.core.Application = adsk.core.Application.get()
    ui: adsk.core.UserInterface = app.userInterface
    sels: adsk.core.Selections = ui.activeSelections

    sels.clear()
    sels.add(SktTxt)
    app.executeTextCommand(u'Commands.Start SketchActivate')

    sels.clear()
    sels.add(SktTxt)
    app.executeTextCommand(u'Commands.Start EditMTextCmd')

    if len(fontName) > 0:
        app.executeTextCommand(
            u'Commands.SetString TextFont {}'.format(fontName))

    if len(text) > 0:
        app.executeTextCommand(
            u'Commands.SetTextAreaString TextString {}'.format(text))

    app.executeTextCommand(u'NuCommands.CommitCmd')
    app.executeTextCommand(u'Commands.Start SketchStop')

 

I think the best thing to do is to rethink the way you create sketch text.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report