OpenSCAD User Manual/Using the 2D Subsystem
Using the 2D Subsystem [edit]
Contents |
2D Primitives [edit]
All 2D primitives can be transformed with 3D transformations. Usually used as part of a 3D extrusion. Altough infinitly thin, they are rendered with a 1 thickness.
square [edit]
Creates a square at the origin of the coordinate system. When center is true the square will be centered on the origin, otherwise it is created in the first quadrant. The argument names are optional if the arguments are given in the same order as specified in the parameters
Parameters
- size
- Decimal or 2 value array. If a single number is given, the result will be a square with sides of that length. If a 2 value array is given, then the values will correspond to the lengths of the X and Y sides. Default value is 1.
- center
- Boolean. This determines the positioning of the object. If true, object is centered at (0,0). Otherwise, the square is placed in the positive quadrant with one corner at (0,0). Defaults to false.
Example
square ([2,2],center = true);
circle [edit]
Creates a circle at the origin of the coordinate system. The argument name is optional.
Parameters
- r
- Decimal. This is the radius of the circle. The resolution of the circle will be based on the size of the circle. If you need a small, high resolution circle you can get around this by making a large circle, then scaling it down by an appropriate factor, or you could set $fn or other special variables. Default value is 1.
Examples
circle(); // uses default radius, r=1
circle(r = 10);
scale([1/100, 1/100, 1/100]) circle(200); // this will create a high resolution circle with a 2mm radius circle(2, $fn=50); // Another way to create a high-resolution circle with a radius of 2.
polygon [edit]
Create a polygon with the specified points and paths.
Parameters
- points
- vector of 2 element vectors, ie. the list of points of the polygon
- paths
- Either a single vector, enumerating the point list, ie. the order to traverse the points, or, a vector of vectors, ie a list of point lists for each seperate curve of the polygon. The latter is required if the polygon has holes. The parameter is optional and if omitted the points are assumed in order. (The 'pN' components of the paths vector are 0-indexed references to the elements of the points vector.)
- convexity
- Integer. Number of "inward" curves, ie. expected path crossings of an arbitraty line through the polygon.
Usage
polygon(points = [ [x, y], ... ], paths = [ [p1, p2, p3..], ...], convexity = N);
Example
polygon(points=[[0,0],[100,0],[0,100],[10,10],[80,10],[10,80]], paths=[[0,1,2],[3,4,5]]);
In this example, we have 6 points (three for the "outer" triangle, and three for the "inner" one). We connect each one with two 2 path. In plain English, each element of a path must correspond to the position of a point defined in the points vector, e.g. "1" refers to [100,0].
Notice: In order to get a 3D object, you either extrude a 2D polygon (linear or (rotation ) or directly use the polyhedron primitive solid. When using extrusion to form solids, its important to realize that the winding direction of the polygon is significant. If a polygon is wound in the wrong direction with respect to the axis of rotation, the final solid (after extrusion) may end up invisible. This problem can be checked for by flipping the polygon using scale([-1,1]) (assuming that extrusion is being done about the Z axis as it is by default).
Notice: Althought the 2D drawing commands operate in axes labeled as X and Y, the extrusion commands implicitly translate these objects in X-Z coordinates and rotate about the Z axis.
Example:
polygon([[0,0],[10,90],[11,-10]], convexity = N);
import_dxf [edit]
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");
3D to 2D Projection [edit]
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 will be considered (effectively cutting the object), with cut=false
, points above and below the plane will be 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, and move it up, out of the X-Y plane, and rotate it:
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:
- example021.scad from Clifford Wolf's site.
- More complicated example from Giles Bathgate's blog
2D to 3D Extrusion [edit]
![]() |
The text in its current form is incomplete. |
It is possible to use extrusion commands to convert 2D objects to 3D objects. This can be done with the built-in 2D primitives, like squares and circles, but also with arbitrary polygons.
Linear Extrude [edit]
Linear Extrusion is a modeling operation that takes a 2D polygon as input and extends it in the third dimension. This way a 3D shape is created.
Usage [edit]
linear_extrude(height = fanwidth, center = true, convexity = 10, twist = -fanrot, slices = 20) {...}
(You must use parameter names due to a backward compatability issue) (NB: The units of the Z-extrusion height are in millimeters)
If the extrusion fails for a non-trival 2D shape, try setting the convexity parameter (default is not 10, but 10 is a "good" value to try) See explanation further down.
Twist [edit]
Twist is the number of degrees of through which the shape is extruded. Setting the parameter twist = 360 will extrude through one revolution. The twist direction follows the left hand rule.
0° of Twist
linear_extrude(height = 10, center = true, convexity = 10, twist = 0) translate([2, 0, 0]) circle(r = 1);
-100° of Twist
linear_extrude(height = 10, center = true, convexity = 10, twist = -100) translate([2, 0, 0]) circle(r = 1);
100° of Twist
linear_extrude(height = 10, center = true, convexity = 10, twist = 100) translate([2, 0, 0]) circle(r = 1);
-500° of Twist
linear_extrude(height = 10, center = true, convexity = 10, twist = -500) translate([2, 0, 0]) circle(r = 1);
Center [edit]
Center determines if the object is centered after extrusion, so it does not extrude up and down from the center as you might expect.
center = true
linear_extrude(height = 10, center = true, convexity = 10, twist = -500) translate([2, 0, 0]) circle(r = 1);
center = false
linear_extrude(height = 10, center = false, convexity = 10, twist = -500) translate([2, 0, 0]) circle(r = 1);
Mesh Refinement [edit]
The slices parameter can be used to improve the output.
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.
linear_extrude(height = 10, center = false, convexity = 10, twist = 360, $fn = 100) translate([2, 0, 0]) circle(r = 1);
Rotate Extrude [edit]
A rotational extrusion is a Linear Extrusion with a twist, literally. Unfortunately, it can not be used to produce a helix for screw threads as the 2D outline must be normal to the axis of rotation.
Examples [edit]
A simple torus can be constructed using a rotational extrude.
rotate_extrude(convexity = 10) translate([2, 0, 0]) circle(r = 1);
Mesh Refinement [edit]
Increasing the number of fragments that the 2D shape is composed of will improve the quality of the mesh, but take 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);
Extruding a Polygon [edit]
Extrusion can also be performed on polygons with points chosen by the user.
Here is a simple polygon.
rotate([90,0,0]) polygon( points=[[0,0],[2,1],[1,2],[1,3],[3,4],[0,5]] );
Here is the same polygon, rotationally extruded, and with the mesh refinement set to 200. The polygon must touch the rotational axis for the extrusion to work, i.e. you can't build a polygon rotation with a hole.
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.
Description of extrude parameters [edit]
Extrude parameters for all extrusion modes [edit]
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. |
This image shows a 2D shape with a convexity of 4, as the ray indicated in red crosses the 2D shape a maximum of 4 times. The convexity of a 3D shape would be determined in a similar way. Setting it to 10 should work fine for most cases.
Extrude parameters for linear extrusion only [edit]
height | The extrusion height |
center | If true the solid will be centered after extrusion |
twist | The extrusion twist in degrees |
slices | Similar to special variable $fn without being passed down to the child 2D shape. |
DXF Extrusion [edit]
![]() |
The text in its current form is incomplete. |
With the DXF Extrusion statements it is possible to convert 2D DXF files directly to 3D Objects.
Linear Extrude [edit]
With the DXF Extrusion statements it is possible to convert 2D objects to 3D objects.
linear_extrude(file = "example009.dxf", layer = "fan_top", height = fanwidth, center = true, convexity = 10);
Rotate Extrude [edit]
A rotational extrusion is a Linear Extrusion with a twist, literally.
rotate_extrude(file = "example009.dxf", layer = "fan_side", origin = fan_side_center, convexity = 10, twist = -fanrot);
Getting Inkscape to work [edit]
Inkscape is an open source drawing program. Tutorials for transferring 2d DXF drawings from Inkscape to OpenSCAD are available here:
- http://repraprip.blogspot.com/2011/05/inkscape-to-openscad-dxf-tutorial.html (Very simple)
- http://tonybuser.com/?tag=inkscape (More complicated, involves conversion to Postscript)
- http://www.damonkohler.com/2010/11/inkscape-dxf-openscad-makerbot.html (Better Better DXF Plugin for Inkscape)
Description of extrude parameters [edit]
Extrude parameters for all extrusion modes [edit]
file | The name of the DXF file to extrude |
layer | The name of the DXF layer to extrude |
convexity | See 2D to 3D Extrusion |
origin | [x,y] coordinates to use as the drawing's center, in the units specified in the DXF file |
scale | FIXME |
Extrude parameters for linear extrusion only [edit]
height | The extrusion height |
center | If true, extrusion is half up and half down. If false, the section is extruded up. |
twist | The extrusion twist in degrees |
slices | FIXME |