OpenSCAD User Manual/Using the 2D Subsystem

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

[edit] Using the 2D Subsystem

Contents


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

The text in its current form is incomplete.


[edit] square

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.

Usage examples

square ([2,2],center =true);

[edit] circle

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. Default value is 1.

Usage Examples

circle(r = 1);
circle(r = 5);
circle(r = 10);
scale([1/100, 1/100, 1/100]) circle(200); // this will create a high resolution circle with a 2mm radius

[edit] polygon

Usage example:

polygon(points = [ [x, y], ... ], paths = [ [p1, p2, p3..], ... ], convexity = N);

Create a polygon with the specified points and paths. The paths parameter is optional. (The 'pN' components of the paths vector are 0-indexed references to the elements of the points vector.)

Real 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 exactly 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].

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.

Notice: In oder to get a 3D object, you can either extrude a polygon 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.

[edit] import_dxf

Usage example:

import_dxf(file="design.dxf", layer="layername", origin = [100,100], scale = 0.5);

Imports a DXF file as a 2D object.

[edit] 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 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. You can load it and render it to example002.stl (with F6). You can then load that stl file back into OpenSCAD, like so:

import_stl("example002.stl");

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) import_stl("example002.stl");

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) import_stl("example002.stl");

Openscad example projection 8x.png



Another Example

What if we take example002 and move it up, out of the X-Y plane? And rotate it?

translate([0,0,25]) rotate([90,0,0]) import_stl("/tmp/d.stl");

Openscad projection example 4x.png


Now, you can project it and get a 'side view' of the entire object.

projection(cut=false) import_stl(“/full/path/to/stl”);

Openscad projection example 5x.png


Links:

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

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.

[edit] Linear Extrude

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.

[edit] Example

linear_extrude(height = fanwidth, center = true, convexity = 10, twist = -fanrot);

[edit] Twist

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

[edit] Center

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

[edit] Mesh Refinement

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

[edit] Rotate Extrude

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.

[edit] Examples

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

[edit] Mesh Refinement

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

[edit] Extruding a Polygon

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

Here is a simple polygon.

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.

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.

[edit] Description of extrude parameters

[edit] Extrude parameters for all extrusion modes

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.

[edit] Extrude parameters for linear extrusion only

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.

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

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.

[edit] Linear Extrude

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, twist = -fanrot);

[edit] Rotate Extrude

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

[edit] Getting Inkscape to work

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

[edit] Description of extrude parameters

[edit] Extrude parameters for all extrusion modes

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

[edit] Extrude parameters for linear extrusion only

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
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export