A
OpenSCAD Language Reference

This language reference provides short descriptions of most OpenSCAD features, serving as a quick reminder of how to use functionality described in this book or a way of discovering new OpenSCAD features. Consult the official OpenSCAD reference at https://openscad.org/documentation for the full manual.

Syntax

  1. Create a 2D or 3D shape with a collection of parameters. Terminate the command with a semicolon (;):
    shape(...);
  2. Create a shape that has been transformed by a series of operations. Terminate the statement with a semicolon (;):
    transformation2(...) transformation1(...) shape(...);
  3. Create a variable to name and refer to an important value; values are assigned once and cannot change:
    var_name = value;
  4. Create a user-defined shape called name with zero or more parameters. User-defined shapes work the same way as built-in shapes:
    module name(...) { ... } 
    name(...); 
  5. Create a user-defined mathematical operation called name with zero or more parameters:
    function name(...) = ...;
    name(...);
    or
    name = function(...) ...;
    name(...);
  6. Import and immediately execute the OpenSCAD code in filename.scad:
    include <filename.scad>
  7. Import and make usable (but don’t immediately execute) the OpenSCAD functions and modules in filename.scad:
    use <filename.scad>

Operators

Operators are listed in decreasing order of precedence. When multiple operators from the same level of precedence occur in an expression, the operators are evaluated in order of occurrence (from left to right):

  1. ^
  2. *, /, %
  3. +, -
  4. <, >, <=, >=
  5. ==, !=
  6. &&
  7. ||

2D Shapes

  1. Draw a circle of the defined radius or diameter:
    circle(radius | d=diameter)
  2. Draw a square with length = size and width = size (equal sides); optionally center the square at (0,0):
    square(size, center)
  3. Draw a rectangle with width along the x-axis and length/depth along the y-axis defined by a vector; optionally center the square at (0,0):
    square([width, height], center)
  4. Draw a polygon that connects all of the points defined by the vector of [x, y] points:
    polygon([[x1, y2], [x2, y2], ..., [xn, yn]])
  5. Draw a polygon that connects all of the points defined by the vector of [x, y] points; optionally define a collection of paths for polygons with holes:
    polygon([points], [paths])
  6. Draw words defined by the text string; optionally specify the size, font, horizontal alignment, vertical alignment, letter spacing, direction, language, and script of the text:
    text(text, size, font, halign, valign, 
    spacing, direction, language, script)
  7. Import a 2D SVG or DXF file:
    import("filename.svg")

3D Shapes

  1. Draw a sphere centered at (0, 0, 0) with the specified radius or diameter:
    sphere(radius | d=diameter)
  2. Draw a cube with length = size, width = size, and height = size (equal sides); optionally center the cube at (0,0,0):
    cube(size, center)
  3. Draw a cuboid with width along the x-axis, length/depth along the y-axis, and height along the z-axis defined by a vector; optionally center the cube at (0,0,0):
    cube([width, depth, height], center)
  4. Draw a cylinder with the specified height and radius or diameter; optionally center the cylinder at (0,0,0):
    cylinder(h, r|d, center)
  5. Draw a cone with the specified height and radii or diameters; optionally center the cone at (0,0,0):
    cylinder(h, r1|d1, r2|d2, center)
  6. Draw a 3D solid defined by vectors of points and faces; optionally use convexity to improve the preview of complex concave shapes:
    polyhedron([points], [faces], convexity)
  7. Import an STL, OFF, 3MF, or AMF file:
    import("filename.stl")
  8. Draw a 3D height map of the data file; optionally center the shape at (0,0) and use convexity to improve the preview of complex concave shapes:
    surface(file = "filename.dat", center, convexity)

Boolean Operations

  1. Group multiple shapes together into one shape:
    union() { ... }
  2. Subtract one or more shapes from an initial shape:
    difference() { ... }
  3. Draw the overlapping region of multiple shapes:
    intersection() { ... }

Shape Transformations

  1. Translate a shape according to a 2D or 3D vector:
    translate([x, y, z])
  2. Rotate a shape around each axis according to the angles defined by a vector:
    rotate([x, y, z])
  3. Rotate a shape a specific angle around the z-axis:
    rotate(angle)
  4. Scale a shape according to the scale factors defined by a 2D or 3D vector:
    scale([x, y, z])
  5. Resize a shape according to the dimensions defined by a 2D or 3D vector; optionally use auto to preserve the object aspect ratio in the unspecified dimensions:
    resize([x, y, z], auto, convexity)
  6. Reflect a shape according to the perpendicular vector of a symmetry plane passing through the origin:
    mirror([x, y, z])
  7. Multiply the geometry of all child elements with the given 4 × 4 affine transformation matrix:
    multmatrix(matrix)
  8. Change a shape’s color according to a predefined color name or hexadecimal color value; optionally make the color (semi) transparent:
    color("colorname | #hex", alpha)
  9. Change a shape’s color according to an RGB or RGBA vector. Each value in the vector ranges from 0 to 1 and represents the proportion of red, green, blue, and alpha present in the color.
    color([r, g, b, a])
  10. Move 2D outlines outward or inward by a given radius (for rounded corners) or delta + chamfer (for sharp or cut-off corners):
    offset(r|delta, chamfer)
  11. Create a 2D shape by projecting a 3D shape onto the xy-plane; when cut = true, create a 2D slice of the intersection of a 3D object and the xy-plane; optionally, when cut = true:
    projection(cut)
  12. Create a convex hull around one or more shapes:
    hull() { ... }
  13. Draw the Minkowski sum of multiple shapes:
    minkowski() { ... }
  14. Extrude a 2D shape into 3D with the given height along the z-axis; optionally center the shape at (0,0) or specify the convexity, twist, slices, and scale of the extrusion:
    linear_extrude(height, center, convexity, twist, slices, scale)
  15. Extrude a 2D shape around the z-axis to form a solid that has rotational symmetry:
    rotate_extrude(angle, convexity)

Loops, Decisions, and List Comprehensions

  1. Repeat a collection of shapes according to the start, step, and end (inclusive) values of a control variable:
    for (var_name = [start:step:end]) { ... }
  2. Draw the intersection of all the shapes generated by the for loop:
    intersection_for(var_name = [start:step:end]) { ... }
  3. Execute commands only if the Boolean test is true:
    if (boolean_test) { ... }
  4. Execute a collection of commands if the Boolean test is true; otherwise, execute alternate commands:
    if (boolean_test) { ... } else { ... }
  5. Generate a list of values according to a for loop:
    list_var = [ for (i = range|list) func(i) ]
  6. Generate a list of values according to a for loop, but only if the value causes a certain condition to be true:
    list_var = [ for (i = ...) if (condition(i)) func(i) else ... ]
  7. Generate a list of lists according to a for loop:
    list_var = [ for (i = ...) let (assignments) func(...) ]

Other Shape Operations

  1. Force the generation of a mesh even in preview mode:
    render(convexity) { ... }
  2. Inside a user-defined module, select the children specified by an index, vector, or range:
    children(index | vector | range)

Modifier Characters

  1. * Disables the drawing of a shape.
  2. ! Shows only a particular shape.
  3. # Highlights a shape in red for debugging purposes; highlighted shape will be rendered.
  4. % Highlights a shape in gray; highlighted shape will not be rendered.

Special Variables

Writable:

  1. $fa Minimum angle for a fragment of an arc.
  2. $fs Minimum size of a fragment of an arc.
  3. $fn Number of fragments used to define an arc; ignores $fa and $fs.
  4. $vpr Viewport rotation angles in degrees.
  5. $vpt Viewport translation.
  6. $vpd Viewport camera distance.
  7. $vpf Viewport field of view.

Read-only:

  1. $t Current animation step, normalized to a value between 0 and 1.
  2. $children Number of module children.
  3. $preview True if Preview mode is used.

Mathematical Functions

  1. sin(ANGLE) Calculates the sine of an angle in degrees.
  2. cos(ANGLE) Calculates the cosine of an angle in degrees.
  3. tan(ANGLE) Calculates the tangent of an angle in degrees.
  4. acos(NUMBER) Calculates the arc (inverse) cosine, in degrees, of a number.
  5. asin(NUMBER) Calculates the arc (inverse) sine, in degrees, of a number.
  6. atan(NUMBER) Calculates the arc (inverse) tangent, in degrees, of a number.
  7. atan2(y, x) Two-value arc (inverse) tangent; returns the full angle (0–360) made between the x-axis and the vector [x, y].
  8. abs(NUMBER) Calculates the absolute value of a number.
  9. sign(NUMBER) Returns a unit value that extracts the sign of a value.
  10. floor(NUMBER) Calculates the largest integer not greater than the number.
  11. ceil(NUMBER) Calculates the next highest integer value.
  12. round(NUMBER) Calculates the rounded version of the number.
  13. ln(NUMBER) Calculates the natural logarithm of a number.
  14. exp(NUMBER) Calculates the mathematical constant e (2.718 . . .) raised to the power of the parameter.
  15. log(NUMBER) Calculates the base 10 logarithm of a number.
  16. pow(NUMBER, NUMBER) Calculates the result of a base raised to an exponent.
  17. sqrt(NUMBER) Calculates the square root of a number.
  18. rands(min, max, count, seed) Generates a vector of random numbers; optionally includes the seed for generating repeatable values.
  19. min(VECTOR | a, b, c) Calculates the minimum value in a vector or list of parameters.
  20. max(VECTOR | a, b, c) Calculates the maximum value in a vector or list of parameters.
  21. norm(VECTOR) Returns the Euclidean norm of a vector.
  22. cross(VECTOR, VECTOR) Calculates the cross-product of two vectors in 3D space.

Other Functions

  1. len(VECTOR|STRING) Calculates the length of a vector or string parameter.
  2. echo(STRING) Prints a value to the console window for debugging purposes.
  3. concat(VECTOR,VECTOR, ...) Returns a new vector that’s the result of appending the elements of the supplied vectors.
  4. lookup(...) Looks up a value in a table and linearly interpolates whether there’s no exact match.
  5. str(...) Converts all parameters to strings and concatenates.
  6. chr(NUMBER | VECTOR | STRING) Converts ASCII or Unicode values to a string.
  7. ord(CHARACTER) Converts a character into an ASCII or Unicode value.
  8. search(...) Finds all occurrences of a value or list of values in a vector, string, or more complex list-of-list construct.
  9. version() Returns the OpenSCAD version as a vector.
  10. version_num() Returns the OpenSCAD version as a number.
  11. parent_module(INDEX) Returns the name of the module idx levels above the current module in the instantiation stack.
  12. is_undef(VARIABLE), is_list(VARIABLE), is_num(VARIABLE), is_bool(VARIABLE), is_string(VARIABLE), is_function(VARIABLE) Returns true if the argument is of the specified type.
  13. assert(expression) Will cause a compilation error if the expression is not true.
  14. let (variable = value) ... Assigns a value to a variable only in the following expression.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.141.202