OpenSCAD User Manual/Using the 2D Subsystem

From Wikibooks, open books for an open world
< OpenSCAD User Manual
Jump to: navigation, search

Using the 2D Subsystem[edit]

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

All 2D primitives can be transformed with 3D transformations. Usually used as part of a 3D extrusion. Although infinitely 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

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.
Decimal. This is the diameter 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);
circle(d = 20);
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]]);
Polygon example

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]

DEPRECATED: The import_dxf() module will be removed in future releases. Use import() instead.

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");

0% developed  as of November 17, 2009 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.

Openscad projection example 2x.png

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

projection(cut = true) example002();

Openscad projection example 3x.png

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

projection(cut = false) example002();

Openscad example projection 8x.png

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();

Openscad projection example 4x.png

Now we can get a side view with projection()

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

Openscad projection example 5x.png

Links:

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

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, scale = 1.0) {...}

You must use parameter names due to a backward compatibility issue.

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.

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.

twist = 0

0° of Twist

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

twist = -100

-100° of Twist

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

twist = 100

100° of Twist

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

twist = -500

-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

center = true

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


center = false

center = false

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

Mesh Refinement[edit]

slices = 100

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);

$fn = 100

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);

Scale[edit]

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

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, ie they need to be flat in 2D space.

The 2D shape needs to be either completely on the positive or negative side of the X axis. If the shape crosses the X axis a warning will be shown in the console windows and the rotate_extrude() will be ignored.

Examples[edit]

Openscad rotext 01.jpg

A simple torus can be constructed using a rotational extrude.

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

Mesh Refinement[edit]

Openscad rotext 02.jpg

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);

Openscad rotext 03.jpg

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. Note it has been rotated 90 degrees to show how the rotation will look, the rotate_extrude() needs it flat.

Openscad polygon extrusion 1.png

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.

Openscad polygon extrusion 2.png

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.


Openscad convexity.jpg

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.
scale Scales the 2D shape by this value over the height of the extrusion.

0% developed  as of November 17, 2009 DXF Extrusion[edit]

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

Linear Extrude[edit]

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[edit]

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

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

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:

Description of extrude parameters[edit]

Extrude parameters for all extrusion modes[edit]

scale FIXME
convexity See 2D to 3D Extrusion
file The name of the DXF file to extrude [DEPRECATED]
layer The name of the DXF layer to extrude [DEPRECATED]
origin [x,y] coordinates to use as the drawing's center, in the units specified in the DXF file [DEPRECATED]

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