Fix KiCAD's variable substitution

Hi all,

I would just refer to this thread, Variable substitution does not work reliably - #3 by v923z - Layout - KiCad.info Forums, where the issue has been described in detail. In short, there is a bug that renders KiCAD’s variable substitution useless, if only the .kicad_pcb file is uploaded to the PCB house.

As far as I understand, the fix is in the nightly builds, but not in the stable. As a temporary workaround, the substitution could be done via an external script that simply searches for defined properties, and inserts their values, wherever they appear before passing on the file to the rendering engine.

If needed, I could add additional context, or upload an example, where the idea is demonstrated.

Cheers,

Zoltán

Hi @zvoros-gmail-com, welcome to the community. Thanks for sharing this insight with us! If you can provide an example on how to solve this issue locally, feel free to share it. We have a good place to share: Community Tips 🧑‍🔧 - AISLER Creative Community.

If you want to avoid such issues in the meanwhile, take a look at Push for KiCad as described here. As soon as the fix becomes part of the stable version we will support it.

Greetings Tim, Here is what I use. but I don’t know, whether this is fail-safe. As far as I see, there is no easy way in KiCAD to iterate over text items, and get the value of properties, so I do everything on the string itself, and I don’t use KiCAD’s python interpreter.

def get_file(fn):
    with open(fn, 'r') as fin:
        board = fin.read()
    return board

def get_properties(board):
    properties = {}
    line_start = '(property "'
    for line in board.split('\n'):
        if line.lstrip().startswith(line_start):
            prop, value = line.replace(line_start, '').replace('")', '').split('" "')
            properties[prop.strip()] = value

    return properties

def substitute_properties(board, properties):
    for key, value in properties.items():
        board = board.replace('${{{}}}'.format(key), value)

    return board


board = get_file('test1.kicad_pcb')
properties = get_properties(board)
print(substitute_properties(board, properties))