How to get the list of copper layers in python scrpting?

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:

1 Like

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.

1 Like

As albin said, there is no direct way. But indirectly there are two ways:

  1. 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.
  2. 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.

It looks like it!

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…