Files
kicad2stencil/run_stencil.sh
2026-06-18 15:15:23 +02:00

64 lines
1.8 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_ABS"
echo " Output : $OUTPUT_ABS"
echo ""
source "$FLATCAM_ENV/bin/activate"
# Run FlatCAM, watch for the WriteGCode confirmation line, then kill it
# FlatCAM hangs on quit due to Qt GUI cleanup — we kill it cleanly once done
python "$FLATCAM_PY" --shellfile="$TMP_TCL" --headless=1 2>&1 | \
while IFS= read -r line; do
echo "$line"
if [[ "$line" == *"TclCommandQuit"* ]]; then
# Give it a moment to finish flushing the file
sleep 3
# Kill the FlatCAM process
pkill -f "flatcam.py.*headless" 2>/dev/null || true
fi
done
rm -f "$TMP_TCL"
# Verify output was created
if [ -f "$OUTPUT_ABS" ]; then
SIZE=$(wc -l < "$OUTPUT_ABS")
echo ""
echo "✓ Done: $OUTPUT_ABS ($SIZE lines)"
else
echo ""
echo "✗ ERROR: output file not created — check log above"
exit 1
fi