OpenSCAD Intermediates: Combining Forms

Complex OpenSCAD forms from simple OpenSCAD forms

Complex OpenSCAD forms from simple OpenSCAD forms

In this series of OpenSCAD tutorials we’ve covered the basics of the OpenSCAD interface, how to make 2D forms, how to make some basic 3D forms, and how to position those forms in 3D space.1  Believe it or not, if you’ve followed along with the tutorials so far, you are now capable of creating a 3D model of anything you wish.2 Draw enough circles, cylinders, cones, boxes, position them properly in 3D space and you can make anything you wish.

So, congratulations! From this point forward, you are no longer a beginning user of OpenSCAD! You, dear reader, are an intermediate! Even better, the good news is that everything from this point forward consists of methods to do the things you’ve already learned in an easier way.

A keen observer of the prior tutorials will notice that I don’t tell you how to make holes in things. Strictly speaking, you don’t really need to know this. Instead of building a cube and putting a hole through it, a careful designer could use what they’ve learned from the first four tutorials to build a series of rectangular boxes that together form a cube with a hole through it. Thankfully, there is a better way. Today we’re going to learn how to combine two 3D forms. 3

First let’s create two forms that we’ll use throughout these examples. Let’s use two cylinders of unequal sizes, each one lined up along a different axis. They are as follows:

  1. “translate([0,-25,-25]) cylinder(50,10,10);”
    • This first cylinder is 50mm tall and has a 10mm radius circle at either end. The reason we’re moving it over is so that it will pass through the second cylinder we’re about to draw.
  2. “rotate([90,0,0]) cylinder(50,8,8);”
    • This second cylinder is 50mm tall and has a 8mm radius circle at either end. The reason we’re rotating it is to that it will cross through the first cylinder at a 90 degree angle.

Copy both of those lines into OpenSCAD, hit F5 to run the code, and you should see two cylinders crossing through one another. Now for the fun stuff.

  • Difference.
    • Let’s suppose you want to create a cube with a hole in the center without having to assemble that cube using a bunch of rectangular boxes. What you really want to do is create that cube and then subtract out a section in the desired shape of the hole. If you remember your second grade math, you’ll remember that the result of subtracting one number from another is called the “difference.”
    • That’s what we’re going to do with this command. We’re going to tell OpenSCAD to subtract a certain form from another form.
    • Copy this line into OpenSCAD (minus the line numbers) and hit F5 to see what happens.
      1. “difference() { translate([0,-25,-25]) cylinder(50,10,10); rotate([90,0,0]) cylinder(50,8,8); }”
    • You’ll notice that I’ve combined the two cylinder lines into one long line. I could have written it on several lines, like so:
      1. “difference()
      2. {
      3. translate([0,-25,-25]) cylinder(50,10,10);
      4. rotate([90,0,0]) cylinder(50,8,8);
      5. }”
    • As long as you leave out the line numbers and the quote marks, both of those lines should work identically. It’s a matter of preference and readability.
    • When you hit F5, you should see something like this:
    • OpenSCAD shows you the portions that are going to be subtracted out as a greenish (rather than yellow-ish) 3D form. To really see what it looks like you can go to the menu, “Design->Compile and Render (CGAL).” Now you’ll see the first form as it looks with the second form subtracted from it.
    • There are just a few things to remember when it comes to using the “Difference” command.
      • The second form will be subtracted from the first form.
      • Don’t let two objects share the same surface or edge. OpenSCAD freaks out if any two objects share these characteristics. I don’t know for sure, but I think it’s because when that happens OpenSCAD can’t tell which surface or edge is supposed to be the inside or the outside of the final object. The upshot is that if you want a 20mm cube with a 5mm radius cylindrical hole through it, that cylinder cannot be exactly 20mm tall. (Otherwise, it would share two zero thickness surfaces with the cube.) The solution is to make the cylindrical hole larger than 20mm.
  • Intersection.
    • Instead of trying to punch a hole in one cylinder, let’s suppose you wanted just that hole itself – the section that’s common between the two cylinders. That’s what the “intersection” command does.  The way we use this command is similar to the way we use “difference.”
    • Let’s use the same two cylinders for this example. Copy this line into OpenSCAD (minus the line numbers) and hit F5 to see what happens.
      1. “intersection() { translate([0,-25,-25]) cylinder(50,10,10); rotate([90,0,0]) cylinder(50,8,8); }”
    • As with “difference,” you’ll notice that I’ve combined the two cylinder lines into one long line.  I prefer to write the commands on separate lines for readability, but use whatever is comfortable for you.  I could also have written it on several lines, like so:
      1. “intersection()
      2. {
      3. translate([0,-25,-25]) cylinder(50,10,10);
      4. rotate([90,0,0]) cylinder(50,8,8);
      5. }”
    • When you hit F5 you will just see the two cylinders forming a “plus sign.”  This is probably not what you were expecting given the above description.  To see what it really looks like go to the menu, “Design->Compile and Render (CGAL).” Now you’ll see the just those parts that were common to both cylinders.
    • Unlike the “difference” command, the order is unimportant.
  • Union.
    • In order to take two forms and combine them into a single form you use the “union” command.  Think of it as welding two forms together.  Since you’re just smooshing two forms together, the order is unimportant.  As you might imagine, the way you use “union” is very close to the way you use the “intersection” command.
    • Let’s dust off those cylinders again.  Copy this line into OpenSCAD (minus the line numbers) and hit F5 to see what happens.
      1. “union() { translate([0,-25,-25]) cylinder(50,10,10); rotate([90,0,0]) cylinder(50,8,8); }”
    • As before, you can separate out the lines for ease of reading.  This should work identically to the above line:
      1. “union()
      2. {
      3. translate([0,-25,-25]) cylinder(50,10,10);
      4. rotate([90,0,0]) cylinder(50,8,8);
      5. }”
    • When you hit F5 you will just see the two cylinders forming a “plus sign.”  Asking OpenSCAD to render the command by going to “Design->Compile and Render (CGAL)” won’t make it look any different.

