Selecting an element/node by its Id, how to find which body the element/node corresponds to?

Options
amusthaf
amusthaf Member Posts: 16
Name Dropper First Comment
edited June 2023 in Structures

I have an element id, but I need the body to which the element corresponds to.

In general, I have a named selection with all the elements of a body. I need to find out to which body the elements correspond to in the assembly.

Best Answer

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

    Hi @amusthaf ,

    The below script should help:

    element_ns = ExtAPI.DataModel.GetObjectsByName('NS_Elements')[0] 
    first_element_id = element_ns.Location.Ids[0]
    
    
    mesh_data = ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    
    
    bodies = ExtAPI.DataModel.Project.Model.Geometry.GetChildren(DataModelObjectCategory.Body, True)
    for body in bodies:
        geobody = body.GetGeoBody()
        mesh_for_body = mesh_data.MeshRegionById(geobody.Id)
        if first_element_id in mesh_for_body.ElementIds:
            print('Element ' + str(first_element_id) + ' is in body named: ' + str(body.Name))
    

    This is NS_Elements:


    This is the geometry structure:


    And the script returns:


    Which we can verify as follows:


Answers

  • Ben Klinkhammer
    Ben Klinkhammer Member, Employee Posts: 4
    Name Dropper First Comment
    edited February 8
    Options

    Hi @Pernelle Marone-Hitz
    This looks great and I can confirm it works well if I have an Analysis system.
    That said, I am running in stand-alone Mechanical and haven't attached an Analysis system yet so when I first try it If I do something like:

    mesh_data = ExtAPI.DataModel.Project.Model.Analyses[0].MeshData

    I get

    Index was out of range. Must be non-negative and less than the size of the collection.

    Attaching an analysis system works around the problem but thought I should see if there is any other approach that doesn't require the Analysis to be defined?

    Thanks,
    Ben

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

    Hi @Ben Klinkhammer , in that situation, you can grab MeshData in another way:
    mesh_data = ExtAPI.DataModel.MeshDataByName('Global')
    This should work.