Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
#!/usr/bin/env python3
"""Export the PCB geometry the OpenSCAD case needs to case/expensivo-pcb.scad.
Run with KiCad's bundled python (see build.sh). Everything is written in case
coordinates: millimetres, origin at the centre of the board outline's bounding
box, y pointing up, seen from the front (the left half's top side).
"""
import os
import pcbnew
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.normpath(os.path.join(HERE, ".."))
PCB = os.path.join(ROOT, "pcb", "expensivo.kicad_pcb")
OUT = os.path.join(ROOT, "case", "expensivo-pcb.scad")
T = pcbnew.ToMM
def main():
board = pcbnew.LoadBoard(PCB)
outline = pcbnew.SHAPE_POLY_SET()
assert board.GetBoardPolygonOutlines(outline)
bb = outline.BBox()
cx, cy = T(bb.GetCenter().x), T(bb.GetCenter().y)
def pt(x, y):
return [round(x - cx, 3), round(cy - y, 3)]
def box(b, margin=0.0):
"""KiCad BOX2I -> [x0, y0, x1, y1] in case coordinates (x0 < x1, y0 < y1)."""
x0, y0 = pt(T(b.GetLeft()) - margin, T(b.GetBottom()) + margin)
x1, y1 = pt(T(b.GetRight()) + margin, T(b.GetTop()) - margin)
return [x0, y0, x1, y1]
def fab_box(fp):
fab = pcbnew.B_Fab if fp.IsFlipped() else pcbnew.F_Fab
b = pcbnew.BOX2I()
for g in fp.GraphicalItems():
if g.GetLayer() == fab and isinstance(g, pcbnew.PCB_SHAPE):
b.Merge(g.GetBoundingBox())
return b
def pad_box(fp):
b = pcbnew.BOX2I()
for p in fp.Pads():
b.Merge(p.GetBoundingBox())
return b
o = outline.Outline(0)
points = [pt(T(o.CPoint(i).x), T(o.CPoint(i).y)) for i in range(o.PointCount())]
keys, holes, tht = [], [], []
# every pad on the front copper, for pictures: [x, y, w, h, angle, round, drill]
pads_front = []
for fp in board.GetFootprints():
for p in fp.Pads():
if not p.IsOnLayer(pcbnew.F_Cu):
continue
sz = p.GetSize()
pads_front.append(pt(T(p.GetPosition().x), T(p.GetPosition().y)) + [
round(T(sz.x), 3), round(T(sz.y), 3), round(p.GetOrientationDegrees(), 1),
1 if p.GetShape() in (pcbnew.PAD_SHAPE_CIRCLE, pcbnew.PAD_SHAPE_OVAL) else 0,
round(T(p.GetDrillSize().x), 3) if p.GetAttribute() != pcbnew.PAD_ATTRIB_SMD else 0])
fps = {fp.GetReference(): fp for fp in board.GetFootprints()}
for ref, fp in sorted(fps.items()):
x, y = T(fp.GetPosition().x), T(fp.GetPosition().y)
if ref.startswith("K"):
keys.append(pt(x, y) + [round(fp.GetOrientationDegrees(), 3)])
elif ref.startswith("H"):
holes.append(pt(x, y))
else:
# pins poking out under the board need a pocket in the bottom plate
for p in fp.Pads():
if p.GetAttribute() in (pcbnew.PAD_ATTRIB_PTH, pcbnew.PAD_ATTRIB_NPTH):
tht.append(box(p.GetBoundingBox()))
# on the right half the SW2/SW4 copies on the back are fitted; same position
values = {
"pcb_center": [round(cx, 3), round(cy, 3)],
"outline": points,
"keys": keys,
"mounting_holes": holes,
"tht_pads": tht,
"pads_front": pads_front,
"nano": box(fab_box(fps["U1"])),
"battery_pads": box(pad_box(fps["BT1"])),
"encoder_body": box(fab_box(fps["ENC1"])),
"encoder_pads": box(pad_box(fps["ENC1"])),
"power_switch": box(pad_box(fps["SW1"])),
"reset_button": box(pad_box(fps["SW3"])),
# the straight board edge beside the nano, from its USB end down past reset
"mcu_edge_x": max(x for x, y in points if box(pad_box(fps["SW3"]))[1] <= y <= box(fab_box(fps["U1"]))[3]),
}
with open(OUT, "w") as f:
f.write("// Generated by scripts/gen_case.py from pcb/expensivo.kicad_pcb -- do not edit.\n")
f.write("// Case coordinates: mm, origin at the board outline's centre, y up, front view.\n")
f.write("// Boxes are [x0, y0, x1, y1]; keys are [x, y, rotation].\n\n")
for k, v in values.items():
f.write(f"{k} = {v};\n")
print("wrote", os.path.relpath(OUT, ROOT))
if __name__ == "__main__":
main()
d635cc727This file is pinned to the selected release. Download the original to open it in your design tool.