Sketcher scripting: Difference between revisions

From FreeCAD Documentation
(→‎Constraint types: Added the dimensional constraints)
(Removed "and addGeometry" since below the code examples it already says: "It also adds three lines".)
 
(52 intermediate revisions by 6 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{TOCright}}
<translate>
<translate>


== Creating a constraint using Python ==
== Creating a SketchObject using Python == <!--T:27-->

<!--T:14-->
<!--T:28-->
A geometric constraint [[File:Sketcher_ConstrainCoincident.svg|32px]][[File:Sketcher_ConstrainPointOnObject.svg|32px]][[File:Sketcher_ConstrainVertical.svg|32px]][[File:Sketcher_ConstrainHorizontal.svg|32px]][[File:Sketcher_ConstrainParallel.svg|32px]][[File:Sketcher_ConstrainPerpendicular.svg|32px]][[File:Sketcher_ConstrainTangent.svg|32px]][[File:Sketcher_ConstrainEqual.svg|32px]][[File:Sketcher_ConstrainSymmetric.svg|32px]][[Image:Sketcher_ConstrainBlock.svg|32px]] can be created from macros and from the python console by using the following command:
We create a SketchObject like this:


</translate>
</translate>
{{Code|code=
import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()


sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
{{Code|code=Sketch.addConstraint(Sketcher.Constraint(ConstraintType, EdgeOrPartOfEdge…)) }}
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
App.Vector(15.4, 5.0, 0)), False)


doc.recompute()
}}
<translate>
<translate>


<!--T:33-->
A dimensional constraint [[File:Sketcher_ConstrainLock.svg|32px]][[File:Sketcher_ConstrainDistanceX.svg|32px]][[File:Sketcher_ConstrainDistanceY.svg|32px]][[File:Sketcher_ConstrainDistance.svg|32px]][[File:Sketcher_ConstrainRadius.svg|32px]][[File:Sketcher_ConstrainDiameter.svg|32px]][[File:Sketcher_ConstrainAngle.svg|32px]] can be created from macros and from the python console by using the following command:
It also adds three [[Topological_data_scripting#Line|lines]] in the newly created Sketch.

== Creating a constraint using Python == <!--T:1-->

<!--T:2-->
A geometric constraint [[File:Sketcher_ConstrainCoincident.svg|24px]] [[File:Sketcher_ConstrainPointOnObject.svg|24px]] [[File:Sketcher_ConstrainVertical.svg|24px]] [[File:Sketcher_ConstrainHorizontal.svg|24px]] [[File:Sketcher_ConstrainParallel.svg|24px]] [[File:Sketcher_ConstrainPerpendicular.svg|24px]] [[File:Sketcher_ConstrainTangent.svg|24px]] [[File:Sketcher_ConstrainEqual.svg|24px]] [[File:Sketcher_ConstrainSymmetric.svg|24px]] and [[File:Sketcher_ConstrainBlock.svg|24px]] can be created from macros and from the Python console by using the following command:


</translate>
</translate>
{{Code|code=sketch.addConstraint(Sketcher.Constraint(ConstraintType, EdgeOrPartOfEdge…)) }}
<translate>


<!--T:3-->
{{Code|code=Sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge…, App.Units.Quantity('float_value unit'))) }}
A dimensional constraint [[File:Sketcher_ConstrainLock.svg|24px]] [[File:Sketcher_ConstrainDistanceX.svg|24px]] [[File:Sketcher_ConstrainDistanceY.svg|24px]] [[File:Sketcher_ConstrainDistance.svg|24px]] [[File:Sketcher_ConstrainRadius.svg|24px]] [[File:Sketcher_ConstrainDiameter.svg|24px]] [[File:Sketcher_ConstrainAngle.svg|24px]] and the special constraint [[File:Sketcher_ConstrainSnellsLaw.svg|24px]] [[Sketcher_ConstrainSnellsLaw|Snell's law]] can be created from macros and from the Python console by using the following command:


</translate>
{{Code|code=sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge…, App.Units.Quantity("float_value unit"))) }}
<translate>

<!--T:34-->
e.g.

</translate>
{{Code|code=sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge…, App.Units.Quantity("123.0 mm"))) }}
<translate>
<translate>


