diff main.scad @ 10:a7a1b12fd6bf

latch: latches on any side, back latch for a removable lid Add back_side to put the hinge or a latch on the back, and latch_front, latch_left and latch_right checkboxes. The latch hardware is modelled in a face frame and placed on each latched side. All latches share one width, clamped to the narrowest latched face; the console lists the latched sides and the number of latches to print. A layout other than front latch with back hinge adds an l token with the side letters to the file name. Replace preview_lid_angle with preview_lid_open in percent: with the hinge 100 turns the lid by 180 degrees, without it lifts the lid by one body height. Presets converted. README updated.
author ark
date 2026-11-17 12:53 +0100
parents 4b89f95689a8
children 7bef1d0cb914
line wrap: on
line diff
--- a/main.scad	Tue Nov 17 12:37:14 2026 +0100
+++ b/main.scad	Tue Nov 17 12:53:33 2026 +0100
@@ -1,6 +1,8 @@
-// hinged box with filament hinge at the back and a hinged latch in front.
-// sized by its inner dimensions, e.g. for standing notepad paper.
-// x runs front (-x) to back (+x), y left to right, z up.
+// hinged box with a filament hinge or a latch at the back and hinged
+// latches on any of the other sides. sized by its inner dimensions, e.g.
+// for standing notepad paper.
+// x runs front (-x) to back (+x), y right (-y) to left (+y) seen from the
+// front, z up.
 // the preview turns the model 90 degrees so the front faces -y.
 
 /* [part selection] */
@@ -12,9 +14,9 @@
 preview_content = false;
 // show the filament pins and latch bar in the assembly view
 preview_hardware = false;
-// lid opening angle in the assembly view
-preview_lid_angle = 0; // [0:1:180]
-// latch opening angle in the assembly view
+// lid opening in percent in the assembly view
+preview_lid_open = 0; // [0:1:100]
+// opening angle of all latches in the assembly view
 preview_latch_angle = 0; // [0:1:75]
 
 /* [box] */
@@ -66,9 +68,17 @@
 hinge_lines = 3; // [2:1:8]
 
 /* [latch] */
+// back of the box: the hinge, or a latch for a removable lid
+back_side = "hinge"; // [hinge, latch]
+// latch on the front
+latch_front = true;
+// latch on the left side, seen from the front
+latch_left = false;
+// latch on the right side, seen from the front
+latch_right = false;
 // latch shape: a plate in front of the knuckles joining knuckle and hook, or a solid block between the bar knuckles
 latch_style = "plate"; // [plate, block]
-// latch width
+// latch width, the same for all latches
 latch_width = 20; // [8:1:100]
 // share of the latch width taken by the middle knuckle that carries the latch. the outer knuckles share the rest
 latch_middle_ratio = .33; // [.1:.01:.9]
@@ -134,16 +144,27 @@
 lid_h = top_z - seam_z;
 
 // hinge
+back_hinge = back_side == "hinge";
 hinge_x = out_x/2 + knuckle_u;
 hinge_span = out_y - 2*edge_r;
 knuckle_l = (hinge_span - (hinge_knuckles - 1)*knuckle_gap)/hinge_knuckles;
 
+// sides that can take a latch: front, back, left, right. side_a turns the
+// front to that side, side_d is the distance of its face from the centre,
+// side_span the length of the face between the edge roundings
+side_name = ["front", "back", "left", "right"];
+side_a = [0, 180, -90, 90];
+side_d = [out_x/2, out_x/2, out_y/2, out_y/2];
+side_span = [out_y - 2*edge_r, out_y - 2*edge_r, out_x - 2*edge_r, out_x - 2*edge_r];
+side_on = [latch_front, !back_hinge, latch_left, latch_right];
+latch_sides = [for (i = [0:3]) if (side_on[i]) i];
+
 // latch. the latch hangs from lid knuckles and its hook wraps around a
