TOP SEARCH TERMS
CANCEL
TOP SEARCH TERMS

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. []
Tagged with , 10 comments
 

10 Comments so far

  • Marty
    February 2, 2011 at 2:33 pm
     

    I am loving these. Thanks for putting them together!

     
  • Robert H. Morrison
    February 3, 2011 at 11:40 am
     

    I really like your picture for this article!

     
  • Pattywac Design Challenge: Mashup Design Challenge - MakerBot Industries
    February 3, 2011 at 9:47 pm
     

    [...] If you think remixing and mashups are hard or are only for people who have spent years playing CAD programs, stay tuned for the next installment of my OpenSCAD tutorial series! [...]

     
  • OpenSCAD Intermediates: Mashups - MakerBot Industries
    February 10, 2011 at 3:39 am
     

    [...] to make 2D forms, how to make some basic 3D forms, how to position those forms in 3D space, and the different ways to combine forms.  Although I described the last tutorial as an “intermediate” level, that’s [...]

     
  • Ian Cole
    March 6, 2011 at 7:12 pm
     

    I now wish OpenSCAD had a mode that would document a project just like your picture above. Render before each union, intersection, etc. and build the tree. I’ll use this picture anytime I’m helping someone learn OpenSCAD. Thank you for the picture and the tutorials!

     
  • Will Greene
    January 18, 2012 at 10:16 pm
     

    Hi. I am trying to do a intersection of a sphere and a cube, but even though I am doing compile and render, it still looks like both a cube and a sphere. What am I doing wrong? My code is down below and I am adding in the numbers to make it clearer (they are not in the actual code)

    1. translate([0,0,0]) sphere(r=15);
    2. translate([-12,-12,-12]) cube(24);

    3. intersection()
    4. {
    5. rotate([0,0,0]) sphere(r=15);
    6. translate([-12,-12,-12]) cube(5,5,5);
    7. }

    Thanks

     
    • MakerBlock
      MakerBlock
      January 18, 2012 at 10:22 pm
       

      @Will Greene: Ah, it looks like the object created by lines 1-2 are obscuring your object at lines 3-7. If you comment out 1-2, you’re left with just a little corner (since the small cube is way off center).

       
  • Delory
    May 18, 2012 at 7:46 pm
     

    This example code is great for showing how to operate on simple primatives.
    I want to create a more complex object, which consists of the difference between two simpler objects. Each of the ‘simpler’ object is itself a combination of primitives. How do I do such an operation?
    ie. What would the code look like that creates the complex object at the top of this webpage?

     
  • Delory
    May 26, 2012 at 7:39 am
     

    …Ah! Worked it out from looking at http://edutechwiki.unige.ch/en/OpenScad_beginners_tutorial

    difference()
    {
    intersection()
    {
    sphere(r=8, $fn=100);
    cube(size=[12,12,12],center =true);
    }
    union()
    {

    #cylinder(h=40,r=4,$fn=100,center=true);
    rotate([90,0,0])
    #cylinder(h=40,r=4,$fn=100,center=true);
    rotate([0,90,0])
    #cylinder(h=40,r=4,$fn=100,center=true);
    }
    }

     
  • Jim N
    June 25, 2013 at 4:50 pm
     

    Hey, great tutorial. Thanks. It seems like the formatting may have gotten weird since it was posted. I’m seeing the instructions centered, and not seeing any of the images you refer to, like where you write, “when you hit F5, you should see something like this.”

     
 

Leave your comment

 
 
 

xhtml: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

 
 
Chat
What can we help you with today?
I want to chat with Sales.
I have a question about an existing order.
I have a technical question about my device.
Open Support Case
Existing Orders
For faster service, enter your order number
(found in your confirmation e-mail)
Skip
Submit