#!/usr/bin/env bash set -euo pipefail # Render top-down preview PNGs for any STL in gridfinity-bin-label/labels/ # that does not yet have a sibling .png. Parameters (text + symbols) are # derived from the STL filename and rendered through the 2D-silhouette # helper at gridfinity-bin-label/examples/render.scad so the output # matches the original M2-M5 preview style. REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)" LABELS_DIR="${REPO_DIR}/gridfinity-bin-label/labels" RENDER_SCAD="${REPO_DIR}/gridfinity-bin-label/examples/render.scad" if ! command -v openscad &>/dev/null; then echo "Error: openscad not found in PATH" >&2 exit 1 fi # Top-down ortho view centered on the label. Camera distance and image # aspect were tuned so the label nearly fills the frame with roughly # equal margins on all four sides. CAMERA="18,5.5,0.4,0,0,0,45" IMGSIZE="960,360" # Map a label filename stem (e.g. "M3x16", "M4_washer", "M2_nut_washer", "M5") # to text + symbol parameters for examples/render.scad. Echoes one -D # override per line; returns 1 if the filename is not recognized. derive_params() { local stem="$1" if [[ "$stem" =~ ^M([0-9]+)x([0-9]+)$ ]]; then printf 'example_text=%s\n' "\"M${BASH_REMATCH[1]}*${BASH_REMATCH[2]}\"" printf 'example_symbol1=%s\n' '"side_pan"' printf 'example_symbol2=%s\n' '"bit_hex"' elif [[ "$stem" =~ ^M([0-9]+)_nut_washer$ ]]; then printf 'example_text=%s\n' "\"M${BASH_REMATCH[1]}\"" printf 'example_symbol1=%s\n' '"washer"' printf 'example_symbol2=%s\n' '"nut"' # Original nut+washer renders bumped the left icon inward 1 mm. printf 'sym1_pad=%s\n' '2' elif [[ "$stem" =~ ^M([0-9]+)_washer$ ]]; then printf 'example_text=%s\n' "\"M${BASH_REMATCH[1]}\"" printf 'example_symbol1=%s\n' '"none"' printf 'example_symbol2=%s\n' '"washer"' elif [[ "$stem" =~ ^M([0-9]+)$ ]]; then printf 'example_text=%s\n' "\"M${BASH_REMATCH[1]}\"" printf 'example_symbol1=%s\n' '"none"' printf 'example_symbol2=%s\n' '"bit_hex"' else return 1 fi } count=0 skipped=0 unmatched=0 for stl in "$LABELS_DIR"/*.stl; do [[ -e "$stl" ]] || continue stem="$(basename "$stl" .stl)" png="${LABELS_DIR}/${stem}.png" if [[ -e "$png" ]]; then skipped=$((skipped + 1)) continue fi mapfile -t params < <(derive_params "$stem") || true if [[ ${#params[@]} -eq 0 ]]; then echo "Skipping ${stem}: no parameter mapping for this filename" >&2 unmatched=$((unmatched + 1)) continue fi args=() for p in "${params[@]}"; do args+=( -D "$p" ) done echo "Rendering ${stem}.png" openscad "${args[@]}" \ --render \ --camera="$CAMERA" \ --projection=ortho \ --imgsize="$IMGSIZE" \ -o "$png" \ "$RENDER_SCAD" >/dev/null 2>&1 count=$((count + 1)) done echo "Generated ${count} preview(s); skipped ${skipped} existing, ${unmatched} unmatched."