Sketcher scripting

Creating a constraint using Python
A geometric constraint can be created from macros and from the python console by using the following command:

A dimensional constraint can be created from macros and from the python console by using the following command:


 * 1) e.g.

The first argument is described below in Constraint types.

A constraint can take up to six arguments which are edges or indicate which sub-part of an edge is used by the constraint. See the documentation of individual constraints for details on what combinations of edges and sub-parts of edges can be passed as arguments.

The main issue with this function is to identify correctly the line number and the vertex number of the lines you want to process. The sections below describe how to identify the numbering of a line) and how to Identify the numbering of the sub-parts of a line).

Constraint types
For geometric constraints, the first argument is one of the following:
 * , see Sketcher_ConstrainCoincident.svg Coincident for the possible combinations of arguments
 * , see Sketcher_ConstrainPointOnObject.svg Point On Object for the possible combinations of arguments
 * , see Sketcher_ConstrainVertical.svg Vertical for the possible combinations of arguments
 * , see Sketcher_ConstrainHorizontal.svg Horizontal for the possible combinations of arguments
 * , see Sketcher_ConstrainParallel.svg Parallel for the possible combinations of arguments
 * , see Sketcher_ConstrainPerpendicular.svg Perpendicular for the possible combinations of arguments
 * , see Sketcher_ConstrainTangent.svg Tangent for the possible combinations of arguments
 * , see Sketcher_ConstrainEqual.svg Equal for the possible combinations of arguments
 * , see Sketcher_ConstrainSymmetric.svg Symmetric for the possible combinations of arguments
 * , see [[Image:Sketcher_ConstrainBlock.svg|32px]] Block for the possible combinations of arguments

For dimensional constraints, the first argument is one of the following:
 * , see Sketcher_ConstrainDistanceX.svg Horizontal distance for the possible combinations of arguments
 * , see Sketcher_ConstrainDistanceY.svg Vertical distance for the possible combinations of arguments
 * , see Sketcher_ConstrainDistance.svg Distance for the possible combinations of arguments
 * , see Sketcher_ConstrainRadius.svg Radius for the possible combinations of arguments
 * , see Sketcher_ConstrainDiameter.svg Diameter for the possible combinations of arguments
 * , see Sketcher_ConstrainAngle.svg Angle for the possible combinations of arguments
 * , see Sketcher_ConstrainAngle.svg Angle for the possible combinations of arguments

The Lock constraint is a GUI command which creates a  Horizontal distance and a  Vertical distance constraint, it is not a constraint of its own.

Identifying the numbering of a line
I have drawn three lines as shown in the following figure.



By moving the cursor of the mouse above the line you can see the line number at the bottom left of the FreeCAD windows, see next figure.



Unfortunately the numbering displayed on the FreeCAD windows start from 1 whereas the numbering of the line used to script start from 0: this means that you have to subtract one each time you want to refer to a line.

Positive numbers indicate sketch edges (straight lines, circles, conics, B-splines, and so on). The following values can be used to denote elements that are not sketch edges:


 * denotes the horizontal x axis
 * denotes the vertical y axis
 * denotes the external geometry element number (e.g. the external geometry element with index 0 in the flattened list  would be denoted by -3, the following element in the flattened list would be -4 and so on).

Identifying the numbering of the sub-parts of a line
When qualifying which part of a line is affected by a constraint, the following values can be used:
 * to indicate that the constraint affects the entire edge.
 * to indicate that the constraint affects the starting point of the edge.
 * to indicate that the constraint affects the end point of the edge.
 * to indicate that the constraint affects the center point of the edge. For Sketcher_CompCreateCircle.pnges and Sketcher_CompCreateConic.pngs (ellipses), this is the center of the circle or center (intersection of major and minor axes) of the ellipse. For straight Sketcher_CreateLine.svg, cannot be used to indicate the center point.
 * to indicate that the constraint affects the n-th pole of a Sketcher_CompCreateBSpline.png-Spline.

The vertices indicated by 1 and 2 are numbered according to their order of creation. To find out the order of their creation (If you have a lot of lines, you cannot remember which vertex you have created first), you just have to move the cursor of your mouse above the two vertices of one line, see following figure.



If you read e.g. 4 and 5, it means that the vertex with the lower number (4 in this example) will be referenced by using the number 1 (first in the script command and the vertex with the higher number (5 in this example) will be referenced by using the number 2 in the script command.

Example
Let us take the previous example of the three lines. The subsequent figure indicates the numbering of each line and their vertices according to the convention for scripting.



The command yields following result:



The command yields following result: