view main.scad @ 3:a9b270c1da96

doc update
author ark
date 2026-11-07 03:17 +0100
parents e8b0aed6e3d8
children 80122df4a6c5
line wrap: on
line source

// 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.

/* [part selection] */
// part to show or export
part = "all"; // [all, body, lid, latch]
// cut away the -y half in preview
preview_cut = false;
// show a paper stack mockup in the assembly view
preview_paper = 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
preview_latch_angle = 0; // [0:1:75]

/* [box] */
// inner size front to back
inner_depth = 92; // [10:1:300]
// inner size left to right, the thickness of the paper stack
inner_width = 92; // [10:1:300]
// inner height, floor to lid ceiling
inner_height = 92; // [10:1:300]
// share of the inner height taken by the body
body_ratio = .85; // [.1:.01:.99]
// radius of the vertical outer edges
edge_radius = 2; // [0:.1:10]
// paper size front to back, for the preview mockup and the lid sweep check
paper_depth = 90; // [10:1:300]
// paper height, for the preview mockup and the lid sweep check
paper_height = 90; // [10:1:300]

/* [walls] */
// wall thickness in extrusions
wall_lines = 4; // [2:1:10]
// lip thickness in extrusions: the inner part of the body wall, raised into the lid
lip_lines = 2; // [1:1:8]
// lip height in layers
lip_layers = 4; // [1:1:20]
// floor and lid top thickness in layers
floor_layers = 6; // [2:1:20]
// chamfer height in layers at the build plate edges of body and lid. each layer steps in by one layer height
chamfer_layers = 3; // [0:1:10]

/* [hinge] */
// diameter of the filament used as hinge pin
filament_diameter = 1.75; // [1:.05:3]
// number of hinge knuckles. the body gets the outer ones
hinge_knuckles = 5; // [3:2:15]
// knuckle wall around the pin hole, in extrusions
hinge_lines = 3; // [2:1:8]

/* [latch] */
// latch width
latch_width = 20; // [8:1:100]
// latch plate thickness in layers, printed flat
latch_layers = 8; // [4:1:20]
// radial overlap the hook tip has to pass at the filament bar. 0 for a free swinging latch
latch_detent = .3; // [0:.05:.7]
// grip tab length below the hook
latch_grip = 5; // [0:.5:20]

/* [print settings] */
line_width = .4; // [0:.01:1]
layer_height = .2; // [0:.01:.5]
clearance = .1; // [0:.01:.5]

module dummy() {}

epsilon = .001;
$fn = $preview ? 60 : 180;

lw = line_width;
lh = layer_height;
cl = clearance;

// walls
wall_t = wall_lines*lw;
// lid wall part that overlaps the lip from outside
skirt_t = (wall_lines - lip_lines)*lw;
lip_h = lip_layers*lh;
floor_t = floor_layers*lh;
chamfer_h = chamfer_layers*lh;
out_x = inner_depth + 2*wall_t;
out_y = inner_width + 2*wall_t;
edge_r = min(edge_radius, min(out_x, out_y)/2 - epsilon);

// knuckles, shared by hinge and latch
pin_d = filament_diameter + 2*cl;
knuckle_r = pin_d/2 + hinge_lines*lw;
// axis distance from the outer wall face
knuckle_u = knuckle_r + 2*cl;
// axial gap between knuckles
knuckle_gap = 2*cl;

// heights. the seam between body and lid sits on a layer boundary.
// the lid needs room for the wall around the latch pin hole, which sits
// one knuckle radius plus clearance above the seam, and for one layer of
// skirt wall above the recess over the lip. a body ratio that leaves less
// is lowered to the highest layer that fits
lid_min_h = max(2*knuckle_r + cl, floor_t + lip_h + 2*lh);
body_ih_max = floor((inner_height + floor_t - lid_min_h)/lh)*lh;
body_ih_req = round(body_ratio*inner_height/lh)*lh;
body_ih = min(body_ih_req, body_ih_max);
seam_z = floor_t + body_ih;
top_z = floor_t + inner_height + floor_t;
lid_h = top_z - seam_z;

// 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;

// latch. the flap hangs from lid knuckles and its hook wraps around a
// filament bar held by body knuckles straight below the flap axis.
// in the front frame u runs outward from the front face, z up
latch_x = -out_x/2 - knuckle_u;
latch_z = seam_z + knuckle_r + cl;
bar_z = seam_z - cl - knuckle_r;
bar_r = filament_diameter/2;
// distance between flap axis and bar axis
bar_h = latch_z - bar_z;
hook_ri = bar_r + cl;
hook_ro = hook_ri + hinge_lines*lw;
// outer radius of the bar minus radius of the hook tip, both around the
// flap axis, for a hook reaching angle b past the bottom of the bar
function _detent(b) =
    bar_h + bar_r - norm([hook_ri*sin(b), bar_h + hook_ri*cos(b)]);
