How can I read a legend settings from a file from a script?
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
-
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)
0 -
@GabrielArias, I agree. @sachinverghese and @Pierre Thieffry, here is an example where Mechanical scripting users still need to rely on Jscript.
0
Answers
-
Hi @GabrielArias . This post should help: https://discuss.ansys.com/discussion/513/working-with-legends-in-mechanical-scripting
0 -
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
0 -
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.
0 -
@Landon Mitchell Kanner in 2023R2, Legend attribute has been removed from GfxUtility:
Do you have a suggestion for more recent versions ?0 -
@Pernelle Marone-Hitz I have updated to remove the dependency on InternalObject. Please let me know if it is working.
0 -
@Landon Mitchell Kanner , thanks, this works.
0 -
Which module contains "ExtAPI"?
0 -
What is the module that contains ExtAPI?
0 -
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-workbench1 -
@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?0 -
@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..
0 -
@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())
1 -
@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?0 -
@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?
0 -
@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.0 -
@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.
0