Using OpenSCAD to model 3D structures

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Sergey Jarochkin, 123RF

Sergey Jarochkin, 123RF

Block & Slice

The program OpenSCAD makes it possible to construct 3D objects via just a few commands. STL Exporter makes the software interesting for users who have a 3D printer.

OpenSCAD makes you feel right at home once you know that a radius defines a sphere or that height and diameter define the surface of a cylinder. The OpenSCAD design software also lets you create structures for printing with a 3D printer. The tests performed for this article produced a cap for a USB stick, a model airplane, Lego bricks, and a replacement key.

Popular distributions offer OpenSCAD for installation, which is usually the 2104 version. The software homepage [1] explains how to download the updated version 2015.03-1. Downloading the newer version is worthwhile because it offers the possibility of directly incorporating text objects.

Introduction

Once the software starts, a window will open with an input area to the left, an output area to the right and a console. Various buttons offer basic functions. The icons to the left in the editor window are for common tasks such as opening and saving files, and typical editing actions, such as undoing, repeating and indenting.

The two cube icons start the program either for rapid CSG prototyping when you intend to do an STL export or for rendering via CGAL (see the "Vocabulary" box for more info). The same actions are started via the F5 and F6 function keys. You should try out the interfaces for the output window using a small program.

Vocabulary

  • CSG (which stands for Constructive Solid Geometry) is a technique used in solid modeling. The technique allows the user to build three dimensional objects by combining fundamental geometric structures.
  • STL is derived from stereolithography. STL denotes the standard file format for describing surfaces.
  • CGAL stands for Computational Geometry Algorithms Library. It is a library for rendering 3D surfaces.

To draw a sphere, enter the sphere(); command into the editor. Remember to always include the semicolon at the end of each command. Then, press the F5 function key or click on the cube icon with two prongs. This causes the software to execute the one-line program. In the output area on the right, you will see a blue, angular shape (Figure 1).

Figure 1: You can create the basis for a solid with a one-line command.

You can use the icon bar to change and rotate the viewing angle of the object. You can also change the view between orthographic, where the vanishing point is at infinity, and perspective projection, where distant objects appear smaller.

The easiest way to do this is to move the viewing angle with the mouse. The scroll wheel changes the distance, the left mouse button changes the viewing angle and the right button changes the position. The coordinates for the imaginary camera are shown in the status bar at the bottom.

This first program provides a glimpse into the idiosyncrasies of OpenSCAD. The program also assigns default values to most variables. If you do not enter a radius or position for the solid then the program will assume a sphere having a diameter of one unit at the origin of the coordinate system.

The software does not recognize any curved surfaces. Instead, it approximates the desired shape using a grid of triangles. The menu option View | show_edges this grid visible. Via the $fn variable, you can control the number of edges, as in the following example.

$fn=200;
sphere();

Although the rendering now takes longer, you will get an almost perfect sphere. Instead of globally modifying the variable, the application offers the possibility of restricting it to this one sphere.

sphere();
translate([2,0,0]) sphere(r=2, $fn=200);

At the coordinate center, you will see both a rough rendering of the sphere and a properly formed sphere with a radius of r=2 units (Figure 2). You can prevent the spheres from overlapping with translate() . This moves the sphere by the vector with the coordinates x , y , and z . In our case, the value for x is 2 units.

Figure 2: Using the translate() command lets you move the two objects next to one another without any overlap.

Many commands have an impact on the subsequent object. If a semicolon were to follow the translate command, then the instruction would not affect the subsequent sphere.

The next example involves the construction of a pencil. The object consists of a cylinder with a height of h=50 and a radius of r=5 . Because the pencil will have six edges, you should restrict the rendering to six surfaces via $fn=6 , as follows:

cylinder(h=50, r=5, $fn=6);

For this example, a cylinder with an upper radius of r2=5 , a lower radius of r1=0 , and a height of h=15 serves as the cone.

For the software to smooth out all objects thoroughly except the pencil, you should globally set the variable $fn to 50 by prefixing the assignment to the construction of the cylinder:

$fn=50;
cylinder(h=15, r1=0, r2=60);

The pencil point is created with the cone. The command intersection() restricts the view to elements belonging to both the cone and the pencil shaft. The rotate() command tilts the pencil (Figure 3). As with translate() , this command also affects the element that follows. Therefore, it does not end with a semicolon.

Figure 3: You can create the basic shape of a pencil using a cone and a cylinder.

If you pass only one vector into the rotate() function as shown in the example, then the first value designates the angle of rotation around the X axis, and the second value is used for the subsequent rotation around the Y and Z axis. If you also pass to rotate() and also a value for an angle, then it interprets the vector as a rotational axis.

Since its 2015 version, the program has had a text() command, which generates 2D text. The text really only becomes visible in the CGAL renderer because the quick view has as kludge that extrudes the text with a thickness of one unit. The text actually looks plastic when the linear_extrude() command is used.

Print Template

You can create the software objects using just a few program lines. These objects are suitable for use with a 3D printer. Here I use a cap for a USB stick as an example. It requires commands for only three objects. These include commands for a cuboid, which determines the outer measurements of the cap, commands for a second cuboid corresponding to the size of the USB plug, and finally the difference() command, which subtracts the two cuboids. Listing 1 shows these commands. Figure 4 shows the results.

Listing 1

A USB Stick

difference(){
 translate([0,0,5]) color("red", 0.8) cube([5.5, 13, 12], center=true);
 translate([0,0,15]) color("grey", 1) cube([4.5, 12, 30], center=true);
}
Figure 4: It is possible to create a cap for a USB stick from two cuboids.

The file format STL is understood by most Slice programs for 3D printers. Saving a file with a corresponding template requires that the CGAL renderer finishes successfully. The color information will get lost in the process. Then, you should save the result either via the STL icon or via File | Export | Export_STL .

The software does not understand physical dimensions – only units. The program that further processes the data therefore expects an instruction indicating whether the opening of the cap is 4.5x12 inches or millimeters, and whether a scaling factor for adapting the contraction will be employed. If you put the projection(cut=true) command first, then OpenSCAD defines the cross-sectional area inside of the X/Y plane. This is how you control the thickness of the wall of the cap.

To illustrate the intended use of the cap, you should add a schematic drawing of the USB stick to the script. The color() command colors the object. It understands the names of colors in English and in the form of an RGB vector where the three components determine the red, green, and blue amounts. This is how the color([1,1,0]) command creates the color yellow. You can add transparency as a fourth coordinate or as an individual number when you name the colors. An example here would be color("yellow", 0.5) .

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content