Openscad Manual 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

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


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

triangle (in this case [4,0,5]) from the outside you'll see that the path is clockwise around
the center of the face. The winding order [4,0,5] is clockwise and therefore good. The
winding order [0,4,5] is counter-clockwise and therefore bad. Likewise, any other clockwise
order of [4,0,5] works: [5,4,0] & [0,5,4] are good too. If you use the clockwise technique,
you'll always have your faces outside (outside of OpenSCAD, other programs do use
counter-clockwise as the outside though).

Think of it as a Left Hand Rule:

If you hold the triangle and the fingers of your hand curls is the same order as the points,
then your thumb points outwards.

Polyhedron with badly oriented polygons

Succinct description of a 'Polyhedron'

* Points define all of the points/vertices in the shape.


* Triangles is a list of triangles that connect up the points/vertices.

Each point, in the point list, is defined with a 3-tuple x,y,z position specification. Points in
the point list are automatically given an identifier starting at zero for use in the triangle list
(0,1,2,3,... etc).

Each triangle, in the triangle list, is defined by selecting 3 of the points (using the point
identifier) out of the point list.

e.g. triangles=[ [0,1,2] ] defines a triangle from the first point (points are zero referenced) to
the second point and then to the third point.

43 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

When looking at any triangle from the outside, the triangle must list their 3 points in a
clockwise order.

Transformation affect the child nodes and as the name implies transforms them in various
ways such as moving/rotating or scaling the child. Cascading transformations are used to
apply a variety of transforms to a final child. Cascading is achieved by nesting statements
i.e.

transform() e.g. rotate([45,45,45])


transform() translate([10,20,30])
child() cube(10);

Note: child(...) is deprecated by children(...) in master. (2013.06 still uses child(...)).

Transformations can be applied to a group of child nodes by using '{' & '}' to enclose the
subtree e.g.

translate(0,0,-5) or the more compact translate(0,0,-5) {


{ cube(10);
cube(10); cylinder(r=5,h=10);
cylinder(r=5,h=10); }
}

Advanced concept

As OpenSCAD uses different libraries to implement capabilities this can introduce some
inconsistencies to the F5 preview behaviour of transformations. Traditional transforms
(translate, rotate, scale, mirror & multimatrix) are performed using OpenGL in preview,
while other more advanced transforms, such as resize, perform a CGAL operation,
behaving like a CSG operation affecting the underlying object, not just transforming it. In
particular this can affect the display of modifier characters, specifically "#" and "%", where
the highlight may not display intuitively, such as highlighting the pre-resized object, but
highlighting the post-scaled object.

scale

Scales its child elements using the specified vector. The argument name is optional.

Usage Example:
scale(v = [x, y, z]) { ... }

cube(10);
translate([15,0,0]) scale([0.5,1,2]) cube(10);

44 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

resize

resize() is available since OpenSCAD 2013.06. It modifies the size of the child object to
match the given x,y, and z.

There is a bug with shrinking in the 2013.06 release, that will be fixed in the next release.

Usage Example:

// resize the sphere to extend 30 in x, 60 in y, and 10 in the z directions.


resize(newsize=[30,60,10]) sphere(r=10);

45 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

If x,y, or z is 0 then that dimension is left as-is.

// resize the 1x1x1 cube to 2x2x1


resize([2,2,0]) cube();

If the 'auto' parameter is set to true, it will auto-scale any 0-dimensions to match. For
example.

// resize the 1x2x0.5 cube to 7x14x3.5


resize([7,0,0], auto=true) cube([1,2,0.5]);

The 'auto' parameter can also be used if you only wish to auto-scale a single dimension, and
leave the other as-is.

// resize to 10x8x1. Note that the z dimension is left alone.


resize([10,0,0], auto=[true,true,false]) cube([5,4,1]);

rotate

Rotates its child a degrees about the origin of the coordinate system or around an arbitrary
axis. The argument names are optional if the arguments are given in the same order as
specified above.

When a rotation is specified for multiple axes then the rotation is applied in the following
order: x, y, z.

Usage:

46 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

rotate(a = deg, v = [x, y, z]) { ... }

For example, to flip an object upside-down, you might do this:

rotate(a=[0,180,0]) { ... }

The above example will rotate your object 180 degrees around the 'y' axis.

The optional argument 'v' allows you to set an arbitrary axis about which the object will be
rotated.

Example with arbitrary origin.

rotate(a=45, v=[1,1,0]) { ... }

This example will rotate your object 45 degrees around the axis defined by the vector
[1,1,0] i.e. 45 around X and 45 around Y.

If this is all a bit confusing, this might, or might not, help.


For the case of:

rotate([a, b, c]) { ... };

"a" is a rotation about the X axis, from the +Z axis, toward the -Y axis. NOTE: NEGATIVE Y.
"b" is a rotation about the Y axis, from the +Z axis, toward the +X axis.
"c" is a rotation about the Z axis, from the +X axis, toward the +Y axis.

Thus if "a" is fixed to zero, and "b" and "c" are manipulated appropriately, this is the
spherical coordinate system.

47 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

So, to construct a cylinder from the origin to some other point (x,y,z):

length=sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));


b=acos(z/length);
c= x==0 ? 90 :(x>0 ? atan(y/x): atan(y/x)+180);
rotate([0, b, c]) cylinder(h=length, r=0.5);

