OpenSCAD User Manual/Using the 2D Subsystem

0% developed  as of November 17, 2009 2D Primitives

All 2D primitives can be transformed with 3D transformations. They are usually used as part of a 3D extrusion. Although they are infinitely thin, they are rendered with a 1-unit thickness.

Note: Trying to subtract with difference() from 3D object will lead to unexpected results in final rendering.

square


Creates a square or rectangle in the first quadrant. When center is true the square is centered on the origin. Argument names are optional if given in the order shown here.

square(size = [x, y], center = true/false);
square(size =  x    , center = true/false);
parameters:
size
single value, square with both sides this length
2 value array [x,y], rectangle with dimensions x and y
center
false (default), 1st (positive) quadrant, one corner at (0,0)
true, square is centered at (0,0)
default values:  square();   yields:  square(size = [1, 1], center = false);
examples:

10x10 square

equivalent scripts for this example
 square(size = 10);
 square(10);
 square([10,10]);
 .
 square(10,false);
 square([10,10],false);
 square([10,10],center=false);
 square(size = [10, 10], center = false);
 square(center = false,size = [10, 10] );

OpenScad square 20x10

equivalent scripts for this example
 square([20,10],true);
 a=[20,10];square(a,true);

circle


Creates a circle at the origin. All parameters, except r, must be named.

circle(r=radius | d=diameter);
Parameters
r : circle radius. r name is the only one optional with circle.
circle resolution is based on size, using $fa or $fs.
For a small, high resolution circle you can make a large circle, then scale it down, or you could set $fn or other special variables. Note: These examples exceed the resolution of a 3d printer as well as of the display screen.
scale([1/100, 1/100, 1/100]) circle(200); // create a high resolution circle with a radius of 2.
circle(2, $fn=50);                        // Another way.
d  : circle diameter (only available in versions later than 2014.03).
$fa : minimum angle (in degrees) of each fragment.
$fs : minimum circumferential length of each fragment.
$fn : fixed number of fragments in 360 degrees. Values of 3 or more override $fa and $fs.
If they are used, $fa, $fs and $fn must be named parameters. click here for more details,.
defaults:  circle(); yields:  circle($fn = 0, $fa = 12, $fs = 2, r = 1);

circle for user manual description

Equivalent scripts for this example

 circle(10);
 circle(r=10);
 circle(d=20);
 circle(d=2+9*2);

Ellipse

There is no built-in module that for generating an ellipse in OpenSCAD.

Either scale() or resize() may be used to modify a circle into an ellipse. See OpenSCAD User Manual/Transformations

Ellipse from circle Ellipse from circle top view

both of these statements will create these examples:
 resize([30,10]) circle(d=20); // coerce the circle to the desired X-Y sizes
 scale([1.5,.5]) circle(d=20); // stretch or squish the circle by the given factors

Regular Polygons

There is no built-in module for generating regular polygons.

It is possible to use the special variable $fn as a parameter to the circle() module to set the number of sides of the polygon that is generated to approximate its shape.

 circle(r=1, $fn=4); // generate a square

A user defined module can generate the same shapes using some basic math and the polygon() module:

 module regular_polygon(order = 4, r=1)
   { // default parameters give a unit square
        // first divide 360 to make a vector of angles between vertices
   angles=[ for (i = [0:order-1]) i*(360/order) ];
        // use trigonometry to calculate Cartesian coordinates
   coords=[ for (th=angles) [r*cos(th), r*sin(th)] ];
   
   polygon(coords); // generate the polygon
   }
 regular_polygon(); // generate a default regular_polygon

Polygon shapes are generated by both the circle() and polygon() as if they were inscribed in a circle of the radius given and start from the first point on the positive x axis.

script for these examples
 translate([-42,  0]){circle(20,$fn=3);%circle(20,$fn=90);}
 translate([  0,  0]) circle(20,$fn=4);
 translate([ 42,  0]) circle(20,$fn=5);
 translate([-42,-42]) circle(20,$fn=6);
 translate([  0,-42]) circle(20,$fn=8);
 translate([ 42,-42]) circle(20,$fn=12);
 
 color("black"){
     translate([-42,  0,1])text("3",7,,center);
     translate([  0,  0,1])text("4",7,,center);
     translate([ 42,  0,1])text("5",7,,center);
     translate([-42,-42,1])text("6",7,,center);
     translate([  0,-42,1])text("8",7,,center);
     translate([ 42,-42,1])text("12",7,,center);
 }

Polygon

The polygon() module generates a set of lines using a vector of [x,y] coordinates and, optionally, a vector of the paths to use to draw the lines. Each path must be a closed loop, connecting the last point in a path to its first. Paths may cross each other may need the convexity parameter increased to avoid rendering difficulties. Polygons are always created in the X-Y plane and are co-planar by definition.

