Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
b8e1de25eThis file is pinned to the selected release. Download the original to open it in your design tool.
# Standalone Fabrication Script
Direct fabrication file generation without the MCP server infrastructure.
## Overview
`generate_olivia_fabrication.py` is a standalone Python script that directly generates all fabrication files from a KiCad PCB file. This provides an alternative to the MCP server approach for users who want direct, scriptable fabrication file generation.
### When to Use This Approach
**Use Standalone Script When:**
- You want direct control over fabrication file generation
- You don't need AI interaction
- You're automating fabrication workflows
- You're integrating into build scripts/CI/CD
- You want a simpler, more predictable approach
**Use MCP Server When:**
- You want natural language interaction
- You need AI-assisted design decisions
- You're iterating on design and fabrication
- You want to explore KiCad features conversationally
## Features
✅ **Complete Fabrication Package:**
- Gerber files (all 9 standard layers)
- Drill files (PTH and NPTH, Excellon format)
- Bill of Materials (CSV)
- Component positions for pick-and-place (CSV)
- Automatic ZIP packaging
✅ **KiCad 9.x Compatible:**
- Tested with KiCad 9.0.5 Flatpak
- API compatibility workarounds included
- Handles missing API methods gracefully
✅ **Production Ready:**
- Successfully tested on Olivia Control v0.2 (51 components, 90x100mm, 2-layer)
- Output compatible with JLCPCB, PCBWay, OSH Park
- Generates professional fabrication documentation
## Installation
No additional installation needed beyond KiCad Flatpak:
```bash
# Install KiCad Flatpak if not already installed
flatpak install flathub org.kicad.KiCad
# Clone this repository
cd ~/repos
git clone https://github.com/Pablomonte/MCP-KiCad.git
cd MCP-KiCad
```
## Usage
### Step 1: Edit the Script
Open `generate_olivia_fabrication.py` and modify the paths:
```python
# At the top of the script, around line 15-20
pcb_path = "/path/to/your/project.kicad_pcb"
output_base = "/path/to/output/directory"
```
**Example:**
```python
pcb_path = "/home/pablo/repos/MyProject/hardware/board.kicad_pcb"
output_base = "/home/pablo/repos/MyProject/fabrication"
```
### Step 2: Run with KiCad's Python
**With Flatpak (Recommended):**
```bash
flatpak run --command=python3 \
--filesystem=home \
org.kicad.KiCad \
generate_olivia_fabrication.py
```
**With Native KiCad:**
```bash
# Linux
/usr/lib/kicad/bin/python3 generate_olivia_fabrication.py
# Mac
/Applications/KiCad/KiCad.app/Contents/Frameworks/Python.framework/Versions/Current/bin/python3 generate_olivia_fabrication.py
# Windows
"C:\Program Files\KiCad\9.0\bin\python.exe" generate_olivia_fabrication.py
```
### Step 3: Check Output
The script generates:
```
output_directory/
├── gerber/
│ ├── project-F.Cu.gbr # Front copper
│ ├── project-B.Cu.gbr # Back copper
│ ├── project-F.Mask.gbr # Front solder mask
│ ├── project-B.Mask.gbr # Back solder mask
│ ├── project-F.SilkS.gbr # Front silkscreen
│ ├── project-B.SilkS.gbr # Back silkscreen
│ ├── project-F.Paste.gbr # Front paste
│ ├── project-B.Paste.gbr # Back paste
│ └── project-Edge.Cuts.gbr # Board outline
├── drill/
│ ├── project.drl # PTH drill file
│ └── project-NPTH.drl # NPTH drill file
├── bom.csv # Bill of Materials
├── position.csv # Pick-and-place positions
├── INDEX.md # Documentation index
└── FABRICATION_README.md # Fabrication guide
# Plus a ZIP file:
output_directory_YYYYMMDD_HHMMSS.zip # Complete package
```
## Output Details
### Gerber Files
- **Format:** RS-274X (industry standard)
- **Units:** Millimeters
- **Coordinate Format:** 4.6 (4 integer digits, 6 decimal)
- **Layers:** All standard layers for 2-layer boards
Compatible with:
- ✅ JLCPCB
- ✅ PCBWay
- ✅ OSH Park
- ✅ Any Gerber RS-274X compatible manufacturer
### Drill Files
- **Format:** Excellon
- **Units:** Millimeters or inches (configurable)
- **Separation:** PTH (plated) and NPTH (non-plated) in separate files
- **Includes:** Tool definitions and coordinates
### Bill of Materials (BOM)
CSV format with columns:
- Reference (e.g., "R1, R2, R3")
- Value (e.g., "10k")
- Footprint (e.g., "Resistor_SMD:R_0805_2012Metric")
- Quantity
**Example:**
```csv
Reference,Value,Footprint,Quantity
"R1, R2, R3","10k","Resistor_SMD:R_0805_2012Metric",3
"C1","100nF","Capacitor_SMD:C_0805_2012Metric",1
```
### Position File
CSV format for pick-and-place machines:
- Ref (reference designator)
- Val (component value)
- Package (footprint)
- PosX, PosY (position in mm)
- Rot (rotation in degrees)
- Side (Top/Bottom)
**Example:**
```csv
Ref,Val,Package,PosX,PosY,Rot,Side
R1,10k,R_0805_2012Metric,50.25,30.75,0.0,Top
U1,ATmega328P,TQFP-32_7x7mm,60.00,40.00,45.0,Top
```
## Customization
### Change Layers
Edit the `layers` list in the script (around line 80):
```python
layers = [
("F.Cu", "Front Copper"),
("B.Cu", "Back Copper"),
# Add or remove layers as needed
("In1.Cu", "Inner Layer 1"), # For 4-layer boards
]
```
### Change Drill Settings
Modify drill export settings (around line 150):
```python
drlwriter.SetOptions(
False, # aMirror - False = don't mirror
False, # aMinimalHeader - False = full header
pcbnew.VECTOR2I(0, 0), # aOffset
False # aMerge_PTH_NPTH - False = separate files
)
```
**To merge PTH and NPTH into one file:**
```python
drlwriter.SetOptions(False, False, pcbnew.VECTOR2I(0, 0), True)
```
### Change Units
For imperial units (inches), modify the drill writer:
```python
drlwriter.SetFormat(False) # False = inches, True = metric
```
### Add Custom Layers
For specialized boards (e.g., 4-layer, flex):
```python
layers = [
("F.Cu", "Front Copper"),
("In1.Cu", "Inner Layer 1"),
("In2.Cu", "Inner Layer 2"),
("B.Cu", "Back Copper"),
# ... rest of layers
]
```
## Example: Olivia Control v0.2
Successfully generated fabrication files for a production board:
**Board Specs:**
- Size: 100.0 x 75.0 mm
- Layers: 2 (front and back copper)
- Components: 42 total (25 unique types)
- Complexity: Mixed SMD and through-hole
**Output:**
- 9 Gerber files (complete layer set)
- 2 Drill files (PTH and NPTH separate)
- BOM: 25 unique parts, 42 total components
- Position file: All 42 component placements
- ZIP package: 15KB, ready for manufacturer
**Manufacturing:**
- Successfully uploaded to JLCPCB
- No errors in Gerber viewer
- Automatic quote generation worked
- Production-ready on first attempt
## Advantages Over MCP Server
### Standalone Script Pros:
- ✅ No AI/MCP dependencies
- ✅ Faster execution (no LLM calls)
- ✅ Predictable, repeatable output
- ✅ Easy to integrate into scripts
- ✅ Simple to version control
- ✅ Works offline
- ✅ Lower resource usage
### MCP Server Pros:
- ✅ Natural language interaction
- ✅ AI-assisted decision making
- ✅ Exploratory workflow
- ✅ Good for learning KiCad
- ✅ Conversational refinement
**Recommendation:** Use standalone script for production, MCP server for development/learning.
## Troubleshooting
### Import Error: "No module named 'pcbnew'"
You're not running with KiCad's Python. Use the Flatpak command:
```bash
flatpak run --command=python3 --filesystem=home org.kicad.KiCad generate_olivia_fabrication.py
```
### File Not Found Error
Check that your PCB path is correct and absolute:
```python
# Wrong (relative path may not work)
pcb_path = "../MyProject/board.kicad_pcb"
# Correct (absolute path)
pcb_path = "/home/pablo/repos/MyProject/board.kicad_pcb"
```
### Output Directory Not Created
The script creates the output directory automatically. If it fails:
- Check parent directory exists
- Check write permissions
- Use absolute path for output_base
### API Errors (AttributeError)
These are usually harmless - the script wraps API calls in try/except blocks. The fabrication files will still be generated correctly.
**Example:**
```
AttributeError: 'PCB_PLOT_PARAMS' object has no attribute 'SetPlotFrameRef'
```
This is normal on KiCad 9.x - the script continues without issue.
### Empty BOM or Position File
Check that your PCB has components. The script generates empty files if the board has no components.
```bash
# Verify your PCB has components in KiCad first
kicad /path/to/your/project.kicad_pcb
```
## Integration Examples
### Shell Script Automation
```bash
#!/bin/bash
# build_fabrication.sh
PCB_FILE="/home/pablo/repos/MyProject/hardware/board.kicad_pcb"
OUTPUT_DIR="/home/pablo/repos/MyProject/fabrication"
# Run fabrication generation
flatpak run --command=python3 --filesystem=home \
org.kicad.KiCad \
generate_olivia_fabrication.py
# Upload to manufacturer (example)
curl -X POST https://api.jlcpcb.com/upload \
-F "gerber=@${OUTPUT_DIR}/fabrication_*.zip"
echo "Fabrication files generated and uploaded!"
```
### Python Integration
```python
# In your build script
import subprocess
import os
def generate_fabrication(pcb_path, output_dir):
"""Generate fabrication files for a KiCad PCB."""
# Modify generate_olivia_fabrication.py with paths
# (or pass as environment variables)
result = subprocess.run([
'flatpak', 'run',
'--command=python3',
'--filesystem=home',
'org.kicad.KiCad',
'generate_olivia_fabrication.py'
])
return result.returncode == 0
# Usage
if generate_fabrication("/path/to/board.kicad_pcb", "./output"):
print("Success!")
```
### CI/CD Integration (GitHub Actions)
```yaml
# .github/workflows/fabrication.yml
name: Generate Fabrication Files
on:
push:
branches: [main]
paths: ['hardware/**']
jobs:
fabricate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install KiCad Flatpak
run: |
sudo apt-get install flatpak
flatpak install -y flathub org.kicad.KiCad
- name: Generate Fabrication Files
run: |
flatpak run --command=python3 --filesystem=host \
org.kicad.KiCad \
generate_olivia_fabrication.py
- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: fabrication-files
path: fabrication_output/*.zip
```
## Related Documentation
- [FABRICATION.md](FABRICATION.md) - MCP server fabrication tools
- [README.md](README.md) - Main project documentation
- [QUICKSTART.md](QUICKSTART.md) - Quick start guide
- [EXAMPLES.md](EXAMPLES.md) - Usage examples
## Support
For issues or questions:
- Check the [Troubleshooting](#troubleshooting) section above
- Review KiCad Python API: https://docs.kicad.org/doxygen-python/
- Open an issue: https://github.com/Pablomonte/MCP-KiCad/issues
## License
Same as main project - MIT License.
---
**Pro Tip:** Keep the standalone script in your PCB repository for repeatable fabrication file generation. Version control the script alongside your hardware files for consistent manufacturing output.