"""Board enclosure generator (3DBonanza, 2026-11-29). Run under freecadcmd: SPEC=specs/pi5.json OUT=out/pi5 freecadcmd encl.py
Board frame: x along the board length L (0..L), y along its width W (0..W), z = 0 at the PCB's BOTTOM face.
Sides: S = edge y=0, N = edge y=W, W = edge x=0, E = edge x=L. Every board number in a spec comes from the maker's own
drawing / STEP / board file (see the spec's "sources"); every number in DESIGN below is OUR choice, stated in the blueprint.
Parts:
base - tray: floor, walls, board supports (standoffs with screw pilots, or flexible snap rails for boards without
holes), port openings, wall vents, a rebate round the top inside edge with a snap ridge on its long sides,
and a finger notch to lever the lid out.
lid - flat plate that clicks into the rebate (top flush with the walls; a groove in its edge takes the ridge);
Youblob logo + "#MakersGonnaMake" raised on top. Printed flat, logo up: no supports for either part.
Needs FreeCAD 1.0 or later (freecadcmd) and the DejaVu Sans Bold font (DESIGN["font"]); nothing else.
"""
import os, json, math
import FreeCAD as App, Part
from FreeCAD import Vector as V
DESIGN = {
"clear": 0.5, # gap board edge -> wall
"wall": 2.0,
"floor": 2.0,
"lid_t": 2.0, # lid plate thickness = rebate depth
"rebate": 1.0, # rebate width (wall is 1.0 mm thick beside the lid)
"lid_gap": 0.15, # lid-to-rebate clearance per side
"port_margin": 0.6, # extra round every port opening
"plug_margin": 2.5, # extra round a cable socket's opening, from the socket face outwards (room for the plug body)
"top_gap": 1.0,
"snap_bead": 0.4, # lid snap: ridge on the rebate wall, sticks out this far (0.25 mm past the lid edge)
"snap_groove": 0.5, # matching V-groove in the lid edge # gap between the tallest part and the lid underside
"relief": 0.6, # logo / text height on the lid
"font": "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
"text": "#MakersGonnaMake",
}
HERE = os.path.dirname(os.path.abspath(__file__))
# Youblob logo glyph, traced from the logo image and simplified (unit square, centred on 0,0): outline + holes
LOGO_GLYPH = {"ext":[[-0.2754,-0.1179],[-0.2754,0.3123],[-0.2711,0.3167],[-0.1645,0.3167],[-0.1601,0.3124],[-0.1687,0.2987],[-0.1687,0.1368],[-0.1652,0.1298],[-0.1568,0.1281],[-0.1179,0.148],[-0.0904,0.1583],[-0.0559,0.1669],[-0.0111,0.1721],[0.0353,0.1704],[0.0663,0.1652],[0.1024,0.1549],[0.1559,0.1307],[0.1583,0.1344],[0.1584,0.3124],[0.1627,0.3167],[0.2675,0.3167],[0.2719,0.3124],[0.2651,0.3004],[0.2651,-0.1058],[0.2616,-0.1299],[0.2513,-0.1661],[0.2306,-0.2091],[0.2083,-0.2401],[0.173,-0.2754],[0.1437,-0.296],[0.1145,-0.3115],[0.0766,-0.3253],[0.0473,-0.3322],[0.0163,-0.3356],[-0.0198,-0.3356],[-0.0663,-0.3287],[-0.0955,-0.3201],[-0.1489,-0.2943],[-0.1799,-0.2719],[-0.2048,-0.2487],[-0.2306,-0.2177],[-0.2633,-0.1661],[-0.2702,-0.1454]],"holes":[[[-0.1687,-0.0904],[-0.1633,-0.1208],[-0.1508,-0.1512],[-0.1382,-0.169],[-0.1016,-0.2005],[-0.0768,-0.2164],[-0.0468,-0.2287],[-0.022,-0.2341],[0.0202,-0.2341],[0.0451,-0.2287],[0.0751,-0.2164],[0.0998,-0.2005],[0.1265,-0.1739],[0.1372,-0.1577],[0.153,-0.1226],[0.1583,-0.0874],[0.1583,-0.0572],[0.1549,-0.0473],[0.153,-0.0272],[0.1407,-0.0006],[0.1331,0.0106],[0.103,0.0389],[0.0679,0.0582],[0.0468,0.0652],[0.0168,0.0705],[-0.0151,0.0705],[-0.0451,0.0652],[-0.0661,0.0582],[-0.0906,0.046],[-0.1153,0.0284],[-0.1314,0.0124],[-0.1458,-0.0075],[-0.1598,-0.0354],[-0.1669,-0.0603]]]}
def rrect(x0, y0, x1, y1, r, z=0.0):
"""Closed rounded-rectangle wire."""
r = max(0.0, min(r, (x1 - x0) / 2 - 1e-3, (y1 - y0) / 2 - 1e-3))
if r < 1e-3:
return Part.makePolygon([V(x0, y0, z), V(x1, y0, z), V(x1, y1, z), V(x0, y1, z), V(x0, y0, z)])
e = []
pts = [(x1 - r, y0 + r, -90), (x1 - r, y1 - r, 0), (x0 + r, y1 - r, 90), (x0 + r, y0 + r, 180)]
corners = []
for cx, cy, a in pts:
a0 = math.radians(a)
corners.append(Part.Arc(V(cx + r * math.cos(a0), cy + r * math.sin(a0), z),
V(cx + r * math.cos(a0 + math.pi / 4), cy + r * math.sin(a0 + math.pi / 4), z),
V(cx + r * math.cos(a0 + math.pi / 2), cy + r * math.sin(a0 + math.pi / 2), z)).toShape())
for i, c in enumerate(corners):
e.append(c)
n = corners[(i + 1) % 4]
e.append(Part.LineSegment(c.Vertexes[-1].Point, n.Vertexes[0].Point).toShape())
return Part.Wire(e)
def rbox(x0, y0, x1, y1, r, z0, h):
return Part.Face(rrect(x0, y0, x1, y1, r, z0)).extrude(V(0, 0, h))
def box(x0, x1, y0, y1, z0, z1):
return Part.makeBox(x1 - x0, y1 - y0, z1 - z0, V(x0, y0, z0))
def port_cut(side, a0, a1, z0, z1, L, W, wall_out):
"""A box through the wall on `side`, spanning a0..a1 along that edge and z0..z1 (absolute z)."""
t = wall_out + 12.0 # generous: always reaches past the outer wall
i = 0.25 # stop just inside the board edge: never touches a standoff near the wall
if side == "S": return box(a0, a1, -t, -i, z0, z1)
if side == "N": return box(a0, a1, W + i, W + t, z0, z1)
if side == "W": return box(-t, -i, a0, a1, z0, z1)
if side == "E": return box(L + i, L + t, a0, a1, z0, z1)
raise ValueError(side)
def wall_box(side, a0, a1, d0, z0, z1, L, W, wall_out):
"""Box from d0 mm OUTSIDE the board edge on `side` (negative = inside) to beyond the outer wall."""
t = wall_out + 12.0
if side == "S": return box(a0, a1, -t, -d0, z0, z1)
if side == "N": return box(a0, a1, W + d0, W + t, z0, z1)
if side == "W": return box(-t, -d0, a0, a1, z0, z1)
if side == "E": return box(L + d0, L + t, a0, a1, z0, z1)
raise ValueError(side)
def yz_prism(pts, x0, x1, side, W):
"""Profile (y, z) for the S side (y < 0 outward), extruded along x from x0 to x1; mirrored for side N."""
if side == "N": pts = [(W - y, z) for y, z in pts]
p = [V(x0, y, z) for y, z in pts] + [V(x0, pts[0][0], pts[0][1])]
return Part.Face(Part.makePolygon(p)).extrude(V(x1 - x0, 0, 0))
def xz_prism(pts, y0, y1, side, L):
"""Profile (x, z) for the W side (x < 0 outward), extruded along y; mirrored for side E."""
if side == "E": pts = [(L - x, z) for x, z in pts]
p = [V(x, y0, z) for x, z in pts] + [V(pts[0][0], y0, pts[0][1])]
return Part.Face(Part.makePolygon(p)).extrude(V(0, y1 - y0, 0))
def polyface(pts, z):
p = [V(x, y, z) for x, y in pts] + [V(pts[0][0], pts[0][1], z)]
return Part.Face(Part.makePolygon(p))
def logo_shape(cx, cy, size, z, h):
"""Youblob logo, raised: the white frame of the rounded square + the traced glyph (LOGO_GLYPH below)."""
s = size
outer = rbox(cx - s / 2, cy - s / 2, cx + s / 2, cy + s / 2, 0.15 * s, z, h)
inset = 0.085 * s
inner = rbox(cx - s / 2 + inset, cy - s / 2 + inset, cx + s / 2 - inset, cy + s / 2 - inset, 0.07 * s, z - 1, h + 2)
frame = outer.cut(inner)
ext = [(cx + x * s, cy + y * s) for x, y in LOGO_GLYPH["ext"]]
gl = polyface(ext, z).extrude(V(0, 0, h))
for hole in LOGO_GLYPH["holes"]:
hp = [(cx + x * s, cy + y * s) for x, y in hole]
gl = gl.cut(polyface(hp, z - 1).extrude(V(0, 0, h + 2)))
return frame.fuse(gl)
def text_shape(txt, x0, y0, height, z, h, rot90=False):
chars = Part.makeWireString(txt, DESIGN["font"], height, 0)
solids = []
for ch in chars:
if not ch: continue
f = Part.makeFace(ch, "Part::FaceMakerBullseye")
solids.append(f.extrude(V(0, 0, h)))
t = solids[0].fuse(solids[1:]) if len(solids) > 1 else solids[0]
bb = t.BoundBox
t.translate(V(-bb.XMin, -bb.YMin, 0))
if rot90:
t.rotate(V(0, 0, 0), V(0, 0, 1), 90)
bb = t.BoundBox
t.translate(V(-bb.XMin, -bb.YMin, 0))
t.translate(V(x0, y0, z))
return t
def text_size(txt, height):
chars = Part.makeWireString(txt, DESIGN["font"], height, 0)
xs = [v.Point.x for ch in chars for w in ch for v in w.Vertexes]
ys = [v.Point.y for ch in chars for w in ch for v in w.Vertexes]
return max(xs) - min(xs), max(ys) - min(ys)
def build(spec):
D = dict(DESIGN, **spec.get("design", {}))
L, W, t = spec["L"], spec["W"], spec["pcb_t"]
c, wall, fl, lt, rb = D["clear"], D["wall"], D["floor"], D["lid_t"], D["rebate"]
r_b = spec.get("corner_r", 0.5)
stand = spec["standoff_h"] # floor top -> PCB bottom
zb = fl + stand # PCB bottom (absolute)
above = spec["top_h"] # tallest part above PCB bottom (or our design limit)
z_lid = zb + above + D["top_gap"] # lid underside
z_top = z_lid + lt # top of walls = top of lid
ext = spec.get("cavity_ext_E", 0.0) # extra room past the board's E end (wires), our design value
ey = spec.get("cavity_ext_y", 0.0) # extra room beside both long edges (behind snap rails), our design value
cx0, cy0, cx1, cy1 = -c, -c - ey, L + c + ext, W + c + ey # cavity
rc = r_b + c
base = rbox(cx0 - wall, cy0 - wall, cx1 + wall, cy1 + wall, rc + wall, 0, z_top)
base = base.cut(rbox(cx0, cy0, cx1, cy1, rc, fl, z_top))
base = base.cut(rbox(cx0 - rb, cy0 - rb, cx1 + rb, cy1 + rb, rc + rb, z_lid, lt + 1)) # lid rebate
notes = []
# lid snap: a ridge along the middle half of each long rebate wall; the lid edge has a matching groove
zc, sb, sg = z_lid + lt / 2, D["snap_bead"], D["snap_groove"]
long_x = (cx1 - cx0) >= (cy1 - cy0)
snaps = [] # (side, a0, a1) in board coords along that wall
if long_x:
a0, a1 = cx0 + 0.25 * (cx1 - cx0), cx1 - 0.25 * (cx1 - cx0)
snaps = [("S", a0, a1, cy0 - rb), ("N", a0, a1, cy1 + rb)]
else:
a0, a1 = cy0 + 0.25 * (cy1 - cy0), cy1 - 0.25 * (cy1 - cy0)
snaps = [("W", a0, a1, cx0 - rb), ("E", a0, a1, cx1 + rb)]
for side, a0, a1, wpos in snaps:
if side in ("S", "N"):
yw = wpos if side == "S" else W - wpos # S-frame wall position (mirrored for N)
prof = [(yw - 0.01, zc - sb), (yw + sb, zc), (yw - 0.01, zc + sb)]
base = base.fuse(yz_prism(prof, a0, a1, side, W))
else:
xw = wpos if side == "W" else L - wpos
prof = [(xw - 0.01, zc - sb), (xw + sb, zc), (xw - 0.01, zc + sb)]
base = base.fuse(xz_prism(prof, a0, a1, side, L))
# board supports
sup = spec["support"]
if sup["type"] == "standoffs":
od, pil = sup["od"], sup["pilot"]
for x, y in spec["holes"]:
base = base.fuse(Part.makeCylinder(od / 2, stand + 0.01, V(x, y, fl - 0.005)))
for x, y in spec["holes"]:
base = base.cut(Part.makeCylinder(pil / 2, stand + fl - 0.6, V(x, y, 0.6)))
elif sup["type"] == "grooves":
# rails along both long edges with a groove the bare PCB slides into (from the W end, which stays open
# above the floor to the groove height); groove = pcb_t + 0.25, depth into the board edge sup["bite"]
g_h, bite = t + 0.25, sup["bite"]
rail_w = c + bite
for y_in, y_out in ((-c, bite), (W - bite, W + c)):
rail = box(-c, L + 0.2, y_in, y_out, fl - 0.01, zb + t + 1.2)
base = base.fuse(rail)
groove = box(-c - 0.01, L + 0.21, y_in - 0.01, y_out + 0.01, zb - 0.125, zb + t + 0.125)
base = base.cut(groove)
elif sup["type"] == "ledges":
# boards without holes: a ledge under each long edge (bite mm under the board), board fixed with a drop of glue
bite = sup["bite"]
for y_in, y_out in ((-c, bite), (W - bite, W + c)):
base = base.fuse(box(-c, L + c, y_in, y_out, fl - 0.01, zb))
elif sup["type"] == "snap_rails":
# boards with solder pads round the edge: flexible rails stand free on the floor (a gap behind each, see
# cavity_ext_y) and grip only the bare outer strip of the board edge: a lip under it, a groove, a lip over it
# with a ramp on top. Push the board down: the rails spring apart and click over it; lever a rail out to release.
bite, gap, rt = sup["bite"], sup["side_gap"], sup["rail_t"]
zg1 = zb + t + 0.1
yi, yo, tip = -gap, -gap - rt, bite
prof = [(yo, fl - 0.01), (yi, fl - 0.01), (yi, zb - (tip - yi)), (tip, zb), (yi, zb), (yi, zg1), (tip, zg1),
(tip, zg1 + 0.2), (yi, zg1 + 0.2 + (tip - yi)), (yo, zg1 + 0.2 + (tip - yi))]
for xc in sup["at"]:
for side in ("S", "N"):
base = base.fuse(yz_prism(prof, xc - sup["len"] / 2, xc + sup["len"] / 2, side, W))
if sup.get("end_stop"): # two corner blocks against the board's E end face (touch only the edge), floor to board top
for y0 in (0.3, W - 2.3):
base = base.fuse(box(L + 0.15, L + 1.15, y0, y0 + 2.0, fl - 0.01, zb + t))
notes.append("snap rails")
if sup["type"] == "pegs" or sup.get("pegs"):
sup = sup if sup["type"] == "pegs" else sup["pegs"]
# small boards with small holes: a post under each hole with a locating peg into it; lid posts press from above
od, peg, ph = sup["od"], sup["peg"], sup["peg_h"]
for x, y in spec["holes"]:
base = base.fuse(Part.makeCylinder(od / 2, stand + 0.01, V(x, y, fl - 0.005)))
base = base.fuse(Part.makeCylinder(peg / 2, ph, V(x, y, zb - 0.005)))
# ports
for p in spec["ports"]:
if p.get("face", 0.0) > c: # socket sticks out past the cavity: the board can only drop in if the opening runs to the top
p = dict(p, open_top=True)
m = D["port_margin"]
z0 = zb + p["z0"] - m
z1 = z_top + 1 if p.get("open_top") else zb + p["z1"] + m
base = base.cut(port_cut(p["side"], p["a0"] - m, p["a1"] + m, max(z0, fl), z1, L, W, wall + c))
if "face" in p and p["face"] < wall + c: # socket face inside the wall: open the rest of the wall for the plug body
pm = D["plug_margin"]
base = base.cut(wall_box(p["side"], p["a0"] - pm, p["a1"] + pm, p["face"], max(zb + p["z0"] - pm, fl),
z_top + 1 if p.get("open_top") else zb + p["z1"] + pm, L, W, wall + c))
for v in spec.get("vents", []):
n = int((v["a1"] - v["a0"] + 2.0) // 4.0)
for i in range(n):
a = v["a0"] + i * 4.0
base = base.cut(port_cut(v["side"], a, a + 2.0, zb + v["z0"], zb + v["z1"], L, W, wall + c))
# finger notch to lift the lid (on the side named in the spec)
ns, na = spec.get("notch", ("S", L / 2))
base = base.cut(port_cut(ns, na - 5, na + 5, z_top - 1.5, z_top + 1, L, W, wall + c))
# lid
lx0, ly0, lx1, ly1 = cx0 - rb + D["lid_gap"], cy0 - rb + D["lid_gap"], cx1 + rb - D["lid_gap"], cy1 + rb - D["lid_gap"]
lid = rbox(lx0, ly0, lx1, ly1, rc + rb - D["lid_gap"], 0, lt)
for side, a0, a1, wpos in snaps: # V-groove in the lid edge, a little longer than the ridge
e = wpos + D["lid_gap"] if side in ("S", "W") else wpos - D["lid_gap"]
if side in ("S", "N"):
ye = e if side == "S" else W - e
prof = [(ye - 0.5, lt / 2 - sg - 0.5), (ye + sg, lt / 2), (ye - 0.5, lt / 2 + sg + 0.5)]
lid = lid.cut(yz_prism(prof, a0 - 1, a1 + 1, side, W))
else:
xe = e if side == "W" else L - e
prof = [(xe - 0.5, lt / 2 - sg - 0.5), (xe + sg, lt / 2), (xe - 0.5, lt / 2 + sg + 0.5)]
lid = lid.cut(xz_prism(prof, a0 - 1, a1 + 1, side, L))
for x0, x1, y0, y1 in spec.get("lid_cuts", []):
lid = lid.cut(box(x0, x1, y0, y1, -1, lt + 1))
for x0, x1, y0, y1, zr in spec.get("lid_posts", []): # posts hanging under the lid down to zr above the PCB bottom
lid = lid.fuse(box(x0, x1, y0, y1, -(z_lid - (zb + zr)), 0.01))
# logo + text in the free zone given by the spec (board coords), else the whole lid minus 3 mm
zx0, zx1, zy0, zy1 = spec.get("mark_zone", (lx0 + 3, lx1 - 3, ly0 + 3, ly1 - 3))
txt = D["text"]
mk = spec.get("marks") # explicit placement for narrow lids: {"logo": [cx, cy, size], "text_box": [x0, x1, y0, y1]}
if mk:
cx_, cy_, s = mk["logo"]
logo = logo_shape(cx_, cy_, s, lt, D["relief"])
bx0, bx1, by0, by1 = mk["text_box"]
rot = (by1 - by0) > (bx1 - bx0) # text runs along the long side of its box
run, across = ((by1 - by0), (bx1 - bx0)) if rot else ((bx1 - bx0), (by1 - by0))
th = 10.0
tw, tht = text_size(txt, th)
th *= min(run / tw, across / tht); tw, tht = text_size(txt, th)
if rot: text = text_shape(txt, (bx0 + bx1) / 2 - tht / 2, (by0 + by1) / 2 - tw / 2, th, lt, D["relief"], rot90=True)
else: text = text_shape(txt, (bx0 + bx1) / 2 - tw / 2, (by0 + by1) / 2 - tht / 2, th, lt, D["relief"])
zx0 = zx1 = zy0 = zy1 = None
if not mk: zw, zh = zx1 - zx0, zy1 - zy0
if mk: pass
elif zw / zh >= 2.2: # long, narrow: logo at the left end, text to its right along the length
s = min(zh, 0.32 * zw)
logo = logo_shape(zx0 + s / 2, (zy0 + zy1) / 2, s, lt, D["relief"])
avail = zw - s - 2.0
th = min(0.55 * zh, 6.0)
tw, _ = text_size(txt, th)
if tw > avail: th *= avail / tw
tw, tht = text_size(txt, th)
tx = zx0 + s + 2.0 + (avail - tw) / 2
text = text_shape(txt, tx, (zy0 + zy1) / 2 - tht / 2, th, lt, D["relief"])
elif zh / zw >= 2.2: # tall, narrow: logo at the top, text running up the length
s = min(zw, 0.32 * zh)
logo = logo_shape((zx0 + zx1) / 2, zy1 - s / 2, s, lt, D["relief"])
avail = zh - s - 2.0
th = min(0.55 * zw, 6.0)
tw, _ = text_size(txt, th)
if tw > avail: th *= avail / tw
tw, tht = text_size(txt, th)
text = text_shape(txt, (zx0 + zx1) / 2 - tht / 2, zy0 + (avail - tw) / 2, th, lt, D["relief"], rot90=True)
else: # roomy: logo above, text below, both centred
th = min(0.13 * zh, 7.0)
tw, tht = text_size(txt, th)
if tw > 0.92 * zw:
th *= 0.92 * zw / tw; tw, tht = text_size(txt, th)
s = min(zh - tht - 3.0, 0.6 * zw)
gap = 2.5
block = s + gap + tht
ytop = (zy0 + zy1) / 2 + block / 2
logo = logo_shape((zx0 + zx1) / 2, ytop - s / 2, s, lt, D["relief"])
text = text_shape(txt, (zx0 + zx1) / 2 - tw / 2, ytop - s - gap - tht, th, lt, D["relief"])
lid = lid.fuse(logo).fuse(text)
info = {"outer_mm": [round(cx1 - cx0 + 2 * wall, 2), round(cy1 - cy0 + 2 * wall, 2), round(z_top, 2)],
"board_bottom_z": round(zb, 2), "lid_underside_z": round(z_lid, 2), "text_height_mm": round(th, 2),
"logo_mm": round(s, 2), "lid_mm": [round(lx1 - lx0, 2), round(ly1 - ly0, 2), lt]}
return base, lid, info
if os.environ.get("SPEC") and not os.environ.get("ENCL_IMPORT"): # freecadcmd names the module after the file, so no __main__ test
spec = json.load(open(os.environ["SPEC"]))
out = os.environ["OUT"]
os.makedirs(os.path.dirname(out) or ".", exist_ok=True)
base, lid, info = build(spec)
for name, shp in (("base", base), ("lid", lid)):
shp = shp.removeSplitter()
assert shp.isValid(), name + " invalid"
bb = shp.BoundBox
shp.translate(V(-bb.XMin, -bb.YMin, -bb.ZMin)) # print-bed origin
mesh = shp.tessellate(0.03)
import Mesh
m = Mesh.Mesh(); m.addFacets([(mesh[0][a], mesh[0][b], mesh[0][c]) for a, b, c in mesh[1]])
m.write(f"{out}-{name}.stl")
shp.exportStep(f"{out}-{name}.step")
info[name + "_volume_cm3"] = round(shp.Volume / 1000, 2)
json.dump(info, open(f"{out}-info.json", "w"), indent=1)
print("INFO", json.dumps(info))
import sys; sys.stdout.flush(); os._exit(0)