// bisection for the angle that gives latch_detent
function _hook_angle(lo, hi, n=40) =
    n == 0 ? (lo + hi)/2 :
    let(m = (lo + hi)/2)
    _detent(m) < latch_detent ? _hook_angle(m, hi, n - 1) : _hook_angle(lo, m, n - 1);
hook_a = _hook_angle(0, 90);
plate_t = latch_layers*lh;
plate_u = knuckle_u + knuckle_r + 2*cl;
plate_bz = bar_z - hook_ro - latch_grip;
latch_slot = (latch_width - 2*knuckle_gap)/3;
// y start of the two outer latch slots
latch_outer_y0 = [-latch_width/2, latch_slot/2 + knuckle_gap];

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(latch_width <= hinge_span, "latch wider than the box front");
assert(body_ih > 0, "inner height too low for the lid");
assert(chamfer_h <= floor_t, "chamfer higher than the floor");

// lid sweep over a centred paper stack: radius of the lowest lid ceiling
// point over the paper minus radius of the paper's front top corner
sweep_margin =
    norm([hinge_x + inner_depth/2, lip_h + lh])
    - norm([hinge_x + paper_depth/2, floor_t + paper_height - seam_z]);

echo("wall thickness", wall_t, "mm", wall_lines, "extrusions");
echo("lip thickness", wall_t - skirt_t - cl, "mm, height", lip_h, "mm");
echo("outer size", out_x, out_y, top_z, "mm");
echo("chamfer", chamfer_h, "mm", chamfer_layers, "layers");
if (body_ih < body_ih_req)
    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("pin hole", pin_d, "mm, knuckle diameter", 2*knuckle_r, "mm");
echo("hinge knuckle length", knuckle_l, "mm");
echo("latch plate outer face", plate_u + plate_t, "mm from the front");
echo("latch hook reaches", hook_a, "degrees past the bar bottom, detent", _detent(hook_a), "mm");
echo("lid sweep clearance over centred paper", sweep_margin, "mm");

// rounded box outline, inset by d
module _outline(d) {
    r = max(edge_r - d, 0);
    offset(r=r) square([out_x - 2*d - 2*r, out_y - 2*d - 2*r], center=true);
}

// outer box shape of height h, the bottom edge chamfered in steps of one
// layer, each step inset by one layer height
module _block(h) {
    for (i = [0:1:chamfer_layers - 1])
        translate([0, 0, i*lh]) linear_extrude(lh + epsilon) _outline((chamfer_layers - i)*lh);
    translate([0, 0, chamfer_h]) linear_extrude(h - chamfer_h) _outline(0);
}

// extrude a 2d profile drawn in x/z between y0 and y1
module _yslab(y0, y1) {
    translate([0, y1, 0]) rotate([90, 0, 0]) linear_extrude(y1 - y0) children();
}

// frame with u outward from the back face
module _back() {
    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();
}

function _knuckle_y0(i) = -hinge_span/2 + i*(knuckle_l + knuckle_gap);

module _hinge_pin() {
    translate([hinge_x, 0, seam_z]) rotate([90, 0, 0])
        cylinder(d=pin_d, h=out_y + 2, center=true);
}

module _latch_pin() {
    translate([latch_x, 0, latch_z]) rotate([90, 0, 0])
        cylinder(d=pin_d, h=latch_width + 2, center=true);
}

module _bar_pin() {
    translate([latch_x, 0, bar_z]) rotate([90, 0, 0])
        cylinder(d=pin_d, h=latch_width + 2, center=true);
}

module body() {
    // knuckle at height z with a web down to the wall up to z1, chamfered
    // below for printing
    module _knuckle(z, z1) {
        hull() {
            translate([knuckle_u, z]) circle(r=knuckle_r);
            translate([-epsilon, z - knuckle_r - knuckle_u])
                square([epsilon, z1 - (z - knuckle_r - knuckle_u)]);
        }
    }

    difference() {
        union() {
            _block(seam_z);
            translate([0, 0, seam_z - epsilon])
                linear_extrude(lip_h + epsilon)
                difference() {
                    _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_slot) _knuckle(bar_z, bar_z + knuckle_r);
        }
        translate([0, 0, floor_t]) linear_extrude(top_z) _outline(wall_t);
        _hinge_pin();
        _bar_pin();
    }
}

