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: 

Bug: Setting SketchText properties resets text height since v. 2.0.10244

10 REPLIES 10
Reply
Message 1 of 11
thomasa88
660 Views, 10 Replies

Bug: Setting SketchText properties resets text height since v. 2.0.10244

Fusion 360 v. 2.0.10244 breaks the sketch text API. Changing the any property of a SketchText results in the height of the text being reset to 1.0 (cm).

 

This means that the workaround of getting the height first, setting properties, and the restoring the height, does not work.

 

In the GUI, create a new document. Create a sketch with a sketch text. Set the sketch text height to 20 mm.

 

In the Python console (Ctrl+Alt+C -> py), execute the following lines:

app = adsk.core.Application.get() 
t = app.activeProduct.rootComponent.sketches[0].sketchTexts[0]  

t.height 
3.0

t.text="new" 

t.height 
1.0

t.height=3.0

t.height 
1.0

 

This breaks my ParametricText add-in \o/. Issue: https://github.com/thomasa88/ParametricText/issues/21

 

I think you broke something similar with text spacing in December 2020. https://forums.autodesk.com/t5/fusion-360-api-and-scripts/cannot-select-shx-fonts-on-sketchtext-obje...

Labels (1)
  • bug
10 REPLIES 10
Message 2 of 11
thomasa88
in reply to: thomasa88

A possible workaround could be to set the parameter that is now associated with the text height.

However, if it can't be looked up by parameter name, we will have to do a search through all parameters, which I think will lead to a performance hit. Also, I cannot find any property in ModelParameter that has a matching property in SketchText (my first guess was role (Text1-Height), but we cannot know what text is Text1).

 

d = app.activeProduct

p=d.allParameters.itemByName('d1') 

p.value 
1.0
p.value=3.0 

p.name 
d1

p.component 
<adsk.fusion.Component; proxy of <Swig Object of type 'adsk::core::Ptr< adsk::fusion::Component > *' at 0x0000024506A9BDE0> >

p.role 
Text1-Height

t.height 
3.0
Message 3 of 11
kandennti
in reply to: thomasa88

Hi @thomasa88 .

 

I found a way to access the Sketch Text Height Parameter.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback
import json

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

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

        # get Height Parameter
        prm = getTextHeightParameter(txt)

        # resize
        prm.value = 2

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


def getTextHeightParameter(
    txt :adsk.fusion.SketchText
    ) -> adsk.fusion.ModelParameter:

    def getProp(id :int) -> dict:
        return json.loads(app.executeTextCommand(u'PEntity.Properties {}'.format(id)))

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

    sels.clear()
    sels.add(txt)

    # get ID
    txtId = int(app.executeTextCommand(u'Selections.List').split(':')[-1])
    sels.clear()

    # get parameter Name
    textExProp = getProp(txtId)
    dcDoubleParameterProp = getProp(textExProp['textInfo']['heightDCParam']['entityId'])
    parameterProp = getProp(dcDoubleParameterProp['parameter']['entityId'])
    prmName = parameterProp['userName']

    # get sketch text Height Parameter
    prms :adsk.fusion.ParameterList = txt.parentSketch.parentComponent.parentDesign.allParameters
    return prms.itemByName(prmName)

 

It is faster than this method.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/sketchtexts-createinput2-method-height-par... 

 

Message 4 of 11
thomasa88
in reply to: kandennti

Thanks @kandennti  , you are the master of TextCommands.

 