A polygon is the most general of the 2D objects in that it may be used to generate shapes with both concave and convex sections, and may also have holes with its interior.

polygon(points, paths = undef, convexity = 1);
Parameters
points
required and positional - A vector of [x,y] coords that define the points of the polygon
paths
positional default:undef - a nested vector of vectors of indices to the points vector
If no path is given the points vector is taken as-is.
outer vector - each element is a path defining a closed edge in the polygon
inner vector - each element is an integer index into the vector of points
May use all or part of points in any order.
convexity
Integer number of "inward" curves See below.

Polygon paths that lie within the outer boundary will be rendered as holes. Sections created by lines crossing will be calculated according to the convexity parameter's value. Increasing complexity allows more complex shapes to be rendered correctly but at the cost of more processing.

Without holes

equivalent scripts for this example
 polygon(points=[[0,0],[100,0],[130,50],[30,50]]);
 polygon([[0,0],[100,0],[130,50],[30,50]], paths=[[0,1,2,3]]);
 polygon([[0,0],[100,0],[130,50],[30,50]],[[3,2,1,0]]);
 polygon([[0,0],[100,0],[130,50],[30,50]],[[1,0,3,2]]);
    
 a=[[0,0],[100,0],[130,50],[30,50]];
 b=[[3,0,1,2]];
 polygon(a);
 polygon(a,b);
 polygon(a,[[2,3,0,1,2]]);

One hole

equivalent scripts for this example
 polygon(points=[[0,0],[100,0],[0,100],[10,10],[80,10],[10,80]], paths=[[0,1,2],[3,4,5]],convexity=10);

 triangle_points =[[0,0],[100,0],[0,100],[10,10],[80,10],[10,80]];
 triangle_paths =[[0,1,2],[3,4,5]];
 polygon(triangle_points,triangle_paths,10);

The 1st path vector, [0,1,2], selects the points, [0,0],[100,0],[0,100], for the primary shape. The 2nd path vector, [3,4,5], selects the points, [10,10],[80,10],[10,80], for the secondary shape. The secondary shape is subtracted from the primary ( think difference() ). Since the secondary is wholly within the primary, it leaves a shape with a hole.

Multi hole

[Note: Requires version 2015.03] (for use of concat())


      //example polygon with multiple holes
a0 = [[0,0],[100,0],[130,50],[30,50]];     // main
b0 = [1,0,3,2];
a1 = [[20,20],[40,20],[30,30]];            // hole 1
b1 = [4,5,6];
a2 = [[50,20],[60,20],[40,30]];            // hole 2
b2 = [7,8,9];
a3 = [[65,10],[80,10],[80,40],[65,40]];    // hole 3
b3 = [10,11,12,13];
a4 = [[98,10],[115,40],[85,40],[85,10]];   // hole 4
b4 = [14,15,16,17];
a  = concat (a0,a1,a2,a3,a4);
b  = [b0,b1,b2,b3,b4];
polygon(a,b);
      //alternate 
polygon(a,[b0,b1,b2,b3,b4]);

Extruding a 3D shape from a polygon

   translate([0,-20,10]) {
       rotate([90,180,90]) {
           linear_extrude(50) {
               polygon(
                   points = [
                      //x,y
                       /*
                                  O  .
                       */
                       [-2.8,0],
                       /*
                                O__X  .
                       */
                       [-7.8,0],
                       /*
                              O
                               \
                                X__X  .
                       */
                       [-15.3633,10.30],
                       /*
                              X_______._____O
                               \         
                                X__X  .
                       */
                       [15.3633,10.30],
                       /*
                              X_______._______X
                               \             /
                                X__X  .     O
                       */
                       [7.8,0],
                       /*
                              X_______._______X
                               \             /
                                X__X  .  O__X
                       */
                       [2.8,0],
                       /*
                           X__________.__________X
                            \                   /
                             \              O  /
                              \            /  /
                               \          /  /
                                X__X  .  X__X
                       */
                       [5.48858,5.3],
                       /*
                           X__________.__________X
                            \                   /
                             \   O__________X  /
                              \            /  /
                               \          /  /
                                X__X  .  X__X
                       */
                       [-5.48858,5.3],
                                   ]
                               );
                           }
       }
   }

convexity

The convexity parameter specifies the maximum number of front sides (back sides) a ray intersecting the object might penetrate. This parameter is needed only for correct display of the object in OpenCSG preview mode and has no effect on the polyhedron rendering.

This image shows a 2D shape with a convexity of 2, as the ray indicated in red crosses the 2D shapes outside⇒inside (or inside⇒outside) a maximum of 2 times. The convexity of a 3D shape would be determined in a similar way. Setting it to 10 should work fine for most cases.

import_dxf


[Deprecated: import_dxf() will be removed in future releases. Use import() instead.]

Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20

Read a DXF file and create a 2D shape.

Example

linear_extrude(height = 5, center = true, convexity = 10)
		import_dxf(file = "example009.dxf", layer = "plate");

Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20

0% developed  as of November 17, 2009 Text

The text module creates text as a 2D geometric object, using fonts installed on the local system or provided as separate font file.

[Note: Requires version 2015.03]

Parameters

text
String. The text to generate.
size
Decimal. The generated text has an ascent (height above the baseline) of approximately this value. Default is 10. Fonts vary and may be a different height, typically slightly smaller. The formula to convert the size value to "points" is pt = size/3.937, so a size argument of 3.05 will give about 12pt text, for instance. Note: if you know a point is 1/72" this may not look right, but point measurements of text are the distance from ascent to descent, not from ascent to baseline as in this case.
font
String. The name of the font that should be used. This is not the name of the font file, but the logical font name (internally handled by the fontconfig library). This can also include a style parameter, see below. A list of installed fonts & styles can be obtained using the font list dialog (Help -> Font List).
halign
String. The horizontal alignment for the text. Possible values are "left", "center" and "right". Default is "left".
valign
String. The vertical alignment for the text. Possible values are "top", "center", "baseline" and "bottom". Default is "baseline".
spacing
Decimal. Factor to increase/decrease the character spacing. The default value of 1 results in the normal spacing for the font, giving a value greater than 1 causes the letters to be spaced further apart.
direction
String. Direction of the text flow. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), "ttb" (top-to-bottom) and "btt" (bottom-to-top). Default is "ltr".
language
String. The language of the text (e.g., "en", "ar", "ch"). Default is "en".
script
String. The script of the text (e.g., "latin", "arabic", "hani"). Default is "latin".
$fn
used for subdividing the curved path segments provided by freetype

Example

Example 1: Result.
text("OpenSCAD");



Notes

To allow specification of particular Unicode characters, you can specify them in a string with the following escape codes;

  • \x03     - hex char-value (only hex values from 01 to 7f are supported)
  • \u0123   - Unicode char with 4 hexadecimal digits (note: lowercase \u)
  • \U012345 - Unicode char with 6 hexadecimal digits (note: uppercase \U)

The null character (NUL) is mapped to the space character (SP).

 assert(version() == [2019, 5, 0]);
 assert(ord(" ") == 32);
 assert(ord("\x00") == 32);
 assert(ord("\u0000") == 32);
 assert(ord("\U000000") == 32);

Example

t="\u20AC10 \u263A"; // 10 euro and a smilie

Using Fonts & Styles

Fonts are specified by their logical font name; in addition a style parameter can be added to select a specific font style like "bold" or "italic", such as:

font="Liberation Sans:style=Bold Italic"

The font list dialog (available under Help > Font List) shows the font name and the font style for each available font. For reference, the dialog also displays the location of the font file. You can drag a font in the font list, into the editor window to use in the text() statement.

OpenSCAD font list dialog

OpenSCAD includes the fonts Liberation Mono, Liberation Sans, and Liberation Serif. Hence, as fonts in general differ by platform type, use of these included fonts is likely to be portable across platforms.

For common/casual text usage, the specification of one of these fonts is recommended for this reason. Liberation Sans is the default font to encourage this.


In addition to the installed fonts ( for windows only fonts installed as admin for all users ), it's possible to add project specific font files. Supported font file formats are TrueType Fonts (*.ttf) and OpenType Fonts (*.otf). The files need to be registered with use<>.

 use <ttf/paratype-serif/PTF55F.ttf>

After the registration, the font is listed in the font list dialog, so in case logical name of a font is unknown, it can be looked up as it was registered.

OpenSCAD uses fontconfig to find and manage fonts, so it's possible to list the system configured fonts on command line using the fontconfig tools in a format similar to the GUI dialog.

$ fc-list -f "%-60{{%{family[0]}%{:style[0]=}}}%{file}\n" | sort

...
Liberation Mono:style=Bold Italic /usr/share/fonts/truetype/liberation2/LiberationMono-BoldItalic.ttf
Liberation Mono:style=Bold        /usr/share/fonts/truetype/liberation2/LiberationMono-Bold.ttf
Liberation Mono:style=Italic      /usr/share/fonts/truetype/liberation2/LiberationMono-Italic.ttf
Liberation Mono:style=Regular     /usr/share/fonts/truetype/liberation2/LiberationMono-Regular.ttf
...

