Topological data scripting/pt-br

Introdução
Here we will explain to 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. If you are new to Python, it is a good idea to first read the Introduction to Python.

Class diagram
This is a Unified Modeling Language (UML) overview of the most important classes of the Part module:

Geometry
The geometric objects are the building blocks of all topological objects:
 * Geom Base class of the geometric objects.
 * Line A straight line in 3D, defined by starting point and end point.
 * Circle Circle or circle segment defined by a center point and start and end point.
 * Etc.

Topology
The following topological data types are available:
 * Compound A group of any type of topological objects.
 * 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.

Example: Create simple topology


We will now create a topology by constructing it out of simpler geometry. As a case study we will use a part as seen in the picture which consists of four vertices, two arcs and two lines.

Create geometry
First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later share the same vertices.

So we create the points first:

Arc


For each arc we need a helper point:

Line


The line segments can be created from two points:

Put it 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:

Create basic shapes
You can easily create basic topological objects with the methods from the Part module:

Some available methods: See the Part API page for a complete list of available methods of the Part module.
 * Makes a box located in p and pointing into the direction d with the dimensions (l,w,h).
 * Makes a circle with a given radius.
 * Makes a cone with the given radii and height.
 * Makes a cylinder with a given radius and height.
 * Makes a line from two points.
 * Makes a plane with length and width.
 * Makes a polygon from a list of points.
 * Makes a sphere with a given radius.
 * Makes a torus with the given radii.

Import modules
First we need to import the Part module so we can use its contents in Python. We'll also import the Base module from inside the FreeCAD module:

Create a vector
Vectors are one of the most important pieces of information when building shapes. They usually contain three numbers (but not necessarily always): the X, Y and Z cartesian coordinates. You create a vector like this:

We just created a vector at coordinates X = 3, Y = 2, Z = 0. In the Part module, vectors are used everywhere. Part shapes also use another kind of point representation called Vertex which is simply a container for a vector. You access the vector of a vertex like this:

Create an edge
An edge is nothing but a line with two vertices:

Note: You can also create an edge by passing two vectors:

You can find the length and center of an edge like this:

Put the shape on screen
So far we created an edge object, but it doesn't appear anywhere on the screen. This is because the FreeCAD 3D scene only displays what you tell it to display. To do that, we use this simple method:

The show function creates an object in our FreeCAD document and assigns our "edge" shape to it. Use this whenever it is time to display your creation on screen.

Create a wire
A wire is a multi-edge line and can be created from a list of edges or even a list of wires:

will display the 4 edges that compose our wire. Other useful information can be easily retrieved:

Create a face
Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above):

Only faces will have an area, wires and edges do not.

Create a circle
A circle can be created like this:

If you want to create it at a certain position and with a certain direction:

ccircle will be created at distance 10 from the X origin and will be facing outwards along the X axis. Note: only accepts  for the position and normal parameters, not tuples. You can also create part of the circle by giving a start and an end angle:

Angles should be provided in degrees. If you have radians simply convert them using the formula: or by using Python's module:

Create an arc along points
Unfortunately there is no function, but we have the  function to create an arc through three points. It creates an arc object joining the start point to the end point through the middle point. The arc object's function must be called to get an edge object, the same as when using  instead of.

only accepts for points and not tuples. You can also obtain an arc by using a portion of a circle:

Arcs are valid edges like lines, so they can be used in wires also.

Create a polygon
A polygon is simply a wire with multiple straight edges. The function takes a list of points and creates a wire through those points:

Create a bézier curve
Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a from a series of  points. Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0.

Create a plane
A Plane is a flat rectangular surface. The method used to create one is. By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1) will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the plane facing in the positive X axis direction:

is a cuboid enclosing the plane with a diagonal starting at (3, 0, 0) and ending at (5, 0, 2). Here the thickness along the Y axis is zero, since our shape is totally flat.

Note: only accepts  for start_pnt and dir_normal and not tuples.

Create an ellipse
There are several ways to create an ellipse:

Creates an ellipse with major radius 2 and minor radius 1 with the center at (0, 0, 0).

Creates a copy of the given ellipse.

Creates an ellipse centered on the point Center, where the plane of the ellipse is defined by Center, S1 and S2, its major axis is defined by Center and S1, its major radius is the distance between Center and S1, and its minor radius is the distance between S2 and the major axis.

Creates an ellipse with major and minor radii MajorRadius and MinorRadius, located in the plane defined by Center and the normal (0, 0, 1)

In the above code we have passed S1, S2 and center. Similar to, creates an ellipse object not an edge, so we need to convert it into an edge using  for display.

Note: only accepts  for points and not tuples.

For the above Ellipse constructor we have passed center, MajorRadius and MinorRadius.

