How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
turnip
Posts: 5
Joined: Mon Oct 03, 2022 9:05 am

How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by turnip »

How do I get the vertices and related data of the triangular facet of the model in python FreeCAD? I can see the triangular surface in hidden line mode, but I don't know the interface and method to get the data
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
turnip
Posts: 5
Joined: Mon Oct 03, 2022 9:05 am

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by turnip »

thanks for your help, the pages you suggest help me know how to draw a CAD model by coin or shapes, but I can't figure out how to get the vertices and related data of the triangular facet still. I can get relative data by the code in the picture, but it is in String and hard to analyze. Is there any other methods to get the specific data.
Attachments
2SOW~GTUHCA@MSN})[QZJHX.png
2SOW~GTUHCA@MSN})[QZJHX.png (260.34 KiB) Viewed 801 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by onekk »

turnip wrote: Tue Nov 01, 2022 1:34 am ...

Please, first read this:


https://forum.freecadweb.org/viewtopic.php?f=3&t=2264

And post required informations.

They may be important to not gave you generic answers.

FreeCAD did not model using facets, it use other paradigm internally, facets and generally triangulations are generated only to show the image on the screen, and they are subject to some approximation.

See maybe:

Document Objects
https://wiki.freecadweb.org/App_DocumentObject

TopoShapes
https://wiki.freecadweb.org/Part_TopoShape

Mesh
https://wiki.freecadweb.org/Mesh

OpenCascade
https://wiki.freecad.org/OpenCASCADE

Pivy (Coin3d wrapper for Python)
https://wiki.freecadweb.org/Pivy

So there is no triangulations in FC, at least if you model correctly, if you use Mesh, see above link this is another different thing.

So it depends on what you are trying to do and what you are using.

A little note, you are using a "very old" and outdated version of FC 0.18, so it is difficult even to give some help as the underlying libraries could be very different, on some Linux distributions there are problem to run even the 0.19 due to library mismatch, like in my case, so it could be very hard to supply correct hints for 0.18.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by heda »

Code: Select all

>>> meshobj = App.ActiveDocument.Cube
>>> meshobj
Mesh::Cube object at 0x5588990bd0b0
>>> points, topo = meshobj.Mesh.Topology
>>> points
[Vector (-5.0, -5.0, -5.0), Vector (5.0, -5.0, -5.0), Vector (5.0, -5.0, 5.0), Vector (-5.0, -5.0, 5.0), Vector (-5.0, 5.0, -5.0), Vector (5.0, 5.0, 5.0), Vector (5.0, 5.0, -5.0), Vector (-5.0, 5.0, 5.0)]
>>> topo
[(0, 1, 2), (0, 2, 3), (4, 5, 6), (4, 7, 5), (0, 7, 4), (0, 3, 7), (1, 6, 5), (1, 5, 2), (0, 4, 6), (0, 6, 1), (3, 5, 7), (3, 2, 5)]
>>> 
btw, not sure if you actually expect anyone being able to read anything from the picture that your posted...
turnip
Posts: 5
Joined: Mon Oct 03, 2022 9:05 am

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by turnip »

Indeed, it seems impossible to get the triangular facet data directly from FreeCAD. I want to develop an easy static collision detection method which can work on models created by Part Design workbench, and I find it is hard for me to get raw data.
The image above shows that the 3D model has been cut into small triangles in Hidden Line mode, so is there any way to get the interface for re-rendering the display of the related triangles.
The command in python console which the above picture showed checks the Open Inventor data of model. unfortunately, only models created by Part show points and facets data. (I have just known it recently).
and I am sorry for all misunderstanding I have made.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by mario52 »

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by heda »

"it seems impossible to get the triangular facet data directly from FreeCAD"

not sure how you came to that conclusion,
looks like you have tried to pull the inventor rep, and yes, suppose you will have to roll your own parser for your own purposes if your intention is to use that directly.

but, why would anyone do that, unless you are doing it for the sake of learning about topologies of meshes and mesh formats...

fc already has the tools needed...
(going out on a limb here and suggesting fc would not work at all if it did not...)

the earlier post of mine does exactly what you ask for,
it is "getting the triangular facet data directly from fc"
in a very convenient way to juggle around with.

going from a brep (shape) to mesh topology to play around with...

Code: Select all

import Mesh
import MeshPart

doc = App.ActiveDocument

body = doc.Body

mesh = Mesh.Mesh()
mesh.addMesh(MeshPart.meshFromShape(body.Shape))
Mesh.show(mesh)
mobj = doc.ActiveObject # if you want the doc obj

points, topo = mesh.Topology

print(points)
print(topo)
with that said, if your aim is doing collision detection, well - there are plenty of script-api around in fc to roll one of those based on breps (as well as for meshes), but if the starting point is a brep - why not explore to stay in the brep world before giving up on it?

btw, not sure if there is ever anything easy with a collision detector, regardless of specialized or generic...
sure spotting collisions on the screen just looking at it is fairly simple, doing the same in the computer world is a different animal though, especially in the 3d space... (could be wrong of course...)
turnip
Posts: 5
Joined: Mon Oct 03, 2022 9:05 am

Re: How to get the vertices and related data of the triangular facet of the model in FreeCAD?

Post by turnip »

heda wrote: Sun Dec 04, 2022 2:42 pm "it seems impossible to get the triangular facet data directly from FreeCAD"

not sure how you came to that conclusion,
looks like you have tried to pull the inventor rep, and yes, suppose you will have to roll your own parser for your own purposes if your intention is to use that directly.

but, why would anyone do that, unless you are doing it for the sake of learning about topologies of meshes and mesh formats...

fc already has the tools needed...
(going out on a limb here and suggesting fc would not work at all if it did not...)

the earlier post of mine does exactly what you ask for,
it is "getting the triangular facet data directly from fc"
in a very convenient way to juggle around with.

going from a brep (shape) to mesh topology to play around with...

Code: Select all

import Mesh
import MeshPart

doc = App.ActiveDocument

body = doc.Body

mesh = Mesh.Mesh()
mesh.addMesh(MeshPart.meshFromShape(body.Shape))
Mesh.show(mesh)
mobj = doc.ActiveObject # if you want the doc obj

points, topo = mesh.Topology

print(points)
print(topo)
with that said, if your aim is doing collision detection, well - there are plenty of script-api around in fc to roll one of those based on breps (as well as for meshes), but if the starting point is a brep - why not explore to stay in the brep world before giving up on it?

btw, not sure if there is ever anything easy with a collision detector, regardless of specialized or generic...
sure spotting collisions on the screen just looking at it is fairly simple, doing the same in the computer world is a different animal though, especially in the 3d space... (could be wrong of course...)
thanks a lot, you answer is very helpful.

I didn't know the method of converting BREP model into MESH model before, so I didn't apply the method you replied before. I'm really sorry for that.

Developing collision detection function is not going to be an easy job, but the finally result and performance I expect will probably be simple. I will try some BREPs methods as well.

thanks for all help, and my questions has been handled.

thanks again
Post Reply