Under Windows, fonts are stored in the Windows Registry. To get a file with the font file names, use the command:

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /s > List_Fonts_Windows.txt


Example

Example 2: Result.
 square(10);
 
 translate([15, 15]) {
   text("OpenSCAD", font = "Liberation Sans");
 }
 
 translate([15, 0]) {
   text("OpenSCAD", font = "Liberation Sans:style=Bold Italic");
 }


Alignment

Vertical alignment

top
The text is aligned so the top of the tallest character in your text is at the given Y coordinate.
center
The text is aligned with the center of the bounding box at the given Y coordinate. This bounding box is based on the actual sizes of the letters, so taller letters and descenders will affect the positioning.
baseline
The text is aligned with the font baseline at the given Y coordinate. This is the default, and is the only option that makes different pieces of text align vertically, as if they were written on lined paper, regardless of character heights and descenders.
bottom
The text is aligned so the bottom of the lowest-reaching character in your text is at the given Y coordinate.
OpenSCAD vertical text alignment
 text = "Align";
 font = "Liberation Sans";
 
 valign = [
   [  0, "top"],
   [ 40, "center"],
   [ 75, "baseline"],
   [110, "bottom"]
 ];
 
 for (a = valign) {
   translate([10, 120 - a[0], 0]) {
     color("red") cube([135, 1, 0.1]);
     color("blue") cube([1, 20, 0.1]);
     linear_extrude(height = 0.5) {
       text(text = str(text,"_",a[1]), font = font, size = 20, valign = a[1]);
     }
   }
 }


The text() module doesn't support multi-line text, but you can make a separate call for each line, using translate() to space them. A spacing of 1.4*size is the minimum required to prevent lines overlapping (if they include descenders). 1.6*size is approximately the default single-spacing in many word processing programs. To get evenly spaced lines, use "baseline" vertical alignment; otherwise, lines may be lower or higher depending on their contents.

Horizontal alignment

left
The text is aligned with the left side of the bounding box at the given X coordinate. This is the default.
center
The text is aligned with the center of the bounding box at the given X coordinate.
right
The text is aligned with the right of the bounding box at the given X coordinate.
OpenSCAD horizontal text alignment
 text = "Align";
 font = "Liberation Sans";
 
 halign = [
   [10, "left"],
   [50, "center"],
   [90, "right"]
 ];
 
 for (a = halign) {
   translate([140, a[0], 0]) {
     color("red") cube([115, 2,0.1]);
     color("blue") cube([2, 20,0.1]);
     linear_extrude(height = 0.5) {
       text(text = str(text,"_",a[1]), font = font, size = 20, halign = a[1]);
     }
   }
 }


3D text

3D text example

Text can be changed from a 2 dimensional object into a 3D object by using the linear_extrude function.

//3d Text Example
linear_extrude(4)
    text("Text");

Metrics

[Note: Requires version Development snapshot]

textmetrics()

The textmetrics() function accepts the same parameters as text(), and returns an object describing how the text would be rendered.

The returned object has these members:

  • position: the position of the lower-left corner of the generated text.
  • size: the size of the generated text.
  • ascent: the amount that the text extends above the baseline.
  • descent: the amount that the text extends below the baseline.
  • offset: the lower-left corner of the box containing the text, including inter-glyph spacing before the first glyph.
  • advance: the "other end" of the text, the point at which additional text should be positioned.
   s = "Hello, World!";
   size = 20;
   font = "Liberation Serif";
   
   tm = textmetrics(s, size=size, font=font);
   echo(tm);
   translate([0,0,1]) text("Hello, World!", size=size, font=font);
   color("black") translate(tm.position) square(tm.size);

yields (reformatted for readability):

Using textmetrics() to draw a box around text
   ECHO: {
       position = [0.7936, -4.2752];
       size = [149.306, 23.552];
       ascent = 19.2768;
       descent = -4.2752;
       offset = [0, 0];
       advance = [153.09, 0];
   }

fontmetrics()

The fontmetrics() function accepts a font size and a font name, both optional, and returns an object describing global characteristics of the font.

Parameters

size
Decimal, optional. The size of the font, as described above for text().
font
String, optional. The name of the font, as described above for text().

Note that omitting the size and/or font may be useful to get information about the default font.

Returns an object:

  • nominal: usual dimensions for a glyph:
    • ascent: height above the baseline
    • descent: depth below the baseline
  • max: maximum dimensions for a glyph:
    • ascent: height above the baseline
    • descent: depth below the baseline
  • interline: design distance from one baseline to the next
  • font: identification information about the font:
    • family: the font family name
    • style: the style (Regular, Italic, et cetera)
   echo(fontmetrics(font="Liberation Serif"));

