KiCad scripting - assigning refs to components while respecting spatial layout - Page 1
Forum section: electronics / general

author: kmm
author_rank: Regular Contributor
timestamp: November 10, 2024, 01:24:23 am
content: For some context I'm a Python guy (day job is mostly Python-based IT automation glue) but I'm not really sure where to start with scripting KiCad. The docs are (generously) not particularly great, the API is not particularly "pythonic", and as far as I can tell docs haven't been updated for the 8.x version I'm using.I have this LED matrix and all I want to do with it is iterate through all the LED footprints from top to bottom as on the drawing and assign a sequential ref (D1 .. D576) to each LED (row one would be D1 .. D24, row 2 would be D25 .. D48, and so on). Reasoning is just that it's much easier to lay out the LEDs as untagged footprints and then sync them to the schematic after the fact, since as far as I can tell there's no way to tell Pcbnew to lay out components with a geometry or regular spacing. Shuffling around footprints imported from the schematic with the mouse 576 times sounds like pain.Really all I'm looking for is a link to an example of this sort of use case, if anyone has one handy.

author: abeyer
author_rank: Frequent Contributor
timestamp: November 10, 2024, 05:42:19 am
content: A few things:1. The python bindings are SWIG generated, and in my experience they are about as usable as any autogenerated ones will get (which as you point out is not great) without a lot of documentation work. Failing that you're assumed to be familiar or willing to get familiar with the underlying code SWIG is wrapping.2. The python bindings, last I heard, aren't intended to be the future and may not be getting much love. There's a new IPC based interface that I think is available in recent versions, though I haven't tried it myself.3. KiCad files are lispy text content, and easy to parse and manipulate as such. If you don't absolutely need live interaction in the application interface itself, an external script that transforms your schematic/pcb file in place and the reloading in into KiCad tends to be a faster approach to this kind of this in my experience.

author: MitjaN
author_rank: Contributor
timestamp: November 10, 2024, 07:50:16 am
content: Take a look at PlaceFootprints action plugin (https://github.com/MitjaNemec/PlaceFootprints). It might even be what you need. Otherwise you can at least look at the code for ideas

author: ksjh
author_rank: Regular Contributor
timestamp: November 10, 2024, 08:17:07 am
content: I have not used KiCad Python scripting that often, but I found that KiCad has a built-in function to re-annotate footprints in the PCB editor:Geographical Reannotate...in theToolsmenu. Would that work for your purpose?

author: kmm
author_rank: Regular Contributor
timestamp: November 10, 2024, 02:33:33 pm
content: Quote from: ksjh on November 10, 2024, 08:17:07 amI believe this isexactlywhat I need, thanks!

author: Doctorandus_P
author_rank: Super Contributor
timestamp: November 10, 2024, 02:52:10 pm
content: Replacement of SWIG with a more stable interface is tracked here:https://gitlab.com/kicad/code/kicad/-/issues/9078Quote from: kmm on November 10, 2024, 02:33:33 pmAfter you have done aGeographical Re-annotatethe schematic and the PCB are out of sync. You can push those changes back to the schematic withPCB Editor / Tools / Update Schematic from PCB, but then the annotation in the schematic is a big mess. A much better option is to useSchematic Editor / Tools / Update PCB from Schematic and then use theRe-link footprints to schematic symbols based on their reference designatorsoption. This option is normally off, and in that case KiCad uses (invisible to the user) UUID's to match schematic symbols with footprints on the PCB.

author: kmm
author_rank: Regular Contributor
timestamp: November 10, 2024, 05:34:24 pm
content: Quote from: Doctorandus_P on November 10, 2024, 02:52:10 pmThe latter part of this is pretty much the workflow I had figured out, and with manual renaming of a sample of parts it works great!After playing with Geographical Annotate and not being able to figure out how to just rename all the LEDs without regard to what's already there (REF** would turn into DREFnnn using "D" prefix option and there doesn't seem to be a way to just tell it to smash what's there). I was about to start writing an `awk` one liner to transform the board source (regex "REF**" to "D" then run Geographical and let it handle the spatial sorting part) when I came across the context menu functionCreate from Selection->Create Array...which generates EXACTLY the X-Y grid I need with the correct sequencing of refs.Thanks for all the helpful suggestions. This is pretty much my whole experience with KiCad and why I never bothered to learn the SWIG bindings, it usually can do what I need it to without any scripting it's just a matter of finding the feature and what submenu it's buried in. The IPC interface looks like a much nicer approach than the old bindings though, I'll have to play with it.