Python Bindings to Access PCB Stackup Data

Hey there. I’m looking for some help. For our Coil Generator plugin we want to add a “stats panel” that shows the DC resistance, the inductance etc. To do that, we would like to read the parameters from the board stackup as it already has all the info we need so the user does not have to enter it manually for the calculations.

I’m pretty sure I’m able to access that information via Python because the python bindings seem to expose everything and this looks like what I’m looking for. However I’m unable to get the correct reference.

I think I’m close, but not there yet. The closest I got is:

pcbnew.GetBoard().GetDesignSettings().GetStackupDescriptor()

This returns the following: <Swig Object of type 'BOARD_STACKUP *' at 0x000001C43ADA5050> with these attributes: 'acquire', 'append', 'disown', 'next', 'own'

I think the issue here is that the StackupDescriptor isn’t actually the stackup that I’m looking for. I already tried looking through the C++ code, through other addons and ChatGPT, none of it helped. I guess something like this is possible, if I knew the correct function names:

board = pcbnew.GetBoard()
stackup_manager = board.GetStackup()

for i in range(stackup_manager.GetCount()):
    stackup_item = stackup_manager.GetItem(i)

    layer_name = stackup_item.GetLayerName()
    thickness = stackup_item.GetThickness()
    epsilon_r = stackup_item.GetDielectricConstant()

My current approach is to look through anything that looks promising by printing the result of [method for method in dir(pcbnew.GetSettingsManager()) if method.startswith('__') is False]. But so far, without any results.

Thank you in advance for any pointers!

No, it’s fine.

But, looks like what you are looking for is just not available in SWIG right now @Mineotopia

ah, bummer. Afaik KiCad 9 will ship with an API, is this still planned? If so, will something like this be accessible via API?

Because if it is, then I won’t bother adding it until KiCad 9 is released.

That’s the hope. Note that the new API is totally different from the existing SWIG system. But, exposing the board stackup is possible.

1 Like

awesome, I’m really looking forward to that! I’ll wait with this feature then until version 9 is out. Thanks a lot for your work on KiCad

@Mineotopia
Assuming you know enough about Python (or other) and know what items from stackup and PCB you want to use, getting what you want is very simple. Very!

Just code to read the PCB file and search for the items of interest to you and take desired action on them.

Screenshot below shows snippet of a PCB file’s stackup section…
For inductance/etc you can serarch it for Track Length…

It’s all right there and if something else wanted, use the Kicad_pro file to search… these are Text files so, the world is your Oyster, so to speak…

1 Like

I thought about this as well, however there are edgecases where this will break. E.g. a user wants to create a coil, sees that the inner layers have the wrong thickness and changes this in the stackup. Then attempts to create a new coil. The changes are only detected if the user saves their project after the settings were changed.

While this probably is ok, I think waiting until v9 is released is the cleaner approch here. But thank you!

I must be missing something as it seems to me; whatever a User does, they will (most likely) want to Save the work unless it’s a temporary change.

And, it seems you ‘assume’ v9 won’t require Saving to have changes permanently take effect… I think that is a Bad assumption

The current Stackup comes from the settings and user can click OK but, if user does Not save the project/PCB, the changes are Not saved. I doubt that will change in v9.

I think the logic was that v9 will hopefully ship with an API to replace python bindings. That wouldn’t depend on saving or not - the API stuff reads the current internal state, not the last-saved-file state.

1 Like

that’s what I figured, yes

@Mineotopia @JamesJ
Okay, I get what you want which, is a ‘hair’ more than what you asked for.

No problem, just my short-sightedness. Even so, whatever the API n v9 turns out to be, if ‘it’ doesn’t save the file, the changes will be lost (unless magic happens or I’m just plain ignorant).

The magic that can happen is Simple - the code does the Saving if coded to do so. Thus, unless I’m wrong, at this point, I, (and perhaps you, and other posters) don’t know what is planned re Saving in v9.

That said, and NOT to belabor this topic too much, over morning coffee, I created a Sprial in FreeCAD, exported DXF and loaded into Kicad into SILK-Layer. I Saved the Kicad file before doing further work. I will show that after the following work, without Saving again, reopening the file is as I just saved it.

Then, I made a Track from it and used my API plugin to get Inductance/etc.

As I said, I did NOT save this work so I could show that the API used the Current/New Data (the Track), i.e., Not from Saved Data or a ‘Saved’ file. API reads the current file so whatever I want to Read, it gets and takes Action on.

Now, to the Point: I could include ‘Save’ code in the API to do the Saving (with, or, without Prompts), not a problem (I have many Python codes that do that Saving). But, I don’t want to Automatically Save anything so, I rely on Kicad prompting to Save or Discard.

When Quitting, I did Not select to Save it and thus, you see when re-opening the file, it did Not have any updated stuff - has only the First Save mentioned above.

All this means is that the API doesn’t need Kicad to be Opened or Used to read the Data and do something with it… And, it could certainly Save any Changed/Edited Kicad file/data if desired…

1 Like