How to get the list of all layers and just copper layers using python scripting in kicad 6?
I’m on my phone right now so I can’t write it for you, but you can iterate over all the layers and check if isCopperLayer() returns true.
Here’s how to iterate over all layers (not just the layers being used):
Here’s how to check if it’s a copper layer:
I iterated through all the layers and checked for copper layer, but even though I have only 2 copper layers it is showing 30 copper layers. Any idea?
Yes, that is expected.
I don’t know how to check if a layer is enabled or not unfortunately.
As albin said, there is no direct way. But indirectly there are two ways:
- Using external tools, you can parse the kicad_pcb file with grep filtering for .Cu, and count the lines (to make it more robust, you can parse only first 50 lines or so.
- You can iterate through all the tracks and all the zones and look at which layer they are on. It’ll take a while, but you stay completely within pcbnew API
I s there any way to get to know about 2, 4 or how many layer board?
BOARD_DESIGN_SETTINGS class has
GetEnabledLayers(), at least in nightly, probably in v6 too.
I didn’t link that specifically because those docs are rolling update, they are for 6.99 now.
No doubt you’ll want to stick with Python but, as an alternative…
When it comes to doing things where it’s fairly mindless for me if done in Java, that’s how I do it. Listing the Layers is a good example of this, in that, nothing is going to be done to the file. Thus, reading and displaying data from the PCBnew file is simple.
This example prompts for the File and searches it for empty Layers and lists the layers with something on them…
I added 4 traces (one on each of only 4 layers).
I turned Off the list-out of the Layer Names and show the used layer count (without their layer names…)
It was a few minutes of fun. Video below…
Why not share the code then?
Also Java and GUI… just yuck. And I’m saying that as everyday Java aficionado, just have PTSD from finagling it’s awful GUI libs back in the day.
Perhaps your ‘Yuk’ stems from coding the java GUI crap. I agree and don’t do that any longer. I use the WindowBuilder for Eclipse IDE. It’s basically Drag&Drop. And, like the major-league IDE’s it features the usual panorama of One-Click Code initiators.
Screenshot of a segment of the code and image of WindowBuilder’s Drag&Drop panel…
board = pcbnew.GetBoard()
This works to get to know the copper layer count
Oh, I didn’t know that. So currently there are no docs generated for 6.0.x? Just for 5.1.x and 6.99?
Not that I know of. .
I think that the docs at https://docs.kicad.org/doxygen-python/ are for 6.0.x.
I’ve asked for documentation, but there was no enthusiasm about it.
It may be because it just wasn’t update in a while. To my knowledge nothing pins those docs to v6, they are just sometimes rebuilt from master and pushed to the site. I may be wrong though, maybe @marekr knows.
I pinned them to v6 after I realized the refactoring would completely wreck the docs for anyone trying to make a v6 plugin (highest concern)
Thanks for pinning the docs. Are the 6.99 docs getting built? If they are, is there a link where we can find them? Even if they are at transient storage, are they available (par of CI/CD pipeline)?