Create a torus
Using. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360. Consider a torus as small circle sweeping along a big circle. Radius1 is the radius of the big circle, radius2 is the radius of the small circle, pnt is the center of the torus and dir is the normal direction. angle1 and angle2 are angles in degrees for the small circle; the last angle parameter is to make a section of the torus:

The above code will create a torus with diameter 20 (radius 10) and thickness 4 (small circle radius 2)

The above code will create a slice of the torus.

The above code will create a semi torus; only the last parameter is changed. i.e the remaining angles are defaults. Giving the angle 180 will create the torus from 0 to 180, that is, a half torus.

Create a box or cuboid
Using. By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1).

Create a sphere
Using. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360. Angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3 is the sphere diameter.

Create a cylinder
Using. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.

Create a cone
Using. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.

Modify shapes
There are several ways to modify shapes. Some are simple transformation operations such as moving or rotating shapes, others are more complex, such as unioning and subtracting one shape from another.

Translate a shape
Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:

This will move our shape "myShape" 2 units in the X direction.

Rotate a shape
To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:

The above code will rotate the shape 180 degrees around the Z Axis.

Matrix transformations
A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:

Note: FreeCAD matrixes work in radians. Also, almost all matrix operations that take a vector can also take three numbers, so these two lines do the same thing:

Once our matrix is set, we can apply it to our shape. FreeCAD provides two methods for doing that: and. The difference is that with the first one, you are sure that no deformations will occur (see Scaling a shape below). We can apply our transformation like this:

or

Scale a shape
Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the, we must use :

Subtraction
Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:

Intersection
The same way, the intersection between two shapes is called "common" and is done this way:

Union
Union is called "fuse" and works the same way:

Section
A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.

Extrusion
Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":

If your circle is hollow, you will obtain a hollow tube. If your circle is actually a disc with a filled face, you will obtain a solid cylinder:

Explore shapes
You can easily explore the topological data structure:

By typing the lines above in the Python interpreter, you will gain a good understanding of the structure of Part objects. Here, our 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 at least one closed wire (it can have more if the face has a hole). In the wire, we can look at each edge separately, and inside each edge, we can see the vertices. Straight edges have only two vertices, obviously.

Edge analysis
In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:

Now you can access a lot of properties of the edge by using the length as a position. That means if the edge is 100mm long the start position is 0 and the end position 100.

Use a selection
Here we see now how we can use a selection the user did in the viewer. First of all we create a box and show it in the viewer.

Now select some faces or edges. With this script you can iterate over all selected objects and their sub elements:

Select some edges and this script will calculate the length:

Example: The OCC bottle
A typical example found on the OpenCasCade Technology website is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the folder) and can be called from the Python interpreter by typing:

The script
For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.

Detailed explanation
We will need, of course, the module, but also the  module, which contains basic FreeCAD structures like vectors and matrices.

Here we define our function. This function can be called without arguments, like we did above, in which case default values for width, height, and thickness will be used. Then, we define a couple of points that will be used for building our base profile.

Here we define the geometry: an arc, made of three points, and two line segments, made of two points.

Remember the difference between geometry and shapes? Here we build shapes out of our construction geometry. Three edges (edges can be straight or curved), then a wire made of those three edges.

So far we have built only a half profile. Instead of building the whole profile the same way, we can just mirror what we did and glue both halves together. We first create a matrix. A matrix is a very common way to apply transformations to objects in the 3D world, since it can contain in one structure all basic transformations that 3D objects can undergo (move, rotate and scale). After we create the matrix we mirror it, then we create a copy of our wire and apply the transformation matrix to it. We now have two wires, and we can make a third wire out of them, since wires are actually lists of edges.

Now that we have a closed wire, it can be turned into a face. Once we have a face, we can extrude it. In doing so, we make a solid. Then we apply a nice little fillet to our object because we care about good design, don't we?

At this point, the body of our bottle is made, but we still need to create a neck. So we make a new solid, with a cylinder.

The fuse operation is very powerful. It will take care of gluing what needs to be glued and remove parts that need to be removed.

Then, we return our Part solid as the result of our function.

Finally, we call the function to actually create the part, then make it visible.

Example: Pierced box
Here is a complete example of building a pierced box.

The construction is done one side at a time. When the cube is finished, it is hollowed out by cutting a cylinder through it.

Loading and saving
There are several ways to save your work. You can of course save your FreeCAD document, but you can also save Part objects directly to common CAD formats, such as BREP, IGS, STEP and STL.

Saving a shape to a file is easy. There are, , and  methods available for all shape objects. So, doing:

will save our box into a STEP file. To load a BREP, IGES or STEP file:

To convert a STEP file to an IGS file: