How to retrieve the face with the largest area

Options
Akane Ito
Akane Ito Member Posts: 13
Name Dropper First Comment

I want to retrieve the ID of the face with the largest area among those selected in 'named selection'

Best Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited March 1 Answer ✓
    Options
    my_NS = ExtAPI.DataModel.GetObjectsByName('Casing')[0]
    geodata = ExtAPI.DataModel.GeoData
    area = 0
    my_id = 0
    for face_id in my_NS.Ids:
        if geodata.GeoEntityById(face_id).Area >area:
            area = geodata.GeoEntityById(face_id).Area
            my_id = face_id
    print('Face Id is: ' + str(my_id) + ' and area is: ' + str(area))
    
  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 279
    First Anniversary First Comment 5 Likes Ansys Employee
    Answer ✓
    Options

    You can also use a python sort method on a list and sort on the Area property. Then look at the last in the list. This is less coding and probably faster than a for loop.

  • 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

    One way to go could be:

    my_NS = ExtAPI.DataModel.GetObjectsByName('Casing')[0]
    geodata = ExtAPI.DataModel.GeoData
    face_tuples = [(geodata.GeoEntityById(face_id), geodata.GeoEntityById(face_id).Area) for face_id in my_NS.Ids]
    sorted_faces = sorted(face_tuples, key=lambda face: face[1]) 
    print('Face Id is: ' + str(sorted_faces[-1][0].Id) + ' and area is: ' + str(sorted_faces[-1][1]))
    

Answers

  • Akane Ito
    Akane Ito Member Posts: 13
    Name Dropper First Comment
    Options

    @Mike.Thompson
    Please teach me the specific method you just told me.
    I don't want just the largest area, but I want to get up to the second largest one.

  • Akane Ito
    Akane Ito Member Posts: 13
    Name Dropper First Comment
    Options

    Thank you, I was able to do it with the method you taught me