#!/usr/bin/env bash # run_stencil.sh — headless FlatCAM 8.995 stencil G-code generator # # Usage: # ./run_stencil.sh set -euo pipefail GERBER="${1:?Usage: $0 }" OUTPUT="${2:?Usage: $0 }" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" TCL_SCRIPT="$SCRIPT_DIR/paste_stencil.tcl" FLATCAM_PY="$HOME/flatcam-beta/flatcam.py" FLATCAM_ENV="$HOME/flatcam-beta/.flatcam-env" if [ ! -f "$FLATCAM_PY" ]; then echo "ERROR: flatcam.py not found at $FLATCAM_PY" exit 1 fi GERBER_ABS="$(realpath "$GERBER")" OUTPUT_ABS="$(realpath -m "$OUTPUT")" mkdir -p "$(dirname "$GERBER_ABS")" mkdir -p "$(dirname "$OUTPUT_ABS")" TMP_TCL=$(mktemp /tmp/paste_stencil_XXXXXX.tcl) sed \ -e "s|gerbers/myboard-B_Paste.gbp|${GERBER_ABS}|g" \ -e "s|gcode/stencil.nc|${OUTPUT_ABS}|g" \ "$TCL_SCRIPT" > "$TMP_TCL" echo "→ Running FlatCAM headless..." echo " Gerber : $GERBER" echo " Output : $OUTPUT" echo "" source "$FLATCAM_ENV/bin/activate" # Run FlatCAM, watch for the Quit confirmation line, then kill it # FlatCAM hangs on quit due to Qt GUI cleanup — we kill it cleanly once done. # Disable pipefail here: we intentionally kill the process, so its nonzero # exit status should not abort the rest of this script. set +e set +o pipefail python "$FLATCAM_PY" --shellfile="$TMP_TCL" --headless=1 2>&1 | \ while IFS= read -r line; do # Show WARNING, ERROR, and script puts output; suppress DEBUG and INFO noise if [[ "$line" == *"[WARNING]"* || "$line" == *"[ERROR]"* || "$line" != *"["* ]]; then [[ "$line" == *"RUNNING HEADLESS"* ]] && continue [[ "$line" == *"QWidget::setMaximumSize"* ]] && continue [[ "$line" == *"Image Import plugin"* ]] && continue [[ "$line" == *"Joining"*"polygons"* ]] && continue [[ "$line" == *"Union(buffer)"* ]] && continue [[ "$line" == *"Total number of polygons"* ]] && continue [[ "$line" == *"Number of paths for which"* ]] && continue echo "$line" fi if [[ "$line" == *"TclCommandQuit"* ]]; then sleep 3 pkill -f "flatcam.py.*headless" 2>/dev/null & disown fi done set -e set -o pipefail rm -f "$TMP_TCL" # Verify output was created if [ -f "$OUTPUT_ABS" ]; then SIZE=$(wc -l < "$OUTPUT") echo "" echo "✓ Done: $OUTPUT ($SIZE lines)" else echo "" echo "✗ ERROR: output file not created — check log above" exit 1 fi