Adding or modifying constraints with Python (Become an expert)
In addition to using the Sketcher interactively and graphically we can use Python to program it automatically.
Getting ready
For this recipe, we need the Python console open. In the menu bar, open View, then Views, and then make sure that Python console is checked.
How to do it...
Enter the following code block into the console:
from Sketcher import * import Part import FreeCAD as App from FreeCAD import Vector if(App.activeDocument() == None):App.newDocument() f = App.activeDocument().addObject("Sketcher::SketchObject",\"Sketch") f.addGeometry(Part.Line(Vector(0,0,0),Vector(2,20,0))) f.addGeometry(Part.Line(Vector(0,0,0),Vector(20,2,0))) f.Constraints = [Constraint('Vertical',0),\Constraint('Horizontal',1)] App.activeDocument().recompute()
Double-click on the Sketch icon in the Project tree.
Notice the horizontal and vertical constraint symbols (the small red bars). Also notice that the lines have end points but they aren't constrained.
Grab either of the lines and move it and the other one won't move with it.
Add some more Python code to the console to constrain some points:
StartPoint = 1 ; l = f.Constraints l.append(Constraint('Coincident',0,StartPoint,1,StartPoint)) f.Constraints = l App.activeDocument().recompute()
Notice how one end of each line has connected to the other.
Try moving one of the lines around the screen and you will see that the lines move together.
How it works...
Import all available functions in the Sketcher module:
from Sketcher import *
We will need the Part module to make geometric objects:
import Part
The FreeCAD module will let us manipulate the document:
import FreeCAD as App
We will need to give our line segments end points, so we need:
from FreeCAD import Vector
If a document isn't already open create a new one:
if(App.activeDocument() == None): App.newDocument()
Create a new Sketch object:
f = App.activeDocument().addObject("Sketcher::SketchObject",\"Sketch")
Add geometry to the sketch:
f.addGeometry(Part.Line(Vector(0,0,0),Vector(2,20,0))) f.addGeometry(Part.Line(Vector(0,0,0),Vector(20,2,0)))
Add some constraints to the sketch to make the lines horizontal and vertical:
f.Constraints = [Constraint('Vertical',0),\Constraint('Horizontal',1)]
Recompute the sketch to see things after changes:
App.activeDocument().recompute()
Let's add a name for startpoints on our lines:
StartPoint = 1
Create a proxy object that is equal to our original, because we cannot add constraints directly to it:
l = f.Constraints
Append more constraints to our proxy object.
0
is the first line and1
is the second:l.append(Constraint('Coincident',0,StartPoint,1,StartPoint))
Make the original constraints equal to the proxy and recompute:
f.Constraints = l App.activeDocument().recompute()
There's more...
You can constrain your geometry with dimensions, using Python.
Add a length constraint to a line
You can add a length constraint to our second line as follows:
l.append((Constraint('DistanceX',1,20.0)))