Article Image
Article Image
View All Posts
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

I’ve been making a KiCad PCBNew action plugin recently for generating coils - you can watch a video of the results below, but I wanted to capture what I had learnt for future reference - mostly so that I have something to refer to, but hopefully this will be useful to others as well.

There’s some pretty reasonable documentation here: that gives you the basic structure of a plugin, but beyond that you’ll need to look at other peoples examples and the auto-generated documentation -

Quite often you’ll have to dive into the C++ code to really understand what is going on and how to use the API.

The first thing you will need to do in your plugin is get hold of the current board:

board = pcbnew.GetBoard()

Once you have this you can start adding things to it. You will probably want to add things to a particular layer and you may also want to assign tracks to a particular net. You can get the layer and net objects from the board:

front_copper = pcbnew.F_Cu
back_copper = pcbnew.B_Cu
front_silk = pcbnew.F_SilkS
back_silk = pcbnew.B_SilkS

If you go here: and search for F_Cu you’ll find the complete list of layer constants.

# find the matching net for the track
net = board.FindNet("NET NAME")
# optionally you can create the net if it doesn't exist - handy if you are creating a PCB from scratch
if net is None:
    net = pcbnew.NETINFO_ITEM(board, "NET NAME")

Once you have the layer and the net you can to use you can create tracks - you just need to specify the start and end points along with the width. You can also specify the layer and net:

track = pcbnew.PCB_TRACK(board)
track.SetStart(pcbnew.wxPointMM(x1, y1))
track.SetEnd(pcbnew.wxPointMM(x2, y2))
track.SetWidth(int(thickness * pcbnew.IU_PER_MM))

Similarly you can create vias - you just need to specify the position, the diameter and the drill size. You can also specify the layer and net:

pcb_via = pcbnew.PCB_VIA(board)
pcb_via.SetPosition(pcbnew.wxPointMM(x, y))
pcb_via.SetWidth(int(via_diameter * pcbnew.IU_PER_MM))
pcb_via.SetDrill(int(via_drill_diameter * pcbnew.IU_PER_MM))

One thing that I did find was that I needed to add the vias after I had added the tracks - if I didn’t do this it seemed to cause some errors when running the DRC checks.

Adding silk screen text is also pretty straightforward:

pcb_txt = pcbnew.PCB_TEXT(board)
pcb_txt.SetPosition(pcbnew.wxPointMM(x, y))
pcb_txt.Rotate(pcbnew.wxPointMM(x, y), text["angle"])
pcb_txt.SetTextSize(pcbnew.wxSizeMM(size, size))

If you want the text to flip to the other side of the board them you specify a bottom silk later, or you can just flip the text:

pcb_txt.Flip(pcbnew.wxPointMM(x, y), True)

The first argument is the point to flip around, and the second argument is whether to mirror the text as well.

You’ll probably also need some way of connecting things to your PCB - pins and pads to connect leads components and leads to your board.

I haven’t yet worked out how to add a footprint from the footprint libaries to the board, but I have worked out how to add pins and pads.

To create a plated through hole pin you can do this:

module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(pin_diameter, pin_diameter))
pcb_pad.SetDrillSize(pcbnew.wxSizeMM(pin_drill, pin_drill))
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))

And to create a pad you can do this - I’m still not sure on the use of LSET here, but it seems to work - if you change F_Cu to B_Cu the pad will be on the back of the board:

lset = pcbnew.LSET()
module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(width, height))
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad.Flip(pcbnew.wxPointMM(x, y), False)

To make mounting holes you can create pins, but change them so that they are not plated through holes:

module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(diameter, diameter))
pcb_pad.SetDrillSize(pcbnew.wxSizeMM(diameter, diameter))
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))

Finally you need to add edge cuts to the board - this is the outline of the board - to make a circular board you can do this:

circle = pcbnew.PCB_SHAPE(board)
circle.SetStart(pcbnew.wxPointMM(CENTER_X, CENTER_Y))
circle.SetEnd(pcbnew.wxPointMM(CENTER_X + radius, CENTER_Y))
circle.SetCenter(pcbnew.wxPointMM(CENTER_X, CENTER_Y))
circle.SetWidth(int(0.1 * pcbnew.IU_PER_MM))

Note the use to SetStart and SetEnd to set the radius of the circle - there’s no SetRadius method.

Hopefully the above will be useful for other people. I’ll add and update this post as I learn more and correct any errors in my code…


Related Posts

Scripting KiCad to make coils - Taking inspiration from PCB Motors, learn how to create customized PCB coils by automating the design process with a KiCad plugin and optimized Python algorithms to generate various coil shapes and patterns, making the PCB layout easier and more efficient.

Related Videos

Making PCB Coils the Easy Way - Replace yourself with a script! - Dive into the intriguing process of crafting custom PCB coils, inspired by Carl Bugeja's PCB Motors. Master the art of generating spirals, confronting challenges in creating arbitrary coil shapes, and automating the process with a KiCad plugin.
KiCad Tutorial - Custom Symbol, Footprint and 3D Model - Master the art of creating custom footprints, symbols, and 3D models in KiCad 6 with this comprehensive guide. Design your own LED filament for a custom PCB project, and marvel at your creation in a 3D preview!
First KiCad board from an EasyEDA user - Improve your I2S stereo amplifier board by creating a version 2 using KiCad. Follow this tutorial to set up design rules, import symbols and footprints, generate the PCB layout, add wiring and create custom board outlines.
Sneak Preview of work in progress. - Learn about the intriguing process of designing and experimenting with coils on printed circuit boards (PCBs) in this captivating video, featuring captivating insights and unique applications inspired by the talented @CarlBugeja.
Getting started with Raspberry Pi Pico - MicroPython - Dive into this Raspberry Pi Pico tutorial where you'll solder headers, set up Thonny, write codes in MicroPython, and create a blinking LED project to kick off your Pico journey.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


> Image


A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts