view main.scad @ 0:b5a96b547b01

initial public version
author ark
date 2026-11-03 01:29 +0100
parents
children fe29ace8155e
line wrap: on
line source

// House number: glyphs of a text, printed face down, screwed to a wall
// with distance washers. Glyph outlines and hole positions come from
// glyphs.scad, written by make_glyphs.py.

/* [part selection] */
// part to show or export; the decal needs two-colour mode; the template is
// 2D, for SVG export and make_template.py
part = "all"; // [all, body, decal, washer, template]

/* [layout] */
// whole text in one print, or one glyph per print
layout = "glyph"; // [text, glyph]
// glyph to print in the glyph layout, counted from 0, spaces not counted
glyph_index = 0; // [0:1:30]
// height of the tallest glyph, 0 for the largest that fits
size = 0; // [0:.1:500]
// rotation on the bed in degrees, -1 for the best angle
angle = 0; // [-1:1:90]
// bed size
bed_size = 300; // [100:1:500]
// unusable margin along the bed edges
bed_margin = 10; // [0:.5:50]

/* [glyph] */
// glyph thickness in layers, decal layers included
glyph_layers = 6; // [3:1:60]
// backing behind the glyph, covering its outline and connecting separate parts
backing = false;
// backing thickness in layers
backing_layers = 4; // [1:1:20]

/* [screws] */
// screw diameter
screw_d = 4; // [2:.5:8]
// countersunk holes, 90 degrees
countersink = true;
// screw head diameter
head_d = 8; // [3:.5:16]
// minimum material around the screw head, in line widths, for the fit check
head_wall_lines = 2; // [0:1:10]

/* [two-colour] */
// split into body and decal, for a filament change on a single-colour printer
two_colour = false;
// width of the decal band along the outline
band_width = 4; // [.5:.1:20]

/* [washer] */
// print the washers as part of the body, on the back of the glyph
washers_on_body = false;
// washer wall around the screw hole, in line widths
washer_wall_lines = 8; // [1:1:40]
// washer height, raised to the countersink part inside the washer plus 4 layers
washer_height = 10; // [0:.5:30]

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

module dummy() {}

include <glyphs.scad>

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

lw = line_width;
lh = layer_height;

// thicknesses
glyph_t = glyph_layers * lh;
backing_t = backing ? backing_layers * lh : 0;
total_t = glyph_t + backing_t;
decal_t = 2 * lh;      // 2 layers
frame_t = lh;          // 1 layer

// screw holes: 90 degree countersink, depth is half the diameter difference
hole_d = screw_d + 2 * clearance;
cs_d = head_d + 2 * clearance;
cs_depth = countersink ? (cs_d - hole_d) / 2 : 0;
// part of the countersink that reaches beyond the glyph into the washer
cs_washer = max(0, cs_depth - total_t);

// washer
washer_od = hole_d + 2 * washer_wall_lines * lw;
washer_min = cs_washer + 4 * lh;   // 4 layers below the countersink
washer_h = max(washer_height, washer_min);

// layout and size
n_glyphs = len(g_glyphs);
sel = layout == "text" ? [for (i = [0 : n_glyphs - 1]) i] : [glyph_index];
usable = bed_size - 2 * bed_margin - 4 * lw;   // frame and gap, 1 line width each
best_angle = layout == "text" ? g_max_text[1] : g_max_glyph[1];
print_angle = angle < 0 ? best_angle : angle;
// glyphs are drawn unrotated in the assembly view
ang = part == "all" ? 0 : print_angle;
fit_extent = layout == "text"
    ? _extent([for (g = g_glyphs) for (p = g[4]) [p.x + g[2], p.y]], print_angle)
    : max([for (g = g_glyphs) _extent(g[4], print_angle)]);
max_size = usable / fit_extent * g_size;
final_size = size > 0 ? size : max_size;
k = final_size / g_size;   // scale from the data file to the print

// placement: centre the bounding box of the shown glyphs on the origin
raw_pts = [for (i = sel) for (p = g_glyphs[i][4]) _tf0([p.x + _off(i), p.y])];
bb = [min([for (p = raw_pts) p.x]), min([for (p = raw_pts) p.y]),
      max([for (p = raw_pts) p.x]), max([for (p = raw_pts) p.y])];
centre = [(bb[0] + bb[2]) / 2, (bb[1] + bb[3]) / 2];

// holes of the shown glyphs: [position, largest circle radius at the print size]
holes = [for (i = sel) for (h = g_glyphs[i][5]) [_tf([h.x + _off(i), h.y]), h[2] * k]];
head_r_req = head_d / 2 + head_wall_lines * lw;

assert(glyph_index < n_glyphs, str("glyph_index must be below ", n_glyphs));
assert(part != "decal" || two_colour, "the decal needs two_colour");

echo(str("size: ", final_size, " mm, largest that fits ", max_size, " mm at ",
         print_angle, " degrees"));
echo(str("glyph thickness: ", glyph_t, " mm, with backing ", total_t, " mm"));
echo(str("hole: ", hole_d, " mm, countersink ", cs_d, " mm, depth ", cs_depth,
         " mm, of which in the washer ", cs_washer, " mm"));
echo(str("washer: ", washer_od, " mm outside, ", washer_h, " mm high, wall ",
         washer_wall_lines, " line widths"));
if (size > max_size + .01)
    echo(str("WARNING: size ", size, " mm does not fit the bed, largest is ", max_size, " mm"));
if (washer_height < washer_min)
    echo(str("WARNING: washer height raised to ", washer_min, " mm"));
for (h = holes) if (h[1] < head_r_req)
    echo(str("WARNING: hole at ", h[0], ": largest circle radius ", h[1],
             " mm, head plus wall needs ", head_r_req, " mm"));
if (washers_on_body) for (h = holes) if (h[1] < washer_od / 2)
    echo(str("WARNING: washer at ", h[0], " is cut back to the glyph outline"));

// Size of the bounding box of points rotated by a degrees, along its longer side.
// pts: list of 2D points. a: angle in degrees.
function _extent(pts, a) =
    let (r = [for (p = pts) [p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a)]])
    max(max([for (q = r) q.x]) - min([for (q = r) q.x]),
        max([for (q = r) q.y]) - min([for (q = r) q.y]));

// x offset of glyph i: its place in the text in the text layout, else 0.
function _off(i) = layout == "text" ? g_glyphs[i][2] : 0;

// Point from the data file to the print, before centring: scaled, rotated,
// mirrored for printing face down.
function _tf0(p) =
    let (q = k * p, r = [q.x * cos(ang) - q.y * sin(ang), q.x * sin(ang) + q.y * cos(ang)])
    [-r.x, r.y];

// Point from the data file to its place in the print.
function _tf(p) = _tf0(p) - centre;

// Apply the placement of _tf() to 2D children given in data file coordinates.
module _place() {
    translate(-centre) mirror([1, 0]) rotate(ang) scale(k) children();
}

// Outline of glyph i in data file coordinates, at its place in the layout.
module _glyph2d(i) {
    translate([_off(i), 0]) import(g_glyphs[i][1]);
}

// Backing outline of glyph i in data file coordinates: the outline itself,
// grown and shrunk to connect separate parts, or the convex hull.
module _backing2d(i) {
    r = g_glyphs[i][7];
    if (r > 0) offset(r = -r) offset(r = r) _glyph2d(i);
    else if (r < 0) hull() _glyph2d(i);
    else _glyph2d(i);
}

// Outline of all shown glyphs, placed for printing.
module _placed2d() {
    for (i = sel) _place() _glyph2d(i);
}

// Decal band: the outline minus the outline shrunk by the band width.
module _band2d() {
    difference() {
        _placed2d();
        offset(r = -band_width) _placed2d();
    }
}

// Screw holes and countersinks, opening towards the bed, through washers
// printed on the body.
module _holes() {
    for (h = holes) translate(h[0]) {
        translate([0, 0, -1])
            cylinder(d = hole_d, h = total_t + (washers_on_body ? washer_h : 0) + 2);
        if (countersink)
            translate([0, 0, -epsilon])
                cylinder(d1 = cs_d + 2 * epsilon, d2 = hole_d, h = cs_depth + epsilon);
    }
}

// Frame of one layer and one line width, one line width around the shown
// glyphs. Decal and body both contain it, so the slicer places them alike.
module _frame() {
    linear_extrude(frame_t) difference() {
        translate([bb[0] - centre.x - 2 * lw, bb[1] - centre.y - 2 * lw])
            square([bb[2] - bb[0] + 4 * lw, bb[3] - bb[1] + 4 * lw]);
        translate([bb[0] - centre.x - lw, bb[1] - centre.y - lw])
            square([bb[2] - bb[0] + 2 * lw, bb[3] - bb[1] + 2 * lw]);
    }
}

// Outline of the back of the shown glyphs, placed for printing: the backing
// if there is one, else the glyph outline.
module _back2d() {
    if (backing) for (i = sel) _place() _backing2d(i);
    else _placed2d();
}

