Generating fabrication files (Gerber) with pcbnew's Python API
Export a board's Gerber files by script, without going through KiCad's plot dialog — with pcbnew or the IPC API.
Manually regenerating Gerbers from the File → Plot dialog after every board revision is easy to forget. Scripting the export guarantees up-to-date fabrication files on every build, and fits naturally into a CI pipeline that validates a board before it’s sent to the fab house.
Configuring and running the plot
PLOT_CONTROLLER drives the export layer by layer. Set the output directory, then open and close a plot file for each layer:
from pcbnew import PLOT_CONTROLLER, PLOT_FORMAT_GERBER, F_Cu
pctl = PLOT_CONTROLLER(board)
popt = pctl.GetPlotOptions()
popt.SetOutputDirectory("gerbers")
pctl.SetLayer(F_Cu)
pctl.OpenPlotfile("F_Cu", PLOT_FORMAT_GERBER)
pctl.PlotLayer()
pctl.ClosePlot()With the IPC API, the export goes through a higher-level call on the board, which handles layer configuration internally:
from kicad import KiCad
kicad = KiCad()
board = kicad.get_board()
board.export_gerbers(output_dir="gerbers", layers=["F.Cu", "B.Cu"])Exporting the drill file
Drilling uses a dedicated controller, EXCELLON_WRITER, separate from copper layer plotting:
from pcbnew import EXCELLON_WRITER
drill_writer = EXCELLON_WRITER(board)
drill_writer.SetOptions(False, False, board.GetDesignSettings().GetAuxOrigin(), False)
drill_writer.CreateDrillandMapFilesSet("gerbers", True, False)Same idea on the IPC API side, in a single call:
board.export_drill_files(output_dir="gerbers")Going further
This script drops directly into a CI step: on every commit that changes a .kicad_pcb, automatically regenerating the Gerbers and diffing them against the previous version catches unintended fabrication changes before the board ships to the fab house.