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

Landon Mitchell Kanner
Landon Mitchell Kanner Member, Employee Posts: 291
100 Comments 25 Answers 25 Likes Photogenic
✭✭✭✭

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

Tagged:

Best Answer

  • kanchan_cadfem
    kanchan_cadfem Member Posts: 20
    First Anniversary 10 Comments Name Dropper First Answer
    **
    Answer ✓

    I think, the Tree.Find has more potential in this case


    Tree.Find(string name, Func[IDataModelObject, bool] func, string tag,ObjectState state)

    Gets a list of all objects with the specified parameters.

    Remarks: Note that all parameters of Find are optional.

    Name: Defaults to ""

    Func: Defaults to null

    Tag: Defaults to ""

    State: Defaults to ObjectState.NoState


    Example: The following command finds all objects with an state of Suppressed.

    ExtAPI.DataModel.Tree.Find(state=ObjectState.Suppressed)
    


    Example: The following command finds all objects with a name of "Force" and a tag of "Load".

    ExtAPI.DataModel.Tree.Find(name="Force", tag="Load")
    


    Example: The following command finds all objects with the name "Pressure", the function "defineByVector",

    the Tag "Test", and a ObjectState of Suppressed.

    def defineByVector(obj):
        if (obj.DefineBy == LoadDefineBy.Vector):
            return True
        return False
    ExtAPI.DataModel.Tree.Find(name="Pressure", func=defineByVector, tag="Test", state=ObjectState.Suppressed)
    


Answers

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 291
    100 Comments 25 Answers 25 Likes Photogenic
    ✭✭✭✭

    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: