Script Tip Friday- Filter The Results of a dir Command
Ansys Structures
Structural analysis for every application and experience level
We have dual forces for today’s Script Tip: Pernelle Marone Hitz , Lead APplications Engineer at Ansys, and Vishnu Venkataraman, Sr. Applications Engineer at Ansys. In this tip we’ll go over how to filter the results of a?dir?command.
Pernelle: To check all available methods and properties of an object we often use the?dir()?command.
For example, to check what’s available for an object referenced by “connect”, we’ll use:
dir(connect)
If the returned list is long, it is very useful to filter it by some keywords that we’re interested in. For this, the?filter()?method can be used. For example, to get all methods and properties that have a “Geometry” keyword in their name, use:
领英推荐
filter(lambda x: ("Geometry" in x), dir(connect))
Vishnu: To create a button using what Pernelle mentioned above could be useful tool too.
All you need to do is import the button in mechanical to use this.
How to use:
# Script:
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
a = ExtAPI.DataModel.Tree.ActiveObjects[0]
class Example1Window(Ansys.UI.Toolkit.Window):
aLabel = None
aButton = None
aTextBox = None
mainPanel = None
def __init__(self):
self.Location = Ansys.UI.Toolkit.Drawing.Point(500,500)
self.Text = 'search string'
self.Size = Ansys.UI.Toolkit.Drawing.Size(500,500)
self.StatusBar.Visible = False
self.__BuildUI() # this will be defined later
self.BeforeClose += Ansys.UI.Toolkit.WindowCloseEventDelegate(self.Window_Close)
def __BuildUI(self):
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)
self.aLabel = Ansys.UI.Toolkit.Label('String Below')
self.mainPanel.Controls.Add(self.aLabel, 0, 0)
self.aTextBox = Ansys.UI.Toolkit.TextBox()
self.aTextBox.Text = 'Search string'
self.mainPanel.Controls.Add(self.aTextBox, 1, 0)
self.aButton = Ansys.UI.Toolkit.Button('Search')
self.aButton.Click += Ansys.UI.Toolkit.EventDelegate(self.On_Click)
self.mainPanel.Controls.Add(self.aButton, 2, 0)
def On_Click(self, sender, args):
clr.AddReference("Ans.UI.Toolkit")
clr.AddReference("Ans.UI.Toolkit.Base")
mystring = str(self.aTextBox.Text)
try:
result = filter(lambda x: mystring in x.lower(), dir(a)+dir(a.InternalObject))
except:
result = filter(lambda x: mystring in x.lower(), dir(a))
Ansys.UI.Toolkit.MessageBox.Show(self, str(result), 'Attributes found -->', Ansys.UI.Toolkit.MessageBoxType.Info, Ansys.UI.Toolkit.MessageBoxButtons.OK)
print self.aTextBox.Text
def Window_Close(self, sender, args):
Ansys.UI.Toolkit.Application.Quit()
class ExampleOne:
ex1 = None
def __init__(self):
self.ex1 = Example1Window()
def Run(self):
Ansys.UI.Toolkit.Application.Initialize()
self.ex1.Show()
Ansys.UI.Toolkit.Application.Run()
example = ExampleOne()
example.Run()
Doctorant CIFRE en ingénierie mécanique et numérique Safran / ENSTA - école doctorale de l'Institut Polytechnique de Paris
1 年Hello, do you know some documentation / tutorial to use Ansys.UI.Toolkit ? Thanks.
Helping Ansys users get the most out of structural simulation through scripting
2 年Hi Pavel Urubcik, I'm not sure to understand what do you mean by the unit box. In this example Vishnu used the UI Toolkit but indeed something similar could have been done with Windows Form.
team lead, leadership, FEA simulation and its automation/programming
2 年What do you suggest to use for the unit box? Why the UI toolkit as compared for example to windows forms?