Mesh Scripting

From FreeCAD Documentation
Revision as of 09:48, 1 June 2020 by Roy 043 (talk | contribs) (Replaced the 'difference' code sample with code that produces a valid mesh.)

Introduction

First of all you have to import the Mesh module:

import Mesh

After that you have access to the Mesh module and the Mesh class which facilitate the functions of the FreeCAD C++ Mesh-Kernel.

Creation and Loading

To create an empty mesh object just use the standard constructor:

mesh = Mesh.Mesh()

You can also create an object from a file

mesh = Mesh.Mesh('D:/temp/Something.stl')

(A list of compatible filetypes can be found under 'Meshes' here.)

Or create it out of a set of triangles described by their corner points:

planarMesh = [
# triangle 1
[-0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000], [-0.5000, 0.5000, 0.0000],
#triangle 2
[-0.5000, -0.5000, 0.0000], [0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000],
]
planarMeshObject = Mesh.Mesh(planarMesh)
Mesh.show(planarMeshObject)

The Mesh-Kernel takes care about creating a topologically correct data structure by sorting coincident points and edges together.

Later on you will see how you can test and examine mesh data.

top

Modeling

To create regular geometries you can use the Python script BuildRegularGeoms.py.

import BuildRegularGeoms

This script provides methods to define simple rotation bodies like spheres, ellipsoids, cylinders, toroids and cones. And it also has a method to create a simple cube. A toroid, for instance, can be created as follows:

t = BuildRegularGeoms.Toroid(8.0, 2.0, 50) # list with several thousands triangles
m = Mesh.Mesh(t)

The first two parameters define the radii of the toroid and the third parameter is a sub-sampling factor for how many triangles are created. The higher this value the smoother and the lower the coarser the body is. The Mesh class provides a set of boolean functions that can be used for modeling purposes. It provides union, intersection and difference of two mesh objects.

m1, m2              # are the input mesh objects
m3 = Mesh.Mesh(m1)  # create a copy of m1
m3.unite(m2)        # union of m1 and m2, the result is stored in m3
m4 = Mesh.Mesh(m1)
m4.intersect(m2)    # intersection of m1 and m2
m5 = Mesh.Mesh(m1)
m5.difference(m2)   # the difference of m1 and m2
m6 = Mesh.Mesh(m2)
m6.difference(m1)   # the difference of m2 and m1, usually the result is different to m5

Here is an example that creates a pipe using the difference() method:

import FreeCAD, Mesh, BuildRegularGeoms
cylA = Mesh.Mesh(BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 36))
cylB = Mesh.Mesh(BuildRegularGeoms.Cylinder(1.0, 12.0, True, 1.0, 36))
cylB.Placement.Base = (FreeCAD.Vector(-1, 0, 0)) # move cylB to avoid co-planar faces
pipe = cylA
pipe = pipe.difference(cylB)
pipe.flipNormals() # somehow required
d = FreeCAD.ActiveDocument
d.addObject("Mesh::Feature", "Pipe").Mesh = pipe
d.recompute()

top

Exporting

You can even write the mesh to a python module:

m.write("D:/Develop/Projekte/FreeCAD/FreeCAD_0.7/Mod/Mesh/SavedMesh.py")
import SavedMesh
m2 = Mesh.Mesh(SavedMesh.faces)

top

Notes

An extensive (though hard to use) source of Mesh related scripting are the unit test scripts of the Mesh-Module. In this unit tests literally all methods are called and all properties/attributes are tweaked. So if you are bold enough, take a look at the Unit Test module.

See also Mesh API

top