# KiCad → Stencil G-code pipeline Automates the conversion of a KiCad v9 `F.Paste` and `B.Paste` Gerber to G-code for CNC milling a solder paste stencil, using FlatCAM beta 8.995 headless. Tested on Ubuntu 24.04 with Python 3.12 and a Frezu 3040 / FluidNC controller. --- ## Prerequisites - Ubuntu 24.04 - Python 3.10–3.12 - KiCad v9 - A flat end mill (corn bit), 0.5 mm recommended --- ## Installation ### 1. System dependencies ```bash sudo apt install python3-venv python3-pip libgeos-dev libspatialindex-dev libxcb-cursor0 ``` ### 2. Clone FlatCAM beta 8.995 ```bash git clone --branch mstanciu_Beta_8.995 --depth 1 https://github.com/dwrobel/flatcam.git flatcam-beta ``` ### 3. Create a Python venv and install dependencies ```bash cd flatcam-beta python3 -m venv .flatcam-env source .flatcam-env/bin/activate pip install PyQt6 PyQt6-Qt6 PyQt6-sip # Skip ortools (path optimizer), rasterio, and gdal (image import) — not needed for stencils grep -vE "ortools|rasterio|gdal" requirements.txt | pip install -r /dev/stdin ``` ### 4. Verify ```bash python flatcam.py --version ``` You should see the FlatCAM version string printed without errors. Go back to the project directory: ```bash cd .. ``` ### 5. Export the stencil gerbers Export the gerber layers `F.Paste` and `B.Paste` ```bash kicad-cli pcb export gerbers -o gerbers -l F.Paste,B.Paste ../kicad2panel/panel/Flow_Controller_Panel.kicad_pcb ``` --- ## Usage Generate the stencil G-code for both layers: ```bash ./run_stencil.sh gerbers/Flow_Controller_Panel-F_Paste.gtp \ gcode/Flow_Controller_Panel-F_Paste-stencil.nc ./run_stencil.sh gerbers/Flow_Controller_Panel-B_Paste.gbp \ gcode/Flow_Controller_Panel-B_Paste-stencil.nc ``` G-code for both layers should be in: ``` gcode/Flow_Controller_Panel-F_Paste-stencil.nc gcode/Flow_Controller_Panel-B_Paste-stencil.nc ``` --- ## Parameters All cutting parameters are at the top of `paste_stencil.tcl`: | Variable | Default | Description | |---|---|---| | `TOOL_DIA` | `0.5` | End mill diameter in mm | | `CUT_Z` | `-0.2` | Cutting depth in mm (negative). Set slightly past material thickness | | `TRAVEL_Z` | `2.0` | Safe travel height in mm | | `FEEDRATE` | `120` | XY feed rate in mm/min | | `SPINDLE` | `15000` | Spindle speed in RPM | | `OVERLAP` | `60` | Tool overlap in % — higher = cleaner clearing, more paths | | `MARGIN` | `0.0` | Inset from aperture edge in mm. 0 = cut to exact pad size | ### Depth by material | Material | Thickness | `CUT_Z` | `SPINDLE` | `FEEDRATE` | |---|---|---|---|---| | Brass shim (CW614N) | 0.15 mm | -0.18 | 24000 RPM | 120 mm/min | | Polyimide (Kapton) | 0.10 mm | -0.13 | 24000 RPM | 300 mm/min | | Stainless steel shim | 0.15 mm | -0.18 | 16000 RPM | 60 mm/min | --- ## Preprocessor The script uses the `GRBL_11` preprocessor (`-pp GRBL_11` in `paste_stencil.tcl`), which produces grbl 1.1 compatible G-code. FluidNC implements the grbl protocol so this works directly with no modifications. Alternative: `GRBL_11_no_M6` suppresses tool change commands — not needed for a single-tool stencil job but harmless either way. --- ## Workholding notes - Stencil material is thin and will flex — use double-sided tape on a flat spoilboard - Zero Z directly on the stencil surface, not the spoilboard - For brass shim, clamp the edges with small clips if the tape isn't holding — chatter from an unanchored sheet ruins aperture quality --- ## Troubleshooting **FlatCAM hangs after writing G-code** The `run_stencil.sh` wrapper detects the `TclCommandQuit` log line and kills the process after 3 seconds. This is expected behavior — FlatCAM's Qt GUI cleanup hangs in headless mode. **`ImportError: cannot import name 'Inf' from numpy`** You have the old PyPI FlatCAM 8.5 installed. Follow the installation steps above to switch to the beta 8.995 clone. **`ImportError: cannot import name 'cascaded_union' from shapely.ops`** Same cause — old FlatCAM 8.5 is incompatible with Shapely 2.0. Switch to beta 8.995. **G-code cuts everything except the pads** You used `ncc` (non-copper clear) instead of `paint`. The correct command is `paint` — it pockets the pad polygons. `ncc` clears everything around them, which is the inverse. **`gdal` fails to install** Skip it — `gdal` is only needed for the image import plugin. Use: ```bash grep -vE "ortools|rasterio|gdal" requirements.txt | pip install -r /dev/stdin ```