Creating a KiCAD Plugin - Auto Export Front Paste Layer

Post Reply
Guru Maker
Posts: 603
Joined: Tue Jun 24, 2014 8:25 pm
Location: Busselton

Creating a KiCAD Plugin - Auto Export Front Paste Layer

Post by parkview » Mon Aug 09, 2021 10:42 pm

I laser cut my own plastic stencils at home. To make it easier to export the KiCAD Front Paste layer, I created a KiCAD V5.1 plug-in. This might need to be re-written for KiCAD V6?

It took a bit or experimenting to figure out and mine was based on this sites: Note: URL might change when the docs are upgraded for upcoming KiCAD V6.

The Python 2.7 (used by KiCAD 5.1) script that does the export:

Code: Select all

import pcbnew
import os

class SimplePlugin(pcbnew.ActionPlugin):
    def defaults(self): = "Export Stencil"
        self.category = "Stencil Exporter"
        self.description = "This will export the Front Paste layer as a SVG file"
        self.show_toolbar_button = True # Optional, defaults to False
        self.icon_file_name = os.path.join(os.path.dirname(__file__), 'Stencil.png') # Optional, but needs to be a 26x26px png file

    def Run(self):
        # The entry function of the plugin that is executed on user action
        # Load board and initialize plot controller
        board = pcbnew.GetBoard()  # where to get the current board name from?
        pc = pcbnew.PLOT_CONTROLLER(board)
        po = pc.GetPlotOptions()
        # Set current layer
        pc.SetLayer(pcbnew.F_Paste)   # only export the front paste layer
        print("step 1")
        # Plot single layer to file
        pc.OpenPlotfile("front_paste", pcbnew.PLOT_FORMAT_SVG, "front_paste")
        print("step 2")
        print("Plotting to " + pc.GetPlotFileName())

SimplePlugin().register() # Instantiate and register to Pcbnew
I placed the above script into it's own Windows sub-folder in the KiCAD plugins folder: 'C:\Program Files\KiCad\share\kicad\scripting\plugins'
stencil-folder.jpg (9.2 KiB) Viewed 13107 times
From memory(!), I then had to open KiCAD and click on PCBNew menu items: Tools | External Plugins | Refresh Plugins and it auto installed itself. I had a few issues with my PNG Stencil icon not showing up, but that was because I didn't have it as a 26x26 pixel file. Not many pixels to play with. Mine is meant to be some stencil pads, but it's hard to make that out. You know when it's installed when you see the icon added onto the Plug-in location on the PCBNew screen. I believe KiCAD V6 will use Python 3.

When run, the script will export the PCBNew Front Paste layer as a SVG file and place it in the projects folder. I then import the file into Lightburn software so I can raster etch my plastic stencil: viewtopic.php?f=12&t=2454 This works really well for my projects.
export-plugin.jpg (13.91 KiB) Viewed 13107 times

Post Reply