Python Parser for .kicad_pcb

Hi,

(I’m new to KiCAD) and trying to write a python program that will parse my .kicad_pcb file so I can automagically align some of my numerous components. I have looked at a number of projects that have parsers (e.g. pykicad) but they don’t seem to be able to handle my file (I’m using Version: (5.1.5)-3, release build).

Specifically my .kicad_pcb has multiple user_trace_widths and possibly some other features that maybe rate it as tricky to parse.

Please point me in the direction of a popular/maintained python kicad file parser and I’ll try and take it from there… even it needs some TLC to change it.

Using Python to manipulate the files seems like a very strong feature of KiCAD so it would be nice if I could get it to work

Thanks

Why not use the KiCad internal python API?

I came a bit unstuck with that route… https://docs.kicad.org/doxygen/md_Documentation_development_pcbnew-plugins.html has a link to “user documentation” but the link is 404.

It sounds like what you are suggesting is a good idea… just not quite sure how to get started.

Thanks

I’m making some reasonable progress… managed to configure PyCharm with the Python27 from KiCad and get the libraries pointing in the right direction…

import pcbnew

pcb = pcbnew.LoadBoard("../myDesignCOPY.kicad_pcb")

for module in pcb.GetModules():
    print "* Module: %s"%module.GetReference()

What I would like to do is enumerate components (seems to be 1:1 with Module) obtain their At position and move them according to my own rules.

I don’t see .At on module? I just need to apply a dx dy to the components that match a regexp.

Again any pointers to examples or documentation would be good… I’m not looking for someone to write this for me!

The place footprints action plugin might be what you are looking for. If it is not useful directly, at least it can serve as a code example.

As for documentation I’ve got two bookmarks:
https://kicad-downloads.s3.cern.ch/doxygen-python/namespacepcbnew.html
https://docs.kicad.org/doxygen-python/namespacepcbnew.html

I don’t know what is the difference though

2 Likes

Thanks. I definitely think I’m heading in the right direction now…

import pcbnew

pcb = pcbnew.LoadBoard("../myDesignCOPY.kicad_pcb")

for module in pcb.GetModules():
    ref = module.GetReference()
    print "* Module: %s"%ref
    p = module.GetPosition()

    if ref == "D22":
        p.x = 123
        module.SetPosition(p)

pcb.Save("../myDesignCOPY.kicad_pcb_mod")

It’s not the easiest since it’s all SWIG and hence intellisense doesn’t behave well in PyCharm but it looks do-able. Thanks!

PS
The plugin does looks very similar to what I wanted to write! ROFL

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.