-// filament bar held by body knuckles straight below the latch axis.
-// in the front frame u runs outward from the front face, z up.
-// a latch wider than the hinge span is narrowed to it
-latch_w = min(latch_width, hinge_span);
-latch_x = -out_x/2 - knuckle_u;
+// filament bar held by body knuckles straight below the latch axis. the
+// latch hardware is modelled in a face frame: u runs outward from the
+// face, y along it, z up. all latches have the same width; one wider than
+// the narrowest latched face is narrowed to it
+latch_w = min(concat([latch_width], [for (i = latch_sides) side_span[i]]));
 latch_z = seam_z + knuckle_r + cl;
 bar_z = seam_z - cl - knuckle_r;
 bar_r = filament_diameter/2;
@@ -162,6 +183,10 @@
     _detent(m) < latch_detent ? _hook_angle(m, hi, n - 1) : _hook_angle(lo, m, n - 1);
 hook_a = _hook_angle(0, 90);
 
+// strings in v joined with sep between them
+function _join(v, sep="", i=0) =
+    i >= len(v) ? "" : str(i > 0 ? sep : "", v[i], _join(v, sep, i + 1));
+
 // knuckle slots across the latch width: the middle one carries the latch,
 // the outer ones have a lid knuckle above and a body knuckle below. a
 // ratio that leaves a slot narrower than 2 extrusions is clamped
@@ -199,11 +224,11 @@
 nail_d = 3*lw;
 nail_h = min(8*lw, bar_z - hook_ro - 2*lw - blk_bz);
 
-// how far the latch reaches out from the front face
+// how far the latch reaches out from the face
 latch_front_u = latch_block ? knuckle_u + blk_r : plate_u + plate_t;
 
 assert(_detent(90) > latch_detent, "latch_detent too large for the filament bar");
-assert(knuckle_l > 2*lw, "hinge knuckles too short, reduce hinge_knuckles");
+assert(!back_hinge || knuckle_l > 2*lw, "hinge knuckles too short, reduce hinge_knuckles");
 assert(body_ih > 0, "inner height too low for the lid");
 assert(chamfer_h <= floor_t, "chamfer higher than the floor");
 
@@ -234,14 +259,18 @@
     echo(str("WARNING: body_ratio ", body_ratio, " leaves the lid too low, using ",
         body_ih/inner_height));
 echo("body height", seam_z, "mm, lid height", lid_h, "mm, body ratio", body_ih/inner_height);
+echo(str("back: ", back_side, ", latches: ",
+    len(latch_sides) == 0 ? "none" : _join([for (i = latch_sides) side_name[i]], ", "),
+    ", print ", len(latch_sides), len(latch_sides) == 1 ? " latch" : " latches"));
 if (latch_w < latch_width)
