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

Member, Moderator, Employee Posts: 312
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited January 2024 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

  • Member, Moderator, Employee Posts: 312
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited January 2024

    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.

    1. bx=0.50 # location of interest
    2. by=0.50
    3. bz=4.50
    4. dist=1E9
    5.  
    6.  
    7.  
    8.  
    9. bodies = ExtAPI.DataModel.Project.Model.GetChildren(DataModelObjectCategory.Body,True)
    10. for body in bodies:
    11. bc=body.GetGeoBody().Centroid
    12. cx=bc[0]
    13. cy=bc[1]
    14. cz=bc[2]
    15. distc=sqrt((bx-cx)**2+(by-cy)**2+(bz-cz)**2)
    16. if distc<dist:
    17. myb=body.GetGeoBody().Id
    18. dist=distc
    19.  
    20. selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    21. selection.Ids=[myb]
    22. ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    23. ns.Location=selection

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

    1. bx=1.00
    2. by=0.50
    3. bz=1.00
    4. dist=1E9
    5.  
    6. meshData= ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    7. nodes=meshData.Nodes
    8.  
    9. for node in nodes:
    10. cx=node.X
    11. cy=node.Y
    12. cz=node.Z
    13.  
    14. distc=sqrt((bx-cx)**2+(by-cy)**2+(bz-cz)**2)
    15. if distc<dist:
    16. myn=node.Id
    17. myb=node.BodyIds[0] # body id containing node
    18. dist=distc
    19.  
    20. selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.MeshNodes)
    21. selection.Ids=[myn]
    22. ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    23. ns.Location=selection
    24.  
    25. selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    26. selection.Ids=[myb]
    27. ns=ExtAPI.DataModel.Project.Model.AddNamedSelection()
    28. ns.Location=selection
This discussion has been closed.