"""Closed case for the CXA81 amplifier controller (see ../esphome/cxa81.yaml). Holds, completely hidden: - the AAOTOKK DB9 screw-terminal board, *without* its black plastic housing, - the M5Stack ATOM Lite + Atomic RS232 Base, lying ACROSS the case on a removable shelf above it (the stack is 56 mm long, more than the case is deep). From outside you only see the DB9 plug and its metal flange (flush with the front face), the two knurled bolt heads at the back, and the USB-C and IR cables, which leave through one notch in the side wall (USB-C sits directly above the Grove port on the same end of the stack). The adapter's own long bolts go in from the back, through the case, through the flange holes and into the amp's RS232C socket. So the case is screwed to the amp (and removed) without opening it. Three printed parts, all loaded from the top: body - tray with a slot in the front wall for the DB9 flange. shelf - drops onto ledges above the board; carries the ATOM stack, and its front filler closes the slot above the flange. lid - flat top plate, 4 x M2.5 x 8 countersunk self-tapping screws. Assembly: board in (flange into the front slot), screw the RS-232 wires to the terminals, bolts in from the back, shelf in (wires up through its slot), ATOM on the shelf, plug in USB-C and IR (cables into the side notch), lid on. seen from above (lid off), DB9 end at the bottom ┌───────────────────────────────────────┐ │ ◯ bolt heads (outside, at the back) ◯ │ │ ┌─────────────────────────────────┐ ░ │ USB-C ╡ │ ATOM + RS232 base on the shelf │ ░ │ ░ room for the RS-232 + IR ╡ └─────────────────────────────────┘ ░ │ wires to go down │ (DB9 board under the shelf) │ └──────────────┤ DB9 ├──────────────────┘ Coordinates: x from the front face (0) backwards, y across (0 = centre), z up (0 = bolt/flange centre line). All sizes in mm. Edit and re-run: python enclosure/cxa81_dongle.py # writes enclosure/out/*.stl + preview Needs: pip install manifold3d trimesh numpy matplotlib """ from pathlib import Path import numpy as np import trimesh import manifold3d from manifold3d import Manifold # --- DB9 board (measured on the AAOTOKK adapter with its housing removed) ------- FLANGE_W, FLANGE_H = 30.8, 12.5 # standard DE-9 metal flange FLANGE_HOLE_PITCH = 25.0 # standard DE-9: flange holes 25 mm apart BOARD_DEPTH = 32.0 # MEASURED: outside of the flange to the end of the board BOARD_W = 31.0 # MEASURED: widest part of the board/carrier BOARD_HEIGHT = 12.0 # MEASURED: total height incl. screw terminals # The board sits within the flange's height (12.5 mm, centred on the bolt line), # so use whichever is larger above and below the line. BOARD_BELOW = max(BOARD_HEIGHT, FLANGE_H) / 2 BOARD_ABOVE = max(BOARD_HEIGHT, FLANGE_H) / 2 BOLT_LEN = 52.0 # MEASURED: under the knurled head to the tip BOLT_PROTRUDE = 6.0 # thread that must stick out in front of the flange BOLT_HOLE = 3.4 # clearance for the bolt in the back wall BOLT_HEAD = 8.5 # knurled head diameter (spot-face around it) # --- ATOM Lite on the Atomic RS232 Base (MEASURED) ------------------------------- STACK_L, STACK_W, STACK_H = 56.0, 24.0, 18.0 # length includes the RS-232 cable clip CABLE_SIDE = 1 # +1 or -1: side wall the USB-C/Grove end faces (RS-232 clip opposite) WIRE_ROOM = 6.0 # space beyond the RS-232 clip for its wires to bend down # --- Fit and structure ---------------------------------------------------------- CLEAR = 0.3 # clearance around the flange / board STACK_CLEAR = 0.5 # per side around the ATOM stack WALL = 2.4 # outer walls SHELF = 2.0 # shelf under the ATOM stack RAIL_H = 3.0 # rails on the shelf that locate the stack BOSS = 6.0 # lid screw bosses SCREW_PILOT = 2.2 # M2.5 self-tapping pilot hole SCREW_CLEAR = 2.9 SCREW_CSK = 5.2 # countersink diameter at the lid surface CABLE_NOTCH_W = 13.0 # notch in the side wall for the USB-C and Grove plugs CABLE_NOTCH_DEPTH = 17.0 # from the top edge down (the lid closes it) # --- Styling ---------------------------------------------------------------------- RADIUS = 3.0 GROOVES = 4 GROOVE_H, GROOVE_DEPTH, GROOVE_PITCH = 1.2, 0.5, 3.0 SEGMENTS = 48 OUT_DIR = Path(__file__).parent / "out" def box(x0, y0, z0, x1, y1, z1) -> Manifold: """Axis-aligned box between two corners (given in any order).""" (x0, x1), (y0, y1), (z0, z1) = sorted((x0, x1)), sorted((y0, y1)), sorted((z0, z1)) return Manifold.cube([x1 - x0, y1 - y0, z1 - z0]).translate([x0, y0, z0]) def rounded_box(x0, y0, z0, x1, y1, z1, r) -> Manifold: s = Manifold.sphere(r, SEGMENTS) return Manifold.batch_hull([s.translate([x, y, z]) for x in (x0 + r, x1 - r) for y in (y0 + r, y1 - r) for z in (z0 + r, z1 - r)]) def rounded_rect_x(x0, x1, y0, y1, z0, z1, r) -> Manifold: """Prism along x with a rounded-rectangle cross-section (for openings).""" c = Manifold.cylinder(x1 - x0, r, r, SEGMENTS).rotate([0, 90, 0]) return Manifold.batch_hull([c.translate([x0, y, z]) for y in (y0 + r, y1 - r) for z in (z0 + r, z1 - r)]) def x_cylinder(x0, x1, y, z, r) -> Manifold: return Manifold.cylinder(x1 - x0, r, r, SEGMENTS).rotate([0, 90, 0]).translate([x0, y, z]) def z_cylinder(x, y, z0, z1, r) -> Manifold: return Manifold.cylinder(z1 - z0, r, r, SEGMENTS).translate([x, y, z0]) def z_cone(x, y, z0, z1, r0, r1) -> Manifold: return Manifold.cylinder(z1 - z0, r0, r1, SEGMENTS).translate([x, y, z0]) def layout(): length = BOLT_LEN - BOLT_PROTRUDE # front face to back face x_in0, x_in1 = WALL, length - WALL w_in = max(BOARD_W + 2 * CLEAR, STACK_L + 2 * STACK_CLEAR + WIRE_ROOM) floor = -BOARD_BELOW - CLEAR shelf_z0 = BOARD_ABOVE + 0.5 shelf_z1 = shelf_z0 + SHELF top_in = shelf_z1 + STACK_H + 0.5 assert BOARD_DEPTH < x_in1 - 5, "board does not fit before the back wall" assert STACK_W + 2 * STACK_CLEAR + 2 * BOSS < x_in1 - x_in0, "ATOM stack too wide for the case" return dict(length=length, x_in0=x_in0, x_in1=x_in1, w_in=w_in, floor=floor, shelf_z0=shelf_z0, shelf_z1=shelf_z1, top_in=top_in) def boss_positions(g): o = BOSS / 2 return [(x, y) for x in (g["x_in0"] + o, g["x_in1"] - o) for y in (-g["w_in"] / 2 + o, g["w_in"] / 2 - o)] def build(): g = layout() L, w2, floor, top_in = g["length"], g["w_in"] / 2, g["floor"], g["top_in"] big = 1000 shell = rounded_box(0, -w2 - WALL, floor - WALL, L, w2 + WALL, top_in + WALL, RADIUS) # Decorative grooves along both long sides. for i in range(GROOVES): z = floor + (top_in - floor) * 0.3 + (i - (GROOVES - 1) / 2) * GROOVE_PITCH for y in (-w2 - WALL, w2 + WALL): shell -= Manifold.batch_hull([ Manifold.cylinder(2 * GROOVE_DEPTH, GROOVE_H / 2, GROOVE_H / 2, SEGMENTS) .rotate([-90, 0, 0]).translate([x, y - GROOVE_DEPTH, z]) for x in (9, L - 9)]) # Split first, then hollow the body with a cut that runs out through the top, # so no paper-thin skin is left across the opening at the split line. lid = shell ^ box(-big, -big, top_in, big, big, big) body = shell - box(-big, -big, top_in, big, big, big) body -= box(g["x_in0"], -w2, floor, g["x_in1"], w2, top_in + 1) # --- body (tray) --------------------------------------------------------------- side = CABLE_SIDE # Front: slot for the DB9 flange, open at the top so the board drops in from # above. The shelf insert's front filler closes the slot above the flange. open_w = max(FLANGE_W, BOARD_W) / 2 + CLEAR open_h = max(FLANGE_H, BOARD_HEIGHT) / 2 + CLEAR body -= rounded_rect_x(-1, g["x_in0"] + 0.5, -open_w, open_w, -open_h, top_in + 5, 0.6) # Back: holes for the two bolts, with a shallow spot-face for the knurled heads. for y in (-FLANGE_HOLE_PITCH / 2, FLANGE_HOLE_PITCH / 2): body -= x_cylinder(g["x_in1"] - 1, L + 1, y, 0, BOLT_HOLE / 2) body -= x_cylinder(L - 0.6, L + 1, y, 0, BOLT_HEAD / 2 + 0.5) # Stops behind the board's two outer edges (below the bolt line) so it cannot # be pushed back when plugging in. stop_x = BOARD_DEPTH + CLEAR for y0, y1 in ((-BOARD_W / 2, -BOARD_W / 2 + 4), (BOARD_W / 2 - 4, BOARD_W / 2)): body += box(stop_x, y0, floor - 0.5, stop_x + 2, y1, -BOLT_HOLE / 2 - 0.5) # fused into the floor # Ledges along both side walls that the shelf insert rests on. for y0, y1 in ((-w2, -w2 + 1.5), (w2 - 1.5, w2)): y0, y1 = (y0 - 0.5, y1) if y0 < 0 else (y0, y1 + 0.5) # fuse into the side wall body += box(g["x_in0"] + BOSS + 0.5, y0, g["shelf_z0"] - 1.5, g["x_in1"] - BOSS - 0.5, y1, g["shelf_z0"]) # Lid screw bosses: front ones start above the flange, back ones on the floor. for x, y in boss_positions(g): z0 = FLANGE_H / 2 + 1 if x < L / 2 else floor # Grown 0.5 mm into the two walls it stands against, so it fuses with them. gx = -0.5 if x < L / 2 else 0.5 gy = -0.5 if y < 0 else 0.5 body += box(x - BOSS / 2 + min(gx, 0), y - BOSS / 2 + min(gy, 0), z0, x + BOSS / 2 + max(gx, 0), y + BOSS / 2 + max(gy, 0), top_in - 0.05) body -= z_cylinder(x, y, top_in - 10, top_in + 1, SCREW_PILOT / 2) # The ATOM stack lies across the case (long axis along y): its USB-C/Grove # end touches the CABLE_SIDE wall, the RS-232 clip end has WIRE_ROOM beyond it. s_x0 = (g["x_in0"] + g["x_in1"]) / 2 - STACK_W / 2 - STACK_CLEAR s_x1 = s_x0 + STACK_W + 2 * STACK_CLEAR s_far = side * w2 # cable-end wall s_near = side * (w2 - STACK_L - 2 * STACK_CLEAR) # RS-232 clip end of the stack # One notch in the side wall for the USB-C and Grove plugs, open at the top # edge so the plugged-in cables can be laid in; the lid closes it. x_mid, r = (s_x0 + s_x1) / 2, CABLE_NOTCH_W / 2 y_wall0, y_wall1 = sorted((side * (w2 - 1), side * (w2 + WALL + 1))) body -= Manifold.batch_hull([ box(x_mid - r, y_wall0, top_in - CABLE_NOTCH_DEPTH + r, x_mid + r, y_wall1, top_in + 1), Manifold.cylinder(y_wall1 - y_wall0, r, r, SEGMENTS).rotate([-90, 0, 0]) .translate([x_mid, y_wall0, top_in - CABLE_NOTCH_DEPTH + r])]) # --- shelf insert (removable) -------------------------------------------------- gap = 0.3 shelf = box(g["x_in0"] + gap, -w2 + gap, g["shelf_z0"], g["x_in1"] - gap, w2 - gap, g["shelf_z1"]) for x, y in boss_positions(g): # clear the four bosses shelf -= box(x - BOSS / 2 - gap, y - BOSS / 2 - gap, g["shelf_z0"] - 1, x + BOSS / 2 + gap, y + BOSS / 2 + gap, g["shelf_z1"] + 1) # Slot beyond the RS-232 clip end for the wires down to the terminals. shelf -= box(s_x0 + 2, s_near, g["shelf_z0"] - 1, s_x1 - 2, -side * w2, g["shelf_z1"] + 1) # Rails along the stack's two long sides. for x0, x1 in ((s_x0 - 1.6, s_x0), (s_x1, s_x1 + 1.6)): shelf += box(x0, s_near, g["shelf_z1"], x1, s_far - side * gap, g["shelf_z1"] + RAIL_H) # Front filler: closes the front slot above the flange and holds the flange down. shelf += box(0.05, -open_w + gap, open_h + gap, g["x_in0"] + 1.0, open_w - gap, top_in - 0.1) # --- lid ---------------------------------------------------------------------- for x, y in boss_positions(g): lid -= z_cylinder(x, y, top_in - 1, top_in + WALL + 1, SCREW_CLEAR / 2) lid -= z_cone(x, y, top_in + WALL - 1.3, top_in + WALL + 0.01, SCREW_CLEAR / 2, SCREW_CSK / 2) # Locating lip just inside the walls, cut away around the bosses, the notch, # the end of the ATOM stack (which touches that wall) and the front filler. lip = (box(g["x_in0"] + 0.3, -w2 + 0.3, top_in - 2, g["x_in1"] - 0.3, w2 - 0.3, top_in) - box(g["x_in0"] + 1.5, -w2 + 1.5, top_in - 3, g["x_in1"] - 1.5, w2 - 1.5, top_in + 1)) for x, y in boss_positions(g): lip -= box(x - BOSS / 2 - 0.6, y - BOSS / 2 - 0.6, top_in - 3, x + BOSS / 2 + 0.6, y + BOSS / 2 + 0.6, top_in + 1) lip -= box(min(s_x0, x_mid - r) - 0.5, y_wall0 - 3, top_in - 3, max(s_x1, x_mid + r) + 0.5, y_wall1 + 3, top_in + 1) lip -= box(0, -open_w - 0.5, top_in - 3, g["x_in0"] + 2, open_w + 0.5, top_in + 1) lid += lip # Gentle press-down ribs that keep the ATOM stack seated. for x in (x_mid - STACK_W / 4, x_mid + STACK_W / 4): lid += box(x - 1, s_near + side * 8, g["shelf_z1"] + STACK_H + 0.1, x + 1, s_far - side * 8, top_in) return clean(body), clean(shelf), clean(lid) def clean(m: Manifold) -> Manifold: """Drop zero-volume slivers that boolean operations sometimes leave behind.""" return Manifold.batch_boolean([p for p in m.decompose() if p.volume() > 1.0], manifold3d.OpType.Add) def to_print_orientation(mesh: trimesh.Trimesh, name: str) -> trimesh.Trimesh: """Copy of the part placed on the print bed (z = 0), centred, the right way up for printing: body and shelf as assembled, lid upside down (its outer face on the bed for the best finish).""" m = mesh.copy() if name == "lid": m.apply_transform(trimesh.transformations.rotation_matrix(np.pi, [1, 0, 0])) lo, hi = m.bounds m.apply_translation([-(lo[0] + hi[0]) / 2, -(lo[1] + hi[1]) / 2, -lo[2]]) return m def to_trimesh(m: Manifold) -> trimesh.Trimesh: mesh = m.to_mesh() return trimesh.Trimesh(vertices=np.asarray(mesh.vert_properties)[:, :3], faces=np.asarray(mesh.tri_verts)) def render_preview(parts, path: Path) -> None: import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection light = np.array([0.4, -0.5, 0.8]) / np.linalg.norm([0.4, -0.5, 0.8]) fig = plt.figure(figsize=(12, 4.2)) views = ((18, -125, "Closed, DB9 end"), (18, 55, "Closed, back"), (32, 55, "Exploded: body, shelf, lid")) for i, (elev, azim, title) in enumerate(views): ax = fig.add_subplot(1, 3, i + 1, projection="3d") bounds, tris, cols = [], [], [] for j, (mesh, base) in enumerate(parts): m = mesh.copy() if i == 2: m.apply_translation([0, 0, (0, 14, 32)[j]]) shade = 0.35 + 0.65 * np.clip(m.face_normals @ light, 0, 1) tris.append(m.triangles) cols.append(np.c_[base[0] * shade, base[1] * shade, base[2] * shade, np.ones_like(shade)]) bounds.append(m.bounds) ax.add_collection3d(Poly3DCollection(np.concatenate(tris), facecolors=np.concatenate(cols), edgecolor="none")) lo = np.min([b[0] for b in bounds], axis=0) hi = np.max([b[1] for b in bounds], axis=0) mid, span = (lo + hi) / 2, (hi - lo).max() / 2 ax.set_xlim(mid[0] - span, mid[0] + span) ax.set_ylim(mid[1] - span, mid[1] + span) ax.set_zlim(mid[2] - span, mid[2] + span) ax.view_init(elev=elev, azim=azim) ax.set_title(title, fontsize=10) ax.set_axis_off() fig.tight_layout() fig.savefig(path, dpi=130) def main() -> None: OUT_DIR.mkdir(exist_ok=True) for old in list(OUT_DIR.glob("*.stl")) + list(OUT_DIR.glob("*.png")): old.unlink() body, shelf, lid = build() parts = (("body", body, (0.25, 0.45, 0.75)), ("shelf", shelf, (0.35, 0.65, 0.45)), ("lid", lid, (0.75, 0.55, 0.25))) meshes = [] for name, part, color in parts: mesh = to_trimesh(part) path = OUT_DIR / f"cxa81_case_{name}.stl" to_print_orientation(mesh, name).export(path) size = mesh.bounds[1] - mesh.bounds[0] print(f"{path.name}: {size[0]:.1f} x {size[1]:.1f} x {size[2]:.1f} mm, " f"{mesh.volume / 1000:.1f} cm3 (~{mesh.volume / 1000 * 1.27:.0f} g PETG), " f"watertight={mesh.is_watertight}, pieces={len(part.decompose())}") meshes.append((mesh, color)) for (n1, p1, _), (n2, p2, _) in ((parts[0], parts[1]), (parts[0], parts[2]), (parts[1], parts[2])): print(f"overlap {n1}/{n2} (should be 0): {round((p1 ^ p2).volume(), 3)}") render_preview(meshes, OUT_DIR / "cxa81_case_preview.png") if __name__ == "__main__": main()