OpenSCAD Basics: 2D Forms

Yesterday I gave you some basic information to get started with OpenSCAD1  Today you’re actually going to get to use some of what you learned yesterday.  Admittedly, most of what you’ll be doing is typing things and hitting F5, but don’t kid yourself – that’s progress!

Let’s concentrate on walking before we run.  While the whole point of a 3D modeling program like OpenSCAD is to create 3D forms, learning to make 2D forms will help you get the hang of the OpenSCAD interface and lay the basis for understanding how the language works.  There are three 2D forms that can be created with OpenSCAD, squares, circles, and polygons.

One quick note before we get started.  Each command in OpenSCAD needs to end with a semicolon.  Okay, I think you’re ready to try to make some shapes!  You can follow along at home by copying the text within the quotes and then hitting F5 to get OpenSCAD to draw the shape for you.

  • Circle.
    • Here’s how you create a circle with a radius of 5mm.
      1. “circle(5);”
    • Want a circle with a radius of 6mm?  No problem!
      1. “circle(6);”
    • In either case, it’s good form to actually define the circle by including a reference to the radius itself.  Here’s how you do that:
      1. “circle(r = 5);”
    • That will draw a circle identical to the first example.  Defining the circle in this way helps us quickly understand at a glance what’s being described – and will come in handy a little later.
  • Square.
    • Here’s how you create a square with each side of 5mm.
      1. “square(5);”
    • Want a square with each side at 6mm?  You guessed it!
      1. “square(6);”
    • You can also turn the square into a rectangle by replacing the number with a bracketed pair of numbers.  One number will be the length and the other number the depth.
      1. “square([4,8]);”
    • If you need a rectangle with different dimensions, all you need to do is change those two numbers.  Easy!
  • Polygon.
    • A polygon is basically just a shape that can lie flat in a plane.  They’re made up of a bunch of points and lines connecting those points.  So, it’s only natural that creating a polygon in OpenSCAD requires doing two things – defining a bunch of points and then defining the lines between those points.
    • Points.
      • When we talk about “points” we’re really talking about coordinates on the XY plane.  If you think back to high school geometry you’ll remember coordinates were usually written like this “(2,5)”.  It’s not very different for OpenSCAD, except that each point is written as:
        1. “[2,5]”
      • Before we actually try to draw a square in OpenSCAD, let’s think about how we would describe a square with each side equal to 5mm.  If we’re drawing the square so that one corner is at the “origin” then we already know one of the coordinates – “[0,0]”.  From there we need three more points to help us describe this square.  Going clockwise from the origin, these points would be “[5,0], [5,5], [0,5]”.  Therefore, the full set of all the points needed to describe our square in a clockwise fashion would be “[0,0],[5,0],[5,5],[0,5]”.
      • Thus, in order to define all the points necessary to draw a polygon, we would say:
        1. “[0,0],[5,0],[5,5],[0,5]”
    • Paths.
      • Now anyone who has ever tried to draw by connecting the dots knows that just having a bunch of points doesn’t matter much unless you actually connect those dots in the correct order.  Defining the order, or path, of the points is a lot simpler than you think.  You just specify the rank order of the various points.  OpenSCAD will draw a line from the first point to the second, second to third, et cetera until it gets to the last point – which it will connect to the first point.
      • There’s only one little trick to defining the path – and that is the first number considered is “0” and not “1.”
      • Just as with the points above, we’re going to use brackets to set the paths apart.  If we described the points in order, then we can specify the path to be the exact same order.  Like so:
        1. “[0,1,2,3]”
    • Polygon.
      • The polygon command will look familiar now that you know how to make a circle, square, and rectangle.  It is,
        1. “polygon([points],[paths]);”
      • All we’re doing with this command is telling OpenSCAD what we want it to do (draw a polygon), the points it needs to think about (points), and then the order or path in which it needs to connect those points (path).  As always, we’re going to end that statement with the semicolon.
      • Let’s substitute in the points and paths from above to draw our square:
        1. “polygon( [ [0,0],[5,0],[5,5],[0,5] ] , [ [0,1,2,3] ]);”
        • (Go ahead and drop that into OpenSCAD and hit F5 to see what happens.)
      • There are a few important things to take note of in the above.  First, there is a bracket around all of the points and another bracket around the digits describing the path.  We’re separating those two brackets (the ones for the points and the ones for the path) by a humble comma.
    • Just as with a connect the dots picture, if you have a sufficiently large list of coordinates and a description of the path between those coordinates you can make any flat shape you desire.

Okay, homework time.  Your homework is to try a few of the examples above.  With each one of those examples, change a few numbers, fiddle with it, see if you can make something else.  Nothing bad is going to happen if you get something wrong.  🙂  And, well, my homework for tonight is to learn some more stuff about OpenSCAD so I can write a tutorial for you tomorrow.

  1. Photo of parametric thin profile whistle courtesy of TeamTeamUSA []