// in assembly position, closed
module lid() {
    // knuckle at height z with a web up to the wall from z0, chamfered
    // towards the lid top for printing upside down. the web reaches into
    // the wall by the chamfer height, so where it meets the lid top it
    // fills the edge chamfer. a knuckle that would reach past the lid top
    // is cut off there and stands on the print bed
    module _knuckle(z, z0) {
        web_u = chamfer_h + epsilon;
        intersection() {
            hull() {
                translate([knuckle_u, z]) circle(r=knuckle_r);
                translate([-web_u, z0])
                    square([web_u, z + knuckle_r + knuckle_u - z0]);
            }
            translate([-web_u - 1, z - knuckle_r - 1])
                square([web_u + knuckle_u + knuckle_r + 2, top_z - (z - knuckle_r - 1)]);
        }
    }

    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_slot) _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();
    }
}

// in assembly position, closed
module latch() {
    // hook around the bar, from the plate side down under the bar and
    // hook_a degrees up its wall side
    module _hook() {
        tip_a = 270 - hook_a;
        difference() {
            hull() {
                translate([knuckle_u, bar_z]) circle(r=hook_ro);
                translate([knuckle_u, bar_z - hook_ro])
                    square([plate_u - knuckle_u + epsilon, hook_ro]);
            }
            translate([knuckle_u, bar_z]) circle(r=hook_ri);
            // open wedge over the top and the wall side of the bar
            translate([knuckle_u, bar_z])
                polygon(concat([[0, 0]],
                    [for (a = [0:10:tip_a]) 2*hook_ro*[cos(a), sin(a)]],
                    [2*hook_ro*[cos(tip_a), sin(tip_a)]]));
        }
    }

    difference() {
        _front() union() {
            _yslab(-latch_width/2, latch_width/2)
                translate([plate_u, plate_bz]) square([plate_t, latch_z - plate_bz]);
            _yslab(-latch_slot/2, latch_slot/2) {
                hull() {
                    translate([knuckle_u, latch_z]) circle(r=knuckle_r);
                    translate([plate_u, latch_z - knuckle_r]) square([plate_t, knuckle_r]);
                }
                _hook();
            }
        }
        _latch_pin();
    }
}

module _paper() {
    stack_t = inner_width - 2;
    color("white", .6)
        translate([-paper_depth/2, -stack_t/2, floor_t])
            cube([paper_depth, stack_t, paper_height]);
}

// filament of length l along y at x, z
module _filament(x, z, l) {
    color("dimgray") translate([x, 0, z]) rotate([90, 0, 0])
        cylinder(d=filament_diameter, h=l, center=true);
}

module _assembly() {
    color("steelblue") body();
    if (preview_hardware && $preview) {
        _filament(hinge_x, seam_z, hinge_span);
        _filament(latch_x, bar_z, latch_width);
    }
    translate([hinge_x, 0, seam_z]) rotate([0, preview_lid_angle, 0])
        translate([-hinge_x, 0, -seam_z]) {
            color("lightsteelblue") lid();
            if (preview_hardware && $preview) _filament(latch_x, latch_z, latch_width);
            translate([latch_x, 0, latch_z]) rotate([0, preview_latch_angle, 0])
                translate([-latch_x, 0, -latch_z]) color("orange") latch();
        }
    if (preview_paper && $preview) _paper();
}

module part_view() {
    cut_s = max(out_x/2 + plate_u + plate_t, top_z)
        + (preview_lid_angle > 0 ? out_x : 0) + 1;
    if ($preview && preview_cut)
        difference() {
            children();
            translate([-cut_s, -cut_s, -cut_s]) cube([2*cut_s, cut_s, 2*cut_s]);
        }
    else
        children();
}

module main() {
    if (part == "all")
        part_view() _assembly();
    else if (part == "body")
        part_view() body();
    else if (part == "lid")
        part_view()
            if ($preview) lid();
            else translate([0, 0, top_z]) rotate([180, 0, 0]) lid();
    else if (part == "latch")
        part_view()
            if ($preview) latch();
            else translate([latch_z, 0, out_x/2 + plate_u + plate_t])
                rotate([0, -90, 0]) latch();
}

main();

function paramstring(prefix="", suffix="") = str(
    prefix,
    part == "all" ? "" : str(part, "_"),
    "w", inner_width, "d", inner_depth, "h", inner_height,
    "b", round(100*body_ih/inner_height),
    "_lw", round(100*line_width), "lh", round(100*layer_height),
    suffix);

echo(paramstring("hinged_box_"));