Edit legends with a UI box in Mechanical

Aria
Aria Member, Employee Posts: 67
25 Answers 25 Likes 10 Comments First Anniversary
✭✭✭✭
edited June 2023 in Structures

It has come to my attention that several users have issues editing the legend in the Mechanical UI due to a graphical defect with certain NVIDIA GPUs.

How can I create a python button in Mechanical that allows me to edit the upper/lower bounds?

Answers

  • Aria
    Aria Member, Employee Posts: 67
    25 Answers 25 Likes 10 Comments First Anniversary
    ✭✭✭✭
    edited June 2023

    Following snippet can be imported into the Button Editor to edit the legend










    Triggering the button will prompt the user to fill in the upper/lower bounds of the contour.





    The code itself is pasted below.

    import clr
    clr.AddReference("Ans.Utilities")
    clr.AddReference('Ans.UI.Toolkit')
    clr.AddReference('Ans.UI.Toolkit.Base')
    import Ansys.UI.Toolkit
    import Ansys.UI.Toolkit.Base
    
    def ChangeContours():
        try:
            
            example = ContourGUI()
            example.Run()
        except:
            ExtAPI.Log.WriteError("Exception in ChangeContours()")
        
    def GetMinMax():
        try:
            legendSettings = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings()
            noBands = legendSettings.NumberOfBands
    
    
            minVal = legendSettings.GetLowerBound(1).Value
            maxVal = legendSettings.GetLowerBound(noBands-1).Value
            return minVal, maxVal
        except:
            ExtAPI.Log.WriteError("Exception in GetMinMax()")
     
    class WindowUI(Ansys.UI.Toolkit.Window):
     
        aLabel = None
        aButton = None
        aTextBox = None
        mainPanel = None
     
        def __init__(self):
            self.Location = Ansys.UI.Toolkit.Drawing.Point(100,100)
            self.Text = 'Contour editor'
            self.Size = Ansys.UI.Toolkit.Drawing.Size(250,150)
            self.StatusBar.Visible = False
            self.__BuildUI() #this will be defined later
            self.BeforeClose += Ansys.UI.Toolkit.WindowCloseEventDelegate(self.Window_Close)
     
        def __BuildUI(self):
            res_unit = Tree.FirstActiveObject.Maximum.Unit
            self.mainPanel = Ansys.UI.Toolkit.TableLayoutPanel()
            self.Add(self.mainPanel)
            self.mainPanel.Columns.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 100)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 33)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 34)
            self.mainPanel.Rows.Add(Ansys.UI.Toolkit.TableLayoutSizeType.Percent, 33)
            omin,omax = GetMinMax()
            
            label1 = 'UPPER - Max Value [' + res_unit + ']'
            self.aLabel = Ansys.UI.Toolkit.Label(label1)
            self.mainPanel.Controls.Add(self.aLabel, 0, 0)
            self.aTextBox = Ansys.UI.Toolkit.TextBox()
            self.aTextBox.Text = omax.ToString()
            self.mainPanel.Controls.Add(self.aTextBox, 1, 0)
            
            label1 = 'LOWER Min Value [' + res_unit + ']'
            self.bLabel = Ansys.UI.Toolkit.Label(label1)
            self.mainPanel.Controls.Add(self.bLabel, 2, 0)
            self.bTextBox = Ansys.UI.Toolkit.TextBox()
            self.bTextBox.Text = omin.ToString()
            self.mainPanel.Controls.Add(self.bTextBox, 3, 0)
    
    
    
    
            self.aButton = Ansys.UI.Toolkit.Button('Update Contour')
            self.aButton.Click += Ansys.UI.Toolkit.EventDelegate(self.On_Click)
            self.mainPanel.Controls.Add(self.aButton, 4, 0)
     
        def On_Click(self, sender, args):
            # Here I update the legend
            res_unit = Tree.FirstActiveObject.Maximum.Unit
            upperVal = Quantity(float(self.aTextBox.Text), res_unit)
            lowerVal = Quantity(float(self.bTextBox.Text), res_unit)
    
    
            legendSettings = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings()
            noBands = legendSettings.NumberOfBands
            
            legendSettings.SetLowerBound(noBands-1,upperVal)
            legendSettings.SetLowerBound(1,lowerVal)
    
    
        
        def Window_Close(self, sender, args):
            Ansys.UI.Toolkit.Application.Quit()
     
    class ContourGUI:
     
        ex1 = None
        def __init__(self):
            self.ex1 = WindowUI()
        
        def Run(self):
            Ansys.UI.Toolkit.Application.Initialize()
            self.ex1.Show()
            Ansys.UI.Toolkit.Application.Run()
            
    ChangeContours()