First steps with the OpenSCAD language

OpenSCAD-logo

The OpenSCAD language

This language is based on only a few basic concepts. These concepts are quite elementary, but thanks to their combination you can generate any type of 3D structure.

This article is directly connected to the article OpenSCAD: how to create a solid 3D object, where a brief presentation of the OpenSCAD software and its usefulness is exposed.

In this article we will see the basic concepts that are behind the creation of a 3D object. These concepts are translated in a programming language and with a quick sequence of example you will do a general overview of how 3D models can be implemented.

Basic Shapes

  • Cube
  • Sphere
  • Cylinder
  • Polyhedron (not treated here)

CUBE

Defining a cube is very simple:

cube([width,heigth,depth]);

It creates a cube in the positive quadrant with one corner at (0,0,0).

OpenSCAD_cube
Fig.1: how to create a parallelepiped with OpenSCAD

Instead if we want to draw a cube in the origin of the coordinate system we need to explicitly add the center parameter set as true (false by default).

cube([width,heigth,depth], center = true);

SPHERE

The creation of a sphere is very simple:

sphere(r);

It draws a sphere of radius r centered at the origin of the coordinate system.

OpenSCAD_sphere
FIg.2: how to create a sphere with OpenSCAD

The sphere displayed appears, depending on its size, with the facets. If we want to increase the resolution in order to get a better “curved” surface we need to add the $fn parameter.

OpenSCAD_sphere2
FIg.3: adjusting the resolution improve the curved appearance of the figures

CYLINDER

The creation of cones and cylinder can be done by the same function.

Defining only a radius we get a cone:

cylinder(h,r,center=true,$fn = 100);   

Defining two radius we get a truncated cone, or a cylinder (if r1 = r2):

cylinder(h,r1,r2,center=true,$fn=100);

OpenSCAD_cylinder3
Fig.4: how to create a cone with OpenSCAD
OpenSCAD_cylinder2
Fig.5: how to create a truncated cone with OpenSCAD
OpenSCAD_cylinder
Fig.6: how to create a cylinder with OpenSCAD

Transformations

Once you define a 3D shape to be drawn, it is necessary to subject it to the transformations. if you’re familiar with the basics of math (matrix algebra) that are behind the 3d modeling, you should know that any transformation to which an object is subjected, can be divided into a series of elementary transformations.

  • translation
  • rotation
  • scaling
  • mirroring

TRANSLATION

Translation means moving an object by a distance in a specific direction.

translate([x,y,z])

        “an object”;

OpenSCAD_trasformation
Fig.7: how to translate an object with OpenSCAD

ROTATION

This transformation rotates an object about the origin of the coordinate system or around an arbitrary axis.

rotate([ax,ay,az])

    “an object”;

where ax, ay and az are the angles of rotations with respect to the x-axis, the y-axis and the z-axis respectively. 

OpenSCAD_trasformation2
Fig.8: how to rotate an object with OpenSCAD

SCALING

With the scaling transformation we can shrink and stretch a model along an axis. Often, this transformation is used in order to obtain other shapes which are not not obtainable with the basic shapes. A non-uniform scale on a specific axis deforms a basic shape and doing so we get a new and more complex shape. A good example may be very evident if we apply it to a sphere.

scale([x,y,z])

     “an object”;

OpenSCAD_trasformation3
Fig.9: how to not uniformly scale an object in order to create a new object with OpenSCAD

MIRRORING

This transformation mirrors the object on a plane through the origin.

mirror([nx,ny,nz])

  “an object”;

The argument of the mirror() function is the normal vector of a plane intersecting the origin through which to mirror the object.

OpenSCAD_trasformation4
Fig.10: how to mirror an object on a plane with OpenSCAD

CSG Operations

Once we have defined a set of 3D objects and applied them a sequence of transformations, the third operation to do is combining them. The combination of different object could be done with the CSG operations:

  • union
  • difference
  • intersection

UNION

The union generates a new 3D object  defined by the volume occupied by all objects subject to union.

union(){

“object1”;

“object2”;

}

OpenSCAD_union
Fig.11: a new object created by using the union CSG operation

DIFFERENCE

The difference generates a new 3D object defined by the volume occupied by the first object minus the second.

difference(){

  “object1”;

  “object2”;

}

OpenSCAD_difference
Fig.12: a new object created by using the difference CSG operation

INTERSECTION

The intersection generates a new 3D object defined by the volume occupied by both the objects.

intersection(){

  “object1”;

  “object2”;

}

OpenSCAD_intersection
Fig.13: a new object created by using the intersection CSG operation

MODIFIER CHARACTERS

There are a set of characters (‘%’ and ‘#’) which are used to change the appearance of the objects. They are particularly useful when we want to highlight the volume excluded from the previous CGS operations.

The ‘#’ character is the debug modifier and draw the corresponding object in transparent pink

OpenSCAD_modifiers
Fig.14: the debugger modifier highlights an object with a transparent pink

The ‘%’ character is the background modifier and draw the corresponding object in transparent gray.

OpenSCAD_modifiers2
Fig.15: the background modifier highlights an object with a transparent gray

In next articles some tutorial for implementing complex 3D structure will be presented. Furthermore, some topics not covered here (due their complexity) will be discussed in detail : the hull function, the Minkowski sum and the extrusion from 2D shapes.

GET INVOLVED

If you are an expert and you wish to talk about these topic by yourself. You’re welcome. Or you have developed some complex shape and you wish to share with the world. Please login and send me an article/post or tutorial. I will publish it soon.

So, see you soon!

Leave a Reply