Glancing back at the graphic at the top of this post you can see the complex forms that are possible once you know how to combine more simple forms.  At this point you should be able to build a figure just like that one.  So, here’s your…

Homework assignment

As you can see from the image above, you can make some really complex things out of just the simple forms we’ve already learned and the three new ways to manipulate those forms.  Hopefully you’re following along with these tutorials and learning with me because you’ve got something you want to build.  Today’s your day to show us what you can do.  Your homework is to make something, anything at all, using at least one of the OpenSCAD commands you learned today and upload it to Thingiverse.  If you want to make me extra proud, please tag it with “openscadtutorial.”  That way anyone who clicks that link will be able to see all of our hard work!

Bonus Section 1:  The Tutorials So Far

 

Bonus Section 2:  Other sources

If you like reading ahead or want more information about OpenSCAD, I’ve found these three websites to be very helpful.  A word of warning, as much useful information is on these sites, I found the presentation to be confusing.

  1. Official OpenSCAD website
  2. OpenSCAD User’s Manual
  3. OpenSCAD beginner’s tutorial
  4. OpenSCAD tutorial roundup on the Thingiverse blog

Bonus Section 3:  What’s next?!

The topic of the next tutorial is up to you.  What would you like to learn next?  Do you want to know how to make super awesome mash-ups?  Do you want to know how to make helices?  Leave a comment or shoot me an e-mail to me know!

  1. The above image is a derivative of the CSG forms from Zottie. []
  2. If you haven’t followed along with these tutorials, skip to the bottom of this post and check out the links to the prior tutorials. I’m pretty sure you can breeze through the first four posts in about 30 minutes total. []
  3. Sorry for the lack of pictures to accompany this tutorial.  I tried to include them but they were causing all kinds of formatting problems with the tutorial outline. []