OpenSCAD Library for more basic shapes

OpenSCAD Library for more basic shapes with alignment option and useful transformations
25
410
0
1743
updated March 1, 2025

Description

PDF

This is one of the libraries that I use in several of my projects. I don't want to upload it with each of them again and again.

This OpenSCAD library provides some additional basic shapes. It also provides some standard shapes with additional alignment option.

Alignment means that a shape can be on the positive side of an axis, centered on zero, or on the negative side of the axis. This can be defined for all axis independently.

Example for cube with alignment:

/*
    Creates a cube of size v aligned independently on each axis according to align:
    <0 : on the negative axis part, starting at the origin
    0  : centered on the origin
    >0 : on the positive axis part, starting at the origin
    Default alignment is [0, 0, 0], meaning centered on each axis.
*/
module cube_align(v, align = [0, 0, 0]) {
    […]
}

This library can be used by including the following line:

use <af-basics.scad>

Extended standard shapes with alignment:

  • Square (module square_align)
  • Cube (module cube_align)
  • Cylinder (module cylinder_align)
  • Cylinder in different axis orientations (modules cylinder_x|y|z) (shape H)

Additional shapes (also with alignment):

  • Octahedron (module octahedron) (shape K)
  • Pipe (hollow cylinder) in X, Y and Z orientation (modules pipe & pipe_x|y|z) (shape F)
  • Ring (hollow circle) (module ring) (shape G)
  • Torus in X, Y and Z orientation with optional arc segment (modules torus & torus_x|y|z) (shape L)
  • Tri-prism (diagonally halfed cube) in XY, YZ and XZ orientation (modules triprism_xy|yz|xz) (shape I)
  • Rounded Cube (module cube_round) (shape A - low poly-count example)
  • Rounded Square (module square_round)
  • Rounded & tapered cube (module cube_round_tapered) - tapering at top & bottom makes sure that the rounding does not exceed a given angle (shape B)
  • Beveled Cube (module cube_bevel) (shape J)
  • Arc Fan (module arc_fan) - used for cutting out or intersecting an arc segment on another shape (shape E)
  • Circle segment (module circle_segment) (shape D)

Additional modules:

  • rotate_at - like rotate, but the center of rotation can be any point instead of the origin
  • spiral_extrude - extrudes a 2D shape into a 3D spiral (shape C)

For more details see the comments in the SCAD file and the example code (module basics_test).

Update 2022-06-30:

Added the following transformation modules:

  • mirror_at - like mirror, but the plane of reflection goes through a custom point instead of the origin, additionally there is a clone flag so you get both the original and the mirrored child parts
  • multi_mirror - like mirror_at, but takes a list of mirror plane normal vectors, and mirrors the child parts on all planes, especially useful with the clone flag

Update 2023-10-21:

Modified the modules torus & torus_x|y|z. They now have additional optional parameters fn1 and fn2 that allow you to set the number of steps for the primary (big) and secondary (small) rotation separately. With $fn both rotation can only have the same number of steps, with fn1 & fn2 they can differ.

Update 2023-11-06:

Added this module:

  • fillet - creates a 2D concave fillet with a given radius and corner angle. Optional depth parameter lets the shape extend into the corner objects (to avoid small gaps). The fillet is positioned at the corner point, with center=true it is positioned at the fillet circle segment's center.
    This module can also be used to create convex fillets by subtracting the fillet from the object.

Update 2024-06-10:

Finally added versioning to the list of files (I should have done this sooner).

Added these modules:

  • circle_align - creates a circle with alignment, similar to square_align
  • ring_segment - creates a segment of a ring, similar to ring, but with from and to angle
  • rivet & rivet_x|y|z - creates a low polygon rivet that can be attached to objects as surface detailing. This is more efficient than adding lots of spheres/half-spheres to an object. The x/y/z versions are aligned to surfaces on x/y/z plane. (See examples in basics_test)
  • spike & spike_x|y|z - creates a low polygon spike that can be attached to objects as surface detailing or as fasteners, similar to rivet.
  • space_out - separates all of its child objects by a fixed vector (from center point to center point of the children)
  • marker - a small (by default r=0.1) colored (by default “red”) low polygon sphere. This is meant for preview.
  • show_points - uses marker to show the positions of the points in the given list. If no color is defined, then a rainbow (red-yellow-green-blue-violet) is used. The gap between violet and red is intentional to show were the list starts and ends.
    This is meant for preview to e.g. show the points of a polygon (useful to see the exact points if the polygon does not appear as expected).

Added these functions:

  • angle_to_length - converts an angle (in degrees) into the length of the corresponding arc with the given radius
  • length_to_angle - converts the length of the arc into the corresponding angle based on the radius
  • to_vec2 - converts a vector of any size to a vector of size 2, missing entries are filled with 0
  • to_vec3 - converts a vector of any size to a vector of size 3, missing entries are filled with 0
  • reverse - returns the given list in reversed order
  • hsl_to_rgb - converts a HSL color (components 0.0 - 1.0) into a RGB vector, which can be used with the build-in color module
  • hsv_to_rgb - converts a HSV color (components 0.0 - 1.0) into a RGB vector, which can be used with the build-in color module
  • rainbow - creates a list (of given length) of RGB color vectors. By default there is an intentional gap between the first color (red) and last color (violet), with optional parameter full_wrap=true this can be changed to go from red all the way around back to red.

The basics_test module at the start of the file was updated to include these new features.

Update 2025-03-01:

Changed the inner workings of modules cube_round, cube_round_tapered and cube_bevel to be more efficient.

Added these modules:

  • square_bevel - 2D version of cube_bevel
  • import_svg - imports one or more SVG files as 2D shapes, where succesive entries cut out the previous ones; see module basics_test for an example how this can be used, the example requires the new SVG files af-basics-example-*.svg
  • shape_electro_sign - creates a 2D shape for warning of electricity sign

Added this function:

  • round_prec - rounds the given number to a specific precision of digits after the decimal point (or before the decimal point for negative precision values)

Added these extra files:

  • af-basics-example-tree.svg - SVG shape needed for test of import_svg module
  • af-basics-example-wheel.svg - SVG shape needed for test of import_svg module

The basics_test module at the start of the file was updated to include these new features.
A new screenshot that shows the new features was also added.

Tags



Model origin

The author marked this model as their own original creation.

License