documents/dev/OpenSCAD.md

OpenSCAD

Quick Start

  1. Open OpenSCAD and create a new file.
  2. Paste the code from any section below.
  3. Press F5 for Preview (fast) and F6 for Render (final geometry).
  4. Export as STL with File -> Export -> Export as STL.

First Model (Cube + Cylinder)

// Units are usually millimeters
cube([20, 20, 10]);                 // width, depth, height
translate([30, 10, 0]) cylinder(h=15, r=6, $fn=64);

What this teaches:

  • cube([x,y,z]) creates a box.
  • cylinder(h=..., r=...) creates a cylinder.
  • translate([x,y,z]) moves an object.

Core Transformations

color("tomato") cube([10, 10, 10]);
translate([15, 0, 0]) color("steelblue") cube([10, 10, 10]);
rotate([0, 0, 45]) translate([30, 0, 0]) cube([10, 10, 10]);

Use these constantly:

  • translate([x,y,z]) move
  • rotate([x,y,z]) rotate in degrees
  • scale([x,y,z]) resize

Boolean Modeling (Most Important)

// 1) union: combine solids
union() {
  cube([20, 20, 8]);
  translate([10, 10, 8]) cylinder(h=8, r=6, $fn=64);
}

// 2) difference: subtract one solid from another
translate([35, 0, 0]) difference() {
  cube([20, 20, 8]);
  translate([10, 10, -1]) cylinder(h=10, r=4, $fn=64);
}

// 3) intersection: keep overlap only
translate([70, 0, 0]) intersection() {
  cube([20, 20, 20]);
  rotate([0, 45, 0]) cube([20, 20, 20], center=true);
}

Parametric Design (Reusable)

// Change only these values
plate_w = 80;
plate_d = 50;
plate_h = 4;
hole_r = 2.2;

difference() {
  cube([plate_w, plate_d, plate_h]);

  // 4 mounting holes
  for (x = [10, plate_w-10], y = [10, plate_d-10]) {
    translate([x, y, -1]) cylinder(h=plate_h+2, r=hole_r, $fn=48);
  }
}

Debug / View Shortcuts

  • #shape; highlight this shape (transparent pink preview)
  • !shape; show only this shape
  • %shape; transparent ghost
  • *shape; disable/comment-out shape

Example:

difference() {
  cube([20, 20, 10]);
  #translate([10, 10, -1]) cylinder(h=12, r=4); // debug highlighted cut
}

Good Beginner Workflow

  1. Build from simple primitives (cube, cylinder, sphere).
  2. Use difference() to cut holes and slots.
  3. Convert fixed numbers into variables at the top.
  4. Preview often (F5), then render (F6) before export.
  5. Keep one feature per block so debugging is easy.

Next Practice Task

Create a small electronics enclosure:

  • Base plate with 4 holes
  • Four standoffs for screws
  • A lid made with difference() (hollowed box)

If you want, I can add a second section with a full enclosure example you can edit parameter-by-parameter.