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

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

Welcome!

It looks like you're new here. Sign in or register to get started.

Best Answers

  • Member, Employee, GitHub-issue-creator Posts: 353
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited February 12 Answer ✓
    1. import os
    2. import units
    3.  
    4. def write_legend_settings(result,filepath,guid):
    5. min_qty = result.MinimumOfMinimumOverTime
    6. max_qty = result.MaximumOfMaximumOverTime
    7. minimum = units.ConvertUnit(min_qty.Value, min_qty.Unit, "Pa")
    8. maximum = units.ConvertUnit(max_qty.Value, max_qty.Unit, "Pa")
    9. 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)
    10. with open(filepath,'w') as f:
    11. f.write(template)
    12.  
    13.  
    14. def import_settings(filepath):
    15. cmds = '''
    16. var filePath = "{}";
    17.  
    18. var refLegendStyleMgr = WB.ScriptEngine.Engine.CreateActiveXObject(WB.ScriptEngine.Engine.GenWBProgId("AnsCoreObjects.AnsLegendStyleManager"));
    19. var callback = WB.ScriptEngine.Engine.CreateActiveXObject(WB.ScriptEngine.Engine.GenWBProgId("AnsCoreObjects.AnsDelegate"));
    20. callback.AddCOM(this, "OnOverwriteStyle");
    21.  
    22. refLegendStyleMgr.ImportStyles(filePath, callback,true);
    23. '''.format(filepath.replace('\\','\\\\'))
    24.  
    25. ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    26.  
    27.  
    28. maxstress=ExtAPI.DataModel.Project.Model.Analyses[0].Solution.Children[1]
    29. folder = r'D:\Temp'
    30. legend_xml = os.path.join(folder,'legend_settings.xml')
    31. guid="{79E20481-F97C-46CF-8389-A1B633E3E555}"
    32.  
    33. write_legend_settings(maxstress,legend_xml,guid)
    34.  
    35. import_settings(legend_xml)
    36.  
    37. cmds = '''
    38. DS.Graphics.GfxUtility.Legend.ApplyLegendStyle("{}")
    39. '''.format(guid)
    40.  
    41. ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    42.  

    Note that in newer versions of Ansys (~2024), it may be necessary to replace

    1. cmds = '''
    2. DS.Graphics.GfxUtility.Legend.ApplyLegendStyle("{}")
    3. '''.format(guid)

    with

    1. cmds = '''
    2. DS.Graphics.GfxUtility.Legend.LegendStyle="{}"
    3. '''.format(guid)

    See discussion here: https://discuss.ansys.com/discussion/comment/5911#Comment_5911

  • Member, Employee, GitHub-issue-creator Posts: 353
    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

Welcome!

It looks like you're new here. Sign in or register to get started.