Using Mechanical scripting, how do I search for objects in the tree?

Landon Mitchell Kanner
Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 324
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

For example, how would I get a pressure load named MyPressure or all Named Selections scoped to nodes?

Tagged:

Best Answer

Answers

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 324
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Here are two useful methods for searching for objects in the Mechanical tree:

    ExtAPI.DataModel.GetObjectsByType()
    ExtAPI.DataModel.GetObjectsByName()
    

    Let's take a look at their usage in the context of your examples. To get the pressure loads named MyPressure:

    objs_named_MyPressure = ExtAPI.DataModel.GetObjectsByName('MyPressure')
    all_pressure_load_objs = ExtAPI.DataModel.GetObjectsByType(Ansys.ACT.Automation.Mechanical.BoundaryConditions.Pressure)
    pressure_objs_named_MyPressure = [i for i in objs_named_MyPressure if i in all_pressure_load_objs]
    pressure_objs_named_MyPressure 
    

    Note that pressure_objs_named_MyPressure will be a list of all the pressure objects named MyPressure. If we are only listed in the 1st such object:

    MyObj = pressure_objs_named_MyPressure[0]
    

    To get all Named Selections scoped to nodes:

    NSs = ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection)
    node_NSs = [ns for ns in NSs if ns.Location.SelectionType == ns.Location.SelectionType.MeshNodes]
    

    Note that there are two different ways to specify the argument for GetObjectsByType(), which have both been demonstrated in the above examples: You can either use the full object type or the DataModelObjectCategory. To get the full object type, select a representative object in the tree and run the following command in the scripting console:

    Tree.FirstActiveObject.GetType()
    

    Alternatively, you can browse for a DataModelObjectCategory by typing

     DataModelObjectCategory.
    

    in the scripting console and searching for the appropriate option: