How to retrieve the face with the largest area

Member Posts: 39
10 Comments First Anniversary 5 Likes Name Dropper
**

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

Best Answers

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    edited March 2024 Answer ✓
    1. my_NS = ExtAPI.DataModel.GetObjectsByName('Casing')[0]
    2. geodata = ExtAPI.DataModel.GeoData
    3. area = 0
    4. my_id = 0
    5. for face_id in my_NS.Ids:
    6. if geodata.GeoEntityById(face_id).Area >area:
    7. area = geodata.GeoEntityById(face_id).Area
    8. my_id = face_id
    9. print('Face Id is: ' + str(my_id) + ' and area is: ' + str(area))
  • Member, Employee Posts: 385
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    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.

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    Answer ✓

    One way to go could be:

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

Answers

  • Member Posts: 39
    10 Comments First Anniversary 5 Likes Name Dropper
    **

    @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.

  • Member Posts: 39
    10 Comments First Anniversary 5 Likes Name Dropper
    **

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

Welcome!

It looks like you're new here. Sign in or register to get started.