Book Image

FreeCAD

By : Brad Collette, Daniel Falck
Book Image

FreeCAD

By: Brad Collette, Daniel Falck

Overview of this book

FreeCAD is a general purpose platform for CAD development. It can be extended to use different workbenches to solve different problems. Targeted squarely at the engineering community, FreeCAD is an open source design application built to be extended. Python, the powerful scripting language, is woven deeply into FreeCAD's DNA and gives users unprecedented power to automate and invent. "FreeCAD [How-to]" is a lean, fast tour of FreeCAD's major workbenches and design philosophy. The reader will get a hands-on introduction to several modeling techniques and learn how Python makes macro automation and design possible. FreeCAD allows users to design in a variety of ways; from drawing 2D designs as lines and circles to combining primitive solids into complex 3D shapes. Objects can even be created or modified by code you write in Python. FreeCAD even allows power users to extend the application itself with new dialogs, panels, and scripts. This book will not only show you how to get started using FreeCAD in a traditional GUI mode, but will teach you how to harness its powerful scripting language for more power.
Table of Contents (9 chapters)

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...

  1. 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()
  2. Double-click on the Sketch icon in the Project tree.

  3. Notice the horizontal and vertical constraint symbols (the small red bars). Also notice that the lines have end points but they aren't constrained.

  4. Grab either of the lines and move it and the other one won't move with it.

  5. 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()
  6. Notice how one end of each line has connected to the other.

  7. Try moving one of the lines around the screen and you will see that the lines move together.

How it works...

  1. Import all available functions in the Sketcher module:

    from Sketcher import *  
  2. We will need the Part module to make geometric objects:

    import Part  
  3. The FreeCAD module will let us manipulate the document:

    import FreeCAD as App  
  4. We will need to give our line segments end points, so we need:

    from FreeCAD import Vector
  5. If a document isn't already open create a new one:

    if(App.activeDocument() == None): App.newDocument()
  6. Create a new Sketch object:

    f = App.activeDocument().addObject("Sketcher::SketchObject",\"Sketch")
  7. 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)))
  8. Add some constraints to the sketch to make the lines horizontal and vertical:

    f.Constraints = [Constraint('Vertical',0),\Constraint('Horizontal',1)]
  9. Recompute the sketch to see things after changes:

    App.activeDocument().recompute()
  10. Let's add a name for startpoints on our lines:

    StartPoint = 1
  11. Create a proxy object that is equal to our original, because we cannot add constraints directly to it:

    l = f.Constraints
  12. Append more constraints to our proxy object. 0 is the first line and 1 is the second:

    l.append(Constraint('Coincident',0,StartPoint,1,StartPoint))
  13. 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)))