## OpenSCAD Basics: 2D Forms

Yesterday I gave you some basic information to get started with OpenSCAD. ^{1} 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.
- “circle(5);”

- Want a circle with a radius of 6mm? No problem!
- “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:
- “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.

- Here’s how you create a circle with a radius of 5mm.
**Square.**- Here’s how you create a square with each side of 5mm.
- “square(5);”

- Want a square with each side at 6mm? You guessed it!
- “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.
- “square([4,8]);”

- If you need a rectangle with different dimensions, all you need to do is change those two numbers. Easy!

- Here’s how you create a square with each side of 5mm.
**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:
- “[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:
- “[0,0],[5,0],[5,5],[0,5]”

- 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:
*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:
- “[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,
- “polygon([
*points*],[*paths*]);”

- “polygon([
- 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:
- “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.

- The polygon command will look familiar now that you know how to make a circle, square, and rectangle. It is,
- 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.

Tagged with | 2d, 2d forms, circle, Design Tips, Digital Design, openscad, polygon, square, tutorial | 6 comments |

## 6 Comments so far

## OpenSCAD Basics: 3D Forms - MakerBot Industries

[…] keen observer will note that these shapes are very similar to the 2D shapes from the prior tutorial. The sphere and cylinder commands seem to be extensions of the circle command and the cube […]

## JohnC

note on polygons, from my playing (as a result of this tutorial) you dont need to give it the path array, it defaults to 1,2,3,4 etc

so

points=[[10,10],[20,10],[10,20],[0,1]];

polygon(points);

works just fine

## OpenSCAD tutorial outline | MakerBlock

[…] 2D forms […]

## MTO

Thanks for the tutorials! Just a few points to add:

1) JohnC: thanks for the shortcut. It makes code look a lot more readable.

2) you can’t export to STL a 2D file! To make an STL from your 2D stuff:

– use extrude like this: extrude (height=1) polygon (points);

– F5 to compile

– F6 to render (you skip the F5, but I figure with complex models, it will eventually make a difference…)

– now export your 1mm thick thing as an STL and post it on thingiverse.

3) if you use commands in the form “cylinder(h = 20, r1 = 10, r2 = 5);”, the order of the parameters is not important! That’s a weird, and I think rare, bit of syntactic sugar, but I can see it being useful when you think a library’s parameters are ordered in a non-intuitive way.

## L. Shantz

Just thought I would pop in here and comment. I was so excited to see if I could make autoscad work. Apparently you are making huge assumptions on the part of the viewer. For instance:

Here’s how you create a circle with a radius of 5mm.

“circle(5);”

I hit compile and get:

Parser error in line 1: syntax error

ERROR: Compilation failed!

So obviously there is more to creating a circle in the code line. There must be some basic “code” befrore the circle command. So now I’m stuck. I can’t even use the tutorial because the very basics don’t work for me. ?? I think it is awesome you are trying to teach us. Keep up the good work.

## gaj

In case anyone gets caught L. Shantz’s problem with starting at step 1, don’t put the double quotes around the instruction. e.g. type in:

circle(5);

not:

“circle(5);”

Then press F5. Actually F6 is better. It compiles and displays the result in one keystroke.