3D Genius

By April 30, 2013 4 Comments

Designing a captive sphere: getting started with OpenSCAD


Download OpenSCAD hereOpenSCAD is a powerful, free program for creating 3D objects that’s especially well suited to 3D printing. Unlike visual CAD programs, models are built in OpenSCAD entirely using numerical descriptions of the objects. OpenSCAD is available for Windows, Mac OS X and Linux.

Sounds like a nightmare? Not when you take it one step at a time. We’re going to model a captive sphere inside a hollow cube, using just 10 lines of code. So download OpenSCAD to your computer, install it, and we can get started.

1. Set up the view

When you start OpenSCAD you’ll see a blank area on the left, where you type the code, and a preview window on the right. There’s a readout space beneath the preview where OpenSCAD reports on its progress.

The first thing you need to do is to make sure you can see the X, Y and Z axes – choose Show Axes from the View menu:



2. Build a cube

Units in OpenSCAD are measured in millimetres. To a large extent, the code used is plain English; so to create a cube, we only have to type the following:


Here, cube describes the type of object; the figure inside the parentheses is the length of one side of the cube. The semicolon at the end of the line signifies the end of the instruction.

To see how the model looks, you have to choose Compile from the View menu, or use the shortcut F5. Here’s the result of our instruction:



3. Center the cube

As you can see, the cube has been placed with one corner on the origin (the intersection of the x, y and z axes). Because we’re going to be adding a sphere, it makes sense to have everything centered on the axis.

We can center the cube by adding a single instruction inside the parentheses:

cube (30, center=true)

And then, when we press F5, this is how it looks:



4. Add a sphere

We want to subtract a sphere from the cube, so let’s begin by drawing that sphere. As you’d expect, the word sphere is the code here. Rather than specifying the diameter, though, the number in brackets refers to the radius of the sphere. So to make it slightly larger than a cube of side 30, let’s add a sphere of diameter 40 (and so of radius 20):


And now F5 shows us the result:



5. Increase the resolution

You can see that the sphere we’ve just drawn looks rather lumpy. That’s because it uses the default number of polygons to create it. We can increase that resolution, for all the curved surfaces in the model, using a simple instruction:


The higher the number following the $fn command, the smoother the sphere will be (but the more complex the model, naturally). Here’s how the number affects the look of the sphere:



6. Subtract the sphere

Having got the sphere into place, let’s now subtract it from the cube. We can do this using the difference command. Here’s how it works: the command is followed by a statement inside curly brackets { and }. The first object that appears inside those brackets is drawn as normal; any subsequent objects are subtracted.

So this is the instruction so far:

difference() {
cube(30, center=true);

And this is how the model looks:



7. Refine the sphere

Looking at the model so far, it seems the remaining portions of the cube are too thin. It’s a tricky print as it is, so let’s make it easier by giving our printer something a little more chunky to work with, by reducing the size of the sphere by 1mm:


Because everything’s centered on the axes, the sphere reduces from its center point and so still fits perfectly:



8. The second sphere

To add a sphere printed inside the hollow box, we just use the sphere command again, but this time outside the curly brackets. A radius of 15 will produce a large sphere that will still have room to float inside the box:


And here’s how it looks:



9. The base problem

Adding a second sphere is all very well, but there’s the problem of what it’s going to print on. We can change the angle of view by clicking and dragging in the Preview window, and when we view it from below we can see the bottom of the sphere is flush with the bottom of the cube:


The problem is, with only a single point of contact with the bed, there won’t be enough grip for the sphere to print. Once it gets above a couple of layers it will roll around the bed. We need to flatten at least a portion of the sphere so it sits firmly on the bed.

10. Enlarge the sphere

Let’s start the process by enlarging the sphere a little. Rather than making its radius 15mm, which made the diameter exactly the same as the side of the cube, let’s increase it by a millimetre:


And this is the result:



11. Truncating the sphere

Now that the sphere projects below the cube, we need to slice off the base to give it something flat to print on. We can do this, first, by adding another cube beneath the sphere.

This time, we’ll add the translate command. This will shift the following object along the x, y and z axes respectively. Like the cube and sphere commands, the measurement is included inside parentheses. Because there are three dimensions, the variable is now known as a vector, which needs to be enclosed within square brackets:

translate ([0,0,-30])
cube(30, center=true);

This tells OpenSCAD to translate the following object 0 along the x and y axes, but -30 along the z axis – in other words, its moving the 30 cube down by a value of 30, so it sits directly beneath the original cube:



12. Subtract the cube

Now, we need to subtract that cube from the existing sphere. This means using the difference command again, making sure both the sphere and the cube are inside the curly brackets. Here’s how it works out:



13. View the subtracted cube

To make it easier to understand how the model works, we can choose to see a ghosted version of subtracted shapes. This is easily achieved, by typing a hash symbol in front of it:


When we look at the bottom of the construction, we can see the flat base where the sphere has been truncated. But there seems to be a striped interference on the base of the original cube; what’s going on there? The problem is that the subtracted shape (the new cube) abuts directly against an existing shape (the original cube). This could produce errors, so let’s fix it.


14. Reduce the cube

So far, we’ve given our cubes and spheres a single value – a side length of 30 for the cube, and a radius for the sphere. But the cube can have different dimensions by changing that number so it gives a different value for each axis.

Just as we needed square brackets to indicate the vector values for the translate command, so we need them again when defining the cube:

#cube([12,12,30], center=true);

Reducing two of the sides from 30 to 12 makes them still larger than the base of the sphere, but clear of the remains of the cube:



And that’s it. All that remains is to choose Compile and Render from the View menu, or use the shortcut F6. This builds the completed model. When it’s done, choose Export as STL from the Design menu. All that remains is to slice it and print it.

Here’s the completed code, if you want to copy and paste it into OpenSCAD:

difference() {
cube(30, center=true);
difference() {
translate ([0,0,-30])
#cube([12,12,30], center=true);

We’re able to create this complex structure in just 10 lines of code. And, unlike any other CAD program, it’s easy to see at a glance what’s going on, and easy to make changes when required.

We’ll be looking at OpenSCAD in more detail in the coming weeks.

Share it!
Posted in: Beginners' guides

4 Comments on "Designing a captive sphere: getting started with OpenSCAD"

Trackback | Comments RSS Feed

  1. Greybear says:

    If your model disappears when you add the difference function, you’ll need to force Goldfeather rendering on in OpenSCAD to get the tutorial to display correctly. This a known bug within some graphics card chipsets.

    You can turn on Goldfeather by accessing the OpenSCAD user preferences and choosing the Advanced pane. You’ll see a checkbox to force Goldfeather rendering. Make sure it’s checked, and you should be able to render with F5.

    • Steve Caplin says:

      That’s very helpful. I saw the menu option to Force Goldfeather but I had no idea what it did!

  2. Ron Olsen says:

    Greybear, I’ve been having n issue for weeks with the preview not showing what I expected. Check the box, all good now. Thanks so much.

Post a Comment