## Convert mesh to solid?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules

lhagan
Posts: 105
Joined: Sun Jul 26, 2009 6:54 pm

### 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.
wmayer
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

### 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:

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)
``````
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.
lhagan
Posts: 105
Joined: Sun Jul 26, 2009 6:54 pm

### Re: Convert mesh to solid?

Thanks, Werner. That's just what I was looking for.
basilwatson

### 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
wmayer
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

### Re: Convert mesh to solid?

Sorry I am unfamiliar with this technique can someone point me in the right direction
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.

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.
I would love to be able to convert a stl file to step
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:
import Mesh,Part
mesh=Mesh.Mesh()
shape=Part.Shape()
shape.makeShapeFromMesh(mesh.Topology,0.05) # the second arg is the tolerance for sewing
shape.exportStep("file.step")
lhagan
Posts: 105
Joined: Sun Jul 26, 2009 6:54 pm

### 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.
wmayer
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

### 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...
Conversion can take quite some time during which FreeCAD will appear unresponsive.
At least for the GUI command a waiting cursor appears now to indicate that a long operation is running.
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.
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.
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.
yorik
Posts: 12058
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

### 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
wmayer
Posts: 16633
Joined: Thu Feb 19, 2009 10:32 am

### 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
yorik