Reader small image

You're reading from  FreeCAD

Product typeBook
Published inSep 2012
PublisherPackt
ISBN-139781849518864
Edition1st Edition
Concepts
Right arrow
Authors (2):
Brad Collette
Brad Collette
author image
Brad Collette

Brad Collette once designed software for a big company but doesn't like to remember that. These days, he is an entrepreneur, hobbyist, jack-of-all-trades, and a gentleman farmer. He is engaged in a multi-year project to raise two hacker sons. He has contributed to numerous open source projects and is an organizing member of Columbia Gadget Works, central Missouri's finest hackerspace.
Read more about Brad Collette

Daniel Falck
Daniel Falck
author image
Daniel Falck

Daniel Falck has over 25 years experience in manufacturing, machining, CAD, CAM, and computer programming. He has worked for Gibson Guitar doing design and prototyping for over 14 years. Currently, he runs the Prototype machine shop at King Cycle Group, in Portland, Oregon (USA), where bicycle components are manufactured. His home shop is full of CNC machining equipment that he uses to create guitar parts for customers, using open source software running on Linux. Over the past 10 years he has worked with open source manufacturing software such as Linuxcnc, APT360, HeeksCAD, FreeCAD, and a myriad of specialized python scripts.
Read more about Daniel Falck

View More author details
Right arrow

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)))
Previous PageNext Page
You have been reading a chapter from
FreeCAD
Published in: Sep 2012Publisher: PacktISBN-13: 9781849518864
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Brad Collette

Brad Collette once designed software for a big company but doesn't like to remember that. These days, he is an entrepreneur, hobbyist, jack-of-all-trades, and a gentleman farmer. He is engaged in a multi-year project to raise two hacker sons. He has contributed to numerous open source projects and is an organizing member of Columbia Gadget Works, central Missouri's finest hackerspace.
Read more about Brad Collette

author image
Daniel Falck

Daniel Falck has over 25 years experience in manufacturing, machining, CAD, CAM, and computer programming. He has worked for Gibson Guitar doing design and prototyping for over 14 years. Currently, he runs the Prototype machine shop at King Cycle Group, in Portland, Oregon (USA), where bicycle components are manufactured. His home shop is full of CNC machining equipment that he uses to create guitar parts for customers, using open source software running on Linux. Over the past 10 years he has worked with open source manufacturing software such as Linuxcnc, APT360, HeeksCAD, FreeCAD, and a myriad of specialized python scripts.
Read more about Daniel Falck