Author Topic: KiCad scripting - assigning refs to components while respecting spatial layout  (Read 662 times)

0 Members and 1 Guest are viewing this topic.

Offline kmmTopic starter

  • Contributor
  • Posts: 46
  • Country: us
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.
2430927-0

Really all I'm looking for is a link to an example of this sort of use case, if anyone has one handy.
« Last Edit: November 10, 2024, 01:28:12 am by kmm »
 

Online abeyer

  • Frequent Contributor
  • **
  • Posts: 406
  • Country: us
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.
« Last Edit: November 10, 2024, 05:43:53 am by abeyer »
 

Offline MitjaN

  • Contributor
  • Posts: 35
  • Country: si
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
 

Offline ksjh

  • Regular Contributor
  • *
  • Posts: 50
  • Country: de
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 the Tools menu. Would that work for your purpose?
 

Offline kmmTopic starter

  • Contributor
  • Posts: 46
  • Country: us
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 the Tools menu. Would that work for your purpose?

I believe this is exactly what I need, thanks!
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3964
  • Country: nl
Replacement of SWIG with a more stable interface is tracked here:

https://gitlab.com/kicad/code/kicad/-/issues/9078


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 the Tools menu. Would that work for your purpose?

I believe this is exactly what I need, thanks!
After you have done a Geographical Re-annotate the schematic and the PCB are out of sync. You can push those changes back to the schematic with PCB Editor / Tools / Update Schematic from PCB, but then the annotation in the schematic is a big mess. A much better option is to use Schematic Editor / Tools / Update PCB from Schematic [F8] and then use the Re-link footprints to schematic symbols based on their reference designators option. 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.

 

Offline kmmTopic starter

  • Contributor
  • Posts: 46
  • Country: us
After you have done a Geographical Re-annotate the schematic and the PCB are out of sync. You can push those changes back to the schematic with PCB Editor / Tools / Update Schematic from PCB, but then the annotation in the schematic is a big mess. A much better option is to use Schematic Editor / Tools / Update PCB from Schematic [F8] and then use the Re-link footprints to schematic symbols based on their reference designators option. 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.

The 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 function Create 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf