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

Options
GabrielArias
GabrielArias Member Posts: 3
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 Posts: 208
    First Answer First Comment 5 Likes First Anniversary
    edited November 2023 Answer ✓
    Options
    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 Posts: 208
    First Answer First Comment 5 Likes First Anniversary
    edited October 2023 Answer ✓
    Options

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

Answers