2. Creating a 3D model

2.1 Introduction to OpenSCAD’s language


To understand how to work in OpenSCAD we need to cover some simple code basics. Your 3D model will be build based on primary shapes such as cubes, cylinders, and spheres. Those are called Objects. Objects are coded with Actions, modified by Operators, with Variables and Comments. We will cover exact examples of them in the latter part of the tutorial.


2.2 Basic code commands: Actions

Actions are used to create objects or define variables. A very important thing to remember is that Actions must end with a semicolon! 

Example: 

Cube(x,y,z); 

Cylinder(h,r1,r2);


2.3 Basic code commands: Operators

Operators are used to modify objects (such as size or position) within the curly brackets in the code. It’s essential for you to remember about the curly brackets, they define Operators.

Example:

Union: to combine all the objects within its curly brackets into a single Object

Union()

{Cube(x, center= true);

Sphere(x);


2.4 Basic code commands: Variables, Comments

Variables contain values: numbers, strings, vectors and lists. Comments are used to provide extra information to anyone reading the code. OpenSCAD doesn't read Comments and they have no effect on the code. More advanced options of OpenSCAD are Vectors, Lists, Modules and Functions.


2.5 All commands- cheat sheet

OpenSCAD provides us with a list of all commands:


openscad_img_23.jpghttp://www.openscad.org/cheatsheet/index.html 


2.6 Creating shapes: cube

Now we will move on to actually coding the 3D shapes and letting OpenSCAD translate them. Let’s get started with primary shapes and create a cube:

openscad_img_01.jpg

cube([3,5,7]); 

Notice that we didn’t set the center of the cube, see what happens when we add ‘’true’’ or ‘’false’’


Adding ‘’center= false’’ didn't change anything.

openscad_img_02.jpg

cube([3,5,7, center= false]); 


However, setting the center to true sets the cube’s middle point to 0,0,0 on all axis.

openscad_img_03.jpg

cube([3,5,7, center=true]);



2.7 Creating shapes: sphere

We will set the center of the cube to false and create a sphere.

openscad_img_04.jpg

cube([3,5,7, center=false]); 

sphere([20]); 

Tip: because we didn’t specify if Sphere’s number in the brackets is a radius or diameter, OpenSCAD assumes it’s a radius. 



Notice that the sphere doesn’t really resemble a ball. That’s due to a low number of polygons. We can fix that with this piece of code:

openscad_img_05.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=20]);


It still seems a bit low, we can make the number higher, but remember to check the layer thickness of the 3D printing method you choose, as setting the number too high for your 3D model might cause problems during the 3D printing process.

openscad_img_06.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]);


2.8 Creating shapes: Cylinder and Cone

It’s good to get a fair amount of practice if you’re unfamiliar with coding. If you would like to create a cone, you need to create a cylinder with the Z value set to 0. OpenSCAD will read the code like this:


openscad_img_07.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

cylinder([10,3,0]); 



Changing the Z value to 3 will result in a proportional cylinder.


openscad_img_08.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

cylinder([10,3,3]); 



If you would like your cone or cylinder to be smoother we apply the same code as in the sphere example.


openscad_img_09.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

cylinder([10,3,3, $fn=100]); 


2.9 Moving the objects

Next we will talk about moving the objects. To do that we use the Translate Operator. Remember that you have to apply it in the line of code above the object it applies to and that it doesn’t finish with a semicolon.


openscad_img_10.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

translate([10,0,0]) 

cylinder([10,3,3, $fn=100]); 



openscad_img_11.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

translate([10,6,0]) 

cylinder([10,3,3, $fn=100]); 



openscad_img_13.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

translate([10,6,5]) 

cylinder([10,3,3, $fn=100]); 


To place an object under an angle, we need to apply the Rotate Operator.

openscad_img_14.jpg

cube([3,5,7, center=false]); 

sphere([20, $fn=100]); 

translate([10,6,5]) 

rotate([0,-90,0]) 

cylinder([10,3,3, $fn=100]);


2.10 Modifying the Objects with Operators

Now we will talk about some functions that will be very handy when it comes to 3D printing your model. We will go over four most important Operators: Union, Intersection and Difference. The latter one is especially useful because it can be used for hollowing your 3D model to lower the costs and material waste of 3D printing.


We will create a cube with all the walls of 12 cm length placed in the 0 point of all the axis.

openscad_img_15.jpg

cube([12, center=true]); 


To modify the cube we will code a sphere with a radius of 8.

openscad_img_16.jpg

cube([12, center=true]); 

sphere([8]); 


2.11 Operators: Union

First Operator we will go through is Union. This Operator tells OpenSCAD to unite two objects. They will now act as one. Remember about the curly brackets!


openscad_img_17.jpg

Union () 

{cube([12, center=true]); 

sphere([8]);} 


2.12 Operators: Difference

Another Operator is Difference. Difference command is translated by OpenSCAD to cut out an object out of the other. This function is especially helpful with hollowing your objects. Pay attention to the order of your Objects. In this example, we will cut out the sphere’s shape out of the cube.


openscad_img_18.jpg

Difference () 

{cube([12, center=true]); 

sphere([8]);} 


If you would like to put the cube out of the sphere, you need to change the order of your Objects in the code.

openscad_img_19.jpg

Difference () 

{shpere([12, center=true]); 

cube([8]);} 


2.13 Operators: Intersection

Last but not least, you can also combine common parts of two objects with the Intersection command.


openscad_img_20.jpg

Intersection () 

{cube([12, center=true]); 

sphere([8]);}