Kicad command line method to assemble gerber files

As a follow-up question to this question

I wanted to ask if there is a method to use a command line command to let kicad generate the .drl and .gbr file. I already use a script to stuff all these files in a zip file for manufacturing. But the one thing I still cannot do, is to automate the gerbers assembly.

I have not yet found out how I could do this or whether it is even possible to begin with.

Any pointers would be greatly appreciated.

Kind regards,

Bas

P.S.
if somebody ever makes a scripts which can automatically upload gerbers to jlcpcb, order and pay for them as well as well, please let me know :wink:

In version 6 everything is already included

There is this in the SWIG interface;

pctl.OpenPlotfile(aSuffix: 'wxString', aFormat: 'PLOT_FORMAT', aSheetDesc: 'wxString') -> 'bool)

You should be able to do something like this - I’ve pulled out a bit from the plotpcb.py part of KiDiff and changed the SVG to GERBER. Untested!

import pcbnew as pn
board = pn.LoadBoard(board_path)
pctl = pn.PLOT_CONTROLLER(board)
enabled_layers = board.GetEnabledLayers()

layer_ids = list(enabled_layers.Seq())

layer_names = []
for layer_id in layer_ids:
    layer_names.append(board.GetLayerName(layer_id))
max_string_len = max(layer_names, key=len)

for i, layer_id in enumerate(layer_ids):
    pctl.SetLayer(layer_id)
    layer_name = board.GetLayerName(layer_id).replace(".", "_")
    layer_filename = os.path.join(board_name + ".gbr")
    pctl.OpenPlotfile(plot_sufix, pn.PLOT_FORMAT_GERBER, layer_name)
    pctl.PlotLayer()

Hey,

Thank you for your answer. I have upgrade to kicad 6 (was still on 5). At first I had difficulties with the pcb module in python. To fix that issue I used the python binary in the kicad folder instead of the usual python binary.

Currently I can run the script with:

/c/Program\ Files/KiCad/6.0/bin/python.exe plot.py

You forgot to import os, so that I added.

The python script ‘plot.py’

import pcbnew as pn
import os
board_name = "trainCatcher"
board = pn.LoadBoard("trainCatcher.kicad_pcb")
pctl = pn.PLOT_CONTROLLER(board)
enabled_layers = board.GetEnabledLayers()

layer_ids = list(enabled_layers.Seq())

layer_names = []
for layer_id in layer_ids:
    layer_names.append(board.GetLayerName(layer_id))
max_string_len = max(layer_names, key=len)

for i, layer_id in enumerate(layer_ids):
    pctl.SetLayer(layer_id)
    layer_name = board.GetLayerName(layer_id).replace(".", "_")
    layer_filename = os.path.join(board_name + ".gbr")
    pctl.OpenPlotfile(plot_sufix, pn.PLOT_FORMAT_GERBER, layer_name)
    pctl.PlotLayer()

I have to fill in something for plot_sufix but I do not know what.

Kind regards,

Bas

Something like?

    plot_sufix = str(layer_id).zfill(2) + "-" + layer_name

If you don’t want to write the script yourself, see here:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.