Files
kicad2stencil/run_stencil.sh
2026-06-19 12:57:06 +02:00

79 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# run_stencil.sh — headless FlatCAM 8.995 stencil G-code generator
#
# Usage:
# ./run_stencil.sh <B_Paste.gbp> <output.nc>
set -euo pipefail
GERBER="${1:?Usage: $0 <B_Paste.gbp> <output.nc>}"
OUTPUT="${2:?Usage: $0 <B_Paste.gbp> <output.nc>}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TCL_SCRIPT="$SCRIPT_DIR/paste_stencil.tcl"
FLATCAM_PY="$HOME/proj/kicad2stencil/flatcam-beta/flatcam.py"
FLATCAM_ENV="$HOME/proj/kicad2stencil/.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