Kicad python Scripting

This write-up and the python code are OUT OF DATE. There are some omissions of PCB features that are not handled by this code. And the Kicad file format for PCBs has changed from the version used in this video. I hope it is similar enough to be able to adapt the code to handle the current format. I will update this page when I update my KiCad installation. Sorry if this is a problem.

Give Up and Do PCBs

If you have seen the Serial LED project at all you may know that I have scheduled a build of a 1:18 Crown Victoria police car. I tried several ways to figure out how to build WS2812 LEDs into a roof mounted light bar, along with deck, grill, push-bar, backup, head, and tail lights. I originally wanted a way to hand solder, point to point, all the connections and mount them “simply” just for a demo. Well, after trying four different schemes, and failing, I finally gave up and decided to go with PCBs. I don’t have the capability to make reliable plated-through PCBs and drill all these holes. So the last option was to learn Kicad schematic capture, module building, and panelization.

The first design went pretty well. I had the WS2812s crammed right up against each other for the roof mount light bar. I was planning to lean the PCB back to reduce their over-all height. Visibility should still be good. The main problem was going to be the alley lights on the ends. I told myself I would think of something when the time came (ha ha). So after that 8-light PCB was finished, the 4 and two-light PCBs went pretty quickly.

So, these boards were pretty small. If I sent them to a board house as just the single PCB, it was going to be pretty expensive and pretty well wasteful of real estate.. So I realized that I need to put multiple PCBs on a panel and submit that to the board house as my PCB. And that was really the tedious part. Figuring out the spacing and placing them by hand with the routing lines — that will drive you nuts pretty quickly. But I got them all done.

And when I was ready to send them to OSHPARK, I discovered the SK6812. This is a “mini” version of the WS2812. (Now, I think they are called Mini 3535. But who knows when it comes to Chinese documentation? Jezz!) They’re 3.5mm square as opposed to 5mm square. So I ordered a handful of those. These would work so much better. I had not ordered PCBs yet for the WS2812s, so, start over! I went through the entire process again. I did the 2-light version first, and when it came time to do the panel for them, it had been a good while since I had finished the WS2812 panels. But when I finished this one, I told myself, “Never again!”.

There is a lot of interest in python scripting for Kicad. There is a lot of stuff that can be automated, especially panelization. I looked around on the WEB for something ready to go. I did not find one that I thought would work for me, or if it did, I could not readily understand how the code was working. I did want something I could maintain and improve. So I decided to roll my own.

Now, my code is dumber than most. Since I did not know the best way to organize the code, I just wrote it as I thought about each task. The script is very procedural and does not have any classes. But it does the one thing that I want, and that’s good enough for me. There are no specific operations with Kicad itself. This is all pure file reading/writing and text creation/editing. I think if you are interested in learning python and python for Kicad, then this may be a good script to get some experience with. Just don’t use it as a guide to good scripting practice. I commented it pretty heavily, so I hope that helps.

Very quickly, I’ll run down the script functionality. The basic idea is to give the PCB dimensions, the number of rows and columns, and the file path to the script and let it lay out 1mil routing which leaves a mouse bite in the center of each PCB side. (I do not have a mouse bite footprint. OSHPARK will put them in for you.) First, create a copy of your single PCB layout. On linux I just use the shell to make a copy. The script starts the panel’s top left corner at (50mm, 50mm), so move the copy away from that area. The script defines some constants, then calculates the horizontal and vertical routing and saves it. Next, the panel outline is calculated and saved. Then the XY origins for each PCB placement are calculated and saved.

Now it finds the top left corner of the board outline for the PCB on the drawing (the one you are duplicating). That will be used to “normalize” the position for all boards. Finally, the records for the PCB on the page are saved and will be used as raw data to generate the data for the PCBs that will be placed. All that remains now is to modify the XY points in the data for each PCB and save all that in a list. When that is done, the script writes all the saved data to the file. Waiting until all of the data is ready simplifies writing the file. Note that a Kicad PCB file ends with an unmatched “)” as an end-of-file marker. (I think.)

Limitations:

  • 10mm blank boundary
  • Only rectangular PCBs are accomodated.
  • May have “cut-outs” if there is a full width/height area where the mouse bite will fall.
  • Only 1mil routing allowed.
  • Board outline is on the edge-cuts layer.
  • Routing between PCBs is on the front silkscreen layer.
  • No rotations or translations of PCB position are supported.
  • Be sure the PCB being duplicated does not lie in the vertical or horizontal plane of the panel.

Light Bar Project

I received the first PCB designed for two-light usage: grill lights and head lights, at least. I figured I could also use them as stand-ins for the single position tail and backup lights too. Panelizing this way gets me 20 PCBs per panel. OSHPARK made 3 of these for $35.00. Anyway, here is how they turned out:

PCB picture

PCB picture

On the left is a shot that highlights the drill holes — a bunch of ’em. There are “vias on pad”. I asked for plugged and plated-over vias, but they don’t do that. The 4 circular pads in the center are for 5v, GND, Din, and Dout. On the right is the LED side. No room for much of a silkscreen (10.35mm x 4.35mm). If you compare my footprint with the back of an SK6812, you’ll see that I messed up. I’m hoping some capton tape and lots of solder paste will save these boards. Oh well, they’re just prototypes!

Download

This download is the python 2.7.3 script mPanel.py. I’ll call it version 0.1. If you see a bigger version number, you’ll know it has been updated. It unzips into the current directory.

Download python panelization script:  mPanel-0.1.zip



©copyright 2016 pretzelogic LLC. All rights reserved.
No part of this page may be reproduced without permission.
Software, schematics, and text are presented as reference works only.
No claim as to useability, suitability, or correctness for any application is made.