CLI invocation if KiCAD commands

I am trying to automate two things from KiCAD.

One is the bitmap2component process of opening a JPG and converting it to a library file. The second is running the plotter to generate gerbers from a kicad_pcb file.

Are either of these commands accessible from the command line in any way? Or otherwise automatable in some fashion?

(In MacOS if that makes a difference)

Thanks!

There is no direct CLI invocation in Kicad to do this sort of thing but PCBNew is scriptable and has a Python abstraction layer. This is in a state of flux at present and scripting is not available on all platforms/versions. However, in macOS, you are in luck - both in 4.0.7 and 5.0.1 support python scripting. There is still a bit of finangelling required to get this to work.

There is some useful material about Python scripting here (but bear in mind the comments about the current ongoing changes).

The following script will work on macOS and will generate gerbers. Save the script somewhere in your path or run it in situ. Change the path where you want the output directory (line 11) Invoke it from the command line with the path to a *.kicad_pcb file e.g.

./plotter.py ~/EDA_Workspace/Projects/Thermocouple_datalogger/Ver_5V/ThermocoupleLogger.kicad_pcb

Script;

#!/Applications/Kicad/kicad.app/Contents/Applications/pcbnew.app/Contents/MacOS/Python

import sys
import os

sys.path.insert(0, "/Applications/Kicad/kicad.app/Contents/Frameworks/python/site-packages/")
import pcbnew
from pcbnew import *

file_name = sys.argv[1]
output_dir = '~/Desktop/gerbers'

try:
os.makedirs(output_dir)
except OSError:
pass

board = pcbnew.LoadBoard(file_name)
pctl = pcbnew.PLOT_CONTROLLER(board)
popt = pctl.GetPlotOptions()
popt.SetOutputDirectory('~/Desktop/gerbers')
popt.SetPlotFrameRef(False)
popt.SetLineWidth(pcbnew.FromMM(0.1))

popt.SetAutoScale(False)
popt.SetScale(1)
popt.SetMirror(False)

popt.SetUseGerberAttributes(True)
popt.SetUseGerberProtelExtensions(True)

popt.SetExcludeEdgeLayer(True)
popt.SetUseAuxOrigin(False)
pctl.SetColorMode(True)

popt.SetSubtractMaskFromSilk(False)
popt.SetPlotReference(True)
popt.SetPlotValue(False)

layers = [
("F.Cu", pcbnew.F_Cu, "Top layer"),
("B.Cu", pcbnew.B_Cu, "Bottom layer"),
("F.Paste", pcbnew.F_Paste, "Paste top"),
("B.Paste", pcbnew.B_Paste, "Paste bottom"),
("F.SilkS", pcbnew.F_SilkS, "Silk top"),
("B.SilkS", pcbnew.B_SilkS, "Silk top"),
("F.Mask", pcbnew.F_Mask, "Mask top"),
("B.Mask", pcbnew.B_Mask, "Mask bottom"),
("Edge.Cuts", pcbnew.Edge_Cuts, "Edges"),
]

for layer_info in layers:
pctl.SetLayer(layer_info[1])
pctl.OpenPlotfile(layer_info[0], pcbnew.PLOT_FORMAT_GERBER, layer_info[2])
pctl.PlotLayer()

pctl.ClosePlot()

bitmap2component will only (AFAIK) take bitmap (bmp) images and convert them. You would have to convert from jpg to BMP first. The library format converts to polylines. I think you could probably do this with Inkscape but haven’t tried.

EDIT Further thought - you could try using a workflow based on bmp -> svg using the excellent potrace utility [http://potrace.sourceforge.net] and then converting your svg to a footprint using svg2mod script https://github.com/mtl/svg2mod.

It should be easy enough to tie those together with a bit of bash fu if you have a lot of bitmaps to do.

1 Like

Oh, great news on the plotting capabilities and integration with Python!

As for bitmap2component, I have been using PNGs actually, but it’s always worked upon import. Is there a python abstraction layer to b2c by chance? I’ve tried several of the converters from SVG and always got garbled library files.

What exactly is b2c converting images to? Is that some standard kind of format that I could create in another way?

svg2mod works great for me.

Like @Rene_Poschl said, I I have had good results with svg2mod.

The mod file format is described quite nicely here https://www.compuphase.com/electronics/LibraryFileFormats.pdf (I am sure there is a proper Kicad reference but this is one I have been referring to recently. The new format is s-expression which is a based on arcs/lines/circles and polygons so similar to svg. Have a look at both in a text editor.

There is no Python abstraction to bitmap2component nor any plans for one AFAIK. However, I would have thought that you could cobble together a workflow.

Try something like this - a little Bash script - takes all bmp in a directory, converts to same named svg and then to footprints?

for f in *.bmp; do
    n=${f%.bmp}
    potrace  $f -s -o $n.svg
    svg2mod --input-file $n.svg --output-file $n
done

Enjoy!

I’ll give it a shot. Perhaps there was something weird about my experiences with svg2mod when I tried it out previously.

Thanks a lot!

1 Like

A bitmap file isn’t necessarily a .bmp file. Just like a car isn’t necessarily a Ford Mustang. Jpegs, PNGs, TIFFs, ILBM (originally an Amiga image format), Targas, etc are all bitmap files. As they are maps of image bits.

I haven’t had any file format problem feeding jpegs or PNGs to b2c for logo conversion. Somehow I doubt it would read an ILBM, but I haven’t had access to any in so long that I haven’t tried. (I could be surprised and it can take it, as the choose image requester offers .iff (a common, but inaccurate, extension for ILBM) as a supported image file.)

Good point - TBH I hadn’t actually tried other formats, b2c seems more capable than I thought.

This is a good workflow, but potrace doesn’t create inkscape layers which svg2mod requires in its conversions it seems.

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