-    echo(str("WARNING: latch_width ", latch_width, " is wider than the hinge span, using ",
+    echo(str("WARNING: latch_width ", latch_width, " is wider than the narrowest latched face, using ",
         latch_w));
 if (latch_ratio != latch_middle_ratio)
     echo(str("WARNING: latch_middle_ratio ", latch_middle_ratio,
         " leaves a latch knuckle narrower than 2 extrusions, using ", latch_ratio));
 echo("pin hole", pin_d, "mm, knuckle diameter", 2*knuckle_r, "mm");
-echo("hinge knuckle length", knuckle_l, "mm");
+if (back_hinge)
+    echo("hinge knuckle length", knuckle_l, "mm");
 echo("latch knuckle length: middle", latch_mid, "mm, outer", latch_outer, "mm");
 echo(str("latch style ", latch_style, ", printed ", latch_on_side ? "on its side" : "flat"));
 if (latch_block) {
@@ -254,7 +283,7 @@
     echo("latch plate thickness", plate_t, "mm", plate_lines, "extrusions, width", plate_w, "mm");
 else
     echo("latch plate thickness", plate_t, "mm", latch_layers, "layers, width", plate_w, "mm");
-echo("latch outer face", latch_front_u, "mm from the front");
+echo("latch outer face", latch_front_u, "mm from the box face");
 echo("latch hook reaches", hook_a, "degrees past the bar bottom, detent", _detent(hook_a), "mm");
 if (cont_x < content_depth)
     echo(str("WARNING: content_depth ", content_depth, " is larger than the cell, using ", cont_x));
@@ -263,7 +292,8 @@
 if (cont_z < content_height)
     echo(str("WARNING: content_height ", content_height, " is larger than the inner height, using ",
         cont_z));
-echo("lid sweep clearance over the content in the front row", sweep_margin, "mm");
+if (back_hinge)
+    echo("lid sweep clearance over the content in the front row", sweep_margin, "mm");
 
 // rounded box outline, inset by d
 module _outline(d) {
@@ -289,9 +319,9 @@
     translate([out_x/2, 0, 0]) children();
 }
 
-// frame with u outward from the front face
-module _front() {
-    translate([-out_x/2, 0, 0]) mirror([1, 0, 0]) children();
+// face frame of side i: u outward from the face, y along it
+module _side(i) {
+    rotate([0, 0, side_a[i]]) translate([-side_d[i], 0, 0]) mirror([1, 0, 0]) children();
 }
 
 function _knuckle_y0(i) = -hinge_span/2 + i*(knuckle_l + knuckle_gap);
@@ -301,13 +331,15 @@
         cylinder(d=pin_d, h=out_y + 2, center=true);
 }
 
+// latch pin hole in the face frame
 module _latch_pin() {
-    translate([latch_x, 0, latch_z]) rotate([90, 0, 0])
+    translate([knuckle_u, 0, latch_z]) rotate([90, 0, 0])
         cylinder(d=pin_d, h=latch_w + 2, center=true);
 }
 
+// bar hole in the face frame
 module _bar_pin() {
-    translate([latch_x, 0, bar_z]) rotate([90, 0, 0])
+    translate([knuckle_u, 0, bar_z]) rotate([90, 0, 0])
         cylinder(d=pin_d, h=latch_w + 2, center=true);
 }
 
@@ -341,15 +373,16 @@
                     _outline(skirt_t + cl);
                     _outline(wall_t);
                 }
-            for (i = [0:2:hinge_knuckles - 1])
-                _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l)
-                    _knuckle(seam_z, seam_z - cl);
-            for (y0 = latch_outer_y0)
-                _front() _yslab(y0, y0 + latch_outer) _knuckle(bar_z, bar_z + knuckle_r);
+            if (back_hinge)
+                for (i = [0:2:hinge_knuckles - 1])
+                    _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l)
+                        _knuckle(seam_z, seam_z - cl);
+            for (i = latch_sides, y0 = latch_outer_y0)
+                _side(i) _yslab(y0, y0 + latch_outer) _knuckle(bar_z, bar_z + knuckle_r);
         }
         translate([0, 0, floor_t]) linear_extrude(top_z) _outline(wall_t);
-        _hinge_pin();
-        _bar_pin();
+        if (back_hinge) _hinge_pin();
+        for (i = latch_sides) _side(i) _bar_pin();
     }
     // dividers from the floor to the top of the lip
     translate([0, 0, floor_t - epsilon])
@@ -386,23 +419,24 @@
     difference() {
         union() {
             translate([0, 0, top_z]) mirror([0, 0, 1]) _block(lid_h);
-            for (i = [1:2:hinge_knuckles - 1])
-                _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l)
-                    _knuckle(seam_z, seam_z + cl);
-            for (y0 = latch_outer_y0)
-                _front() _yslab(y0, y0 + latch_outer) _knuckle(latch_z, seam_z + cl);
+            if (back_hinge)
+                for (i = [1:2:hinge_knuckles - 1])
+                    _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l)
+                        _knuckle(seam_z, seam_z + cl);
+            for (i = latch_sides, y0 = latch_outer_y0)
+                _side(i) _yslab(y0, y0 + latch_outer) _knuckle(latch_z, seam_z + cl);
         }
         translate([0, 0, seam_z - epsilon])
             linear_extrude(lid_h - floor_t + epsilon) _outline(wall_t);
         // recess over the lip, one layer higher than the lip
         translate([0, 0, seam_z - epsilon])
             linear_extrude(lip_h + lh + epsilon) _outline(skirt_t);
-        _hinge_pin();
-        _latch_pin();
+        if (back_hinge) _hinge_pin();
+        for (i = latch_sides) _side(i) _latch_pin();
     }
 }
 
