How to get a body or node id associated with a known coordinate point/location

Erik Kostson
Erik Kostson Member, Employee Posts: 87
First Answer Name Dropper First Anniversary Ansys Employee
edited January 2 in Structures

Say we have a point/location of interest (x,y,z coordinates), is there a way to get a body closest to this point?

Comments

  • Erik Kostson
    Erik Kostson Member, Employee Posts: 87
    First Answer Name Dropper First Anniversary Ansys Employee
    edited January 2

    Below is a sample code that does this (can of course be done in other ways also). Given a location it will find the nearest body centroid to that point and create a named selection.

    bx=0.50 # location of interest 
    by=0.50
    bz=4.50
    dist=1E9
    
    
    
    
    bodies =  ExtAPI.DataModel.Project.Model.GetChildren(DataModelObjectCategory.Body,True)
    for body in bodies:
        bc=body.GetGeoBody().Centroid
        cx=bc[0]
        cy=bc[1]
        cz=bc[2]
        distc=sqrt((bx-cx)**2+(by-cy)**2+(bz-cz)**2)
        if distc<dist:
            myb=body.GetGeoBody().Id
            dist=distc
    
    selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    selection.Ids=[myb]
    ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    ns.Location=selection
    

    Same thing (find nearest node to location of interest) can be done for a node:

    bx=1.00
    by=0.50
    bz=1.00
    dist=1E9
    
    meshData= ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    nodes=meshData.Nodes
    
    for node in nodes:
        cx=node.X
        cy=node.Y
        cz=node.Z
    
        distc=sqrt((bx-cx)**2+(by-cy)**2+(bz-cz)**2)
        if distc<dist:
            myn=node.Id
            myb=node.BodyIds[0] # body id containing node
            dist=distc
    
    selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.MeshNodes)
    selection.Ids=[myn]
    ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    ns.Location=selection
    
    selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    selection.Ids=[myb]
    ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    ns.Location=selection
    
This discussion has been closed.