OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
ln
Mathematical natural logarithm. See: Natural logarithm
len
Mathematical length function. Returns the length of an array, a vector or a string
parameter.
Usage examples:
str1="abcdef"; len_str1=len(str1);
echo(str1,len_str1);
a=6; len_a=len(a);
echo(a,len_a);
array1=[1,2,3,4,5,6,7,8]; len_array1=len(array1);
echo(array1,len_array1);
array2=[[0,0],[0,1],[1,0],[1,1]]; len_array2=len(array2);
echo(array2,len_array2);
len_array2_2=len(array2[2]);
echo(array2[2],len_array2_2);
Results:
ECHO: "abcdef", 6
ECHO: 6, undef
ECHO: [1, 2, 3, 4, 5, 6, 7, 8], 8
ECHO: [[0, 0], [0, 1], [1, 0], [1, 1]], 4
ECHO: [1, 0], 2
This function allows (e.g.) the parsing of an array, a vector or a string.
Usage examples:
str2="4711";
for (i=[0:len(str2)-1])
echo(str("digit ",i+1," : ",str2[i]));
Results:
ECHO: "digit 1 : 4"
ECHO: "digit 2 : 7"
ECHO: "digit 3 : 1"
ECHO: "digit 4 : 1"
Note that the len() function is not defined when a simple variable is passed as the
30 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
parameter.
This is useful when handling parameters to a module, similar to how shapes can be defined
as a single number, or as an [x,y,z] vector; i.e. cube(5) or cube([5,5,5])
For example
module doIt(size) {
if (len(size) == undef) {
// size is a number, use it for x,y & z. (or could be undef)
do([size,size,size]);
} else {
// size is a vector, (could be a string but that would be stupid)
do(size);
}
}
doIt(5); // equivalent to [5,5,5]
doIt([5,5,5]); // similar to cube(5) v's cube([5,5,5])
log
Mathematical logarithm. See: Logarithm
lookup
Look up value in table, and linearly interpolate if there's no exact match. The first
argument is the value to look up. The second is the lookup table -- a vector of key-value
pairs.
Parameters
key
A lookup key
<key,value> array
keys and values
Notes
There is a bug where out-of-range keys will return the first value in the list. Newer versions
of Openscad should use the top or bottom end of the table as appropriate instead.
Usage example:
Will create a sort of 3D chart made out of cylinders of different
height.
function get_cylinder_h(p) = lookup(p, [
[ -200, 5 ],
[ -50, 20 ],
31 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
[ -20, 18 ],
[ +80, 25 ],
[ +150, 2 ]
]);
for (i = [-100:5:+100]) {
// echo(i, get_cylinder_h(i));
translate([ i, 0, -30 ]) cylinder(r1 = 6, r2 = 2, h = get_cylinder_h(i)*3);
}
OpenSCAD Lookup
max
Returns the maximum of the two parameters.
Parameters
<a>
Decimal.
<b>
Decimal.
Usage Example:
max(3.0,5.0);
max(8.0,3.0);
Results:
5.0
8.0
min
Returns the minimum of the two parameters.
Parameters
<a>
Decimal.
<b>
Decimal.
Usage Example:
min(3.0,5.0);
32 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
min(8.0,3.0);
Results:
3.0
3.0
Looking for mod - it's not a function, see modulo operator (%)
norm
[Note: Requires version 2014.Q1(see https://raw.github.com/openscad/openscad/master
/RELEASE_NOTES)]
Returns the euclidean norm of a vector. Note this returns is the actual numeric length
while len returns the number of elements in the vector or array.
Usage examples:
a=[1,2,3,4];
b="abcd";
c=[];
d="";
e=[[1,2,3,4],[1,2,3],[1,2],[1]];
echo(norm(a)); //5.47723
echo(norm(b)); //undef
echo(norm(c)); //0
echo(norm(d)); //undef
echo(norm(e[0])); //5.47723
echo(norm(e[1])); //3.74166
echo(norm(e[2])); //2.23607
echo(norm(e[3])); //1
Results:
ECHO: 5.47723
ECHO: undef
ECHO: 0
ECHO: undef
ECHO: 5.47723
ECHO: 3.74166
ECHO: 2.23607
ECHO: 1
pow
Mathematical power function.
Parameters
33 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
<base>
Decimal. Base.
<exponent>
Decimal. Exponent.
Usage examples:
for (i = [0:5]) {
translate([i*25,0,0]) {
cylinder(h = pow(2,i)*5, r=10);
echo (i, pow(2,i));
}
}
echo(pow(10,2)); // means 10^2 or 10*10
// result: ECHO: 100
echo(pow(10,3)); // means 10^3 or 10*10*10
// result: ECHO: 1000
rands
Random number generator. Generates a constant vector of pseudo random numbers, much
like an array. When generating only one number, you still call it with variable[0]
Parameters
min_value
Minimum value of random number range
max_value
Maximum value of random number range
value_count
Number of random numbers to return as a vector
seed_value (optional)
Seed value for random number generator for repeatable results.
Usage Examples:
// get a single number
single_rand = rands(0,10,1)[0];
echo(single_rand);
// get a vector of 4 numbers
seed=42;
random_vect=rands(5,15,4,seed);
echo( "Random Vector: ",random_vect);
sphere(r=5);
for(i=[0:3]) {
rotate(360*i/4) {
translate([10+random_vect[i],0,0])
sphere(r=random_vect[i]/2);
34 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
}
}
round
The "round" operator returns the greatest or least integer part, respectively, if the numeric
input is positive or negative.
Some examples:
round(x.5) = x+1.
round(x.49) = x.
round(-(x.5)) = -(x+1).
round(-(x.49)) = -x.
round(5.4); //-> 5
round(5.5); //-> 6
round(5.6); //-> 6
sign
Mathematical signum function. Returns a unit value that extracts the sign of a value see:
Signum function
Parameters
<x>
Decimal. Value to find the sign of.
Usage examples:
sign(-5.0);
sign(0);
sign(8.0);
Results:
-1.0
0.0
1.0
sqrt
Mathematical square root function.
35 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
Usage Examples:
translate([sqrt(100),0,0])sphere(100);
str
Convert all arguments to strings and concatenate.
Usage examples:
number=2;
echo ("This is ",number,3," and that's it.");
echo (str("This is ",number,3," and that's it."));
Results:
ECHO: "This is ", 2, 3, " and that's it."
ECHO: "This is 23 and that's it."
Also See search()
search() for text searching.
cube
Creates a cube at the origin of the coordinate system. When center is true the cube will be
centered on the origin, otherwise it is created in the first octant. The argument names are
optional if the arguments are given in the same order as specified in the parameters
Parameters
size
Decimal or 3 value array. If a single number is given, the result will be a cube with
sides of that length. If a 3 value array is given, then the values will correspond to the
lengths of the X, Y, and Z sides. Default value is 1.
center
Boolean. This determines the positioning of the object. If true, object is centered at
(0,0,0). Otherwise, the cube is placed in the positive quadrant with one corner at
(0,0,0). Defaults to false
Usage examples:
cube(size = 1, center = false);
36 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
cube(size = [1,2,3], center = true);
sphere
Creates a sphere at the origin of the coordinate system. The argument name is optional.
Parameters
r
Decimal. This is the radius of the sphere. The resolution of the sphere will be based on
the size of the sphere and the $fa, $fs and $fn variables. For more information on
these special variables look at: OpenSCAD_User_Manual/Other_Language_Features
d
Decimal. This is the diameter of the sphere. [Note: Requires version 2014.03(see [2]
(http://www.openscad.org/news.html))]
$fa
Fragment angle in degrees
$fs
Fragment size in mm
$fn
Resolution
Usage Examples
sphere(r = 1);
sphere(r = 5);
sphere(r = 10);
37 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
sphere(d = 2);
sphere(d = 10);
sphere(d = 20);
// this will create a high resolution sphere with a 2mm radius
sphere(2, $fn=100);
// will also create a 2mm high resolution sphere but this one
// does not have as many small triangles on the poles of the sphere
sphere(2, $fa=5, $fs=0.1);
cylinder
Creates a cylinder or cone at the origin of the coordinate system. A single radius (r) makes a
cylinder, two different radi (r1, r2) make a cone.
Parameters
h
Decimal. This is the height of the cylinder. Default value is 1.
r
Decimal. The radius of both top and bottom ends of the cylinder. Use this parameter if
you want plain cylinder. Default value is 1.
r1
Decimal. This is the radius of the cone on bottom end. Default value is 1.
r2
Decimal. This is the radius of the cone on top end. Default value is 1.
d
Decimal. The diameter of both top and bottom ends of the cylinder. Use this
parameter if you want plain cylinder. Default value is 1. [Note: Requires version
2014.03(see [3] (http://www.openscad.org/news.html))]
d1
38 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
Decimal. This is the diameter of the cone on bottom end. Default value is 1. [Note:
Requires version 2014.03(see [4] (http://www.openscad.org/news.html))]
d2
Decimal. This is the diameter of the cone on top end. Default value is 1. [Note:
Requires version 2014.03(see [5] (http://www.openscad.org/news.html))]
center
boolean. If true will center the height of the cone/cylinder around the origin. Default
is false, placing the base of the cylinder or r1 radius of cone at the origin.
$fa
Angle in degrees
$fs
Angle in mm
$fn
Resolution
Usage Examples
cylinder(h = 10, r=20);
cylinder(h = 10, r=20, $fs=6);
cylinder(h = 10, r1 = 10, r2 = 20, center = false);
cylinder(h = 10, r1 = 20, r2 = 10, center = true);
cylinder(h = 10, d=40);
cylinder(h = 10, d=40, $fs=6);
cylinder(h = 10, d1 = 20, d2 = 40, center = false);
cylinder(h = 10, d1 = 40, d2 = 20, center = true);
polyhedron
Create a polyhedron with a list of points and a list of triangles. The point list is all the
vertices of the shape, the triangle list is how the points relates to the surfaces of the
polyhedron.
Parameters
39 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
points
vector of points or vertices (each a 3 vector).
triangles
vector of point triplets (each a 3 number vector). Each number is the 0-indexed point
number from the point vector.
faces
this parameter will replace triangles [Note: Requires version 2014.03]. vector of point
n-tuples with n >= 3. Each number is the 0-indexed point number from the point
vector. When referencing more than 3 points in a single tuple, the points must all be
on the same plane.
convexity
Integer. The convexity parameter specifies the maximum number of front sides (back
sides) a ray intersecting the object might penetrate. This parameter is only needed for
correctly displaying the object in OpenCSG preview mode and has no effect on the
polyhedron rendering.
Syntax example
polyhedron(points = [ [x, y, z], ... ], triangles = [ [p1, p2, p3..], ... ], convexity = N);
Triangle points ordering When looking at the face from the outside inwards, the points
must be clockwise. You can rearrange the order of the points or the order they are
referenced in each triangle triple. The order of triangles is immaterial. Note that if your
polygons are not all oriented the same way OpenSCAD will either print an error or crash
completely, so pay attention to the vertex ordering. Again, remember that the 'pN'
components of the triangles vector are 0-indexed references to the elements of the points
vector.
Example, a square base pyramid:
polyhedron(
points=[ [10,10,0],[10,-10,0],[-10,-10,0],[-10,10,0], // the four points at base
[0,0,10] ], // the apex point
triangles=[ [0,1,4],[1,2,4],[2,3,4],[3,0,4], // each triangle side
[1,0,3],[2,1,3] ] // two triangles for square base
);
40 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
A simple polyhedron, square based pyramid
Ordering of triangle points An example of a more complex polyhedron, and showing how
to fix polyhedrons with badly oriented polygons.
When you select 'Thrown together' from the view menu and compile the design (not
compile and render!) you will see a preview with the mis-oriented polygons highlighted.
Unfortunately this highlighting is not possible in the OpenCSG preview mode because it
would interfere with the way the OpenCSG preview mode is implemented.)
Below you can see the code and the picture of such a problematic polyhedron, the bad
polygons (triangles or compositions of triangles) are in pink.
// Bad polyhedron
polyhedron
(points = [
[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60],
[10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]
],
triangles = [
[0,2,3], [0,1,2], [0,4,5], [0,5,1], [5,4,2], [2,4,3],
[6,8,9], [6,7,8], [6,10,11], [6,11,7], [10,8,11],
[10,9,8], [0,3,9], [9,0,6], [10,6, 0], [0,4,10],
[3,9,10], [3,10,4], [1,7,11], [1,11,5], [1,7,8],
[1,8,2], [2,8,11], [2,11,5]
]
);
41 sur 128 11/03/2014 10:03
OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...
Polyhedron with badly oriented polygons
A correct polyhedron would be the following:
polyhedron
(points = [
[0, -10, 60], [0, 10, 60], [0, 10, 0], [0, -10, 0], [60, -10, 60], [60, 10, 60],
[10, -10, 50], [10, 10, 50], [10, 10, 30], [10, -10, 30], [30, -10, 50], [30, 10, 50]
],
triangles = [
[0,3,2], [0,2,1], [4,0,5], [5,0,1], [5,2,4], [4,2,3],
[6,8,9], [6,7,8], [6,10,11],[6,11,7], [10,8,11],
[10,9,8], [3,0,9], [9,0,6], [10,6, 0],[0,4,10],
[3,9,10], [3,10,4], [1,7,11], [1,11,5], [1,8,7],
[2,8,1], [8,2,11], [5,11,2]
]
);
Beginner's tip:
If you don't really understand "orientation", try to identify the mis-oriented pink triangles
and then permute the references to the points vectors until you get it right. E.g. in the
above example, the third triangle ([0,4,5]) was wrong and we fixed it as [4,0,5]. In addition,
you may select "Show Edges" from the "View Menu", print a screen capture and number
both the points and the triangles. In our example, the points are annotated in black and the
triangles in blue. Turn the object around and make a second copy from the back if needed.
This way you can keep track.
Clockwise Technique:
Orientation is determined by clockwise indexing. This means that if you're looking at the
42 sur 128 11/03/2014 10:03