FreeCAD Logo FreeCAD 1.0
  • English Afrikaans Arabic Belarusian Catalan Czech German Greek Spanish Spanish Basque Finnish Filipino French Galician Croatian Hungarian Indonesian Italian Japanese Kabyle Korean Lithuanian Dutch Norwegian Bokmal Polish Portuguese Portuguese Romanian Russian Slovak Slovenian Serbian Swedish Turkish Ukrainian Valencian Vietnamese Chinese Chinese
  • Features
  • Download
  • Blog
  • Documentation
    Documentation index Getting started Users documentation The FreeCAD manual Workbenches documentation Python coding documentation C++ coding documentation Tutorials Frequently asked questions Privacy policy About FreeCAD
  • Contribute
    How to help Sponsor Report a bug Make a pull request Jobs and funding Contribution guidelines Developers handbook Translations
  • Community
    Code of conduct Forum The FPA GitHub GitLab Codeberg Mastodon Matrix IRC IRC via Webchat Gitter Discord Reddit Twitter Facebook LinkedIn Calendar
  • ♥ Donate

Donate

$
SEPA Information
Please set up your SEPA bank transfer to:
Beneficiary: The FreeCAD project association
IBAN: BE04 0019 2896 4531
BIC/SWIFT: GEBABEBBXXX
Bank agency: BNP Paribas Fortis
Address: Rue de la Station 64, 1360 Perwez, Belgium

While Stripe doesn't support monthly donations, you can still become a sponsor! Simply make a one-time donation equivalent to 12 months of support, and you'll gain access to the corresponding sponsoring tier. It's an easy and flexible way to contribute.

If you are not sure or not able to commit to a regular donation, but still want to help the project, you can do a one-time donation, of any amount.

Choose freely the amount you wish to donate one time only.

You can support FreeCAD by sponsoring it as an individual or organization through various platforms. Sponsorship provides a steady income for developers, allowing the FPA to plan ahead and enabling greater investment in FreeCAD. To encourage sponsorship, we offer different tiers, and unless you choose to remain anonymous, your name or company logo will be featured on our website accordingly.

from 1 USD / 1 EUR per month. You will not have your name displayed here, but you will have helped the project a lot anyway. Together, normal sponsors maintain the project on its feet as much as the bigger sponsors.

from 25 USD / 25 EUR per month. Your name or company name is displayed on this page.

from 100 USD / 100 EUR per month. Your name or company name is displayed on this page, with a link to your website, and a one-line description text.

from 200 USD / 200 EUR per month. Your name or company name and logo displayed on this page, with a link to your website and a custom description text. Companies that have helped FreeCAD early on also appear under Gold sponsors.

Instead of donating each month, you might find it more comfortable to make a one-time donation that, when divided by twelve, would give you right to enter a sponsoring tier. Don't hesitate to do so!

Choose freely the amount you wish to donate each month.

Please inform your forum name or twitter handle as a notein your transfer, or reach to us, so we can give you proper credits!

Sketcher scripting

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:

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:

e.g.

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" | | | | | | | ++++ | | | Point On Object | | "PointOnObject" | | | | | | | ++++ | | | 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 | +============================+====================================================================================+===============================================================+ | | | Horizontal distance | | "DistanceX" | | | | | | | ++++ | | | Vertical distance | | "DistanceY" | | | | | | | ++++ | | | Distance | | "Distance" | | | | | | | ++++ | | | Radius | | "Radius" | | | | | | | ++++ | | | Diameter | | "Diameter" | | | | | | | ++++ | | | Angle | | "Angle" | | | | | | | ++++ | | | Angle | | "AngleViaPoint" | | | | | | | ++++

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 | +========================+====================================================================================+========================================================+ | | | Snell\'s law | | "SnellsLaw" | | | | | | | ++++

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:

  • -1denotes the horizontal x axis

  • -2denotes the vertical y axis

  • -ndenotes 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:

  • 0to indicate that the constraint affects the entire edge.

  • 1to indicate that the constraint affects the start point of the edge (a full circle has no starting point).

  • 2to indicate that the constraint affects the endpoint of the edge.

  • 3to indicate that the constraint affects the center point of the edge. Can only be used for circles, circular arcs, ellipses and elliptical arcs.

  • nto 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()

{{Sketcher Tools navi}}


??? documentation index > Sketcher > Sketcher scripting

This page is retrieved from https://github.com/FreeCAD/FreeCAD-documentation/blob/main/wiki/Sketcher_scripting.md

Get in touch!
Forum GitHub Mastodon Matrix IRC Gitter.im Discord Reddit Twitter Facebook LinkedIn

© The FreeCAD Team. Homepage image credits (top to bottom): ppemawm, r-frank, epileftric, regis, rider_mortagnais, bejant.

This project is supported by: , KiCad Services Corp. and other sponsors

GitHubImprove this page on GitHub