Mesh to Part

From FreeCAD Documentation
Jump to navigation Jump to search
This page is a translated version of the page Mesh to Part and the translation is 7% complete.
Outdated translations are marked like this.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎español • ‎français • ‎italiano • ‎polski • ‎română • ‎svenska • ‎čeština • ‎русский • ‎日本語
Under construction icon-blue.svg
This documentation is work in progress. Please don't mark it as translatable since it will change the next hours and days.
Arrow-left.svg Previous: Mesh Scripting
Next: Scenegraph Arrow-right.svg

Convert Part objects to meshes

Converting higher-level objects such as Part shapes to simpler objects such as meshes is a pretty simple operation where all faces of a Part object get triangulated. The result of that triangulation (tessellation) is then used to construct a mesh:

The following code assumes our document contains one Part object.

# let's assume our document contains one Part object
import Mesh
faces = []
shape = FreeCAD.ActiveDocument.ActiveObject.Shape
triangles = shape.tessellate(1) # the number represents the precision of the tessellation
for tri in triangles[1]:
    face = []
    for i in tri:
        face.append(triangles[0][i])
    faces.append(face)

m = Mesh.Mesh(faces)
Mesh.show(m)

Sometimes the triangulation of certain faces offered by OpenCascade is quite ugly. If the face has a rectangular parameter space and doesn't contain any holes or other trimming curves you can also create a mesh manually:

import Mesh
def makeMeshFromFace(u, v, face):
    (a, b, c, d) = face.ParameterRange
    pts = []
    for j in range(v):
        for i in range(u):
            s = 1.0 / (u - 1) * (i * b + (u - 1 - i) * a)
            t = 1.0 / (v - 1) * (j * d + (v - 1 - j) * c)
            pts.append(face.valueAt(s, t))

    mesh = Mesh.Mesh()
    for j in range(v - 1):
        for i in range(u - 1):
            mesh.addFacet(pts[u * j + i], pts[u * j + i + 1], pts[u * (j + 1) + i])
            mesh.addFacet(pts[u * (j + 1) + i], pts[u * j + i + 1], pts[u * (j + 1) + i + 1])

    return mesh

Convert meshes to Part objects

Converting meshes to Part objects is a very common operation. Very often you will receive 3D data in a mesh format. Meshes are very practical for representing free-form geometry and big visual scenes, as they are very lightweight. But in FreeCAD we generally prefer higher-level objects, solids, that can carry much more information and allow for curved faces.

Converting meshes to those higher-level objects (handled by the Part Module in FreeCAD) is not an easy operation. Meshes can be made of thousands of triangles (for example when generated by a 3D scanner), and having solids made of the same number of faces would be extremely heavy to manipulate. So you generally want to optimize the object when converting.

FreeCAD currently offers two methods to convert meshes to Part objects. The first method is a simple, direct conversion without any optimization:

import Mesh
import 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)

The second method offers the possibility to consider mesh facets co-planar when the angle between them is under a certain value. This allows to build much simpler shapes:

The following code assumes our document contains one Mesh object.

# let's assume our document contains one Mesh object
import Mesh
import Part
import MeshPart

faces = []
mesh = App.ActiveDocument.ActiveObject.Mesh
segments = mesh.getPlanes(0.00001) # use rather strict tolerance here
 
for i in segments:
  if len(i) > 0:
     # a segment can have inner holes
     wires = MeshPart.wireFromSegment(mesh, i)
     # we assume that the exterior boundary is that one with the biggest bounding box
     if len(wires) > 0:
        ext = None
        max_length=0
        for i in wires:
           if i.BoundBox.DiagonalLength > max_length:
              max_length = i.BoundBox.DiagonalLength
              ext = i

        wires.remove(ext)
        # all interior wires mark a hole and must reverse their orientation, otherwise Part.Face fails
        for i in wires:
           i.reverse()

        # make sure that the exterior wires comes as first in the list
        wires.insert(0, ext)
        faces.append(Part.Face(wires))

shell = Part.Compound(faces)
Part.show(shell)
# solid = Part.Solid(Part.Shell(faces))
# Part.show(solid)

Template:Powerdocnavi/cs