How to assign a legend style to an object by referencing the guid in Ansys 2025R1

Matthias
Matthias Member Posts: 27
10 Comments Name Dropper First Anniversary
**

@Landon Mitchell Kanner

I have been using the same commands that you have posted here for some releases. However, they stopped working in one of the 2024 releases and are not working in 2025R1:

cmds = '''
DS.Graphics.GfxUtility.Legend.ApplyLegendStyle("{}")
'''.format(guid)
ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)

What commands can I use in 2025R1? Python is of course preferred.

Best Answer

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 353
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited February 12 Answer ✓
    cmds = '''
    DS.Graphics.GfxUtility.Legend.LegendStyle="{}"
    DS.Graphics.Redraw(true)
    '''.format(guid)
    
    ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    

Answers

  • Matthias
    Matthias Member Posts: 27
    10 Comments Name Dropper First Anniversary
    **

    And if somebody is looking at the changes which have been done in JS, could you please also check if retrieving the legend style is still working?

    res_lgd_stl = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand("""res_lgd_stl = DS.Graphics.GfxUtility.Legend.LegendStyle
    returnFromScript(res_lgd_stl)""")

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 353
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    It is working on my end at 2025R1 and R2

  • Matthias
    Matthias Member Posts: 27
    10 Comments Name Dropper First Anniversary
    **

    @Landon Mitchell Kanner
    Can you please check if the legend style in your models is really applied correctly?

    When running your script from the other thread the values from the legend style are copied to the legend of the object. But there is no thorough linking of the legend to the legend style.

    The name of your legend style is "0_9999". Yet it is not linked to the active object in the tree. The style is still shown as "unnamed".

    Can you confirm this behavior?

    In 2023R2 your script as well as mine are working as intended as I have stated in my first post. See figure below

  • Matthias
    Matthias Member Posts: 27
    10 Comments Name Dropper First Anniversary
    **

    @Landon Mitchell Kanner
    This is exactly the code I am using.

    Does it really assign the legend style in your model? If you right click on the legend and go to named legends is there a check mark in front of your legend style?

    Or does it just copy/assign the values without actually linking/assigning the style as shown in the first figure above?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 353
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    @Matthias said:
    And if somebody is looking at the changes which have been done in JS, could you please also check if retrieving the legend style is still working?

    res_lgd_stl = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand("""res_lgd_stl = DS.Graphics.GfxUtility.Legend.LegendStyle
    returnFromScript(res_lgd_stl)""")

    Yes, this is still working in 2025R1

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 353
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    @Matthias said:
    @Landon Mitchell Kanner
    This is exactly the code I am using.

    Does it really assign the legend style in your model? If you right click on the legend and go to named legends is there a check mark in front of your legend style?

    Or does it just copy/assign the values without actually linking/assigning the style as shown in the first figure above?

    Based on my testing at 25.1:
    LegendStyle="{guid}" assigns the legend style
    ApplyLegendStyle("{guid}") copies the values without actual assigning

    My guess is it should have always been this way but in older versions ApplyLegendStyle had a bug where it would also assign the legend style

  • Matthias
    Matthias Member Posts: 27
    10 Comments Name Dropper First Anniversary
    **

    @Landon Mitchell Kanner said:

    @Matthias said:
    And if somebody is looking at the changes which have been done in JS, could you please also check if retrieving the legend style is still working?

    res_lgd_stl = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand("""res_lgd_stl = DS.Graphics.GfxUtility.Legend.LegendStyle
    returnFromScript(res_lgd_stl)""")

    Yes, this is still working in 2025R1

    Thanks for checking.

    Regarding the assignment issue: Did I describe the issue that occurs in 2025R1 in my test model clear enough? And can you reproduce it?

  • Matthias
    Matthias Member Posts: 27
    10 Comments Name Dropper First Anniversary
    **

    @Landon Mitchell Kanner said:

    cmds = '''
    DS.Graphics.GfxUtility.Legend.LegendStyle="{}"
    '''.format(guid)
     
    ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    

    @Landon Mitchell Kanner
    Sorry, my bad. I did overlook the change in the code from
    Legend.ApplyLegendStyle="{}"
    to
    Legend.LegendStyle="{}"

    I have not been using the latter and will try it out.