Convert mesh to solid?

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
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!
lhagan
Posts: 105
Joined: Sun Jul 26, 2009 6:54 pm

Convert mesh to solid?

Postby lhagan » Thu Feb 11, 2010 8:11 pm

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

Re: Convert mesh to solid?

Postby wmayer » Fri Feb 12, 2010 12:00 am

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?

Postby lhagan » Fri Feb 12, 2010 4:15 am

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

Re: Convert mesh to solid?

Postby basilwatson » Wed Feb 24, 2010 6:08 am

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

Re: Convert mesh to solid?

Postby wmayer » Wed Feb 24, 2010 10:28 am

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

Re: Convert mesh to solid?

Postby lhagan » Wed Feb 24, 2010 12:57 pm

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

Re: Convert mesh to solid?

Postby wmayer » Wed Feb 24, 2010 1:47 pm

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.
User avatar
yorik
Site Admin
Posts: 12058
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

Re: Convert mesh to solid?

Postby yorik » Wed Feb 24, 2010 3:06 pm

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

Re: Convert mesh to solid?

Postby wmayer » Wed Feb 24, 2010 5:09 pm

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
User avatar
yorik
Site Admin
Posts: 12058
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

Re: Convert mesh to solid?

Postby yorik » Wed Feb 24, 2010 7:43 pm

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?