holder: fit-check — translucent max-box + manual enclosure overlay

The hex_cell.scad echoes its computed dimensions (pack_height_holder,
total_*_holder, box_total_*) to stderr. Capture those and surface them
in the viewer so the user can sanity-check whether the assembled
battery will fit in a target case.

Backend:
- holder.py: render_stl() now returns (stl_bytes, dims) where dims is
  a {name: float} dict parsed from openscad ECHO lines.
- app.py: /api/holder/render emits the dims dict as an
  X-Holder-Dimensions response header (JSON) with the matching
  Access-Control-Expose-Headers entry so fetch() can read it under
  any proxy / future CORS setup.

Viewer (holder-viewer.js):
- New setGhostBox(name, {w,d,h}, {visible,color}) and clearGhostBox(name)
  helpers. Each ghost is a Group of a translucent BoxGeometry mesh +
  matching EdgesGeometry wireframe, positioned to match how the STL
  mesh is placed (centred XY, bottom on Z=0).

UI (holder.html / holder.css):
- New "Fit check" panel under Status with two sections:
    • Show max bounding box (auto, from ECHO — defaults to box_total_*
      dims, falls back to total_*_holder + pack_height_holder).
    • Show enclosure (manual W × D × H inputs in mm).
- Verdict line under the enclosure inputs: "✓ fits" green or
  "✗ too small — battery won't fit" red.

Controller (holder-app.js):
- Reads X-Holder-Dimensions after each render, updates the max-box
  ghost in blue, prints the dimensions label.
- Watches enclosure inputs + toggles, drives the enclosure ghost
  (green if it fits, red if smaller than the max box on any axis).
- Fit comparison is orientation-independent in the XY plane (sorted
  W,D pair) but strict on Z (height).
This commit is contained in:
wenil
2026-05-25 12:19:31 +03:00
parent 3418e01689
commit dfef1453aa
6 changed files with 278 additions and 6 deletions
+29 -3
View File
@@ -14,6 +14,7 @@ from __future__ import annotations
import json
import math
import os
import re
import shutil
import subprocess
import tempfile
@@ -220,8 +221,33 @@ def _filter_params(params: dict) -> dict:
return out
def render_stl(params: dict) -> bytes:
"""Render the .scad with given parameter overrides; return STL bytes."""
_ECHO_RE = re.compile(r'^ECHO:\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*([-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?)\s*$')
def _parse_echo_dims(stderr_bytes: bytes) -> dict:
"""Pick up `ECHO: name = N` numeric pairs from openscad stderr.
The hex_cell.scad echoes derived measurements (pack_height_holder,
total_length_holder, box_total_height, etc.) — useful for fit-check
against a user enclosure without us mirroring all the SCAD math.
"""
out: dict = {}
for line in (stderr_bytes or b"").decode(errors="replace").splitlines():
m = _ECHO_RE.match(line.strip())
if m:
try:
out[m.group(1)] = float(m.group(2))
except ValueError:
pass
return out
def render_stl(params: dict) -> tuple[bytes, dict]:
"""Render the .scad with given parameter overrides.
Returns (stl_bytes, dimensions) where dimensions is a {name: float}
dict harvested from openscad ECHO output (pack/box/holder sizes).
"""
_check_cell_limit(params)
if not openscad_available():
raise RuntimeError(
@@ -249,4 +275,4 @@ def render_stl(params: dict) -> bytes:
raise RuntimeError(f"openscad failed (exit {r.returncode}):\n{err[-800:]}")
if not out.exists() or out.stat().st_size == 0:
raise RuntimeError("openscad produced no STL (geometry empty?)")
return out.read_bytes()
return out.read_bytes(), _parse_echo_dims(r.stderr or b"")