rotate with a single scalar argument rotates around the Z axis. This is useful in 2D contexts
where that is the only axis for rotation. For example:

rotate(45) square(10);

translate

Translates (moves) its child elements along the specified vector. The argument name is
optional.

IExample

translate(v = [x, y, z]) { ... }

cube(2,center = true);
translate([5,0,0])
sphere(1,center = true);

mirror

Mirrors the child element on a plane through the origin. The argument to mirror() is the

48 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

normal vector of a plane intersecting the origin through which to mirror the object.

Usage example:
mirror([ 0, 1, 0 ]) { ... }

mirror([1,0,0]); mirror([1,1,0]); mirror([1,1,1]);

rotate([0,0,10]) cube([3,2,1]);
mirror([1,0,0]) translate([1,0,0]) rotate([0,0,10]) cube([3,2,1]);

multmatrix

Multiplies the geometry of all child elements with the given 4x4 transformation matrix.

Usage: multmatrix(m = [...]) { ... }

Example (translates by [10, 20, 30]):

multmatrix(m = [ [1, 0, 0, 10],


[0, 1, 0, 20],

49 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

[0, 0, 1, 30],
[0, 0, 0, 1]
]) cylinder();

Example (rotates by 45 degrees in XY plane and translates by [10,20,30]):

angle=45;
multmatrix(m = [ [cos(angle), -sin(angle), 0, 10],
[sin(angle), cos(angle), 0, 20],
[0, 0, 1, 30],
[0, 0, 0, 1]
]) union() {
cylinder(r=10.0,h=10,center=false);
cube(size=[10,10,10],center=false);
}

color

Displays the child elements using the specified RGB color + alpha value. This is only used
for the F5 preview as CGAL and STL (F6) do not currently support color. The alpha value
will default to 1.0 (opaque) if not specified.

Usage example:
color([r, g, b, a]) { ... }

Note that the r, g, b, a values are limited to floating point values in the range { 0.0 ... 1.0 }
rather than the more traditional integers { 0 ... 255 }. However you can specify the values as
fractions, e.g. for R,G,B integers in {0 ... 255} you can use:

color([ R/255, G/255, B/255 ]) { ... }

As of the 2011.12 version, colors can also be chosen by name; name is not case sensitive. For
example, to create a red sphere, you can use this code:

color("red") sphere(5);

Alpha is also available with named colors:

color("Blue",0.5) cube(5);

The available color names are taken from the World Wide Web consortium's SVG color list
(http://www.w3.org/TR/css3-color/). A chart of the color names is as follows, (note that both
spelling of grey/gray including slategrey/slategray etc are valid) :

Purples Blues Greens Yellows Whites


Lavender Aqua GreenYellow Gold White

50 sur 128 11/03/2014 10:03


OpenSCAD User Manual/Print version - Wikibooks, o... http://en.wikibooks.org/w/index.php?title=OpenSCA...

Thistle Cyan Chartreuse Yellow Snow


Plum LightCyan LawnGreen LightYellow Honeydew
Violet PaleTurquoise Lime LemonChiffon MintCream
Orchid Aquamarine LimeGreen LightGoldenrodYellow Azure
Fuchsia Turquoise PaleGreen PapayaWhip AliceBlue
Magenta MediumTurquoise LightGreen Moccasin GhostWhite
MediumOrchid DarkTurquoise MediumSpringGreen PeachPuff WhiteSmok
MediumPurple CadetBlue SpringGreen PaleGoldenrod Seashell
BlueViolet SteelBlue MediumSeaGreen Khaki Beige
DarkViolet LightSteelBlue SeaGreen DarkKhaki OldLace
DarkOrchid PowderBlue ForestGreen Browns FloralWhite
DarkMagenta LightBlue Green Cornsilk Ivory
Purple SkyBlue DarkGreen BlanchedAlmond AntiqueWh
Indigo LightSkyBlue YellowGreen Bisque Linen
DarkSlateBlue DeepSkyBlue OliveDrab NavajoWhite LavenderBlush
SlateBlue DodgerBlue Olive Wheat MistyRose
MediumSlateBlue CornflowerBlue DarkOliveGreen BurlyWood Grays
Pinks RoyalBlue MediumAquamarine Tan Gainsboro
Pink Blue DarkSeaGreen RosyBrown LightGrey
LightPink MediumBlue LightSeaGreen SandyBrown Silver
HotPink DarkBlue DarkCyan Goldenrod DarkGray
DeepPink Navy Teal DarkGoldenrod Gray
MediumVioletRed MidnightBlue Oranges Peru DimGray
PaleVioletRed Reds LightSalmon Chocolate LightSlateGr
IndianRed Coral SaddleBrown SlateGray
LightCoral Tomato Sienna DarkSlateGr
Salmon OrangeRed Brown Black
DarkSalmon DarkOrange Maroon
LightSalmon Orange
Red
Crimson
FireBrick
DarkRed

Here's a code fragment that draws a wavy multicolor object

for(i=[0:36])
{ for(j=[0:36])
{ color([0.5+sin(10*i)/2,0.5+sin(10*j)/2,0.5+sin(10*(i+j))/2])
translate([i,j,0])
cube(size=[1,1,11+10*cos(10*i)*sin(10*j)]);

51 sur 128 11/03/2014 10:03

You might also like