Why Use OpenSCAD?


There are a variety of 3D modeling packages out there, and many of them can easily produce the STL or OBJ files typically necessary for slicing and then printing. Most use a CAD-style interface, where you interact with an object in three dimensions by rotating it and using various tools and brushes to achieve the desired effect with your mouse. OpenSCAD takes a different approach. It uses scad files to define primitives (cubes, spheres, cylinders, etc.) and transformations to those objects as text. This is all done using a C-like syntax which should be familiar to anyone with some programming experience.


But when it comes to 3D printing, how does this help? Since it's programmatic, it allows for very controlled, specific modifications to be made to parts. It also easily works with many mature development tools for working with source code. Leveraging these in conjunction with OpenSCAD and it's libraries provides a great environment for parts design.


Before You Start


The first step is getting an OpenSCAD development environment. You can download the main package from openscad.org, or use ScorchCAD on Android (although ScorchCAD does not necessarily support the complete builtin instruction set). Although you can use these for creating your models, I typically only use them for rendering. For this tutorial, I'll be working with just the OpenSCAD IDE. If you are a programmer, I suggest using your text editor of choice and any associated tools you are most comfortable with (git for version control and diff for comparing files, for example).


Set up? Let's create our first simple object


Our First Object


Start OpenSCAD. You'll see the menu at the top of the screen. Under File you'll see your normal Save/New/Open items, along with the Export menu. Export is where you will go to create your final STL file to send to your printer. Edit contains many typical editing functions, along with Preferences. Design and View contain a number of rendering options, and finally there are Help links to the OpenSCAD site that gives build information for the version of the software you are using.




On the left there's the editor where we'll enter our code, the large canvas to the right will show the render when we create an object, and the bottom will give us information about our last render, including errors in our code.


Enter the following code:


 

cube([2,2,2]);

 


Press F5 to compile the object. You should see a small cube (2 mm on each side) appear in the render area.




Press F6 to render the object for export. You should not get any errors; if you do, double check what you typed. Go to File -> Export -> Export as STL... Save the STL, and you now have an object you can slice and print. This is typically the process you will follow every time to create something you can print from your code.




You may want to perform some post-processing in another tool, like Blender or RepetierHost on the STL file before printing, but this will be based on the particular quirks of your printer or filament you're working with. If you want you can work out some of these details now, or continue on to work on something a little more practical.


Something More Useful

Let's look in more detail on how you can use OpenSCAD to make a specific part. We're going to make a smooth bolt with a hole for pinning two other parts together.

  1. Go to File -> New. If you want, you can save our previous cube.
  2. Enter the following code into the empty editor area:


 

// The head of our bolt. 
cylinder(h=1,r=1.5,$fn=30);

// The rest of the bolt needs to be displaced the height of the head. 
translate([0,0,1]) difference(){

	//The rest of the bolt.
	cylinder(h=10,r=.75,$fn=30);

	//This is cut from the rest of the bolt. 
	translate([-.75,0,8.5]) {

	//It needs to be rotated. 
	rotate([0,90,0])cylinder(h=1.5,r=.5,$fn=10); 
	}
}

 


You can omit the information preceded by the "//" sign if you like. Like many languages, this denotes a comment and does not actually impact the creation of the model. It allows us to create notes in our code.


Compile it with F5, and you should now have a model like this:




Let's look at this line by line: 

 

cylinder(h=1,r=1.5,$fn=30); 

 

 This defines the head of the bolt as a cylinder. We're setting a few variables here: "h" is the height of the cylinder, "r" is the radius, "$fn" dictates how many polygons we should use for the curved outside of the bolt. These variables are builtin to the primitive objects, but we'll define our own in the next section. 

 

translate([0,0,1]) difference(){ 

 

 This line defines a block of code (surrounded by curly brackets) that should do two things. First, we're the objects in the block one millimeter on the Z-axis. Second, we're going to take the first object in this block and remove from it all subsequent objects. This is like cutting from the first object.

 

    cylinder(h=10,r=.75,$fn=30); 

 

This defines the shaft of the bolt. It's 10 mm long, and has a radius of .75 mm. 30 polygons seemed reasonable for the outside of it. Since it's the first object defined, this is what we'll be cutting from in this block of code.

 

    translate([-.75,0,8.5]) { 

 

For the hole that we're placing a pin in eventually, we need to do two things. First, we need to offset it since it will be rotated (the rotation is relative to one end, not the center). Second, we need to put it at the end of the bolt. Note that this is another block of code nested inside of the first. This is pretty common for more complex differences. 


     rotate([0,90,0])cylinder(h=1.5,r=.5,$fn=10); 

 

This is the actual hole we're making. It's radius is .5 mm, so you can use a 1 mm diameter pin to hold it in place. It's height is the diameter of the shaft. We're rotating it 90 degrees on one axis so it's perpendicular to the shaft.


 

	}
} 

  

These close up the previous code blocks.

Making It Adjustable

So we've designed our bolt/pin, but it's only 1.1 centimeters long, fitting parts that are 8 mm in total width. What would be great is if we could adjust that. This is where assigning our own variables really shines. Start a new file, and enter the following code:


 

depth = 8;
radius = .75;

cylinder(h=1,r=(radius+.5),$fn=30);

translate([0,0,1]) difference(){

    cylinder(h=depth+2,r=radius,$fn=30);

    translate([(-1*radius),0,(depth+.5)]) {

     rotate([0,90,0])cylinder(h=(2*radius),r=.5,$fn=10);
    }
} 

 


Note that we're defining a few variable in the beginning. Render it, then try changing them to this:


    

depth = 5;
radius = 2;

    

Render it again. It should now look like this:




What we're doing now is using math to define the part, instead of constant values. For example, this line says that the head of the bolt should always have a radius .5 mm larger than the shaft:

 

cylinder(h=1,r=(radius+.5),$fn=30); 

 

Note that the actual computation is in it's own set of parenthesis. By embedding little equations into the object definitions, we can have a set of variables that are easily adjusted each time we need a similar part. Each of these can go into its own set of parenthesis, or define another single variable used later on to make the code more readable, like this:

 

x=(y+z); 

 

What's Next?

This just scratches the surface for OpenSCAD. There are many other powerful builtin functions, and external libraries that can be imported to expand it's functionality. Check out some of the other tutorials on OpenSCAD located here on Printspace3D, along with OpenSCAD's website and the manual offered on Wikibooks. Most importantly, keep making things!