<!--T:4-->
The first argument {{incode|ConstraintType}} is described below in [[#Constraint types|Constraint types]].
The first argument {{incode|ConstraintType}} is described below in [[#Constraint_types|Constraint types]].


<!--T:5-->
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.
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.
<!--T:10-->
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 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 [[#Identifying the numbering of a line|identify the numbering of a line]]) and how to [[#Identifying the numbering of the sub-parts of a line|Identify the numbering of the sub-parts of a line]]).
The sections below describe how to [[#Identifying_the_numbering_of_a_line|identify the numbering of a line]], and how to [[#Identifying_the_numbering_of_the_sub-parts_of_a_line|Identify the numbering of the sub-parts of a line]].


== Constraint types ==
== Constraint types == <!--T:6-->


<!--T:7-->
For geometric constraints, the first argument is one of the following:
For geometric constraints, the first argument is one of the following. See the corresponding feature page for the possible combinations of arguments allowed for each constraint.
</translate>
* {{incode|'Coincident'}}, see [[File:Sketcher_ConstrainCoincident.svg|32px]] [[Sketcher_ConstrainCoincident|Coincident]] for the possible combinations of arguments
* {{incode|'PointOnObject'}}, see [[File:Sketcher_ConstrainPointOnObject.svg|32px]] [[Sketcher_ConstrainPointOnObject|Point On Object]] for the possible combinations of arguments
* {{incode|'Vertical'}}, see [[File:Sketcher_ConstrainVertical.svg|32px]] [[Sketcher_ConstrainVertical|Vertical]] for the possible combinations of arguments
* {{incode|'Horizontal'}}, see [[File:Sketcher_ConstrainHorizontal.svg|32px]] [[Sketcher_ConstrainHorizontal|Horizontal]] for the possible combinations of arguments
* {{incode|'Parallel'}}, see [[File:Sketcher_ConstrainParallel.svg|32px]] [[Sketcher_ConstrainParallel|Parallel]] for the possible combinations of arguments
* {{incode|'Perpendicular'}}, see [[File:Sketcher_ConstrainPerpendicular.svg|32px]] [[Sketcher_ConstrainPerpendicular|Perpendicular]] for the possible combinations of arguments
* {{incode|'Tangent'}}, see [[File:Sketcher_ConstrainTangent.svg|32px]] [[Sketcher_ConstrainTangent|Tangent]] for the possible combinations of arguments
* {{incode|'Equal'}}, see [[File:Sketcher_ConstrainEqual.svg|32px]] [[Sketcher_ConstrainEqual|Equal]] for the possible combinations of arguments
* {{incode|'Symmetric'}}, see [[File:Sketcher_ConstrainSymmetric.svg|32px]] [[Sketcher_ConstrainSymmetric|Symmetric]] for the possible combinations of arguments
* {{incode|'Block'}}, see [[Image:Sketcher_ConstrainBlock.svg|32px]] [[Sketcher_ConstrainBlock|Block]] for the possible combinations of arguments


<!--T:35-->
For dimensional constraints, the first argument is one of the following:
{| class="wikitable"
* {{incode|'DistanceX'}}, see [[File:Sketcher_ConstrainDistanceX.svg|32px]] [[Sketcher_ConstrainDistanceX|Horizontal distance]] for the possible combinations of arguments
|-
* {{incode|'DistanceY'}}, see [[File:Sketcher_ConstrainDistanceY.svg|32px]] [[Sketcher_ConstrainDistanceY|Vertical distance]] for the possible combinations of arguments
! Code !! Icon !! Feature
* {{incode|'Distance'}}, see [[File:Sketcher_ConstrainDistance.svg|32px]] [[Sketcher_ConstrainDistance|Distance]] for the possible combinations of arguments
|-
* {{incode|'Radius'}}, see [[File:Sketcher_ConstrainRadius.svg|32px]] [[Sketcher_ConstrainRadius|Radius]] for the possible combinations of arguments
* {{incode|'Diameter'}}, see [[File:Sketcher_ConstrainDiameter.svg|32px]] [[Sketcher_ConstrainDiameter|Diameter]] for the possible combinations of arguments
| {{incode|"Coincident"}} || [[File:Sketcher_ConstrainCoincident.svg|24px]] || [[Sketcher_ConstrainCoincident|Coincident]]
|-
* {{incode|'Angle'}}, see [[File:Sketcher_ConstrainAngle.svg|32px]] [[Sketcher_ConstrainAngle|Angle]] for the possible combinations of arguments
| {{incode|"PointOnObject"}} || [[File:Sketcher_ConstrainPointOnObject.svg|24px]] || [[Sketcher_ConstrainPointOnObject|Point On Object]]
|-
| {{incode|"Vertical"}} || [[File:Sketcher_ConstrainVertical.svg|24px]] || [[Sketcher_ConstrainVertical|Vertical]]
|-
| {{incode|"Horizontal"}} || [[File:Sketcher_ConstrainHorizontal.svg|24px]] || [[Sketcher_ConstrainHorizontal|Horizontal]]
|-
| {{incode|"Parallel"}} || [[File:Sketcher_ConstrainParallel.svg|24px]] || [[Sketcher_ConstrainParallel|Parallel]]
|-
| {{incode|"Perpendicular"}} || [[File:Sketcher_ConstrainPerpendicular.svg|24px]] || [[Sketcher_ConstrainPerpendicular|Perpendicular]]
|-
| {{incode|"Tangent"}} || [[File:Sketcher_ConstrainTangent.svg|24px]] || [[Sketcher_ConstrainTangent|Tangent]]
|-
| {{incode|"Equal"}} || [[File:Sketcher_ConstrainEqual.svg|24px]] || [[Sketcher_ConstrainEqual|Equal]]
|-
| {{incode|"Symmetric"}} || [[File:Sketcher_ConstrainSymmetric.svg|24px]] || [[Sketcher_ConstrainSymmetric|Symmetric]]
|-
| {{incode|"Block"}} || [[Image:Sketcher_ConstrainBlock.svg|24px]] || [[Sketcher_ConstrainBlock|Block]]
|}


<!--T:9-->
The [[File:Sketcher_ConstrainLock.svg|32px]] [[Sketcher_ConstrainLock|Lock]] constraint is a GUI command which creates a [[File:Sketcher_ConstrainDistanceX.svg|32px]] [[Sketcher_ConstrainDistanceX|Horizontal distance]] and a [[File:Sketcher_ConstrainDistanceY.svg|32px]] [[Sketcher_ConstrainDistanceY|Vertical distance]] constraint, it is not a constraint of its own.
For dimensional constraints, the first argument is one of the following. See the corresponding feature page for the possible combinations of arguments allowed for each constraint.


<!--T:37-->
{| class="wikitable"
|-
! Code !! Icon !! Feature
|-
| {{incode|"DistanceX"}} || [[File:Sketcher_ConstrainDistanceX.svg|24px]] || [[Sketcher_ConstrainDistanceX|Horizontal distance]]
|-
| {{incode|"DistanceY"}} || [[File:Sketcher_ConstrainDistanceY.svg|24px]] || [[Sketcher_ConstrainDistanceY|Vertical distance]]
|-
| {{incode|"Distance"}} || [[File:Sketcher_ConstrainDistance.svg|24px]] || [[Sketcher_ConstrainDistance|Distance]]
|-
| {{incode|"Radius"}} || [[File:Sketcher_ConstrainRadius.svg|24px]] || [[Sketcher_ConstrainRadius|Radius]]
|-
| {{incode|"Diameter"}} || [[File:Sketcher_ConstrainDiameter.svg|24px]] || [[Sketcher_ConstrainDiameter|Diameter]]
|-
| {{incode|"Angle"}} || [[File:Sketcher_ConstrainAngle.svg|24px]] || [[Sketcher_ConstrainAngle|Angle]]
|-
| {{incode|"AngleViaPoint"}} || [[File:Sketcher_ConstrainAngle.svg|24px]] || [[Sketcher_ConstrainAngle|Angle]]
|}


<!--T:10-->
<translate>
The [[File:Sketcher_ConstrainSnellsLaw.svg|24px]] [[Sketcher_ConstrainSnellsLaw|Snell's law]] constraints behave like dimensional contraints for the purposes of scripting. Again, see the corresponding feature page for the possible combinations of arguments allowed for each constraint.


<!--T:38-->
==Identifying the numbering of a line== <!--T:15-->
{| class="wikitable"
|-
! Code !! Icon !! Feature
|-
| {{incode|"SnellsLaw"}} || [[File:Sketcher_ConstrainSnellsLaw.svg|24px]] || [[Sketcher_ConstrainSnellsLaw|Snell's law]]
|}


<!--T:12-->
<!--T:11-->
The [[File:Sketcher_ConstrainLock.svg|24px]] [[Sketcher_ConstrainLock|Lock]] constraint is a GUI command which creates a [[File:Sketcher_ConstrainDistanceX.svg|24px]] [[Sketcher_ConstrainDistanceX|Horizontal distance]] and a [[File:Sketcher_ConstrainDistanceY.svg|24px]] [[Sketcher_ConstrainDistanceY|Vertical distance]] constraint, it is not a constraint of its own.

==Identifying the numbering of a line== <!--T:12-->

<!--T:13-->
I have drawn three lines as shown in the following figure.
I have drawn three lines as shown in the following figure.


</translate>
<!--T:16-->
[[File:PartDesignConstraintPointOnPointScriptingFigure1.jpg|600px|center]]
[[File:PartDesignConstraintPointOnPointScriptingFigure1.jpg|600px]]
<translate>


<!--T:17-->
<!--T:14-->
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.
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.


</translate>
<!--T:18-->
[[File:PartDesignConstraintPointOnPointScriptingFigure2.jpg|600px|center]]
[[File:PartDesignConstraintPointOnPointScriptingFigure2.jpg|600px]]
<translate>


<!--T:19-->
<!--T:15-->
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.
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.


<!--T:16-->
==Identifying the numbering of the sub-parts of a line== <!--T:20-->
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:


<!--T:21-->
<!--T:17-->
* {{incode|-1}} denotes the horizontal x axis
* {{incode|-2}} denotes the vertical y axis
* {{incode|-n}} denotes the external geometry element number {{incode|n-3}} (e.g. the external geometry element with index 0 in the flattened list {{incode|sketch.ExternalGeometry}} 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== <!--T:18-->

<!--T:19-->
When qualifying which part of a line is affected by a constraint, the following values can be used:
When qualifying which part of a line is affected by a constraint, the following values can be used:
* {{incode|0}} to indicate that the constraint affects the entire edge.
* {{incode|0}} to indicate that the constraint affects the entire edge.
* {{incode|1}} to indicate that the constraint affects the starting point of the edge.
* {{incode|1}} to indicate that the constraint affects the starting point of the edge (a full circle has no starting point).
* {{incode|2}} to indicate that the constraint affects the end point of the edge.
* {{incode|2}} to indicate that the constraint affects the end point of the edge.
* {{incode|3}} to indicate that the constraint affects the center point of the edge. For conical and circular shapes, this is the center of the circle or center (intersection of major and minor axes) of the ellipse.
* {{incode|3}} to indicate that the constraint affects the center point of the edge. For [[File:Sketcher_CompCreateCircle.png|x24px]][[Sketcher_CompCreateCircle|Circles]] and [[File:Sketcher_CompCreateConic.png|x24px]][[Sketcher_CompCreateConic|Conics]] (ellipses), this is the center of the circle or center (intersection of major and minor axes) of the ellipse. For straight [[File:Sketcher_CreateLine.svg|24px]][[Sketcher_CreateLine|Lines]], {{incode|3}} cannot be used to indicate the center point.
* {{incode|n}} to indicate that the constraint affects the n-th pole of a [[File:Sketcher_CompCreateBSpline.png|x24px]][[Sketcher_CompCreateBSpline|B-Spline]].


<!--T:20-->
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.
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.


</translate>
<!--T:22-->
[[File:PartDesignConstraintPointOnPointScriptingFigure3.jpg|600px|center]]
[[File:PartDesignConstraintPointOnPointScriptingFigure3.jpg|600px]]
<translate>


<!--T:23-->
<!--T:21-->
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.
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 == <!--T:24-->
== Example == <!--T:22-->


<!--T:25-->
<!--T:23-->
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.
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.


</translate>
<!--T:26-->
[[Image:PartDesignConstraintPointOnPointScriptingFigure3Bis.jpg|600px|center]]
[[Image:PartDesignConstraintPointOnPointScriptingFigure3Bis.jpg|600px]]
<translate>

<!--T:24-->
{{Caption|<b>blue text:</b> numbering of line, <b>black text:</b> numbering of vertices}}
{{Caption|<b>blue text:</b> numbering of line, <b>black text:</b> numbering of vertices}}


<!--T:27-->
<!--T:25-->
The command {{incode|Sketch.addConstraint(Sketcher.Constraint('Coincident',1,2,2,1))}} yields following result:
The command {{incode|sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1))}} yields following result:


</translate>
<!--T:29-->
[[Image:PartDesignConstraintPointOnPointScriptingFigure4.jpg|600px|center]]
[[Image:PartDesignConstraintPointOnPointScriptingFigure4.jpg|600px]]
<translate>


<!--T:30-->
<!--T:39-->
The full code to draw the three lines and add a Coincident constraint on two points from two lines is like this:
The command {{incode|Sketch.addConstraint(Sketcher.Constraint('Coincident',0,2,2,2))}} yields following result:

</translate>
{{Code|code=
import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()

sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
App.Vector(15.4, 5.0, 0)), False)
sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1))

doc.recompute()
}}
<translate>


<!--T:38-->
[[Image:PartDesignConstraintPointOnPointScriptingFigure5.jpg|600px|center]]


</translate>
</translate>
{{Sketcher Tools navi{{#translation:}}}}
{{Userdocnavi{{#translation:}}}}

Latest revision as of 18:43, 12 October 2023

Creating a SketchObject using Python

We create a SketchObject like this:

import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()  

sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
                                    App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
                                    App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
                                    App.Vector(15.4, 5.0, 0)), False)

doc.recompute()

It also adds three lines in the newly created Sketch.

Creating a constraint using Python

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

sketch.addConstraint(Sketcher.Constraint(ConstraintType, EdgeOrPartOfEdge))

A dimensional constraint and the special constraint Snell's law can be created from macros and from the Python console by using the following command:

sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge, App.Units.Quantity("float_value unit")))

e.g.

sketch.addConstraint(Sketcher.Constraint(DimensionalConstraintType, EdgeOrPartOfEdge, App.Units.Quantity("123.0 mm")))

The first argument ConstraintType 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 the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"Coincident" Coincident
"PointOnObject" Point On Object
"Vertical" Vertical
"Horizontal" Horizontal
"Parallel" Parallel
"Perpendicular" Perpendicular
"Tangent" Tangent
"Equal" Equal
"Symmetric" Symmetric
"Block" Block

For dimensional constraints, the first argument is one of the following. See the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"DistanceX" Horizontal distance
"DistanceY" Vertical distance
"Distance" Distance
"Radius" Radius
"Diameter" Diameter
"Angle" Angle
"AngleViaPoint" Angle

The Snell's law constraints behave like dimensional contraints for the purposes of scripting. Again, see the corresponding feature page for the possible combinations of arguments allowed for each constraint.

Code Icon Feature
"SnellsLaw" Snell's law

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:

  • -1 denotes the horizontal x axis
  • -2 denotes the vertical y axis
  • -n denotes the external geometry element number n-3 (e.g. the external geometry element with index 0 in the flattened list sketch.ExternalGeometry 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:

  • 0 to indicate that the constraint affects the entire edge.
  • 1 to indicate that the constraint affects the starting point of the edge (a full circle has no starting point).
  • 2 to indicate that the constraint affects the end point of the edge.
  • 3 to indicate that the constraint affects the center point of the edge. For Circles and Conics (ellipses), this is the center of the circle or center (intersection of major and minor axes) of the ellipse. For straight Lines, 3 cannot be used to indicate the center point.
  • n to indicate that the constraint affects the n-th pole of a B-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.

blue text: numbering of line, black text: numbering of vertices

The command sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1)) yields following result:

The full code to draw the three lines and add a Coincident constraint on two points from two lines is like this:

import FreeCAD as App
import Part
import Sketcher

doc = App.newDocument()  

sketch = doc.addObject("Sketcher::SketchObject", "Sketch")
sketch.addGeometry(Part.LineSegment(App.Vector(1.2, 1.8, 0),
                                    App.Vector(5.2, 5.3, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(6.5, 1.5, 0),
                                    App.Vector(10.2, 5.0, 0)), False)
sketch.addGeometry(Part.LineSegment(App.Vector(12.2, 1.0, 0),
                                    App.Vector(15.4, 5.0, 0)), False)
sketch.addConstraint(Sketcher.Constraint("Coincident", 1, 2, 2, 1))

doc.recompute()