-// in assembly position, closed. the plate style has a knuckle and a hook
+// in the face frame, closed. the plate style has a knuckle and a hook
 // in the middle slot, joined by a plate in front of the outer knuckles. the
 // block style is one solid profile across the middle slot
 module latch() {
@@ -485,9 +519,8 @@
     }
 
     difference() {
-        _front()
-            if (latch_block) _yslab(-latch_mid/2, latch_mid/2) _block();
-            else _plate();
+        if (latch_block) _yslab(-latch_mid/2, latch_mid/2) _block();
+        else _plate();
         _latch_pin();
     }
 }
@@ -506,18 +539,25 @@
         cylinder(d=filament_diameter, h=l, center=true);
 }
 
+// the lid turns about the hinge axis with the back hinge, otherwise it
+// lifts straight up. in the face frame, a latch opens with its bottom
+// swinging outward
 module _assembly() {
+    lid_a = back_hinge ? 1.8*preview_lid_open : 0;
+    lid_lift = back_hinge ? 0 : preview_lid_open/100*seam_z;
     color("steelblue") body();
     if (preview_hardware && $preview) {
-        _filament(hinge_x, seam_z, hinge_span);
-        _filament(latch_x, bar_z, latch_w);
+        if (back_hinge) _filament(hinge_x, seam_z, hinge_span);
+        for (i = latch_sides) _side(i) _filament(knuckle_u, bar_z, latch_w);
     }
-    translate([hinge_x, 0, seam_z]) rotate([0, preview_lid_angle, 0])
+    translate([hinge_x, 0, seam_z + lid_lift]) rotate([0, lid_a, 0])
         translate([-hinge_x, 0, -seam_z]) {
             color("lightsteelblue") lid();
-            if (preview_hardware && $preview) _filament(latch_x, latch_z, latch_w);
-            translate([latch_x, 0, latch_z]) rotate([0, preview_latch_angle, 0])
-                translate([-latch_x, 0, -latch_z]) color("orange") latch();
+            for (i = latch_sides) _side(i) {
+                if (preview_hardware && $preview) _filament(knuckle_u, latch_z, latch_w);
+                translate([knuckle_u, 0, latch_z]) rotate([0, -preview_latch_angle, 0])
+                    translate([-knuckle_u, 0, -latch_z]) color("orange") latch();
+            }
         }
     if (preview_content && $preview) _content();
 }
@@ -526,8 +566,8 @@
 // and with preview_cut remove the +x half, the right half seen from the
 // front. exports stay unturned
 module part_view() {
-    cut_s = max(out_x/2 + latch_front_u, top_z)
-        + (preview_lid_angle > 0 ? out_x : 0) + 1;
+    cut_s = max(max(out_x, out_y)/2 + latch_front_u, hinge_x + knuckle_r, top_z)
+        + (preview_lid_open > 0 ? (back_hinge ? out_x : seam_z) : 0) + 1;
     if ($preview)
         difference() {
             rotate([0, 0, 90]) children();
@@ -552,10 +592,10 @@
             // on its side: the width along z, standing on the print bed
             else if (latch_on_side)
                 translate([0, 0, latch_mid/2]) rotate([90, 0, 0])
-                    translate([-latch_x, 0, -latch_z]) latch();
+                    translate([-knuckle_u, 0, -latch_z]) latch();
             // flat: the plate face on the print bed
-            else translate([latch_z, 0, out_x/2 + latch_front_u])
-                rotate([0, -90, 0]) latch();
+            else translate([-latch_z, 0, latch_front_u])
+                rotate([0, 90, 0]) latch();
 }
 
 main();
@@ -566,6 +606,9 @@
     part == "latch" ?
         str(latch_block ? "block" : latch_plate_full ? "plate" : "plate-middle", "_") : "",
     columns > 1 || rows > 1 ? str("c", columns, "r", rows, "_") : "",
+    // latched sides, only when they differ from the front latch and back hinge
+    side_on == [true, false, false, false] ? "" :
+        str("l", _join([for (i = latch_sides) "fblr"[i]]), "_"),
     "w", inner_width, "d", inner_depth, "h", inner_height,
     "b", round(100*body_ih/inner_height),
     // latch middle share, only when it differs from the default