Need help with dotSCAD?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

JustinSDK
356 Stars 53 Forks Other 3.1K Commits 0 Opened issues

Description

Reduce the burden of mathematics when playing OpenSCAD

Services available

!
?

Need anything else?

Contributors list

# 133,376
opensca...
c-plus-...
CSS
macOS
600 commits
# 376,199
C
Shell
3d-mode...
opensca...
2 commits
# 727,414
3d-mode...
opensca...
2 commits

dotSCAD 3.0

Reduce the burden of mathematics/algorithm when playing OpenSCAD.

dotSCAD

license/LGPL

Introduction

This version Breaks Backward Compatibility and requires OpenSCAD 2021.01 or later!! Please see Release Notes. You can still download v2.5 or older versions from Releases.

Some of my 3D models require complex mathematics/algorithm. I extract them into dotSCAD. Hope it helps when you're playing OpenSCAD.

The idea of the name dotSCAD comes from the filename extension ".scad" of OpenSCAD.

Get Started

OpenSCAD uses three library locations, the installation library, built-in library, and user defined libraries. It's convenient to set

OPENSCADPATH
. Check Setting OPENSCADPATH in OpenSCAD User Manual/Libraries for details.

I set

OPENSCADPATH
to the
src
folder of dotSCAD so all examples here start searching modules/functions from
src
.

Every public module/function is in the .scad whose filename is the same as the module/function name. Here's an example using the

line2d
module:
use ;

line2d(p1 = [0, 0], p2 = [5, 0], width = 1); </line2d.scad>

Some modules/functions are organized in a directory. For example, vxcircle.scad exists in

voxel
directory. You have to prefix the directory name when using `vxcircle`.

use ;

points = vx_circle(radius = 10); for(pt = points) { translate(pt) square(1); }

Examples

These examples incubate dotSCAD and dotSCAD refactors these examples. See examples.

examples

API Documentation

2D Module

Signature

Description
arc(radius, angle, width = 1, widthmode = "LINECROSS") create an arc.
hexagons(radius, spacing, levels) create hexagons in a hexagon.
hull_polyline2d(points, width = 1) create a 2D polyline from a list of

[x, y]
.
line2d(p1, p2, width = 1, p1Style = "CAPSQUARE", p2Style = "CAPSQUARE") create a line from two points.
multilinetext(lines, line_spacing = 15, size = 10, font = "Arial", ...) create multi-line text from a list of strings.
pie(radius, angle) create polyline2de a pie (circular sector).
polyline2d(points, width = 1, startingStyle = "CAPSQUARE", endingStyle = "CAPSQUARE", ...) create a polyline from a list of
[x, y]
coordinates.
polygon_hull(points) create a convex polygon by hulling a list of points. It avoids using hull and small 2D primitives to create the polygon.
rounded_square(size, corner_r, center = false) create a rounded square in the first quadrant.

3D Module

Signature

Description
crystal_ball(radius, theta = 360, phi = 180, thickness = undef) create a crystal ball based on spherical coordinates (r, θ, φ) used in mathematics.
function_grapher(points, thickness = 1, style = "FACES") create the graph of

f(x, y)
from a list of
[x, y, f(x, y)]
.
hull_polyline3d(points, diameter = 1) create a 3D polyline from a list of
[x, y, z]
.
line3d(p1, p2, diameter = 1, p1Style = "CAPCIRCLE", p2Style = "CAPCIRCLE") create a 3D line from two points.
loft(sections, slices = 1) develop a smooth skin between crosssections with different geometries.
polyhedron_hull(points) create a convex polyhedron by hulling a list of points. It avoids using
hull
and small 3D primitives to create the polyhedron.
polyline3d(points, diameter, startingStyle = "CAPCIRCLE", endingStyle = "CAPCIRCLE") create a polyline from a list of
[x, y, z]
.
rounded_cube(size, corner_r, center = false) create a cube in the first octant.
rounded_cylinder(radius, h, round_r, convexity = 2, center = false) create a rounded cylinder.
starburst(r1, r2, n, height) a 3D version of
shape_starburst
.
sweep(sections, triangles = "SOLID") develop a smooth skin from crosssections with the same umber of sides.

Transformation

Signature

Description
along_with(points, angles, twist = 0, scale = 1.0, method = "AXIS_ANGLE") put children along the given path. If there's only one child, put the child for each point.
bend(size, angle, frags = 24) bend a 3D object.
hollow_out(shell_thickness) hollow out a 2D object.
shear(sx = [0, 0], sy = [0, 0], sz = [0, 0]) shear all child elements along the X-axis, Y-axis, or Z-axis.

2D Function

Signature

Description
bijection_offset(pts, d, epsilon = 0.0001) move 2D outlines outward or inward by a given amount. Each point of the offsetted shape is paired with exactly one point of the original shape.
contours(points, threshold) compute contour polygons by applying marching squares to a rectangular list of numeric values.
in_shape(shaptpts, pt, includeedge = false, epsilon = 0.0001) check whether a point is inside a shape.
trim_shape(shape_pts, from, to, epsilon = 0.0001) trim a tangled-edge shape to a non-tangled shape.

2D/3D Function

Signature

Description
angle_between(vt1, vt2) return the angle between two vectors.
bezier_surface(tstep, ctrlpts) given a set of control points, this function returns points of the Bézier surface.
bezier_smooth(pathpts, roundd, tstep = 0.1, closed = false, anglethreshold = 0) use bazier curves to smooth a path.
cross_sections(shapepts, pathpts, angles, twist = 0, scale = 1.0) given a 2D shape, points and angles along the path, this function returns all cross-sections.
in_polyline(line_pts, pt, epsilon = 0.0001) check whether a point is on a line.
lines_intersection(line1, line2, ext = false, epsilon = 0.0001) find the intersection of two line segments. Return

[]
if lines don't intersect.
paths2sections(paths) given a list of paths, this function returns all cross-sections described by those paths.
pathscalingsections(shapepts, edgepath) given an edge path with the first point at the outline of a shape, this function uses the path to calculate scaling factors and returns all scaled sections in the reversed order of the edge path.
midpt_smooth(points, n, closed = false) given a 2D path, this function constructs a mid-point smoothed version by joining the mid-points of the lines of the path.

Path

Signature

Description
arc_path(radius, angle) create an arc path.
archimedean_spiral(armdistance, initangle, pointdistance, numofpoints, rtdir = "CT_CLK") get all points and angles on the path of an archimedean spiral.
bauer_spiral(n, radius = 1, rtdir = "CTCLK") create visually even spacing of n points on the surface of the sphere. Successive points will all be approximately the same distance apart.
bezier_curve(t_step, points) given a set of control points, this function returns points of the Bézier path.
bspline_curve(t_step, degree, points, knots, weights) B-spline interpolation using de Boor's algorithm.
curve(t_step, points, tightness = 0) create a curved path. An implementation of Centripetal Catmull-Rom spline.
fibonacci_lattice(n, radius = 1, dir = "CT_CLK") create visually even spacing of n points on the surface of the sphere. Nearest-neighbor points will all be approximately the same distance apart.
golden_spiral(from, to, pointdistance, rtdir = "CT_CLK") get all points and angles on the path of a golden spiral based on Fibonacci numbers. The distance between two points is almost constant.
helix(radius, levels, leveldist, vtdir = "SPIDOWN", rtdir = "CT_CLK") get all points on the path of a spiral around a cylinder.
sphere_spiral(radius, zastep, zcircles = 1, beginangle = 0, endangle = 0, ...) create all points and angles on the path of a spiral around a sphere. It returns a vector of

[[x, y, z], [ax, ay, az]]
.
torus_knot(p, q, phi_step) generate a path of The (p,q)-torus knot.

Extrusion

Signature

Description
bend_extrude(size, thickness, angle, frags = 24) extrude and bend a 2D shape.
box_extrude(height, shellthickness, bottomthickness, offset_mode = "delta", chamfer = false, ...) create a box (container) from a 2D object.
ellipse_extrude(semiminoraxis, height, center = false, convexity = 10, twist = 0, slices = 20) extrude a 2D object along the path of an ellipse from 0 to 180 degrees.
rounded_extrude(size, round_r, angle = 90, twist = 0, convexity = 10) extrude a 2D object roundly from 0 to 180 degrees.
stereographic_extrude(shadowsideleng) take a 2D polygon as input and extend it onto a sphere.

2D Shape

Signature

Description
shape_arc(radius, angle, width, widthmode = "LINECROSS") return points on the path of an arc shape.
shape_circle(radius, n) return points on the path of a circle.
shape_cyclicpolygon(sides, circler, cornerr) return points on the path of a regular cyclic polygon.
shape_ellipse(axes) return points on the path of an ellipse.
shapeliquidsplitting(radius, centredist, tangentangle = 30, t_step = 0.1) return shape points of two splitting liquid shapes, kind of how cells divide.
shapepathextend(strokepts, pathpts, scale = 1.0, closed = false) extend a 2D stroke along a path to create a 2D shape.
shape_pentagram(r) return shape points of a pentagram.
shape_pie(radius, angle) return shape points of a pie (circular sector) shape.
shape_square(size, corner_r = 0) return shape points of a rounded square or rectangle.
shape_starburst(r1, r2, n) returns shape points of a star.
shape_superformula(phi_step, m1, m2, n1, n2 = 1, n3 = 1, a = 1, b = 1) return shape points of Superformula.
shape_taiwan(h, distance = 0) return shape points of Taiwan.
shape_trapezium(length, h, corner_r = 0) return shape points of an isosceles trapezoid.

2D Shape Extrusion

Signature

Description
archimedeanspiralextrude(shapepts, armdistance, initangle, pointdistance, numofpoints, ...) extrude a 2D shape along the path of an archimedean spiral.
goldenspiralextrude(shapepts, from, to, pointdistance, ...) extrude a 2D shape along the path of a golden spiral.
helix_extrude(shapepts, radius, levels, leveldist, ...) extrude a 2D shape along a helix path.
path_extrude(shapepts, pathpts, ...) extrude a 2D shape along a path.
ring_extrude(shape_pts, radius, angle = 360, ...) rotational extrusion spins a 2D shape around the Z-axis.
spherespiralextrude(shapepts, radius, zastep, ...) extrude a 2D shape along the path of a sphere spiral.

Util

util/list

Signature

Description
util/bsearch(sorted, target) search a value in a list whose elements must be sorted by zyx.
util/has(lt, elem, sorted = false) return

true
if
lt
contains elem.
util/find_index(lt, test) return the index of the first element that satisfies the testing function.
util/dedup(lt, ...) eliminate duplicate vectors.
util/flat(lt, depth = 1) return a new list with all sub-list elements concatenated into it recursively up to the specified depth.
util/reverse(lt) reverse a list.
util/slice(lt, begin, end) return a list selected from
begin
to
end
, or to the
end
of the list (
end
not included).
util/sort(lt, by = "idx", idx = 0) sort a list.
util/sum(lt) use
+
to sum up all elements in a list.
util/swap(lt, i, j) swap two elements in a list.
util/zip(lts, combine) make a list that aggregates elements from each of the lists.
util/every(lt, test) test whether all elements in the list pass the test implemented by the provided function.
util/some(lt, test) test whether at least one element in the list passes the test implemented by the provided function.

util/random

Signature

Description
util/choose(choices, seed) choose an element from the given list.
util/rand(minvalue = 0, maxvalue = 1, seed_value = undef) generate a pseudo random number.
util/shuffle(lt, seed = undef) randomizes the order of the elements.

util/string

Signature

Description
util/parse_number(t) parse the string argument as an number.
util/split_str(t, delimiter) split the given string around matches of the given delimiting character.
util/sub_str(t, begin, end) return the part of the string from

begin
to
end
, or to the
end
of the string (
end
not included).

util/math

Signature

Description
util/degrees(radians) convert a radian measurement to the corresponding value in degrees.
util/radians(degrees) convert a degree measurement to the corresponding value in radians.
util/polar_coordinate(point) convert from Cartesian to Polar coordinates.
util/spherical_coordinate(point) convert from Cartesian to Spherical coordinates (used in mathematics).
util/lerp(v1, v2, amt) linear interpolate the vector v1 to v2.
util/fibseq(from, to) generate a Fibonacci sequence.

util/set

Signature

Description
util/set/hashset(lt, ...) model the mathematical set, backed by a hash table.
util/set/hashset_add(set, elem, ...) add an element to a

hashset
.
util/set/hashset_has(set, elem, ...) return
true
if a
hashset
contains the specified element.
util/set/hashset_del(set, elem, ...) del an element from a
hashset
.
util/set/hashset_len(set) return the length of the elements in a
hashset
.
util/set/hashset_elems(set) returns a list containing all elements in a
hashset
. No guarantees to the order.

util/map

Signature

Description
util/map/hashmap(kv_lt, ...) map keys to values.
util/map/hashmap_put(map, key, value, ...) put a key/value pair to a

hashmap
.
util/map/hashmap_get(map, key, ...) get the value of the specified key from a
hashmap
.
util/map/hashmap_del(map, key, ...) delete the mapping for the specified key from a
hashmap
if present.
util/map/hashmap_len(map) return the length of a
hashmap
.
util/map/hashmap_keys(map) return a list containing all keys in a
hashmap
.
util/map/hashmap_values(map) return a list containing all values in a
hashmap
.
util/map/hashmap_entries(map) return a list containing all
[key, value]
s in a
hashmap
.

Matrix

Signature

Description
matrix/m_determinant(m) calculate a determinant of a square matrix.
matrix/m_mirror(v) generate a transformation matrix which can pass into

multmatrix
to mirror the child element on a plane through the origin.
matrix/m_rotation(a, v) Generate a transformation matrix which can pass into
multmatrix
to rotate the child element about the axis of the coordinate system or around an arbitrary axis.
matrix/m_scaling(s) generate a transformation matrix which can pass into
multmatrix
to scale its child elements using the specified vector.
matrix/m_shearing(sx = [0, 0], sy = [0, 0], sz = [0, 0]) generate a transformation matrix which can pass into
multmatrix
to shear all child elements along the X-axis, Y-axis, or Z-axis in 3D.
matrix/m_translation(v) generate a transformation matrix which can pass into multmatrix to translates (moves) its child elements along the specified vector.

Point Transformation

Signature

Description
ptf/ptf_bend(size, point, radius, angle) transform a point inside a rectangle to a point of an arc.
ptf/ptf_circle(size, point) transform a point inside a rectangle to a point inside a circle.
ptf/ptf_ring(size, point, radius, angle = 360, twist = 0) transform a point inside a rectangle to a point of a ring.
ptf/ptf_rotate(point, a, v) rotate a point a degrees around the axis of the coordinate system or an arbitrary axis.
ptf/ptf_sphere(size, point, radius, angle = [180, 360]) transform a point inside a rectangle to a point of a sphere.
ptf/ptf_torus(size, point, radius, angle = [360, 360], twist = 0) transform a point inside a rectangle to a point of a torus.
ptf/ptfxtwist(size, point, angle) twist a point along the x-axis.
ptf/ptfytwist(size, point, angle) twist a point along the y-axis.


Turtle

Signature

Description
turtle/footprints2(cmds, start = [0, 0]) drive a turtle with

["forward", length]
or
["turn", angle]
. This function is intended to use a turtle to imitate freehand drawing.
turtle/footprints3(cmds, start = [0, 0, 0]) a 3D verion of
footprint2
.
turtle/lsystem2(axiom, rules, n, angle, leng = 1, heading = 0, ...) 2D implementation of L-system.
turtle/lsystem3(axiom, rules, n, angle, leng = 1, heading = 0, ...) 3D implementation of L-system.
turtle/t2d(t, cmd, point, angle, leng) an implementation of Turtle Graphics.
turtle/t3d(t, cmd, point, unit_vectors, leng, angle) a 3D version of
t2d
.

Voxel

Signature

Description
voxel/vx_ascii(char, center = false, invert = false) generate 8x8 voxel points of printable ASCII characters (codes 32dec to 126dec).
voxel/vx_bezier(p1, p2, p3, p4) return voxel-by-voxel points of Bézier Curve.
voxel/vx_circle(radius, filled = false) return points that can be used to draw a voxel-style circle.
voxel/vx_contour(points, sorted = false) return the contour which encircles the area.
voxel/vx_curve(points, tightness = 0) the curve is drawn only from the 2nd control point to the second-last control point.
voxel/vx_cylinder(r, h, filled = false, thickness = 1) return points that can be used to draw a voxel-style cylinder.
voxel/vx_difference(points1, points2) create a difference of two lists of points.
voxel/vx_from(binaries, center = false, invert = false) given a list of 0s and 1s that represent a black-and-white image. This function translates them into voxel points.
voxel/vx_gray(levels, center = false, invert = false, normalize = false) given a list of numbers (0 ~ 255) that represent a gray image. This function translates them into a list of

[x, y, level]
s.
voxel/vx_intersection(points1, points2) create an intersection of two lists of points.
voxel/vx_line(p1, p2) given two points. it returns points that can be used to draw a voxel-style line.
voxel/vx_polygon(points, filled = false) return points that can be used to draw a voxel-style polygon.
voxel/vx_polyline(points) return points that can be used to draw a voxel-style polyline.
voxel/vx_sphere(radius, filled = false, thickness = 1) return points that can be used to draw a voxel-style sphere.
voxel/vx_union(points1, points2) create a union of two lists of points.

Part

Signature

Description
part/cone(radius, length = 0, spacing = 0.5, angle = 50, void = false, ends = false) create a cone for rotatable models.
part/connector_peg(radius, height, spacing = 0.5, void = false, ends = false) create a connector peg.
part/joint_T(shaftr, shafth, t_leng, thickness, spacing = 0.5, center = false) create a joint_T for rotatable models.

Surface

Signature

Description
surface/sf_bend(levels, radius, thickness, depth, angle = 180, invert = false) bend a photo.
surface/sf_ring(levels, radius, thickness, depth, angle = 360, twist = 0, invert = false) turn a photo into a ring.
surface/sf_solidify(surface1, surface2, slicing = "SLASH") solidify two square surfaces.
surface/sf_sphere(levels, radius, thickness, depth, angle = [180, 360], invert = false) map a photo onto a sphere.
surface/sf_square(levels, thickness, depth, xtwist = 0, ytwist = 0, invert = false) turn a photo into a twistable square.
surface/sf_torus(levels, radius, thickness, depth, angle = [360, 360], twist = 0, invert = false) turn a photo to a torus.

Noise

Signature

Description
noise/nz_cell(points, p, dist = "euclidean") an implementation of Worley noise.
noise/nz_perlin1(x, seed = undef) return the 1D Perlin noise value at the x coordinate.
noise/nz_perlin1s(xs, seed = undef) return 1D Perlin noise values at x coordinates.
noise/nz_perlin2(x, y, seed = undef) return the 2D Perlin noise value at the (x, y) coordinate.
noise/nz_perlin2s(points, seed = undef) return 2D Perlin noise values at (x, y) coordinates.
noise/nz_perlin3(x, y, z, seed = undef) return the 3D Perlin noise value at the (x, y, z) coordinate.
noise/nz_perlin3s(points, seed = undef) return 3D Perlin noise values at (x, y, z) coordinates.
noise/nz_worley2(x, y, seed = undef, grid_w = 10, dist = "euclidean") return the 2D Worley noise value at the (x, y) coordinate.
noise/nz_worley2s(points, seed = undef, grid_w = 10, dist = "euclidean") return 2D Worley noise values at (x, y) coordinates.
noise/nz_worley3(x, y, z, seed = undef, tile_w = 10, dist = "euclidean") return the 3D Worley noise value at the (x, y, z) coordinate.
noise/nz_worley3s(points, seed = undef, tile_w = 10, dist = "euclidean") return 3D Worley noise values at (x, y, z) coordinates.

Voronoi

Signature

Description
voronoi/vrn2cellsfrom(points) create cell shapes of Voronoi from a list of points.
voronoi/vrn2cellsspace(size, grid_w, seed = undef) create cell shapes of Voronoi in the first quadrant.
voronoi/vrn2_from(points, spacing = 1, ...) create a Voronoi from a list of points.
voronoi/vrn2_space(size, grid_w, seed = undef, spacing = 1, ...) create a Voronoi in the first quadrant.
voronoi/vrn3_from(points, spacing = 1) create a 3D version of Voronoi.
voronoi/vrn3_space(size, grid_w, seed = undef, spacing = 1) create a Voronoi in the first octant.

Maze

Signature

Description
maze/mzsquarecells(rows, columns, start = [0, 0], ...) return cell data of a square maze.
maze/mzsquareget(cell, query) a helper for getting data from a square-maze cell.
maze/mzsquarewalls(cells, rows, columns, cell_width, ...) a helper for creating square wall data from maze cells.
maze/mzhexwalls(cells, rows, columns, cell_radius, ...) a helper for creating hex wall data from maze cells.
maze/mzsquareinitialize(rows, columns, mask) a helper for initializing cell data of a maze.
maze/mz_hamiltonian(rows, columns, start = [0, 0], seed = undef) create a hamiltonian path from a maze.
maze/mzthetacells(rows, beginning_number, start = [0, 0], seed = undef) return cell data of a theta maze.
maze/mzthetaget(cell, query) a helper for getting data from a theta-maze cell.


Preview

Triangle

Signature

Description
triangle/tri_circumcenter(shape_pts) return the circumcenter of a triangle.
triangle/tri_incenter(shape_pts) return the incenter of a triangle.
triangle/triearclipping(shapepts, ret = "TRIINDICES", ...) triangulation by ear clipping.
triangle/tri_delaunay(points, ret = "TRIINDICES") return the Delaunay triangulation of the points.
*triangle/tridelaunayindices(d) return triangle indices from a delaunay object.
triangle/tridelaunayshapes(d) return triangle shapes from a delaunay object.
*triangle/tridelaunay_voronoi**(d) return Voronoi cells from a delaunay object.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.