Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
mntruell
Posts: 8
Joined: Mon Jun 27, 2022 2:16 am

Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by mntruell »

Using Python, is there a way to look up all objects (shells/faces/edges/vertices) in a body that intersect with a particular point in 3D space?

For example, imagine my body is a cube occupying [0, 1]x[0, 1]x[0, 1]. Given the point (0, 0, 0.5), I'd like to get the Python edge from (0, 0, 0) to (0, 0, 1). Given the point (0, 0.5, 0.5), I'd like to get the face on the YZ-plane in Python.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by Roy_043 »

Code: Select all

doc = App.ActiveDocument
box = doc.addObject("Part::Box", "Box")
doc.recompute()
point = App.Vector(5, 0, 5)
distance, points, info = box.Shape.distToShape(Part.Vertex(point))
print(info)
mntruell
Posts: 8
Joined: Mon Jun 27, 2022 2:16 am

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by mntruell »

What is the structure of info?
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by Roy_043 »

Code: Select all

[('Face', 2, (5.0, 5.0), 'Vertex', 0, None)]:
'Face' = subelement
2 = index of subelement (zero-based)
(5.0, 5.0) = (u, v) parameters of the point.
mntruell
Posts: 8
Joined: Mon Jun 27, 2022 2:16 am

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by mntruell »

Fantastic. Thank you!
mntruell
Posts: 8
Joined: Mon Jun 27, 2022 2:16 am

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by mntruell »

How do I evaluate the subelement at the supplied parameters?
edwilliams16
Veteran
Posts: 3107
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by edwilliams16 »

Code: Select all

face = box.Shape.Faces[2]
print(face.valueAt(5,5)) #-> Vector (5.0, 0.0, 5.0)
face.Surface.normal(5,5)# normal at location etc.
mntruell
Posts: 8
Joined: Mon Jun 27, 2022 2:16 am

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by mntruell »

How do I transform the result of valueAt into a correct 3D point? I'm trying to use it to produce a point cloud of a BREP, but it is giving incorrect results.
User avatar
onekk
Veteran
Posts: 6145
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Get all Shells/Faces/Edges/Vertices at a particular point in 3D space

Post by onekk »

mntruell wrote: Mon Jul 04, 2022 2:32 am How do I transform the result of valueAt into a correct 3D point? I'm trying to use it to produce a point cloud of a BREP, but it is giving incorrect results.
valeAt is returning a Vector() and usually a Vector() will suffice to feed many methods that uses "3d Points" like lines and other entities. In other word where you have to use the returned info?

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply