Close

Diode Placement with Python and KICAD 7.0

A project log for A+ Split Keyboard

Over 104+ Key Split Keyborad

ironparkIronpark 05/10/2023 at 17:163 Comments

I found it necessary to adjust the position and orientation of the diodes across the board to make the routing effective. Fortunately, KICAD supports a Python console to automate this repetitive task, and since I had previously used KICAD to create scripts to automate part placement, I used my previous code to create a diode placement script.

KICAD does not guarantee that the PYTHON API will remain compatible between versions.
The script below only works with KICAD 7.x.
import pcbnew
from pcbnew import *

def SetPosition(footprint,x,y):
    footprint.SetPosition(pcbnew.VECTOR2I(pcbnew.wxPoint(x,y)))

def DiodeRePosition(relX,relY,angle):
    footprints = pcbnew.GetBoard().GetFootprints()
    diodeToKeyPosition = {}
    for f in footprints:
        ref = f.GetReference()
        pos = f.GetPosition()
        if "K_" in ref:
            keyNum = int(ref.split("_")[1])
            diodeRef = "D_{}".format(keyNum+1)
            diodeToKeyPosition[diodeRef] = [pos.getWxPoint().x,pos.getWxPoint().y]
            print(diodeRef,diodeToKeyPosition[diodeRef])
    for f in footprints:
        ref = f.GetReference()
        if "D_1" == ref:
            continue
        if "D_" in ref:
            [x,y] = diodeToKeyPosition[ref]
            f.SetOrientation(EDA_ANGLE(angle, DEGREES_T))
            SetPosition(f,x + FromMM(relX),y - FromMM(relY))
    pcbnew.Refresh()

DiodeRePosition(5,8.8,330)

 
  To create the above script, I referenced the official documentation and forum posts below

Discussions

Ken Yap wrote 05/10/2023 at 22:51 point

>KICAD does not guarantee that the PYTHON API will remain compatible between versions.

That's because it's not an API. It's a binding, autogenerated. As the code structure changes, so does the binding. Your first link above names it correctly as a binding. An API would be mostly stable, but it hasn't been implemented yet.

  Are you sure? yes | no

Ironpark wrote 05/11/2023 at 11:20 point

I understand the context of what you are saying, but in the official documentation, you can see a link to the automatically generated Doxygen documentation in the "Pcbnew Python API" description. (https://dev-docs.kicad.org/en/python/pcbnew/) If we consider API (Application Programming Interface) in a broad sense, we can say that the binding interface using SWIG is also an API.

  Are you sure? yes | no

Ken Yap wrote 05/11/2023 at 11:40 point

The KiCad developers have stated that it's not an API in the sense that people expect. So one can argue over the name but the fact remains that the bindings apply only to one major version. Lots of plugin developers had to revise their code between 6 and 7.

If the auto-generated bindings were stable then it would be a proper API. But they are not as the code structure is a work in progress.

  Are you sure? yes | no