OpenSCAD User Manual/Tips and Tricks

= A note on licensing =

All code snippets shown on this page are intended to be used freely without any attribution and for any purpose, e.g. consider any code contribution here to be placed under Public Domain or CC0 license. This is not meant to change the normal license of the page as a whole and/or the manual itself.

= Data =

Caring about undef
Most illegal operations in OpenSCAD return. Some return. However, the program keeps running and  values may cause unpredictable future behaviour if no precaution is taken. When a function argument is missing in a function call, an  value is assigned to it in evaluating the function expression. To avoid this, a default value may be assigned to optional function arguments.

Sometimes the default value depends on other parameters of the call and cannot be set as before; a conditional expression solve this:

The function  returns undesirable values when   and generates a warning (try it!). A simple solution would be to return the empty list  in this case:

The output  above is the empty list because   and the comparison of   and   evaluates as  : the default value of   has been lost. To overcome this it is enough to invert the test:

Now, when  is undefined, the first test evaluates as false and the   is executed. With careful choices of tests, we can deal with  values.

= Geometry =

Minimum rotation problem
In 2D, except in very special cases, there are only two rotations that make a vector to align to another one. In 3D, there are infinitely many. Only one, however, has the minimum rotation angle. The following function builds the matrix for that minimum rotation. The code is a simplification of a function found in the Oskar Linde's sweep.scad.

Drawing "lines" in OpenSCAD
Another approach to the module line is found in Rotation rule help.

hull sequence or chain
Using loops, complex models can be composed by iteratively producing hull segments, even with a small number of parameters. The body of the loop in the following example is evaluated repeatedly, using values for 'i', beginning at, incrementing by 1, until evaluating   and terminating before evaluating at. This produces the following 2-dimensional array for values of 'i' and 'j':

Fit text into a given area
There is currently no way to query the size of the geometry generated by. Depending on the model it might be possible to calculate a rough estimate of the text size and fit the text into the known area. This works using  with the assumption the length is the dominating value.

Create a mirrored object while retaining the original
The  module just transforms the existing object, so it can't be used to generate symmetrical objects. However using the  module, it's easily possible define a new module   that generates the mirrored object in addition to the original one.



Arrange parts on a spatial array
An operator to display a set of objects on an array.



A handy operator to display a lot of parts of a project downloaded from Thingiverse.

Note: the following usage fails: because the  statement do an implicit union of the inside objects creating only one child.

Rounding polygons
Polygons may be rounded by the offset operator in several forms.



Filleting objects
Filleting is the 3D counterpart of the rounding of polygons. There is no offset operators for 3D objects, but it may be coded using minkowski operator.

Note that this is a very time consuming process. The minkowski operator adds vertices to the model so each new offset_3d takes longer than the previous one.

Computing a bounding box
There is no way to get the bounding box limits of an object with OpenSCAD. However, it is possible to compute its bounding box volume. Its concept is simple: hull the projection of the model on each axis (1D sets) and minkowski them. As there is no way to define a 1D set in OpenSCAD, the projections are approximated by a stick whose length is the size of the projection. The image shows the (transparent) bounding box of a red model generated by the code: The cubes in the offset3D operator code of the Filleting objects tip could well be replaced by the object bounding box dispensing the artificial argument size.

As an example of solving problems with this, with a little manipulation of the result, the bounding box can be used to augment features around arbitrary text without knowing the size of the text. In this example a square base plate for the text is created with two holes inserted into it at the ends of the text, all having fixed margins. This works by taking the projection of the bounding box, expanding it evenly, shrinking the y dimension to a sliver, and extending the x direction outward by a sliver, and subtracting off the expanded bounding box projection again, leaving two near point-like objects which can be expanded with offset into the holes.

Data Heightmap
The builtin module surface is able to create a 3D object that represents the heightmap of data in a matrix of numbers. However, the data matrix for surface should be stored in an external text file. The following module does the exact heightmap of surface for a data set generated by the user code.

= Strings =

Integer from Numeric String (Decimal or Hex)
Converts number in string format to an integer, (s2d - String 2 Decimal - named before I added hex to it...)

e.g. echo(s2d("314159")/100000); // shows ECHO: 3.14159

= Debug =

Debug Tap function
Similar to Ruby's Tap function. This function encapsulates the echo to console side-effect, if $_debug is true and returns the object.

e.g. given $_debug is true; x = debugTap(2 * 2, "Solution is: "); // shows ECHO: Solution is: 4