#!/bin/python3 from __future__ import annotations import subprocess import sys from pathlib import Path from utils import ask_int, ask_float BASE_DIR = Path(__file__).resolve().parent SCAD_DIR = BASE_DIR / "seedlings_tray" OUT_DIR = BASE_DIR / "stl" def render_scad( scad_file: Path, out_file: Path, cols: int, rows: int, cell_size: float, tray_height: float, water_tray_height: float, render_mode: str, ) -> None: cmd = [ "openscad", "-D", f"cols={cols}", "-D", f"rows={rows}", "-D", f"cell_size={cell_size}", "-D", f"tray_height={tray_height}", "-D", f"water_tray_height={water_tray_height}", "-D", f'render_mode="{render_mode}"', "-o", str(out_file), str(scad_file), ] result = subprocess.run( cmd, cwd=BASE_DIR, text=True, capture_output=True, ) if result.returncode != 0: print(f"\nFailed to render {scad_file.name} ({render_mode})") 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 20 mm may result in unusable STL files.\n") cols = ask_int("Number of columns (cols): ") rows = ask_int("Number of rows (rows): ") cell_size = ask_float("Inner cell size in mm (cell_size): ") tray_height = ask_float("Tray height in mm (tray_height): ") water_tray_height = ask_float("Water tray wall height in mm (water_tray_height): ") OUT_DIR.mkdir(parents=True, exist_ok=True) scad_file = SCAD_DIR / "seedlings-tray.scad" if not scad_file.exists(): raise SystemExit(f"Missing file: {scad_file}") # seedling tray render_scad( scad_file, OUT_DIR / "seedlings-tray.stl", cols, rows, cell_size, tray_height, water_tray_height, "tray_only", ) # removable plates render_scad( scad_file, OUT_DIR / "plates.stl", cols, rows, cell_size, tray_height, water_tray_height, "plates_only", ) # water tray render_scad( scad_file, OUT_DIR / "water-tray.stl", cols, rows, cell_size, tray_height, water_tray_height, "water_tray_only", ) # pressing tool render_scad( scad_file, OUT_DIR / "pusher-tool.stl", cols, rows, cell_size, tray_height, water_tray_height, "tool_only", ) print("\nDone.") if __name__ == "__main__": main()