yields (reformatted for readability):

   ECHO: {
       nominal = {
           ascent = 12.3766;
           descent = -3.0043;
       };
       max = {
           ascent = 13.6312;
           descent = -4.2114;
       };
       interline = 15.9709;
       font = {
           family = "Liberation Serif";
           style = "Regular";
       };
   }


Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20

0% developed  as of November 17, 2009 3D to 2D Projection

Using the projection() function, you can create 2d drawings from 3d models, and export them to the dxf format. It works by projecting a 3D model to the (x,y) plane, with z at 0. If cut=true, only points with z=0 are considered (effectively cutting the object), with cut=false(the default), points above and below the plane are considered as well (creating a proper projection).

Example: Consider example002.scad, that comes with OpenSCAD.

Then you can do a 'cut' projection, which gives you the 'slice' of the x-y plane with z=0.

projection(cut = true) example002();

You can also do an 'ordinary' projection, which gives a sort of 'shadow' of the object onto the xy plane.

projection(cut = false) example002();

Another Example

You can also use projection to get a 'side view' of an object. Let's take example002, rotate it, and move it up out of the X-Y plane:

translate([0,0,25]) rotate([90,0,0]) example002();

Now we can get a side view with projection()

projection() translate([0,0,25]) rotate([90,0,0]) example002();

Links:

0% developed  as of November 17, 2009 2D to 3D Extrusion

Extrusion is the process of creating an object with a fixed cross-sectional profile. OpenSCAD provides two commands to create 3D solids from a 2D shape: linear_extrude() and rotate_extrude(). Linear extrusion is similar to pushing Playdoh through a press with a die of a specific shape.

linear_extrude() works like a Playdoh extrusion press

Rotational extrusion is similar to the process of turning or "throwing" a bowl on the Potter's wheel.

rotate_extrude() emulates throwing a vessel

Both extrusion methods work on a (possibly disjointed) 2D shape which exists on the X-Y plane. While transformations that operates on both 2D shapes and 3D solids can move a shape off the X-Y plane, when the extrusion is performed the end result is not very intuitive. What actually happens is that any information in the third coordinate (the Z coordinate) is ignored for any 2D shape, this process amounts to an implicit projection() performed on any 2D shape before the extrusion is executed. It is recommended to perform extrusion on shapes that remains strictly on the X-Y plane.

linear_extrude

A Linear Extrusion must be given a 2D child object. This child object is first projected onto the X-Y plane along the Z axis to create the starting face of the extrusion. The start face is duplicated at the Z position given by the height parameter to create the extrusion's end face. The extrusion is then formed by creating a surface that joins each point along the edges of the two faces.

The 2D shape may be any 2D primitive shape, a 2d polygon, or a combination of them. The 2D shape may have a Z value that moves it out of the X-Y plane, and it may even be rotated out of parallel with it. As stated above, the extrusion's starting face is the projection of the 2D shape onto the X-Y plane, which, if it is rotated, will have the effect of fore-shortening it normal to the axis of the rotation.

Using a 3D object as the extrusion's child will cause a compile time error. Including a 3D object in a composite of 2D objects (using union or difference) will be detected, the 3D object(s) will be deleted from it and the remaining 2D objects will be the basis for projecting their shape onto the X-Y plane.

Parameters For Linear Extrusion

height
a non-negative integer, default 100, giving the length of the extrusion
center
a boolean, default false, that, when true, causes the resulting solid to be vertically centered at the X-Y plane.
convexity
a non-negative integer, default 1, giving a measure of the complexity of the generated surface. See the Section on Convexity later on this page.
twist
a signed decimal, default 0.0, specifying how many degrees of twist to apply between the start and end faces of the extrusion. 180 degrees is a half twist, 360 is all the way around, and so on.
v - twist axis vector
a vector of 3 signed decimal values, default [0,0,1], used as an eigen vector specifying the axis of rotation for the twist. [Note: Requires version Development snapshot]
scale
a non-negative, decimal value, default 1.0, greater than 0.0, that specifies the factor by which the end face should be scaled up, or down, in size from that of the start face.
a vector containing these values in this order
[
slices
a non-negative integer value that acts on the extruded surface as $fn does, but which is not applied to the child 2D shape.
segments
Similar to slices but adding points on the polygon's segments without changing the polygon's shape.
$fn
the special variable but applied only to the extrusion
$fs
the special variable but applied only to the extrusion
$fa
the special variable but applied only to the extrusion
]

