Creating a KiCAD Plugin - Auto Export Front Paste Layer
Posted: 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:
The Python 2.7 (used by KiCAD 5.1) script that does the export:
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'
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.
It took a bit or experimenting to figure out and mine was based on this sites:
- http://scottbezek.blogspot.com/2016/04/ ... ports.html title says it all
- https://docs.kicad.org/5.1/en/pcbnew/pc ... ction_menu how to create a KiCAD plug-in
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):
self.name = "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()
po.SetPlotFrameRef(False)
# 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())
pc.PlotLayer()
pc.ClosePlot()
print("Finished")
SimplePlugin().register() # Instantiate and register to Pcbnew
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.