Python Print Interface

Currently I can find a python interface to plot individual PCB layers to PDF with one PDF per layer. If I try to plot multiple layers to a single PDF this does not work. Now this is expected as even the plot GUI does not have a method to plot multiple layers to a single PDF.

My question is then, how can the Print interface be used to generate a single PDF with multiple layers? Looking at the print dialog window this can be done but I can’t seem to find this ability within the python API. Is the ability there?

It is very unlikely this functionality will exist until v7 where a new outputs system will be implemented.

1 Like

Okay, so I do have the right understanding. With V6 the print API is not exposed.

Doable in 5.1.x. This is something I’ve dabled with a long time ago. I got PDFs

def generate_pdf(kicad_pcb_file, temp_dir, layer, layername):
    # Load board and initialize plot controller
    board = pcbnew.LoadBoard(kicad_pcb_file)
    pc = pcbnew.PLOT_CONTROLLER(board)
    po = pc.GetPlotOptions()
    po.SetPlotFrameRef(False)

    # Set current layer
    pc.SetLayer(layer)

    # Plot single layer to file
    pc.OpenPlotfile(layername, pcbnew.PLOT_FORMAT_PDF, layername)
    file_temp = pc.GetPlotFileName()
    pc.PlotLayer()
    pc.SetLayer(layer)
    pc.ClosePlot()
    # rename file
    black_and_white_file = os.path.join(temp_dir, os.path.basename(file_temp))
    # move to temporary folder
    shutil.move(file_temp, black_and_white_file)

    return black_and_white_file


def generate_pdfs(kicad_pcb_file, temp_dir):
    output_filename = generate_pdf(kicad_pcb_file, temp_dir, pcbnew.F_Cu, 'F_Cu')

You may also want take a look at KiAuto. I used it to export some documents which i can’t export via Python.

What you mention is the “Plot” interface, not the “Print” interface. Plot allows generation of a Gerber/PDF/DXF of a single layer.

The python example that you reference also showcases the “Plot” interface. As you will see, if one was to use multiple SetLayer(), only the most recent layer that that was called with will hbe plotted.

What I am after is having a document that has multiple layers in a single image, for example edge cuts, dimension, and silkscreen.

Thanks, I did not know about KiAuto. This will achieve what I need, even if its in a round about way.

Thanks for pointing out the difference.
You might misuse the plot interface to generate svg files, and then use Inkscape to color each layer as you want to and join all layer into one file.

That is the method that I currently use, export all layers as SVG without frame, then have python call a post process step to merge all into a single image. A roundabout way but does kind of work as needed.

Ideal would be to not have to do additional post processing and just use the KiCAD scripting interface directly.

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