OpenSCAD User Manual/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#2D%20Primitives%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#2D%20Primitives%20
Category:Book:OpenSCAD User Manual