wobblyhead hg > 3dprints > hinged_box
annotate main.scad @ 5:fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
| author | ark |
|---|---|
| date | 2026-11-16 19:26 +0100 |
| parents | 80122df4a6c5 |
| children | f2694e49cec1 |
| rev | line source |
|---|---|
| 0 | 1 // hinged box with filament hinge at the back and a hinged latch in front. |
| 2 // sized by its inner dimensions, e.g. for standing notepad paper. | |
| 3 // x runs front (-x) to back (+x), y left to right, z up. | |
| 4 | |
| 5 /* [part selection] */ | |
| 6 // part to show or export | |
| 7 part = "all"; // [all, body, lid, latch] | |
| 8 // cut away the -y half in preview | |
| 1 | 9 preview_cut = false; |
| 0 | 10 // show a paper stack mockup in the assembly view |
| 11 preview_paper = false; | |
| 12 // show the filament pins and latch bar in the assembly view | |
| 13 preview_hardware = false; | |
| 14 // lid opening angle in the assembly view | |
| 15 preview_lid_angle = 0; // [0:1:180] | |
| 16 // latch opening angle in the assembly view | |
| 17 preview_latch_angle = 0; // [0:1:75] | |
| 18 | |
| 19 /* [box] */ | |
| 20 // inner size front to back | |
| 21 inner_depth = 92; // [10:1:300] | |
| 22 // inner size left to right, the thickness of the paper stack | |
| 23 inner_width = 92; // [10:1:300] | |
| 24 // inner height, floor to lid ceiling | |
| 25 inner_height = 92; // [10:1:300] | |
| 26 // share of the inner height taken by the body | |
| 2 | 27 body_ratio = .85; // [.1:.01:.99] |
| 0 | 28 // radius of the vertical outer edges |
| 29 edge_radius = 2; // [0:.1:10] | |
| 2 | 30 // paper size front to back, for the preview mockup and the lid sweep check |
| 31 paper_depth = 90; // [10:1:300] | |
| 32 // paper height, for the preview mockup and the lid sweep check | |
| 33 paper_height = 90; // [10:1:300] | |
| 0 | 34 |
| 35 /* [walls] */ | |
| 36 // wall thickness in extrusions | |
| 37 wall_lines = 4; // [2:1:10] | |
| 38 // lip thickness in extrusions: the inner part of the body wall, raised into the lid | |
| 39 lip_lines = 2; // [1:1:8] | |
| 40 // lip height in layers | |
| 41 lip_layers = 4; // [1:1:20] | |
| 42 // floor and lid top thickness in layers | |
| 43 floor_layers = 6; // [2:1:20] | |
| 1 | 44 // chamfer height in layers at the build plate edges of body and lid. each layer steps in by one layer height |
| 45 chamfer_layers = 3; // [0:1:10] | |
| 0 | 46 |
| 47 /* [hinge] */ | |
| 48 // diameter of the filament used as hinge pin | |
| 49 filament_diameter = 1.75; // [1:.05:3] | |
| 50 // number of hinge knuckles. the body gets the outer ones | |
| 51 hinge_knuckles = 5; // [3:2:15] | |
| 52 // knuckle wall around the pin hole, in extrusions | |
| 53 hinge_lines = 3; // [2:1:8] | |
| 54 | |
| 55 /* [latch] */ | |
| 56 // latch width | |
| 57 latch_width = 20; // [8:1:100] | |
| 58 // latch plate thickness in layers, printed flat | |
| 59 latch_layers = 8; // [4:1:20] | |
| 60 // radial overlap the hook tip has to pass at the filament bar. 0 for a free swinging latch | |
| 61 latch_detent = .3; // [0:.05:.7] | |
| 62 // grip tab length below the hook | |
| 63 latch_grip = 5; // [0:.5:20] | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
64 // hook wall around the filament bar, in extrusions |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
65 latch_hook_lines = 3; // [2:1:8] |
| 0 | 66 |
| 67 /* [print settings] */ | |
| 68 line_width = .4; // [0:.01:1] | |
| 69 layer_height = .2; // [0:.01:.5] | |
| 70 clearance = .1; // [0:.01:.5] | |
| 71 | |
| 72 module dummy() {} | |
| 73 | |
| 74 epsilon = .001; | |
| 75 $fn = $preview ? 60 : 180; | |
| 76 | |
| 77 lw = line_width; | |
| 78 lh = layer_height; | |
| 79 cl = clearance; | |
| 80 | |
| 81 // walls | |
| 82 wall_t = wall_lines*lw; | |
| 83 // lid wall part that overlaps the lip from outside | |
| 84 skirt_t = (wall_lines - lip_lines)*lw; | |
| 85 lip_h = lip_layers*lh; | |
| 86 floor_t = floor_layers*lh; | |
| 1 | 87 chamfer_h = chamfer_layers*lh; |
| 0 | 88 out_x = inner_depth + 2*wall_t; |
| 89 out_y = inner_width + 2*wall_t; | |
| 90 edge_r = min(edge_radius, min(out_x, out_y)/2 - epsilon); | |
| 91 | |
| 92 // knuckles, shared by hinge and latch | |
| 93 pin_d = filament_diameter + 2*cl; | |
| 94 knuckle_r = pin_d/2 + hinge_lines*lw; | |
| 95 // axis distance from the outer wall face | |
| 96 knuckle_u = knuckle_r + 2*cl; | |
| 97 // axial gap between knuckles | |
| 98 knuckle_gap = 2*cl; | |
| 99 | |
| 2 | 100 // heights. the seam between body and lid sits on a layer boundary. |
| 101 // the lid needs room for the wall around the latch pin hole, which sits | |
| 102 // one knuckle radius plus clearance above the seam, and for one layer of | |
| 103 // skirt wall above the recess over the lip. a body ratio that leaves less | |
| 104 // is lowered to the highest layer that fits | |
| 105 lid_min_h = max(2*knuckle_r + cl, floor_t + lip_h + 2*lh); | |
| 106 body_ih_max = floor((inner_height + floor_t - lid_min_h)/lh)*lh; | |
| 107 body_ih_req = round(body_ratio*inner_height/lh)*lh; | |
| 108 body_ih = min(body_ih_req, body_ih_max); | |
| 109 seam_z = floor_t + body_ih; | |
| 110 top_z = floor_t + inner_height + floor_t; | |
| 111 lid_h = top_z - seam_z; | |
| 112 | |
| 0 | 113 // hinge |
| 114 hinge_x = out_x/2 + knuckle_u; | |
| 115 hinge_span = out_y - 2*edge_r; | |
| 116 knuckle_l = (hinge_span - (hinge_knuckles - 1)*knuckle_gap)/hinge_knuckles; | |
| 117 | |
| 118 // latch. the flap hangs from lid knuckles and its hook wraps around a | |
| 119 // filament bar held by body knuckles straight below the flap axis. | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
120 // in the front frame u runs outward from the front face, z up. |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
121 // a latch wider than the hinge span is narrowed to it |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
122 latch_w = min(latch_width, hinge_span); |
| 0 | 123 latch_x = -out_x/2 - knuckle_u; |
| 124 latch_z = seam_z + knuckle_r + cl; | |
| 125 bar_z = seam_z - cl - knuckle_r; | |
| 126 bar_r = filament_diameter/2; | |
| 127 // distance between flap axis and bar axis | |
| 128 bar_h = latch_z - bar_z; | |
| 129 hook_ri = bar_r + cl; | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
130 hook_ro = hook_ri + latch_hook_lines*lw; |
| 0 | 131 // outer radius of the bar minus radius of the hook tip, both around the |
| 132 // flap axis, for a hook reaching angle b past the bottom of the bar | |
| 133 function _detent(b) = | |
| 134 bar_h + bar_r - norm([hook_ri*sin(b), bar_h + hook_ri*cos(b)]); | |
| 135 // bisection for the angle that gives latch_detent | |
| 136 function _hook_angle(lo, hi, n=40) = | |
| 137 n == 0 ? (lo + hi)/2 : | |
| 138 let(m = (lo + hi)/2) | |
| 139 _detent(m) < latch_detent ? _hook_angle(m, hi, n - 1) : _hook_angle(lo, m, n - 1); | |
| 140 hook_a = _hook_angle(0, 90); | |
| 141 plate_t = latch_layers*lh; | |
| 142 plate_u = knuckle_u + knuckle_r + 2*cl; | |
| 143 plate_bz = bar_z - hook_ro - latch_grip; | |
| 4 | 144 latch_slot = (latch_w - 2*knuckle_gap)/3; |
| 0 | 145 // y start of the two outer latch slots |
| 4 | 146 latch_outer_y0 = [-latch_w/2, latch_slot/2 + knuckle_gap]; |
| 0 | 147 |
| 148 assert(_detent(90) > latch_detent, "latch_detent too large for the filament bar"); | |
| 149 assert(knuckle_l > 2*lw, "hinge knuckles too short, reduce hinge_knuckles"); | |
| 2 | 150 assert(body_ih > 0, "inner height too low for the lid"); |
| 1 | 151 assert(chamfer_h <= floor_t, "chamfer higher than the floor"); |
| 0 | 152 |
| 153 // lid sweep over a centred paper stack: radius of the lowest lid ceiling | |
| 154 // point over the paper minus radius of the paper's front top corner | |
| 155 sweep_margin = | |
| 156 norm([hinge_x + inner_depth/2, lip_h + lh]) | |
| 2 | 157 - norm([hinge_x + paper_depth/2, floor_t + paper_height - seam_z]); |
| 0 | 158 |
| 159 echo("wall thickness", wall_t, "mm", wall_lines, "extrusions"); | |
| 160 echo("lip thickness", wall_t - skirt_t - cl, "mm, height", lip_h, "mm"); | |
| 161 echo("outer size", out_x, out_y, top_z, "mm"); | |
| 1 | 162 echo("chamfer", chamfer_h, "mm", chamfer_layers, "layers"); |
| 2 | 163 if (body_ih < body_ih_req) |
| 164 echo(str("WARNING: body_ratio ", body_ratio, " leaves the lid too low, using ", | |
| 165 body_ih/inner_height)); | |
| 166 echo("body height", seam_z, "mm, lid height", lid_h, "mm, body ratio", body_ih/inner_height); | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
167 if (latch_w < latch_width) |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
168 echo(str("WARNING: latch_width ", latch_width, " is wider than the hinge span, using ", |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
169 latch_w)); |
| 0 | 170 echo("pin hole", pin_d, "mm, knuckle diameter", 2*knuckle_r, "mm"); |
| 171 echo("hinge knuckle length", knuckle_l, "mm"); | |
| 172 echo("latch plate outer face", plate_u + plate_t, "mm from the front"); | |
| 173 echo("latch hook reaches", hook_a, "degrees past the bar bottom, detent", _detent(hook_a), "mm"); | |
| 174 echo("lid sweep clearance over centred paper", sweep_margin, "mm"); | |
| 175 | |
| 176 // rounded box outline, inset by d | |
| 177 module _outline(d) { | |
| 178 r = max(edge_r - d, 0); | |
| 179 offset(r=r) square([out_x - 2*d - 2*r, out_y - 2*d - 2*r], center=true); | |
| 180 } | |
| 181 | |
| 1 | 182 // outer box shape of height h, the bottom edge chamfered in steps of one |
| 183 // layer, each step inset by one layer height | |
| 184 module _block(h) { | |
| 185 for (i = [0:1:chamfer_layers - 1]) | |
| 186 translate([0, 0, i*lh]) linear_extrude(lh + epsilon) _outline((chamfer_layers - i)*lh); | |
| 187 translate([0, 0, chamfer_h]) linear_extrude(h - chamfer_h) _outline(0); | |
| 188 } | |
| 189 | |
| 0 | 190 // extrude a 2d profile drawn in x/z between y0 and y1 |
| 191 module _yslab(y0, y1) { | |
| 192 translate([0, y1, 0]) rotate([90, 0, 0]) linear_extrude(y1 - y0) children(); | |
| 193 } | |
| 194 | |
| 195 // frame with u outward from the back face | |
| 196 module _back() { | |
| 197 translate([out_x/2, 0, 0]) children(); | |
| 198 } | |
| 199 | |
| 200 // frame with u outward from the front face | |
| 201 module _front() { | |
| 202 translate([-out_x/2, 0, 0]) mirror([1, 0, 0]) children(); | |
| 203 } | |
| 204 | |
| 205 function _knuckle_y0(i) = -hinge_span/2 + i*(knuckle_l + knuckle_gap); | |
| 206 | |
| 207 module _hinge_pin() { | |
| 208 translate([hinge_x, 0, seam_z]) rotate([90, 0, 0]) | |
| 209 cylinder(d=pin_d, h=out_y + 2, center=true); | |
| 210 } | |
| 211 | |
| 212 module _latch_pin() { | |
| 213 translate([latch_x, 0, latch_z]) rotate([90, 0, 0]) | |
| 4 | 214 cylinder(d=pin_d, h=latch_w + 2, center=true); |
| 0 | 215 } |
| 216 | |
| 217 module _bar_pin() { | |
| 218 translate([latch_x, 0, bar_z]) rotate([90, 0, 0]) | |
| 4 | 219 cylinder(d=pin_d, h=latch_w + 2, center=true); |
| 0 | 220 } |
| 221 | |
| 222 module body() { | |
| 223 // knuckle at height z with a web down to the wall up to z1, chamfered | |
| 224 // below for printing | |
| 225 module _knuckle(z, z1) { | |
| 226 hull() { | |
| 227 translate([knuckle_u, z]) circle(r=knuckle_r); | |
| 228 translate([-epsilon, z - knuckle_r - knuckle_u]) | |
| 229 square([epsilon, z1 - (z - knuckle_r - knuckle_u)]); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 difference() { | |
| 234 union() { | |
| 1 | 235 _block(seam_z); |
| 0 | 236 translate([0, 0, seam_z - epsilon]) |
| 237 linear_extrude(lip_h + epsilon) | |
| 238 difference() { | |
| 239 _outline(skirt_t + cl); | |
| 240 _outline(wall_t); | |
| 241 } | |
| 242 for (i = [0:2:hinge_knuckles - 1]) | |
| 243 _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l) | |
| 244 _knuckle(seam_z, seam_z - cl); | |
| 245 for (y0 = latch_outer_y0) | |
| 246 _front() _yslab(y0, y0 + latch_slot) _knuckle(bar_z, bar_z + knuckle_r); | |
| 247 } | |
| 248 translate([0, 0, floor_t]) linear_extrude(top_z) _outline(wall_t); | |
| 249 _hinge_pin(); | |
| 250 _bar_pin(); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 // in assembly position, closed | |
| 255 module lid() { | |
| 256 // knuckle at height z with a web up to the wall from z0, chamfered | |
| 2 | 257 // towards the lid top for printing upside down. the web reaches into |
| 258 // the wall by the chamfer height, so where it meets the lid top it | |
| 259 // fills the edge chamfer. a knuckle that would reach past the lid top | |
| 260 // is cut off there and stands on the print bed | |
| 0 | 261 module _knuckle(z, z0) { |
| 2 | 262 web_u = chamfer_h + epsilon; |
| 263 intersection() { | |
| 264 hull() { | |
| 265 translate([knuckle_u, z]) circle(r=knuckle_r); | |
| 266 translate([-web_u, z0]) | |
| 267 square([web_u, z + knuckle_r + knuckle_u - z0]); | |
| 268 } | |
| 269 translate([-web_u - 1, z - knuckle_r - 1]) | |
| 270 square([web_u + knuckle_u + knuckle_r + 2, top_z - (z - knuckle_r - 1)]); | |
| 0 | 271 } |
| 272 } | |
| 273 | |
| 274 difference() { | |
| 275 union() { | |
| 1 | 276 translate([0, 0, top_z]) mirror([0, 0, 1]) _block(lid_h); |
| 0 | 277 for (i = [1:2:hinge_knuckles - 1]) |
| 278 _back() _yslab(_knuckle_y0(i), _knuckle_y0(i) + knuckle_l) | |
| 279 _knuckle(seam_z, seam_z + cl); | |
| 280 for (y0 = latch_outer_y0) | |
| 281 _front() _yslab(y0, y0 + latch_slot) _knuckle(latch_z, seam_z + cl); | |
| 282 } | |
| 283 translate([0, 0, seam_z - epsilon]) | |
| 284 linear_extrude(lid_h - floor_t + epsilon) _outline(wall_t); | |
| 285 // recess over the lip, one layer higher than the lip | |
| 286 translate([0, 0, seam_z - epsilon]) | |
| 287 linear_extrude(lip_h + lh + epsilon) _outline(skirt_t); | |
| 288 _hinge_pin(); | |
| 289 _latch_pin(); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 // in assembly position, closed | |
| 294 module latch() { | |
| 295 // hook around the bar, from the plate side down under the bar and | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
296 // hook_a degrees up its wall side |
| 0 | 297 module _hook() { |
| 298 tip_a = 270 - hook_a; | |
| 299 difference() { | |
|
5
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
300 hull() { |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
301 translate([knuckle_u, bar_z]) circle(r=hook_ro); |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
302 translate([knuckle_u, bar_z - hook_ro]) |
|
fdd022a1bfb1
reworked latch hook reinforcement, now a simple line count
ark
parents:
4
diff
changeset
|
303 square([plate_u - knuckle_u + epsilon, hook_ro]); |
| 0 | 304 } |
| 305 translate([knuckle_u, bar_z]) circle(r=hook_ri); | |
| 306 // open wedge over the top and the wall side of the bar | |
| 307 translate([knuckle_u, bar_z]) | |
| 308 polygon(concat([[0, 0]], | |
| 309 [for (a = [0:10:tip_a]) 2*hook_ro*[cos(a), sin(a)]], | |
| 310 [2*hook_ro*[cos(tip_a), sin(tip_a)]])); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 difference() { | |
| 315 _front() union() { | |
| 4 | 316 _yslab(-latch_w/2, latch_w/2) |
| 0 | 317 translate([plate_u, plate_bz]) square([plate_t, latch_z - plate_bz]); |
| 318 _yslab(-latch_slot/2, latch_slot/2) { | |
| 319 hull() { | |
| 320 translate([knuckle_u, latch_z]) circle(r=knuckle_r); | |
| 321 translate([plate_u, latch_z - knuckle_r]) square([plate_t, knuckle_r]); | |
| 322 } | |
| 323 _hook(); | |
| 324 } | |
| 325 } | |
| 326 _latch_pin(); | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 module _paper() { | |
| 331 stack_t = inner_width - 2; | |
| 332 color("white", .6) | |
| 2 | 333 translate([-paper_depth/2, -stack_t/2, floor_t]) |
| 334 cube([paper_depth, stack_t, paper_height]); | |
| 0 | 335 } |
| 336 | |
| 337 // filament of length l along y at x, z | |
| 338 module _filament(x, z, l) { | |
| 339 color("dimgray") translate([x, 0, z]) rotate([90, 0, 0]) | |
| 340 cylinder(d=filament_diameter, h=l, center=true); | |
| 341 } | |
| 342 | |
| 343 module _assembly() { | |
| 344 color("steelblue") body(); | |
| 345 if (preview_hardware && $preview) { | |
| 346 _filament(hinge_x, seam_z, hinge_span); | |
| 4 | 347 _filament(latch_x, bar_z, latch_w); |
| 0 | 348 } |
| 349 translate([hinge_x, 0, seam_z]) rotate([0, preview_lid_angle, 0]) | |
| 350 translate([-hinge_x, 0, -seam_z]) { | |
| 351 color("lightsteelblue") lid(); | |
| 4 | 352 if (preview_hardware && $preview) _filament(latch_x, latch_z, latch_w); |
| 0 | 353 translate([latch_x, 0, latch_z]) rotate([0, preview_latch_angle, 0]) |
| 354 translate([-latch_x, 0, -latch_z]) color("orange") latch(); | |
| 355 } | |
| 356 if (preview_paper && $preview) _paper(); | |
| 357 } | |
| 358 | |
| 359 module part_view() { | |
| 360 cut_s = max(out_x/2 + plate_u + plate_t, top_z) | |
| 361 + (preview_lid_angle > 0 ? out_x : 0) + 1; | |
| 362 if ($preview && preview_cut) | |
| 363 difference() { | |
| 364 children(); | |
| 365 translate([-cut_s, -cut_s, -cut_s]) cube([2*cut_s, cut_s, 2*cut_s]); | |
| 366 } | |
| 367 else | |
| 368 children(); | |
| 369 } | |
| 370 | |
| 371 module main() { | |
| 372 if (part == "all") | |
| 373 part_view() _assembly(); | |
| 374 else if (part == "body") | |
| 375 part_view() body(); | |
| 376 else if (part == "lid") | |
| 377 part_view() | |
| 378 if ($preview) lid(); | |
| 379 else translate([0, 0, top_z]) rotate([180, 0, 0]) lid(); | |
| 380 else if (part == "latch") | |
| 381 part_view() | |
| 382 if ($preview) latch(); | |
| 383 else translate([latch_z, 0, out_x/2 + plate_u + plate_t]) | |
| 384 rotate([0, -90, 0]) latch(); | |
| 385 } | |
| 386 | |
| 387 main(); | |
| 388 | |
| 389 function paramstring(prefix="", suffix="") = str( | |
| 390 prefix, | |
| 391 part == "all" ? "" : str(part, "_"), | |
| 392 "w", inner_width, "d", inner_depth, "h", inner_height, | |
| 2 | 393 "b", round(100*body_ih/inner_height), |
| 0 | 394 "_lw", round(100*line_width), "lh", round(100*layer_height), |
| 395 suffix); | |
| 396 | |
| 397 echo(paramstring("hinged_box_")); |
