How to copy legend settings from a Mechanical result object to a Figure using scripting?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 249
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

How to copy legend settings from a Mechanical result object to a Figure using scripting?
Currently Figures don't support native legendsettings API.

Tagged:

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 249
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    The below script (tested in 25R1) shows the automation script for the below steps. Please note that below script needs unit system in Mechanical to be in MKS (during export/import of legend) which is taken care of by the script as well.

    1. Export legend settings from a particular result object (say Equivalent Stress) as an xml file
    2. Extract GUID from the exported xml
    3. Import the legend as a Named legend back into Mechanical
    4. Apply this legend to a figure object
    import os
    import units
    import re
    
    #Inputs: Name and Directory where the legend file is exported
    xmlFileName = "test.xml"
    xmlFileDir = r"D:\del5"
    xmlFilePath = os.path.join(xmlFileDir,xmlFileName)
    
    #Inputs: Specify from which object (Eqv Stress) to which object (Figure) legend settings are copied
    parentObj = DataModel.AnalysisList[0].Solution.Children[2]
    childObj = parentObj.Children[0]
    
    #-----------------------------------------------------------------------------------
    #Function to import a named legend xml file into Mechanical
    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)
    
    #Function to read guid from a legend xml file
    def extract_guid_from_xml(file_path):
        # Read the XML file
        with open(file_path, "r") as file:
            xml_content = file.read()
    
        # Regular expression to match the GUID
        guid_pattern = r'guid="([^"]+)"'
    
        # Search for the GUID
        match = re.search(guid_pattern, xml_content)
    
        if match:
            return match.group(1)  # Return the GUID
        else:
            return None  # Return None if no GUID is found
    #-----------------------------------------------------------------------------------
    
    #Extract Current Unit system and activate mks unit system
    currUnitSys = ExtAPI.Application.ActiveUnitSystem
    ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardMKS
    
    #Activate Parent Object and Export the Legend as an XML file
    parentObj.Activate()
    legendSettings = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings()
    legendSettings.ExportLegend(xmlFilePath)
    
    #Extract guid from the exported legend file
    guid = extract_guid_from_xml(xmlFilePath)
    
    #import the legend
    import_settings(xmlFilePath)
    
    cmds = '''
    DS.Graphics.GfxUtility.Legend.LegendStyle="{}"
    '''.format(guid)
    
    #Activate the figure object needed
    childObj.Activate()
    
    ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(cmds)
    Tree.Refresh()
    ExtAPI.Application.ActiveUnitSystem = currUnitSys