view main.scad @ 9:4b89f95689a8

preview: turn the model so the front faces the default camera part_view turns the preview 90 degrees about z so the front faces -y. preview_cut now removes the +x half of the turned model, the right half seen from the front, which keeps the hinge and latch profiles in the cut face. Exports are not turned.
author ark
date 2026-11-17 12:37 +0100
parents ac8f2ecb6c18
children a7a1b12fd6bf
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.
// the preview turns the model 90 degrees so the front faces -y.

/* [part selection] */
// part to show or export
part = "all"; // [all, body, lid, latch]
// cut away the +x half in preview, the right half seen from the front
preview_cut = false;
// show a content mockup in each cell in the assembly view
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
preview_latch_angle = 0; // [0:1:75]

/* [box] */
// inner size front to back, of one cell
inner_depth = 92; // [10:.1:300]
// inner size left to right of one cell
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]

/* [dividers] */
// number of cells left to right. inner_width is the width of one cell
columns = 1; // [1:1:10]
// number of cells front to back. inner_depth is the depth of one cell
rows = 1; // [1:1:10]
// divider wall thickness in extrusions
divider_lines = 2; // [1:1:10]

/* [content] */
// content size front to back, for the preview mockup and the lid sweep check. at most the cell depth
content_depth = 90; // [1:.1:300]
// content size left to right, for the preview mockup. at most the cell width
content_width = 90; // [1:.1:300]
// content height, for the preview mockup and the lid sweep check. at most the inner height
content_height = 90; // [1:.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.85; // [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 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 = 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]
// plate style: plate across the full latch width, printed flat. otherwise only as wide as the middle knuckle, printed standing on its side
latch_plate_full = true;
// latch plate thickness in layers. a plate printed on its side is rounded up to whole extrusions
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 length below the hook: the plate's grip tab, or the block's extension
latch_grip = 5; // [0:.5:20]
// hook wall around the filament bar, in extrusions
latch_hook_lines = 3; // [2:1:8]

