Convert mesh to solid?
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
Convert mesh to solid?
Is is possible to convert a mesh to a solid in FreeCAD? Specifically, I'm looking to import an STL and export a STEP. There's some wording in the wiki that suggests this might be possible, but I can't find a command that does this.
Re: Convert mesh to solid?
We have some Python functionality but no command in the GUI for this. The following example demonstrates how to do:
But note: The internal algorithm is quite stupid because it creates one face for each triangle. Even for this simple example the shape becomes quite fat. A more intelligent solution would be to divide the mesh into segments where each segment describes one kind of geometric surface like sphere, plane, cone, ... Then creating one face per segments results into a much smaller shape object.
Code: Select all
import Mesh,Part
mesh=Mesh.createTorus()
shape=Part.Shape()
shape.makeShapeFromMesh(mesh.Topology,0.05) # the second arg is the tolerance for sewing
solid=Part.makeSolid(shape)
Part.show(solid)
Re: Convert mesh to solid?
Thanks, Werner. That's just what I was looking for.
Re: Convert mesh to solid?
Sorry I am unfamiliar with this technique can some on e point me in the right direction
I would love to be able to convert a stl file to step
Stephen
I would love to be able to convert a stl file to step
Stephen
Re: Convert mesh to solid?
The technique is actually quite simple. The internal representation of a mesh in FreeCAD is indexed-based, i.e. we have an array of 3d points and another array of faces. Each face has three indexes to the point array to describe a triangle.Sorry I am unfamiliar with this technique can someone point me in the right direction
In our Python binding for the Mesh class we have the attribute 'Topology' which is a tuple of the both arrays. We pass this tuple to the method 'makeShapeFromMesh' of our shape object. Internally, we create a compound shape and create a face (TopoDS_Face in OCC terminology) for each triangle and add it to the compound. At the end we run a sewing operation so that the faces are topological connected.
As optional step a solid can be created with 'Part.makeSolid(shape)'. This, however, only makes sense if the original mesh was a solid.
ATM the conversion cannot be done via GUI. Thus, you can use a modified version of the above script. In your case it can look like this:I would love to be able to convert a stl file to step
import Mesh,Part
mesh=Mesh.Mesh()
mesh.read("file.stl")
shape=Part.Shape()
shape.makeShapeFromMesh(mesh.Topology,0.05) # the second arg is the tolerance for sewing
shape.exportStep("file.step")
Re: Convert mesh to solid?
Just a couple of notes:
- Conversion can take quite some time during which FreeCAD will appear unresponsive.
- I tried this on a very large and complex STL object, and it crashed FreeCAD. However, it does work quite well on files of a reasonable size.
Re: Convert mesh to solid?
Right now I have implemented a GUI command to run the conversion. You'll find it under Part > Create shape from mesh...
After debugging the current approach it appeared that there are two time- and memory-consuming blocks: the sewing and the visualization -- which is actually quite obvious.
At least for the GUI command a waiting cursor appears now to indicate that a long operation is running.Conversion can take quite some time during which FreeCAD will appear unresponsive.
Yes, that's true. But this feature was actually only supposed to be used for rather small objects. If we want to use it for more complex objects then we have to implement a more intelligent algorithm where we collect several triangles to one face.I tried this on a very large and complex STL object, and it crashed FreeCAD. However, it does work quite well on files of a reasonable size.
After debugging the current approach it appeared that there are two time- and memory-consuming blocks: the sewing and the visualization -- which is actually quite obvious.
Re: Convert mesh to solid?
Maybe there is one simple thing we can do to optimize a bit, without attacking heavy stuff like detecting which parts are pieces of sphere etc, which is join coplanar faces into single faces. Actually that could be useful as a separate command too, that could be run after importing the mesh.
Something like Face.findCoplanarFaces and a shape.mergeCoplanarFaces tools? I think I could try to do that...
Of course that wouldn't help much for very "organic" meshes, but for example in case of architecture, where most objects are very simple, that would totally optimize 90% of the objects...
It's cool because turning meshes into shapes was the next step I wanted to look at
Something like Face.findCoplanarFaces and a shape.mergeCoplanarFaces tools? I think I could try to do that...
Of course that wouldn't help much for very "organic" meshes, but for example in case of architecture, where most objects are very simple, that would totally optimize 90% of the objects...
It's cool because turning meshes into shapes was the next step I wanted to look at
Re: Convert mesh to solid?
Just some ideas:
I can think of basically two approaches how to do that:
1.)
1. Start with one triangle and its neighbours. Check what fits best of a list of supported geometries (sphere, cylinder, plane, ...)
Then grow starting from the seed triangles, i.e. check their neighbour triangles as long as they still fit well to the original geometry.
2. Once this aborts search for an unvisited triangle and run step 1 again
3. At the end we have an array of segments where one OCC face can be created per segment.
What we need therefore are some robust algorithms to fit cylinders, sphere, ... For planes we have already which is based on eigenvectors and works very reliably.
2.)
The other idea is to compute the maximum and minimum curvature and their directions. Now we can as in 1.) start an algorithm to check the neighbours of a triangle by their curvature. If they have approx. the same curvature values they can be grouped together.
Depending on the curvature we can also classify some geometries:
1. If maximum and minimum curvature is 0 we have a plane
2. If maximum curvature > 0 and minimum is 0 we have a cylinder
3. If maximum and minimum curvature are equal and > 0 we have a sphere
I can think of basically two approaches how to do that:
1.)
1. Start with one triangle and its neighbours. Check what fits best of a list of supported geometries (sphere, cylinder, plane, ...)
Then grow starting from the seed triangles, i.e. check their neighbour triangles as long as they still fit well to the original geometry.
2. Once this aborts search for an unvisited triangle and run step 1 again
3. At the end we have an array of segments where one OCC face can be created per segment.
What we need therefore are some robust algorithms to fit cylinders, sphere, ... For planes we have already which is based on eigenvectors and works very reliably.
2.)
The other idea is to compute the maximum and minimum curvature and their directions. Now we can as in 1.) start an algorithm to check the neighbours of a triangle by their curvature. If they have approx. the same curvature values they can be grouped together.
Depending on the curvature we can also classify some geometries:
1. If maximum and minimum curvature is 0 we have a plane
2. If maximum curvature > 0 and minimum is 0 we have a cylinder
3. If maximum and minimum curvature are equal and > 0 we have a sphere
Re: Convert mesh to solid?
Your idea 1 has the advantage that it can be implemented progressively, and that since many algorithms can be written, there is an infinite quantity of optimization methods possible...
Actually, in a mesh cylinder, you would only have 2 different curvature values... zero and the angle between cylinder "facets"... That's pretty easy. In a sphere, would there be only one curvature value? I'm not sure... And specially that some "facets" of the sphere can be made of several triangles (small squares for example), so you would also have curvature values of zero, and then it's hard to decide if it's a sphere or a cylinder.
Not easy Would those mesh approximation software you guys use be of any help for such cases?
Actually, in a mesh cylinder, you would only have 2 different curvature values... zero and the angle between cylinder "facets"... That's pretty easy. In a sphere, would there be only one curvature value? I'm not sure... And specially that some "facets" of the sphere can be made of several triangles (small squares for example), so you would also have curvature values of zero, and then it's hard to decide if it's a sphere or a cylinder.
Not easy Would those mesh approximation software you guys use be of any help for such cases?