How to get a list of each object in named selection use script

ZZ.tang Member Posts: 14
Name Dropper First Comment
edited December 2023 in Structures

Ansys 2023r1

And how to get the location or area information of these geometrys.


Best Answers

  • ZZ.tang
    ZZ.tang Member Posts: 14
    Name Dropper First Comment
    Answer ✓

    def Geo_list():
    for geoid in ExtAPI.SelectionManager.CurrentSelection.Ids :
    geoEntity = DataModel.GeoData.GeoEntityById(geoid)
    return geo_list

    for geo in Geo_list():

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 221
    First Answer First Comment 5 Likes First Anniversary
    Answer ✓
    all_named_selections = ExtAPI.DataModel.GetObjectsByType(Ansys.ACT.Automation.Mechanical.NamedSelection)
    for ns in all_named_selections:
        print "named selection: {}".format(ns.Name)
        entities = ns.Entities
        for entity in entities:
            print "  entity: {}".format(entity.Id)
            print "    area: {}".format(entity.Area)
            print "    centroid: {}".format(entity.Centroid)

    Note: Depending on the type of the entity that is in the named selection (body, face, edge, vertex, node, element, or element face), the last two lines of the above code may behave differently. This version was tested using a face Named Selection.


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

    Hi @ZZ.tang , the code you shared will work on the active selection in Mechanical, not on a named selection. For it to work on a named selection, the principle will be the same:

    • access the named selection (for example by its name)
    • check the ids of the entities scoped in that NS
    • use GeoData to obtain information on the geometry