/* [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;
// dividers and the total inner size over all cells
div_t = divider_lines*lw;
in_x = rows*inner_depth + (rows - 1)*div_t;
in_y = columns*inner_width + (columns - 1)*div_t;
out_x = in_x + 2*wall_t;
out_y = in_y + 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 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;
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 + latch_hook_lines*lw;
// outer radius of the bar minus radius of the hook tip, both around the
// latch 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);

// 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
latch_span = latch_w - 2*knuckle_gap;
slot_min = 2*lw;
latch_ratio = min(max(latch_middle_ratio, slot_min/latch_span), 1 - 2*slot_min/latch_span);
latch_mid = latch_ratio*latch_span;
latch_outer = (latch_span - latch_mid)/2;
// y start of the two outer latch slots
latch_outer_y0 = [-latch_w/2, latch_mid/2 + knuckle_gap];

// the latch prints standing on its side, its profile in the print plane,
// unless it is a plate across the full latch width
latch_block = latch_style == "block";
latch_on_side = latch_block || !latch_plate_full;

// plate style. on its side, the thickness is rounded up to whole
// extrusions; the small margin keeps an exact multiple from rounding up
plate_w = latch_on_side ? latch_mid : latch_w;
plate_lines = ceil(latch_layers*lh/lw - .001);
plate_t = latch_on_side ? plate_lines*lw : latch_layers*lh;
plate_u = knuckle_u + knuckle_r + 2*cl;
plate_bz = bar_z - hook_ro - latch_grip;

// block style: the profile of the body knuckles, reaching from the latch
// knuckle down past the hook by latch_grip. blk_r is the radius of the
// rounded front, blk_back the gap between back face and wall, blk_cz the
// centre of the lowest round and blk_bz the bottom end of the back face
blk_r = max(knuckle_r, hook_ro);
blk_back = 2*cl;
blk_cz = bar_z - latch_grip;
blk_bz = blk_cz - blk_r - (knuckle_u - blk_back);
// fingernail notch in the back face at the bottom edge: 3 extrusions
// deep, 8 extrusions high, kept 2 extrusions below the hook
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
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(body_ih > 0, "inner height too low for the lid");
assert(chamfer_h <= floor_t, "chamfer higher than the floor");

// cell centres: x for row i from the front, y for column j from the left
function _cell_x(i) = -in_x/2 + inner_depth/2 + i*(inner_depth + div_t);
function _cell_y(j) = -in_y/2 + inner_width/2 + j*(inner_width + div_t);

// content, clamped to one cell
cont_x = min(content_depth, inner_depth);
cont_y = min(content_width, inner_width);
cont_z = min(content_height, inner_height);

// lid sweep over the content centred in the front row, the one furthest
// from the hinge: radius of the lowest lid ceiling point over the content
// minus radius of the content's front top corner
sweep_margin =
    norm([hinge_x + in_x/2, lip_h + lh])
    - norm([hinge_x - _cell_x(0) + cont_x/2, floor_t + cont_z - seam_z]);

echo("wall thickness", wall_t, "mm", wall_lines, "extrusions");
echo("lip thickness", wall_t - skirt_t - cl, "mm, height", lip_h, "mm");
if (columns > 1 || rows > 1)
    echo("divider thickness", div_t, "mm", divider_lines, "extrusions");
echo("inner size", in_x, in_y, inner_height, "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);
if (latch_w < latch_width)
    echo(str("WARNING: latch_width ", latch_width, " is wider than the hinge span, 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");
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) {
    echo("latch block length below the hook", latch_grip, "mm");
    if (nail_h > 0)
        echo("fingernail notch", nail_d, "mm deep, reaching", nail_h, "mm above the bottom edge");
    else
        echo("WARNING: latch_grip too short for the fingernail notch");
} else if (latch_on_side)
    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 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));
if (cont_y < content_width)
    echo(str("WARNING: content_width ", content_width, " is larger than the cell, using ", cont_y));
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");

// 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, convexity=4) 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_w + 2, center=true);
}

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

module body() {
    // divider walls between the cells, reaching into the outer wall
    module _dividers() {
        for (j = [1:1:columns - 1])
            translate([-in_x/2 - epsilon, _cell_y(j) - inner_width/2 - div_t])
                square([in_x + 2*epsilon, div_t]);
        for (i = [1:1:rows - 1])
            translate([_cell_x(i) - inner_depth/2 - div_t, -in_y/2 - epsilon])
                square([div_t, in_y + 2*epsilon]);
    }

    // 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_outer) _knuckle(bar_z, bar_z + knuckle_r);
        }
        translate([0, 0, floor_t]) linear_extrude(top_z) _outline(wall_t);
        _hinge_pin();
        _bar_pin();
    }
    // dividers from the floor to the top of the lip
    translate([0, 0, floor_t - epsilon])
        linear_extrude(seam_z + lip_h - floor_t + epsilon) _dividers();
}

// 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 joins the wall
    // through a strip one epsilon thick just inside the outer face, so it
    // stays out of the body wall below the seam. where the web reaches the
    // edge chamfer at the lid top, a block fills the 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_z1 = z + knuckle_r + knuckle_u;
        fill_z0 = max(z0, top_z - chamfer_h);
        intersection() {
            union() {
                hull() {
                    translate([knuckle_u, z]) circle(r=knuckle_r);
                    translate([-epsilon, z0]) square([epsilon, web_z1 - z0]);
                }
                if (web_z1 > fill_z0)
                    translate([-chamfer_h - epsilon, fill_z0])
                        square([chamfer_h + epsilon, web_z1 - fill_z0]);
            }
            translate([-epsilon - 1, z - knuckle_r - 1])
                square([epsilon + 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_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();
    }
}

// in assembly position, 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() {
    tip_a = 270 - hook_a;
    axis = [knuckle_u, latch_z];
    bar = [knuckle_u, bar_z];
    // length of the hook tip line from the bar centre to past the back face
    tip_l = (knuckle_u + 1)/max(abs(cos(tip_a)), .1);

    // sector of radius r from angle a0 to a1, counterclockwise
    module _sector(r, a0, a1) {
        polygon(concat([[0, 0]],
            [for (a = [a0:10:a1]) r*[cos(a), sin(a)]],
            [r*[cos(a1), sin(a1)]]));
    }

    // hook around the bar, from the plate side down under the bar and
    // hook_a degrees up its wall side
    module _hook() {
        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(bar) circle(r=hook_ri);
            // open wedge over the top and the wall side of the bar
            translate(bar) _sector(2*hook_ro, 0, tip_a);
        }
    }

    module _plate() {
        _yslab(-plate_w/2, plate_w/2)
            translate([plate_u, plate_bz]) square([plate_t, latch_z - plate_bz]);
        _yslab(-latch_mid/2, latch_mid/2) {
            hull() {
                translate(axis) circle(r=knuckle_r);
                translate([plate_u, latch_z - knuckle_r]) square([plate_t, knuckle_r]);
            }
            _hook();
        }
    }

    // profile of the block: rounded at the front, the back face flat along
    // the wall, sloped at the bottom like the body knuckles
    module _block() {
        difference() {
            intersection() {
                hull() {
                    translate(axis) circle(r=knuckle_r);
                    translate(bar) circle(r=blk_r);
                    translate([knuckle_u, blk_cz]) circle(r=blk_r);
                    translate([blk_back, blk_bz]) square([epsilon, latch_z - blk_bz]);
                }
                // keep the back face off the wall where the hook is larger
                // than the knuckle
                translate([blk_back, blk_bz - 1])
                    square([knuckle_u + blk_r + 1, latch_z + knuckle_r - blk_bz + 2]);
            }
            translate(bar) circle(r=hook_ri);
            // opening for the bar, leaving the knuckle ring and the front
            // wall: from the knuckle down to the bar, front to the far side
            // of the bar hole, and on the wall side down to the line of the
            // hook tip face, which runs on through the bar centre to the
            // back face
            difference() {
                polygon([
                    bar,
                    bar + tip_l*[cos(tip_a), sin(tip_a)],
                    [-1, latch_z],
                    [knuckle_u + hook_ri, latch_z],
                    [knuckle_u + hook_ri, bar_z]]);
                translate(axis) circle(r=knuckle_r);
            }
            // fingernail notch
            if (nail_h > 0)
                translate([blk_back - epsilon, blk_bz - epsilon])
                    square([nail_d + epsilon, nail_h + epsilon]);
        }
    }

    difference() {
        _front()
            if (latch_block) _yslab(-latch_mid/2, latch_mid/2) _block();
            else _plate();
        _latch_pin();
    }
}

// content block centred in each cell
module _content() {
    color("white", .6)
        for (i = [0:1:rows - 1], j = [0:1:columns - 1])
            translate([_cell_x(i) - cont_x/2, _cell_y(j) - cont_y/2, floor_t])
                cube([cont_x, cont_y, cont_z]);
}

// 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_w);
    }
    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_w);
            translate([latch_x, 0, latch_z]) rotate([0, preview_latch_angle, 0])
                translate([-latch_x, 0, -latch_z]) color("orange") latch();
        }
    if (preview_content && $preview) _content();
}

// in preview, turn the model so its front faces the default camera at -y,
// 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;
    if ($preview)
        difference() {
            rotate([0, 0, 90]) children();
            if (preview_cut) translate([0, -cut_s, -cut_s]) cube([cut_s, 2*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();
            // 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();
            // flat: the plate face on the print bed
            else translate([latch_z, 0, out_x/2 + latch_front_u])
                rotate([0, -90, 0]) latch();
}

main();

function paramstring(prefix="", suffix="") = str(
    prefix,
    part == "all" ? "" : str(part, "_"),
    part == "latch" ?
        str(latch_block ? "block" : latch_plate_full ? "plate" : "plate-middle", "_") : "",
    columns > 1 || rows > 1 ? str("c", columns, "r", rows, "_") : "",
    "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
    round(100*latch_ratio) == 33 ? "" : str("lm", round(100*latch_ratio)),
    "_lw", round(100*line_width), "lh", round(100*layer_height),
    suffix);

echo(paramstring("hinged_box_"));