Skip to content

Instantly share code, notes, and snippets.

@aewallin
Last active December 6, 2018 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aewallin/a9b3d785c13584c0bac84e6099a8f5ed to your computer and use it in GitHub Desktop.
Save aewallin/a9b3d785c13584c0bac84e6099a8f5ed to your computer and use it in GitHub Desktop.
# AW2018-12-06
# very simple auto-courtyard for KiCad footprints
# 1. reads bounding-box of F.fab and pads
# 2. expands by clearance
# 4. writes rectangle to F.CrtYd layer
# 5. writes modified footprint to a new file
import sys
import os
import math
import kicad_mod # https://github.com/KiCad/kicad-library-utils
import rules.klc_constants # https://github.com/KiCad/kicad-library-utils
if __name__ == "__main__":
fname = "/home/anders/Desktop/kicad-footprints/Package_SO.pretty/Mini-Circuits_PL-094.kicad_mod"
mod = kicad_mod.KicadMod(fname)
# BB to contain pads and F.Fab
box_all = kicad_mod.BoundingBox()
bb = mod.geometricBoundingBox('F.Fab')
bp = mod.overpadsBounds()
box_all.addBoundingBox(bb)
box_all.addBoundingBox(bp)
print "pads X", bp.xmin, bp.xmax
print "pads Y", bp.ymin, bp.ymax
print "fab X", bb.xmin, bb.xmax
print "fab Y", bb.ymin, bb.ymax
print "all X", box_all.xmin, box_all.xmax
print "all Y", box_all.ymin, box_all.ymax
# BB for Courtyard
crt = kicad_mod.BoundingBox(box_all.xmin, box_all.ymin, box_all.xmax, box_all.ymax)
# Clearance
# needs to change depending on footprint...
clearance = 0.25
crt.expand(clearance)
grid = rules.klc_constants.KLC_CRTYD_GRID
# snap to grid
gxmin, gxmax, gymin, gymax = round(crt.xmin/grid), round(crt.xmax/grid), round(crt.ymin/grid), round(crt.ymax/grid)
# new, snapped Courtyard BB
crt_grid = kicad_mod.BoundingBox( gxmin*grid, gymin*grid, gxmax*grid, gymax*grid )
print "crt X", crt.xmin, crt.xmax
print "crt Y", crt.ymin, crt.ymax
print "gcrt X", crt_grid.xmin, crt_grid.xmax
print "gcrt Y", crt_grid.ymin, crt_grid.ymax
# add Rectangle to footprint
mod.addRectangle( (crt_grid.xmin, crt_grid.ymin), (crt_grid.xmax, crt_grid.ymax), 'F.CrtYd', 0.05)
# write to disk
fname2 = fname[:-10]+"_AutoCrtYd.kicad_mod"
mod.save(fname2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment