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

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

Ansys 2023r1

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

Tagged:

Best Answer

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

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

    for geo in Geo_list():
    Print(geo.Area)

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 734
    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
  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 183
    First Answer First Comment 5 Likes First Anniversary
    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.