Part scripting

Introduction
The main data structure used in the Part module is the BRep data type from OpenCASCADE. Almost all contents and object types of the Part module are available in Python scripting. This includes geometric primitives, such as Lines, Circles and Arcs, and the whole range of TopoShapes, like Vertexes, Edges, Wires, Faces, Solids and Compounds. For each of those objects, several creation methods exist, and for some of them, especially the TopoShapes, advanced operations like boolean union/difference/intersection are also available. Explore the contents of the Part module, as described in the FreeCAD Scripting Basics page, to know more.

The most basic object that can be created is a Part Feature, which has a simple property, and basic properties to define its color and appearance.

Another simple object used in 2D geometrical objects is the Part Part2DObject, which is the base of the Sketcher SketchObject and most Draft elements.

Test script
Test the creation of Part Primitives with a script.

This script is located in the installation directory of the program, and can be examined to see how the basic primitives are built.

Line
To create a line element switch to the Python console and enter:

Let's go through the above Python example step by step:

This loads the FreeCAD and Part modules and creates a new document.

Line is actually a line segment, hence the start and endpoint.

This adds a Part object type to the document and assigns the shape representation of the line segment to the property of the added object. It is important to understand here that we use a geometric primitive (the ) to create a TopoShape out of it (with the method). Only shapes can be added to the document. In FreeCAD geometric primitives are used as "building structures" for shapes.

Updates the document. This also prepares the visual representation of the new Part object.

Note that a line segment can also be created by specifying its start and endpoint directly in the constructor, for example, or we can create a default line and set its properties afterwards, as we did here.

A Line can also be created using:

Circle
A circle can be created in a similar way:

Or using:

Alternatively we can create a circle by defining its center, axis and radius:

Or by defining three points on its circumference:

Note again, we used the circle (geometric primitive) to construct a shape. We can of course still access our construction geometry afterwards, by doing:

Here we take the of our object  and then its list of. In this case there will be only one edge because we made the shape out of a single circle. So we take only the first item in the list, and then take its curve. Every edge has a, which is the geometric primitive it is based on.

Arc
An arc can be created like this:

This draws a half circle. The center is at (0, 0, 0). The radius is 10. P1 is the start point on +X axis. P2 is the middle point on +Y axis and P3 is the end point on -X axis.

We can also create an arc from a circle:

It needs a circle, and a start angle and end angle in radians.