How can I read a legend settings from a file from a script?

GabrielArias
GabrielArias Member Posts: 3
First Anniversary Name Dropper First Comment
**
edited October 2023 in Structures

I have saved many Legends settings in xml files. I can access them interactively using the "Manage Named Legends" RMC on the results legend -> Named Legends -> Manage -> Import

Also once the legends settings are loaded. How do I pick one from the list in my mechanical script?

Thanks a lot

Best Answers

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 319
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited November 2023 Answer ✓
    import os
    import units
    
    def write_legend_settings(result,filepath,guid):
        min_qty = result.MinimumOfMinimumOverTime
        max_qty = result.MaximumOfMaximumOverTime
        minimum = units.ConvertUnit(min_qty.Value, min_qty.Unit, "Pa")
        maximum = units.ConvertUnit(max_qty.Value, max_qty.Unit, "Pa")
        template = '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?><styles version="11"><style colorMode="0" contourCount="9" editable="-1" factoryStyle="0" guid="{}" independentBands="0" log="0" mode="1" name="0_9999" namedStyle="-1" numericMode="1" orientation="1" significantDigits="5" transparency="0" unitless="0"><band color="0" colorAuto="-1" label="" value="{}" valueAuto="0"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band color="0" colorAuto="-1" label="" value="0" valueAuto="-1"/><band value="{}" valueAuto="0"/></style></styles>'''.format(guid,minimum,maximum)
        with open(filepath,'w') as f:
            f.write(template)
    
    
    def import_settings(filepath):
        cmds = '''
    var filePath = "{}";
    
    var refLegendStyleMgr = WB.ScriptEngine.Engine.CreateActiveXObject(WB.ScriptEngine.Engine.GenWBProgId("AnsCoreObjects.AnsLegendStyleManager"));
    var callback = WB.ScriptEngine.Engine.CreateActiveXObject(WB.ScriptEngine.Engine.GenWBProgId("AnsCoreObjects.AnsDelegate"));
    callback.AddCOM(this, "OnOverwriteStyle");
    
    refLegendStyleMgr.ImportStyles(filePath, callback,true);
    '''.format(filepath.replace('\\','\\\\'))
    
        ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    
    
    maxstress=ExtAPI.DataModel.Project.Model.Analyses[0].Solution.Children[1]
    folder = r'D:\Temp'
    legend_xml = os.path.join(folder,'legend_settings.xml')
    guid="{79E20481-F97C-46CF-8389-A1B633E3E555}"
    
    write_legend_settings(maxstress,legend_xml,guid)
    
    import_settings(legend_xml)
    
    cmds = '''
    DS.Graphics.GfxUtility.Legend.ApplyLegendStyle("{}")
    '''.format(guid)
    
    ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    
    
  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 319
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited October 2023 Answer ✓

    @GabrielArias, I agree. @sachinverghese and @Pierre Thieffry, here is an example where Mechanical scripting users still need to rely on Jscript.

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
  • GabrielArias
    GabrielArias Member Posts: 3
    First Anniversary Name Dropper First Comment
    **

    Thanks @Pernelle, I've read your article before, I've used it to customise my legends, but one thing I cannot control is the addition of the independent bands, either top, bottom or both. It seems that the object does not have the IndependentBands property. The problem, not having this, is that when the settings are copied from a legend (MakeCopy) with independent bands to a new one (Copyto), the new legend looks different as the independent bands are not applied. The workaround I've found is to save the legend in a xm files (manage named legends tool) and then assign it to the new result.

    Not sure if there is a way to control the independent bands from the script, if there is it would be much appreciated if you can tell me how to access it.

    Thanks again for you help

  • GabrielArias
    GabrielArias Member Posts: 3
    First Anniversary Name Dropper First Comment
    **

    Thanks a lot Landon, this is what I need, it is bit cumbersome, but it does the job. It would be nice to have a more direct way of implementing this in Python without calling jscripts, but happy with the results.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    @Landon Mitchell Kanner in 2023R2, Legend attribute has been removed from GfxUtility:

    Do you have a suggestion for more recent versions ?

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

    @Pernelle Marone-Hitz I have updated to remove the dependency on InternalObject. Please let me know if it is working.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    @Landon Mitchell Kanner , thanks, this works.

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

    Which module contains "ExtAPI"?

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

    What is the module that contains ExtAPI?

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

    ExtAPI is already available in Mechanical Scripting. Outside of Mechanical Scripting, see:
    https://discuss.ansys.com/discussion/1444/is-it-possible-to-access-extapi-from-the-project-context-in-workbench

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

    @Landon Mitchell Kanner
    Is it now (Version 2024R2) possible or will it soon (2025R1) be possible to import the legend styles from xml files into mechanical without using JS?

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

    @Matthias said:
    @Landon Mitchell Kanner
    Is it now (Version 2024R2) possible or will it soon (2025R1) be possible to import the legend styles from xml files into mechanical without using JS?

    Maybe @mohamed-koubaa or someone on @AKD-Scripting-Team knows..

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 105
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭
    edited December 18

    @Landon Mitchell Kanner @Matthias

    If you are looking for a way to import settings from xml file and apply it, then it is available since 2020R2. Hope it helps.

    ls = ExtAPI.Graphics.ImportLegend("D:/test_legend.xml", unit='mm') 
    ls.CopyTo(Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings())
    
  • Matthias
    Matthias Member Posts: 13
    10 Comments Name Dropper First Anniversary
    **

    @Rajesh Meena
    When I test your suggested approach in 2024R2 the legend style is not displayed if I right click on the legend and then select 'Named Legends'. And also, if I select 'Named Legends'\'Manage' the imported style is not displayed. Does it work for you?

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 105
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @Matthias it does not create imported named legend object, rather it will just create legend settings using xml which you can copy to your current legend settings (instead of you setting all the attributed individually via script).

    Does it not show effects based on xml file you use in the API?

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

    @Rajesh Meena
    The values in the legend are changed. But since it does not create an imported named legend object, it is not suitable for use in our company.

  • Rajesh Meena
    Rajesh Meena Moderator, Employee Posts: 105
    Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
    ✭✭✭✭

    @Matthias Oh Okay. Thanks for informing. I would like to understand why.

    I will also ask again to @mohamed-koubaa if the python API to create named legend is available in latest version. I could not find any.