However, I believe the TextCommands in themselves being a performance hit (haven't measured though) and the select+unselect procedure is not acceptable for my add-in.

 

I tried digging for the entityId using the regular API, but I did not find a match (entityId is a number while entityToken is something base64-encoded).

 

I first thought that the entityToken for the parameter and the SketchText was the same (what!), but it turns out that all objects' entity tokens look very similar - but differ at the end.. (Also, I know that entityTokens are unstable and should no be compared)

d1=design.allParameters.itemByName('d1')
d1.entityToken 
/v4BAAAARlJLZXkAH4sIAAAAAAAA/zNQsFAwAEJDBSMgBrOMLIAsQwUTkIiZwso3N0+lfHK9s/Dbq9z9J/eUA1UYKRibKSQmGlkapaZY6lpYpFnqmqQapuhaGBul6BqYp6WZmZgCpc2TwWqBZjCgAbB9Bgpg60DWG1maAACiqmK+iAAAAA==

sketch_text.entityToken 
/v4BAAAARlJLZXkAH4sIAAAAAAAA/zNQsFAwAEJDBSMgBrOMLIAsQwUTkIiZwso3N0+lfHK9s/Dbq9z9J/eUA1UYKRibKSQmGlkapaZY6lpYpFnqmqQapuhaGBul6BqYp6WZmZgCpc2TwWqBZjCgAbB9Bgpg60DWG1kYAAD6XxSgiAAAAA==

d1.entityToken==sketch_text.entityToken
False # :(



#not close enough - need the SketchText object:
d1.createdBy 
'
<adsk.fusion.Sketch; proxy of <Swig Object of type 'std::vector< adsk::core::Ptr< adsk::fusion::Sketch > >::value_type *

 

Message 5 of 11
kandennti
in reply to: thomasa88

Unfortunately, I have not found a way to get the entityId without using selection.


As for entityTokens, we users will still not be able to understand them in the future.


The most desirable thing would be for the bug to be fixed.

Message 6 of 11
thomasa88
in reply to: thomasa88

Latest idea!


1. Save all .value from sketch_text.parentSketch.parentComponent.modelParameters.
2. Change the sketch.
3. Loop modelParameters again. If a value does not match the original value, restore it.
4. Tada!...

 

It seems to work, but I was hoping to be able to detect if the bug was in effect so that I could turn off this potentially heavy workaround if the bug was not there.

I thought that I could let my workaround only apply if SketchText.height != 1.0 and then if the correction did not have to correct any values --> disable the workaround.

However, SketchText.height value seems to be trash, so I can only tell if I needed to do a fix by correcting the value (keep the workaround on), but not if a fix is no longer needed, as I cannot see if the user used a text with a height that is affected by the bug (i.e. the user has a text with height = 1.0 cm). 😞

 

I know that it is a double, but come on.. (height is set to 20mm in the GUI):

sketch_text.height
0.9999999999999996

Message 7 of 11
thomasa88
in reply to: thomasa88

Edit:

To check: What happens if a user has assigned an expression? Will we overwrite it? Will the expression itself "protect" the height? (Likely, it will just be reset)

Message 8 of 11
MichaelT_123
in reply to: thomasa88

Hi Mr ThomasA88,

 

You seem to be quite desperate with the issue.😓

Well,... I do understand. 

I have been shaken by textObject development in F360 quite a few times. So if you are interested, it is not difficult to find out why&where&how.😉

In your current case (under the condition that textOnPath is not on your agenda), find an old file (~> one year old) with textObject in it and run your height changing script on it. There is a good chance that it will work.

 

Regards

MichaelT

MichaelT
Message 9 of 11
thomasa88
in reply to: thomasa88

Hi MichaelT, I need my add-in to work for every condition, as ParametricText is a general add-in, so using an old document is unfortunately a viable solution.

 

For anyone that's interested, my current workaround is done in two steps:

  1. Check if Fusion 360 is affected by the bug
    1. Store parent component model parameters' expressions
    2. Change text height of sketch to a value not close to 1.0
    3. Read back the value. Is it close to 1.0 (Fusion sometimes sets it to 0.99999996), then the bug is present.
    4. Loop through the model parameters and restore the one that was changed
  2. If the bug is present:
    1. Store parent component model parameters' expressions
    2. Change the sketch text
    3. Loop through the model parameters and restore the one that was changed

 

EDIT: And I don't want anyone to mark this as the solution of this issue, as it is only a workaround!

Message 10 of 11
thomasa88
in reply to: thomasa88

ParametricText users are now reporting problems with the workaround for the bug. https://github.com/thomasa88/ParametricText/issues/46

 

A quick test with the code in the original post in this forum thread indicates that the bug might now be gone. Can anyone else confirm?

Message 11 of 11
BrianEkins
in reply to: thomasa88

I tested the code in the first post in this thread, and it changes the text without changing the size. The size remains constant.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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