#!/bin/python3 from __future__ import annotations import subprocess import sys from pathlib import Path from utils import ask_float BASE_DIR = Path(__file__).resolve().parent SCAD_DIR = BASE_DIR / "box" OUT_DIR = BASE_DIR / "stl" def render_scad( scad_file: Path, out_file: Path, length: float, width: float, height: float, ) -> None: cmd = ( f'openscad ' f'-D length={length} ' f'-D width={width} ' f'-D height={height} ' f'-D part="print" ' f'-o "{out_file}" ' f'"{scad_file}"' ) result = subprocess.run( ["bash", "-lc", cmd], cwd=BASE_DIR, text=True, capture_output=True, ) if result.returncode != 0: print(f"\nFailed to render {scad_file.name}") if result.stdout: print(result.stdout) if result.stderr: print(result.stderr, file=sys.stderr) raise SystemExit(result.returncode) print(f"Generated {out_file}") def main() -> None: if not SCAD_DIR.exists(): raise SystemExit(f"Missing SCAD directory: {SCAD_DIR}") print("Dimensions less than 20mm may result in unusable STL files.\n") length = ask_float("Box depth / length (X) in mm: ") width = ask_float("Box width (Y) in mm: ") height = ask_float("Box height (Z) in mm: ") OUT_DIR.mkdir(parents=True, exist_ok=True) box_scad = SCAD_DIR / "parametric_box.scad" if not box_scad.exists(): raise SystemExit(f"Missing file: {box_scad}") render_scad( box_scad, OUT_DIR / "box.stl", length, width, height, ) print("\nDone.") if __name__ == "__main__": main()