1
\$\begingroup\$

I am trying to programatically add a new track to a KiCad board using the Python API. My code fails when trying to assign the layer to the new track.

My code is modeled off this example...

https://github.com/mmccoo/kicad_mmccoo/blob/master/basics/basics.py#L141

...which builds a dictionary of available layers, and then uses that dictionary to look up the desired layer by name.

Unfortunately the field pcbnew.LAYER_ID_COUNT doe snot seem to exist in the latest version of KiCad (5.1.2). To confirm, I typed the following into the Python command prompt...

import pcbnew
pcbnew.LAYER_ID_COUNT
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LAYER_ID_COUNT'

How can I work around this issue?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The following code will create the layertable table used by the above example (and many other examples)...

layertable = {}

for i in range(1000):
    name = board.GetLayerName(i)
    if name != "BAD INDEX!":
        layertable[name]=i

It is a horrible hack, but it works. It makes the reasonable assumption that there will never be more than 1000 layers.

Once the above code as set up the layertable dictionary, you can assign a layer to a track like this...

track.SetLayer(layertable["F.Cu"])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.