Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
FreeCAD

You're reading from   FreeCAD Solid Modeling with the power of Python with this book and ebook.

Arrow left icon
Product type Paperback
Published in Sep 2012
Publisher Packt
ISBN-13 9781849518864
Length 70 pages
Edition 1st Edition
Concepts
Arrow right icon
Authors (2):
Arrow left icon
Brad Collette Brad Collette
Author Profile Icon Brad Collette
Brad Collette
Daniel Falck Daniel Falck
Author Profile Icon Daniel Falck
Daniel Falck
Arrow right icon
View More author details
Toc

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)))
lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image