Changing pad layers with python

Folks I’m trying to change the layer of pads for a module with the following code:

import sys
import pcbnew
from shutil import copyfile

file_in  = 'test1.kicad_pcb'
file_out = 'test2.kicad_pcb'
copyfile(file_in, file_out)

board = pcbnew.LoadBoard(file_out)
pctl = pcbnew.PLOT_CONTROLLER(board)
popt = pctl.GetPlotOptions()

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

modref = "LDFM1"
mod = board.FindModuleByReference(modref)

for pad in mod.Pads():

    print("pad {}({}) on {}({}) at {},{} shape {} size {},{} {}"
        .format(pad.GetPadName(),
                pad.GetNet().GetNetname(),
                mod.GetReference(),
                mod.GetValue(),
                pad.GetPosition().x, pad.GetPosition().y,
                pad.GetShape(),
                pad.GetSize().x, pad.GetSize().y,
                board.GetLayerName(pad.GetLayer())
    ))

print ("\nshow change\n")

for pad in mod.Pads():
    # I would have expected this line to change pad layer, it does in the following print statement
    pad.SetLayer(layertable["Bottom"])

    print("pad {}({}) on {}({}) at {},{} shape {} size {},{} {}"
        .format(pad.GetPadName(),
                pad.GetNet().GetNetname(),
                mod.GetReference(),
                mod.GetValue(),
                pad.GetPosition().x, pad.GetPosition().y,
                pad.GetShape(),
                pad.GetSize().x, pad.GetSize().y,
                board.GetLayerName(pad.GetLayer())
    ))

pcbnew.SaveBoard(file_out, board)

The output of the second for loop shows the pads are on a new layer, but the pads are not changed to the saved file (test2.kicad_pcb). Any suggestions?

I think the iterable (y) iterated via for x in y is always immutable. (https://stackoverflow.com/questions/9414399/modifying-a-list-iterator-in-python-not-allowed)

note that there also is the question if the mod.Pads() function returns an iterable or a generator. (if it is the later then i am not sure if there is a way to use this function as the starting point for anything that intents to change the underlying data. if it is a list then it depends if the list contains a copy of the original pads or if it is a list of references)

I’d try to duplicate the footprint, change the pad layer add the duplicated footprint to board and remove original footprint.

hm. it doesnt like my deleting the pad:
board.Delete(pad)

it gives:
class_board.cpp(1023): assert “Assert failure” failed in Remove(): BOARD::Remove() needs more ::Type() support

You can not delete pad from board, as the pad is contained within module (footprint) which is contained within board.

Read carefully.

  1. duplicate footprint (module in pcbnew namespace)
  2. Edit the duplicated footprint so that you change pad layers. You could also duplicate pad, remove original edit the duplicate and add the duplicate to the footprint
  3. Then delete original footprint.
  4. Add duplicated footprint to board.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.