Notes

  1. centering the extrusion only affects its vertical position. Its X-Y position is always set by the projection of its starting face.
  2. A Scale Factor greater than one increases the size of the extrusion's end face, while a value greater than 0.0 and less than 1 shrink it. A value of 0.0 causes the end face to degenerate to a point, turning the extrusion into a pyramid, cone, or complex pointy shape according to what the starting shape is.
  3. the twist is applied, by default, as a rotation about the Z Axis. When the start face is at the origin a twist creates a spiral out of any corners of the child shape. If the start face is translated away from the origin the twist creates a spring shape.
  4. positive twist rotates clockwise, negative twist the opposite.
  5. when the twist axis vector "v" is tilted away from the Z axis the twist is applied between the start and end faces without making them normal to the axis. They remain as flat shapes parallel to the X-Y plane and the twisted surfaces are thus distorted from what they would be normally. To have it otherwise it would be necessary to form the twisted extrusion on the Z Axis and then rotate it into position.
  6. all the parameters need to be given as named as the addition of the "v" vector parameter causes a error when only positional parameters are given

twist = 0

A Unit Circle with No Twist

Generate an extrusion from a circle 2 units along the X Axis from the origin, centered vertically on the X-Y plane, with no twist. The extrusion appears to have a pentagonal cross-section because the extrusion's child is a 2D circle with the default value for $fn.

linear_extrude(height = 10, center = true, convexity = 10, twist = 0)
    translate([2, 0, 0])
        circle(r = 1);

twist = -100

A Unit Circle Twisted Left 100 Degrees

The same circle, but now with 100 degrees of counter-clockwise twist.

linear_extrude(height = 10, center = true, convexity = 10, twist = -100)
    translate([2, 0, 0])
        circle(r = 1);

twist = 100

A Unit Circle Twisted Right 100 Degrees

The same circle, but now with 100 degrees of clockwise twist.

linear_extrude(height = 10, center = true, convexity = 10, twist = 100)
    translate([2, 0, 0])
        circle(r = 1);

twist = -500

A Unit Circle Twisted Into a Spiral

The same circle, but made into a spiral by 500 degrees of counter-clockwise twist.

linear_extrude(height = 10, center = true, convexity = 10, twist = -500)
    translate([2, 0, 0])
        circle(r = 1);

Center

With center=false, the default, the extrusion is based on the X-Y plane and rises up in the positive Z direction. center = false

When true it is centered vertically at the X-Y plane, as seen here:

center = true

Mesh Refinement

The slices parameter defines the number of intermediate points along the Z axis of the extrusion. Its default increases with the value of twist. Explicitly setting slices may improve the output refinement. Additional the segments parameter adds vertices (points) to the extruded polygon resulting in smoother twisted geometries. Segments need to be a multiple of the polygon's fragments to have an effect (6 or 9.. for a circle($fn=3), 8,12.. for a square() ).

slices = 100

linear_extrude(height = 10, center = false, convexity = 10, twist = 360, slices = 100)
translate([2, 0, 0])
circle(r = 1);

The special variables $fn, $fs and $fa can also be used to improve the output. If slices is not defined, its value is taken from the defined $fn value.

$fn = 100

linear_extrude(height = 10, center = false, convexity = 10, twist = 360, $fn = 100)
translate([2, 0, 0])
circle(r = 1);

Scale

Scales the 2D shape by this value over the height of the extrusion. Scale can be a scalar or a vector:

 linear_extrude(height = 10, center = true, convexity = 10, scale=3)
 translate([2, 0, 0])
 circle(r = 1);

OpenScad linear_extrude scale example

 linear_extrude(height = 10, center = true, convexity = 10, scale=[1,5], $fn=100)
 translate([2, 0, 0])
 circle(r = 1);

OpenScad linear_extrude scale example2

Note that if scale is a vector, the resulting side walls may be nonplanar. Use twist=0 and the slices parameter to avoid asymmetry.

 linear_extrude(height=10, scale=[1,0.1], slices=20, twist=0)
 polygon(points=[[0,0],[20,10],[20,-10]]);

Using with imported SVG

A common usage of this function is to import a 2D svg

 linear_extrude(height = 10, center = true)
 import("knight.svg");

rotate_extrude

Rotational extrusion spins a 2D shape around the Z-axis to form a solid which has rotational symmetry. One way to think of this operation is to imagine a Potter's wheel placed on the X-Y plane with its axis of rotation pointing up towards +Z. Then place the to-be-made object on this virtual Potter's wheel (possibly extending down below the X-Y plane towards -Z). The to-be-made object is the cross-section of the object on the X-Y plane (keeping only the right half, X >= 0). That is the 2D shape that will be fed to rotate_extrude() as the child in order to generate this solid. Note that the object started on the X-Y plane but is tilted up (rotated +90 degrees about the X-axis) to extrude.

Since a 2D shape is rendered by OpenSCAD on the X-Y plane, an alternative way to think of this operation is as follows: spins a 2D shape around the Y-axis to form a solid. The resultant solid is placed so that its axis of rotation lies along the Z-axis.

Just like the linear_extrude, the extrusion is always performed on the projection of the 2D polygon to the XY plane. Transformations like rotate, translate, etc. applied to the 2D polygon before extrusion modify the projection of the 2D polygon to the XY plane and therefore also modify the appearance of the final 3D object.

  • A translation in Z of the 2D polygon has no effect on the result (as also the projection is not affected).
  • A translation in X increases the diameter of the final object.
  • A translation in Y results in a shift of the final object in Z direction.
  • A rotation about the X or Y axis distorts the cross section of the final object, as also the projection to the XY plane is distorted.

Don't get confused, as OpenSCAD displays 2D polygons with a certain height in the Z direction, so the 2D object (with its height) appears to have a bigger projection to the XY plane. But for the projection to the XY plane and also for the later extrusion only the base polygon without height is used.

You cannot use rotate_extrude to produce a helix or screw thread. Doing this properly can be difficult, so it's best to find a thread library to make them for you.

The 2D shape must lie completely on either the right (recommended) or the left side of the Y-axis. More precisely speaking, every vertex of the shape must have either x >= 0 or x <= 0. If the shape spans the X axis a warning appears in the console windows and the rotate_extrude() is ignored. If the 2D shape touches the Y axis, i.e. at x=0, it must be a line that touches, not a point, as a point results in a zero thickness 3D object, which is invalid and results in a CGAL error. For OpenSCAD versions prior to 2016.xxxx, if the shape is in the negative axis the resulting faces are oriented inside-out, which may cause undesired effects.

Usage

rotate_extrude(angle = 360, start=0, convexity = 2) {...}
Right-hand grip rule

In 2021.01 and previous, you must use parameter names due to a backward compatibility issue.

convexity : If the extrusion fails for a non-trival 2D shape, try setting the convexity parameter (the default is not 10, but 10 is a "good" value to try). See explanation further down.
angle [Note: Requires version 2019.05] : Defaults to 360. Specifies the number of degrees to sweep, starting at the positive X axis. The direction of the sweep follows the Right Hand Rule, hence a negative angle sweeps clockwise.
start [Note: Requires version Development snapshot] : Defaults to 0 if angle is specified, and 180 if not. Specifies the starting angle of the extrusion, counter-clockwise from the positive X axis.
$fa : minimum angle (in degrees) of each fragment.
$fs : minimum circumferential length of each fragment.
$fn : fixed number of fragments in 360 degrees. Values of 3 or more override $fa and $fs
$fa, $fs and $fn must be named parameters. click here for more details,.

Examples

A simple torus can be constructed using a rotational extrude.

rotate_extrude(convexity = 10)
    translate([2, 0, 0])
        circle(r = 1);

Mesh Refinement

Increasing the number of fragments composing the 2D shape improves the quality of the mesh, but takes longer to render.

rotate_extrude(convexity = 10)
    translate([2, 0, 0])
        circle(r = 1, $fn = 100);

The number of fragments used by the extrusion can also be increased.

rotate_extrude(convexity = 10, $fn = 100)
    translate([2, 0, 0])
        circle(r = 1, $fn = 100);

Using the parameter angle (with OpenSCAD versions 2016.xx), a hook can be modeled .

OpenSCAD - a hook
eps = 0.01;
translate([eps, 60, 0])
    rotate_extrude(angle=270, convexity=10)
        translate([40, 0]) circle(10);
rotate_extrude(angle=90, convexity=10)
    translate([20, 0]) circle(10);
translate([20, eps, 0])
    rotate([90, 0, 0]) cylinder(r=10, h=80+eps);

Extruding a Polygon

Extrusion can also be performed on polygons with points chosen by the user.

Here is a simple polygon and its 200 step rotational extrusion. (Note it has been rotated 90 degrees to show how the rotation appears; the rotate_extrude() needs it flat).

rotate([90,0,0])        polygon( points=[[0,0],[2,1],[1,2],[1,3],[3,4],[0,5]] );
rotate_extrude($fn=200) polygon( points=[[0,0],[2,1],[1,2],[1,3],[3,4],[0,5]] );

For more information on polygons, please see: 2D Primitives: Polygon.

Orientation

If you're making a round 360 degree extrusion, it doesn't matter where it starts. If, on the other hand, you're using $fn to make an extrusion with some specific number of sides, it can matter. With an odd number of sides, there will be a vertex on either the left or the right, and a side opposite it.

With angle not specified, the extrusion starts along the negative X axis, to the left of the origin. With an odd number of sides, there is a vertex on the left and a side on the right. (Note that this is inconsistent with the behavior for angle less than 360, and with the behavior for circle and other round primitives.)

