Skip to content

Instantly share code, notes, and snippets.

@atx
Created July 22, 2018 10:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atx/39dea1ab7dc33ac7b0d9782d5e437e03 to your computer and use it in GitHub Desktop.
Save atx/39dea1ab7dc33ac7b0d9782d5e437e03 to your computer and use it in GitHub Desktop.
Script which eats JSONs from http://www.keyboard-layout-editor.com/ and spits out a KiCad PCB
#! /usr/bin/env python2
from __future__ import print_function, with_statement, division, generators
import argparse
import collections
import json
import pcbnew
Button = collections.namedtuple("Button", ["x", "y", "x_s", "y_s"])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", required=True)
parser.add_argument("-b", "--board", required=True)
parser.add_argument("-o", "--output", required=True)
parser.add_argument("-r", "--reference", required=True)
parser.add_argument("-p", "--pattern", default="SK%d")
parser.add_argument("--step", default=19.0, type=float)
args = parser.parse_args()
buttons = []
with open(args.input) as fin:
data = json.load(fin)
for y, row in enumerate(data):
row.reverse()
x = 0
attrs = {}
while row:
item = row.pop()
if isinstance(item, dict):
assert not attrs
attrs = item
continue
x += attrs.get("x", 0)
btn = Button(
x=x,
y=(y + attrs.get("y", 0)),
x_s=(attrs.get("w", 1)),
y_s=(attrs.get("h", 1)),
)
x += btn.x_s
buttons.append(btn)
attrs = {}
def pt(x, y):
scale = 1000000
x += 40
y += 40
return pcbnew.wxPoint(round(x*scale), round(y*scale))
pcb = pcbnew.LoadBoard(args.board)
reference = pcb.FindModuleByReference(args.reference)
reference.SetPosition(pt(-20, -20))
for i, btn in enumerate(buttons):
designator = args.pattern % i
print(btn, "->", designator)
mod = pcbnew.Cast_to_BOARD_ITEM(reference.Clone()).Cast_to_MODULE()
mod.SetReference(designator)
mod.SetPosition(
pt((btn.x + (btn.x_s-1)/2) * args.step,
(btn.y + (btn.y_s-1)/2) * args.step)
)
pcb.Add(mod)
pcbnew.SaveBoard(args.output, pcb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment