Docs · OpenSCAD
OpenSCAD lessons
Parametric parts in code. Open any lesson in the OpenRSCAD Editor, drag its Customizer sliders and bake it into your scene.
OpenSCAD describes a part in code, so it can be shared and re-sized by anyone. ToolBox3D runs .scad files unchanged: open the OpenRSCAD Editor (Model editor ▸ Quick Actions ▸ Editors, Create (Misc) ▸ OpenSCAD Part…, or File ▸ New OpenSCAD Script), and the Customizer beside the code turns its parameters into sliders with a live preview. Bake to Scene adds the part to your scene, ready to export for 3D printing. BOSL2 and the Gridfinity, threads, gears and enclosure libraries are built in: include <BOSL2/std.scad> just works, in the browser and in the Windows app.
Parametric box with a hole, built with BOSL2 (bundled: no download needed).
The Customizer on the right lists the parameters below; the preview follows every edit. Ctrl+Enter (or Bake to Scene) adds the part to the Model scene. Units are millimetres. Other bundled libraries: Gridfinity, threads, gears, YAPP Box... see Examples > OpenSCAD.
include <BOSL2/std.scad>
/* [Dimensions] */
// Box width
width = 30; // [10:60]
// Box depth
depth = 20; // [10:60]
// Box height
height = 15; // [5:40]
// Edge rounding
rounding = 2; // [0:0.5:2.5]
/* [Hole] */
hole = true;
// Hole diameter
hole_d = 10; // [2:1:25]
/* [Quality] */
// Facets of round surfaces
facets = 48; // [12:4:96]
$fn = facets;
diff()
cuboid([width, depth, height], rounding = rounding)
if (hole) tag("remove") cyl(h = height + 1, d = hole_d);difference() with a hole pattern
/* difference() with a for-loop hole grid. All dimensions in mm. */
plate_w = 80; // plate width (X)
plate_d = 60; // plate depth (Y)
plate_h = 4; // plate thickness
margin = 10; // clear border around the hole grid
spacing = 14; // centre-to-centre distance between holes
hole_d = 7; // diameter of each hole
$fn = 32;
difference() {
// Base plate
cube([plate_w, plate_d, plate_h]);
// Grid of holes: for loops generate a list of children for difference()
for (x = [margin : spacing : plate_w - margin],
y = [margin : spacing : plate_d - margin])
translate([x, y, -1])
cylinder(d = hole_d, h = plate_h + 2);
}for + rotate ring
/* Ring of pegs - demonstrating for + rotate to place objects in a circle.
The loop variable a is the angle step, derived from the total peg count.
Rotating a translated peg sweeps it around the origin. */
peg_count = 10; // how many pegs in the ring
ring_r = 30; // radius from centre to peg centre (mm)
peg_d = 7; // peg diameter (mm)
peg_h = 18; // peg height (mm)
base_h = 3; // thin base disk height (mm)
base_d = ring_r * 2 + peg_d + 4; // base diameter sized to hold all pegs
$fn = 40;
// flat base disk so the part is printable without support
cylinder(d = base_d, h = base_h);
// place one peg per iteration, rotating it around the Z axis
for (a = [0 : 360 / peg_count : 359]) {
rotate([0, 0, a])
translate([ring_r, 0, base_h])
cylinder(d = peg_d, h = peg_h);
}hull() in 3D
/* hull() in 3D - a box with fully rounded corners and edges. All dimensions in mm. */
box_w = 60; // outer width (X)
box_d = 40; // outer depth (Y)
box_h = 25; // outer height (Z)
r = 5; // corner sphere radius (determines how round the edges are)
$fn = 40;
// Place one sphere at each of the 8 corners of the (r-inset) bounding box.
// hull() wraps them all - the result is a box where every edge and corner
// is a smooth arc of radius r.
hull() for (x = [r, box_w - r], y = [r, box_d - r], z = [r, box_h - r])
translate([x, y, z]) sphere(r = r);linear_extrude(twist=)
/* linear_extrude(twist=) - rotates the cross-section as it rises to create a spiral. */
col_size = 16; // side length of the square cross-section
col_height = 60; // total column height
twist_deg = 120; // total twist in degrees over the height
taper = 0.85; // slight width reduction at top (scale factor)
$fn = 48;
// A square with notched corners for a more interesting silhouette.
module cross_section(s) {
r = s * 0.12; // small corner radius expressed as square fraction
offset(r = r) offset(r = -r)
square([s, s], center = true);
}
// linear_extrude with twist - the teaching moment.
linear_extrude(
height = col_height,
twist = twist_deg,
slices = 60, // more slices = smoother spiral
scale = taper
)
cross_section(col_size);minkowski() rounding
/* minkowski() rounding - a box with all edges and corners rounded. All dimensions in mm. */
box_w = 50; // inner box width before rounding (final = box_w + 2*r)
box_d = 35; // inner box depth before rounding
box_h = 20; // inner box height before rounding
r = 4; // rounding radius applied to ALL edges and corners
$fn = 32;
// minkowski() sweeps every point of the first child through every point of the second.
// A box swept through a sphere produces a box whose corners become spherical and
// whose edges become cylindrical - a perfect all-direction fillet.
minkowski() {
cube([box_w, box_d, box_h]); // base shape (un-rounded)
sphere(r = r); // rounding element - radius = fillet size
}offset() rounded
/* offset(r=) rounding - an L-shape inflated with smooth corners. All dimensions in mm. */
arm_w = 12; // width of each arm of the L
arm_l = 40; // length of each arm
r = 4; // rounding radius (positive = outward + rounded corners)
h = 6; // extrusion height
$fn = 48;
// The raw L-shape as a polygon (two rectangles forming an L)
module l_shape_2d(aw, al) {
polygon([
[0, 0],
[al, 0],
[al, aw],
[aw, aw],
[aw, al],
[0, al]
]);
}
// offset(r=) with a positive radius rounds ALL corners (concave ones become fillets,
// convex ones become circular arcs) and inflates the outline by r.
linear_extrude(height = h)
offset(r = r)
l_shape_2d(arm_w, arm_l);polyhedron()
/* polyhedron() - square pyramid from scratch.
Points are listed as [x, y, z] coordinates.
Each face is a list of point indices ordered so the outward
normal follows the right-hand rule (counter-clockwise when
viewed from outside). */
base = 30; // square base side length (mm)
apex = 25; // pyramid height (mm)
half = base / 2;
// 5 vertices: 4 base corners + 1 apex
pts = [
[-half, -half, 0], // 0 - front-left
[ half, -half, 0], // 1 - front-right
[ half, half, 0], // 2 - back-right
[-half, half, 0], // 3 - back-left
[ 0, 0, apex] // 4 - apex
];
// 5 faces: 1 square base (split into 2 triangles) + 4 triangular sides
faces = [
[0, 3, 2, 1], // base (winding viewed from below)
[0, 1, 4], // front face
[1, 2, 4], // right face
[2, 3, 4], // back face
[3, 0, 4] // left face
];
polyhedron(points = pts, faces = faces);recursive module
/* Branching tree - demonstrating a recursive module.
branch() calls itself twice with smaller dimensions and opposite angles,
stopping when the branch becomes shorter than min_len.
A 2D shape is built and then extruded once at the top level. */
trunk_len = 30; // length of the main trunk (mm)
trunk_w = 5; // width of the main trunk (mm)
depth = 3; // extrude depth for the printable plaque (mm)
split_angle = 28; // angle each child branch fans outward (degrees)
shrink = 0.65; // child branch length as a fraction of parent length
min_len = 4; // stop recursing when branch length drops below this (mm)
// 2D recursive branch - uses square() so it works inside linear_extrude
module branch2d(len, w) {
square([w, len]);
if (len * shrink > min_len) {
child_len = len * shrink;
child_w = max(1, w * shrink);
// left child
translate([w / 2, len, 0])
rotate(split_angle)
translate([-child_w / 2, 0, 0])
branch2d(child_len, child_w);
// right child
translate([w / 2, len, 0])
rotate(-split_angle)
translate([-child_w / 2, 0, 0])
branch2d(child_len, child_w);
}
}
linear_extrude(height = depth)
translate([-trunk_w / 2, 0])
branch2d(trunk_len, trunk_w);rotate_extrude()
/* rotate_extrude() - revolves a 2D profile 360 degrees around the Z axis. */
ring_r = 24; // distance from Z axis to centre of the cross-section
cs_width = 10; // cross-section width (radial)
cs_height = 14; // cross-section height (axial)
corner_r = 3; // rounding on the cross-section corners
$fn = 64;
// A rounded rectangle cross-section sitting to the right of the Z axis.
// Important: every point of the profile must have x >= 0 for rotate_extrude.
module cross_section() {
translate([ring_r - cs_width / 2, -cs_height / 2, 0])
offset(r = corner_r) offset(r = -corner_r)
square([cs_width, cs_height]);
}
// rotate_extrude sweeps the profile all the way around - the key call.
rotate_extrude($fn = 64)
cross_section();engraving text
/* Engraving text into a plate with difference(). */
plate_w = 80; // plate width
plate_d = 40; // plate depth (Y)
plate_h = 6; // plate total thickness
engrave_d = 1.5; // how deep the text is cut in
font_size = 10; // character height
label = "MADE WITH"; // first line of text
label2 = "OPENSCAD"; // second line of text
font_name = "Liberation Sans:style=Bold";
$fn = 32;
difference() {
// The solid plate
cube([plate_w, plate_d, plate_h], center = true);
// Engrave first line - translate up from plate surface by (plate_h/2 - engrave_d)
translate([0, 5, plate_h / 2 - engrave_d])
linear_extrude(height = engrave_d + 0.1) // +0.1 ensures clean boolean cut
text(label, size = font_size, font = font_name, halign = "center", valign = "center");
// Engrave second line below the first
translate([0, -8, plate_h / 2 - engrave_d])
linear_extrude(height = engrave_d + 0.1)
text(label2, size = font_size, font = font_name, halign = "center", valign = "center");
}