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!
https://docs.kicad.org/doxygen-python/classpcbnew_1_1BOARD__DESIGN__SETTINGS.html#a63764c064539f9d0e294b5ae08bf25be

1 Like

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…

Layer_Lister

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()
board.GetCopperLayerCount()
This works to get to know the copper layer count

1 Like

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.

The reason for my thinking being the pcbnew.FOOTPRINT.SetPosition stil takes a wxpoint as an argument, while the 6.99 changed to take VECTOR2I as an argument.

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)

2 Likes

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)?