Export to SVG from Python

Is it possible to do this

from Python? How? I have zero experience with Python+KiCad, any hint is appreciated, I find many links but are quite confusing. I could not install pcbnew Python module using pip.

As an alternative, you can also use kicad-cli

type or paste code herepaul@cezanne:~$ kicad-cli pcb export svg
input: 1 argument(s) expected. 0 provided.
Usage: svg [--help] [--output OUTPUT_FILE] [--layers LAYER_LIST] [--drawing-sheet SHEET_PATH] [--define-var KEY=VALUE] [--mirror] [--theme THEME_NAME] [--negative] [--black-and-white] [--page-size-mode MODE] [--exclude-drawing-sheet] [--drill-shape-opt SHAPE_OPTION] INPUT_FILE

Generate SVG outputs of a given layer list

Positional arguments:
  INPUT_FILE               Input file 

Optional arguments:
  -h, --help               Shows help message and exits 
  -o, --output             Output file [nargs=0..1] [default: ""]
  -l, --layers             Comma separated list of untranslated layer names to include such as F.Cu,B.Cu [nargs=0..1] [default: ""]
  --drawing-sheet          Path to drawing sheet, this overrides any existing project defined sheet when used [nargs=0..1] [default: ""]
  -D, --define-var         Overrides or adds project variables, can be used multiple times to declare multiple variables.
                           Use in the format of '--define-var key=value' or '-D key=value' [nargs=0..1] [default: {}]
  -m, --mirror             Mirror the board (useful for trying to show bottom layers) 
  -t, --theme              Color theme to use (will default to PCB editor settings) [nargs=0..1] [default: ""]
  -n, --negative           Plot as negative (useful for directly etching from the export) 
  --black-and-white        Black and white only 
  --page-size-mode         Set page sizing mode (0 = page with frame and title block, 1 = current page size, 2 = board area only) [nargs=0..1] [default: 0]
  --exclude-drawing-sheet  No drawing sheet 
  --drill-shape-opt        Set pad/via drill shape option (0 = no shape, 1 = small shape, 2 = actual shape) [nargs=0..1] [default: 2]

1 Like

Thanks, that helps a lot in the automation. What I don’t know is how to automatically handle the number of inner copper layers. If I invent an inner copper layer that is not present in the design, for example In11.Cu, it will export an image of an empty layer, although the in the design it only has 4 layers. I was expecting to get this:

$ kicad-cli pcb export svg my_project.kicad_pcb --layers dont_exist --output deleteme.svg
Invalid layer name "dont_exist"
At least one layer must be specified

Do you know how to (easily) parse the .kicad_pcb file to get the actual layers?

Maybe you can do something with:

https://dev-docs.kicad.org/en/file-formats/sexpr-pcb/index.html

Another option is to generate all layers, and then delete files from your script, depending on their file size.

Additonally, KiCad-cli is also still being deveoped, just like the rest of KiCad, and it may be worthwhile to make a feature request for this.