Topological data scripting

Introduction
We will here explain you how to control the Part Module directly from the FreeCAD python interpreter, or from any external script. Be sure to browse the Scripting section and the FreeCAD Scripting Basics pages if you need more information about how python scripting works in FreeCAD.

First to use the Part module functionality you have to load the Part module into the interpreter:

Class Diagram
This is a UML overview about the most important classes of the Part module:

Geometry
The geomtric objects are the building block of all topological objects:
 * GEOM Base class of the geometric objects
 * LINE A straight line in 3D, defined by starting point and and point
 * CIRCLE Circle or circle segment defined by a center point and start and end point
 * ...... And soon some more ;-)

Topology
The following topological data types are available:
 * COMPOUND A group of any type of topological object.
 * COMPSOLID A composite solid is a set of solids connected by their faces. It expands the notions of WIRE and SHELL to solids.
 * SOLID A part of space limited by shells. It is three dimensional.
 * SHELL A set of faces connected by their edges. A shell can be open or closed.
 * FACE In 2D it is part of a plane; in 3D it is part of a surface. Its geometry is constrained (trimmed) by contours. It is two dimensional.
 * WIRE A set of edges connected by their vertices. It can be an open or closed contour depending on whether the edges are linked or not.
 * EDGE A topological element corresponding to a restrained curve. An edge is generally limited by vertices. It has one dimension.
 * VERTEX A topological element corresponding to a point. It has zero dimension.
 * SHAPE A generic term covering all of the above.

Creating basic types
You can easily create basic topological objects with the "make..." methods from the Part Module:

A couple of other make... methods available:
 * makeBox(x,y,z,l,w,h) -- Make a box located in (x,y,z) with the dimensions (l,w,h)
 * makeCircle(radius,[angle1,angle2]) -- Make a circle with a given radius. By default angle1=0 and angle2=2*PI
 * makeCompound(list) -- Create a compound out of a list of geometries
 * makeCylinder(radius,height,[angle]) -- Make a cylinder with a given radius and height. By default angle=2*PI
 * makeLine((x1,y1,z1),(x2,y2,z2)) -- Make a line of two points
 * makePlane(lenght,width) -- Make a plane
 * makePolygon(list) -- Make a polygon of a list of points
 * makeSphere(radius,[angle1,angle2,angle3]) -- Make a sphere with a given radius. By default angle1=0, angle2=0.5*PI and angle3=2*PI

Exploring shapes
You can easily explore the topological data structure:

By typing the line above in the python interpreter, you will gain a good understanding of the structure of Part objects. Here, our makeBox command created a solid shape. This solid, like all Part solids, contains faces. Faces always contain wires, which are lists of edges that border the face. Each face has exactly one closed wire. In the wire, we can look at each edge separately, and inside each edge, we can see the vertexes. Straight edges have only two vertexes, obviously. Part Vertexes are OCC shapes, but they have a Point attribute which returns a nice FreeCAD Vector.

Example 1: Creating simple topology
We will now create a topology by constructing it out of simpler geometry. As a case study we use a part as seen in the picture which consists of four vertexes, two circles and two lines.

Creating Geometry
First we have to create the distinct geometric parts of this wire. And we have to take care that the vertexes of the geometric parts are at the same position. Otherwise later on we might not be able to connect the geometric parts to a topology!

So we create first the points:

Arc
To create an arc of circle we make a helper point and create the arc of circle through three points:

Line
The line can be created very simple out of the points:

Put all together
The last step is to put the geometric base elements together and bake a topological shape:

Make a prism
Now extrude the wire in a direction and make an actual 3D shape:

Example 2: The OCC bottle
A typical example found on the OpenCasCade Getting Started Page is how to build a bottle. This is a good exercise for FreeCAD too. In fact, you can follow our example below and the OCC page simultaneously, you will understand well how OCC structures are implemented in FreeCAD.

The complete script below is also included in FreeCAD installation (inside the Mod/Part folder) and can be called from the python interpreter by typing:

Here is the complete MakeBottle script:

To be continued...

Load and Save
There are several ways to save your work in the Part module. You can of course save your FreeCAD document, but you can also save Part objects directly to common CAD formats, such as IGS, STEP and STL.

To be continued...