With angle specified, and not equal to 360, the extrusion starts along the positive X axis, to the right of the origin.

For 2021.01 and earlier, if angle is equal to 360, the extrusion starts along the negative X axis, as for angle not being specified.

For the development snapshot, if angle is 360, the extrusion starts along the positive X axis, as for other cases where angle is specified. Explicitly specifying angle=360 thus yields results consistent with other round primitives.

A future release may change this behavior so that when angle is not specified the extrusion starts along the positive X axis, making all of these cases consistent.

start directly controls the start point. [Note: Requires version Development snapshot]

Description of extrude parameters

Parameters Used In All Extrusions

convexity
an integer that works as a measure of complexity based on the maximum number of sides a ray might penetrate as it passes through a shape.

OpenSCAD uses OpenCSG to render objects in preview mode using the Goldfeather algorithm which takes account of convexity to calculate which parts of a shape to render. The algorithm that generates the mesh for the full rendering operation uses a different method so this parameter that ignores convexity.

The preview render works by presuming an eye point from which rays are emitted towards the objects in view, which are then reflected back to the eye point with information about what to display in each pixel. When a shape may have parts hidden from view by a fold or opening the preview algorithm must process along each ray until it can be sure that it it has processed all the surfaces that the eye might see from its view point.

The details of how convexity is used are fully explained on the OpenCSG site in the Usage section but the gist is that the algorithm needs to be informed that it must work harder on more complex shapes. For instance, the convexity of a sphere is one and the convexity of a torus is two. It is certainly possible that from some view points every ray from the eye faces only a single surface, which is certainly true for the sphere. But from a viewpoint well off the torus central axis rays can pass through its front and back parts so a given pixel may need to show the front most face, or the back part of the doughnut, or the background. A value of convexity greater than the default 1 informs the renderer of the need to be more careful in its work.

This 2D shape has a convexity of 2, as there is at least one ray (the red line) that passes through its boundary twice on its way to or from the eye point. If convexity was at the default 1 the rendering process would stop at the first crossing and the possibility of that pixel needing to show the crossing at the other surface will not be evaluated, leading to faults in the preview image.

3D shapes are evaluated in the same way, higher convexity being needed when a ray might pass through many internal surfaces on its way to the view point. Arbitrarily setting convexity to 10 should solve most preview rendering issues, at the expense of slowing preview rendering. When a model has several convoluted shapes values more than 15 can slow rendering to the point of freezing the application.

Parameters For Linear Extrusion

height
a non-negative integer, default 100, giving the length of the extrusion
center
a boolean, default false, that, when true, causes the resulting solid to be vertically centered at the X-Y plane.
twist
a signed decimal, default 0.0, specifying how many degrees of twist to apply between the start and end faces of the extrusion
scale
a non-negative, decimal value, default 1.0, greater than 0.0, that specifies the factor by which the end face should be scaled up, or down, in size from that of the start face.
a vector containing these values in this order
[
slices
a non-negative integer value that acts on the extruded surface as $fn does, but which is not applied to the child 2D shape.
segments
Similar to slices but adding points on the polygon's segments without changing the polygon's shape.
$fn
the special variable but applied only to the extrusion
$fs
the special variable but applied only to the extrusion
$fa
the special variable but applied only to the extrusion
]

Note

  1. centering the extrusion only affects its vertical position. Its X-Y position is always set by its starting face.
  2. A Scale Factor greater than one increases the size of the extrusion's end face, while a value greater than 0.0 and less than 1 shrink it. A value of 0.0 causes the end face to degenerate to a point, turning the extrusion into a pyramid, cone, or complex pointy shape according to what the starting shape is. Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20

0% developed  as of November 17, 2009 DXF Extrusion

With the import() and extrusion modules it is possible to convert 2D objects read from DXF files to 3D objects. See also 2D to 3D Extrusion.

Linear Extrude

Example of linear extrusion of a 2D object imported from a DXF file.

linear_extrude(height = fanwidth, center = true, convexity = 10)
   import (file = "example009.dxf", layer = "fan_top");

Rotate Extrude

Example of rotational extrusion of a 2D object imported from a DXF file.

rotate_extrude(convexity = 10)
   import (file = "example009.dxf", layer = "fan_side", origin = fan_side_center);

Getting Inkscape to work

Inkscape is an open source drawing program. Tutorials for transferring 2d DXF drawings from Inkscape to OpenSCAD are available here:

Previous

CSG Export

Next

Other 2D formats Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20

Category:Book:OpenSCAD User Manual#Using%20the%202D%20Subsystem%20
Category:Book:OpenSCAD User Manual