Example of exporting and importing legend settings

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

Example of exporting and importing legend settings

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited December 2023
    Options

    Below is an example. Note - it uses undocumented / unsupported commands.

    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-9999-46CF-8389-A1B633E3E555}"
    
    write_legend_settings(maxstress,legend_xml,guid)
    
    import_settings(legend_xml)
    
    legend = ExtAPI.DataModel.InternalObject["ds"].Graphics.GfxUtility.Legend
    legend.ApplyLegendStyle(guid)