How to select active object ?



I have a script for the creation of an APDL command on object :

joint_33 = DataModel.GetObjectById(33)
command_snippet_37 = joint_33.AddCommandSnippet()
command_snippet_37.Input = r"""toto"""

I would like my script to be more robust and change the first command line.

Is there a way to select the object not by the ID but with the selection on Mechanical tree ?


Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    Hi @Jean,
    There are many ways to select objects in the Mechanical tree in a more robust manner.
    To select the active object, use Tree.FirstActiveObject (you can also refer to this post)
    You can also select objects based on their names: ExtAPI.DataModel.GetObjectsByName() will return a list of all objects having the same name (this is robust only if you have unique namings, and hence select the first object in the returned list).
    You can also grab objects based on their types (ie, a named selection, a fixed support, etc) through ExtAPI.DataModel.GetObjectsByType().
    This other post provides examples.