// Glyph body in print orientation, face on the bed. In two-colour mode the
// decal band plus one line width is left out of the decal layers. Washers
// printed on the body stand on its back, cut back to the outline of the back
// so they do not overhang.
// frame: include the frame (two-colour mode only).
module body(frame = true) {
    difference() {
        union() {
            linear_extrude(glyph_t) _placed2d();
            if (backing)
                translate([0, 0, glyph_t - epsilon]) linear_extrude(backing_t + epsilon)
                    _back2d();
            if (washers_on_body)
                translate([0, 0, total_t - epsilon]) intersection() {
                    for (h = holes) translate(h[0]) cylinder(d = washer_od, h = washer_h + epsilon);
                    linear_extrude(washer_h + epsilon) _back2d();
                }
        }
        _holes();
        if (two_colour)
            translate([0, 0, -epsilon]) linear_extrude(decal_t + epsilon)
                offset(delta = lw) _band2d();
    }
    if (two_colour && frame) _frame();
}

// Decal in print orientation: the band along the outline, with the frame.
// frame: include the frame.
module decal(frame = true) {
    difference() {
        linear_extrude(decal_t) _band2d();
        _holes();
    }
    if (frame) _frame();
}

// Distance washer, printed with the glyph side up. With countersinks deeper
// than the glyph, the cone continues in the top of the washer.
module washer() {
    difference() {
        cylinder(d = washer_od, h = washer_h);
        translate([0, 0, -1]) cylinder(d = hole_d, h = washer_h + 2);
        if (cs_washer > 0)
            translate([0, 0, washer_h - cs_washer])
                cylinder(d1 = hole_d, d2 = hole_d + 2 * cs_washer + 2 * epsilon,
                         h = cs_washer + epsilon);
    }
}

// Drilling template in 2D, as seen on the wall: all glyphs of the text at
// their place, a circle and cross at each hole, a level line along the
// baseline and a 100 mm scale bar with 10 mm ticks. Everything is made of
// thin strips, so the outlines of the SVG export show each line.
module template() {
    t = .2;   // strip width
    // wall layout: text positions, no rotation, no mirroring
    function pos(g, p) = k * [p.x + g[2], p.y];
    pts = [for (g = g_glyphs) for (p = g[4]) pos(g, p)];
    x0 = min([for (p = pts) p.x]);
    x1 = max([for (p = pts) p.x]);
    y0 = min([for (p = pts) p.y]);
    arm = washer_od / 2 + 5;
    for (g = g_glyphs) {
        scale(k) translate([g[2], 0]) difference() {
            import(g[1]);
            offset(delta = -t / k) import(g[1]);
        }
        for (h = g[5]) translate(pos(g, h)) {
            difference() {
                circle(d = hole_d);
                circle(d = hole_d - 2 * t);
            }
            square([2 * arm, t], center = true);
            square([t, 2 * arm], center = true);
        }
    }
    // level line along the baseline
    translate([x0 - 20, -t / 2]) square([x1 - x0 + 40, t]);
    // scale bar below the text
    translate([x0, y0 - 20]) {
        square([100, t]);
        for (i = [0 : 10])
            translate([10 * i - t / 2, 0]) square([t, i % 5 == 0 ? 6 : 3]);
    }
}

// Assembly view: glyphs face up as on the wall, washers behind the holes.
module assembly() {
    rotate([0, 180, 0]) {
        color("lightgray") body(frame = false);
        if (two_colour) color("firebrick") decal(frame = false);
        if (!washers_on_body)
            color("steelblue") for (h = holes)
                translate([h[0].x, h[0].y, total_t + washer_h]) mirror([0, 0, 1]) washer();
    }
}

// Output the selected part.
module main() {
    if (part == "all") assembly();
    else if (part == "body") body();
    else if (part == "decal") decal();
    else if (part == "washer") washer();
    else if (part == "template") template();
}

main();

// Export file name without extension, see the print settings for lw and lh.
// prefix: project name with trailing underscore. suffix: appended as is.
function paramstring(prefix = "", suffix = "") = str(
    prefix,
    part == "all" ? "" : str(part, "_"),
    part == "template" ? str(g_name, "_s", round(10 * final_size))
    : part == "washer"
        ? str(countersink ? "cs_" : "", "d", round(10 * screw_d), "w", washer_wall_lines,
              "h", round(10 * washer_h), countersink ? str("c", round(10 * cs_washer)) : "")
        : str(g_name, "_", layout == "glyph" ? str("g", glyph_index, "_") : "",
              two_colour ? "2c_" : "", backing ? "bk_" : "", countersink ? "cs_" : "",
              washers_on_body ? str("wh", round(10 * washer_h), "_") : "",
              "s", round(10 * final_size), "a", round(print_angle), "t", glyph_layers),
    "_lw", round(100 * line_width), "lh", round(100 * layer_height),
    suffix);

echo(paramstring("house_number_"));