diff options
author | Fredrik Thulin <fredrik@thulin.net> | 2018-04-26 11:12:18 +0200 |
---|---|---|
committer | Fredrik Thulin <fredrik@thulin.net> | 2018-04-26 11:12:18 +0200 |
commit | 309d0462641717f2f842ea99f0a749256ae4d404 (patch) | |
tree | 24de69858b2ce906acf546503724e6a407889e51 | |
parent | 53ca1682aa9e5c7aaaac0233fa2e12f08bb830ca (diff) |
Set footprints in the schematics from the PCB.
-rwxr-xr-x | convert.sh | 5 | ||||
-rwxr-xr-x | set-footprints-from-pcb.py | 77 |
2 files changed, 81 insertions, 1 deletions
@@ -43,7 +43,7 @@ cd .. rm -rf "${kicaddir}" mkdir "${kicaddir}" -git checkout "${kicaddir}"/GerberOutput +git checkout "${kicaddir}"/{GerberOutput,footprints.pretty,fp-lib-table} cp ${altiumdir}/*.{sch,lib} "${kicaddir}"/ rm ${kicaddir}/rev02*-cache.lib cp ${altiumdir}/CrypTech-PcbDoc.kicad_pcb "${kicaddir}/Cryptech Alpha.kicad_pcb" @@ -97,6 +97,9 @@ ls Cryptech*Alpha.lib rev02*sch | while read file; do sed -i -e "s#I/SN#I_SN#g" "${file}" done +# Set all schematic footprints from the PCB +../set-footprints-from-pcb.py Cryptech?Alpha.kicad_pcb *.sch + # Make further modifications to the layout using KiCAD's Python bindings test -d ../tmp || mkdir ../tmp cp "Cryptech Alpha.kicad_pcb" "../tmp/Cryptech Alpha.kicad_pcb.a2k-out" diff --git a/set-footprints-from-pcb.py b/set-footprints-from-pcb.py new file mode 100755 index 0000000..ad0683b --- /dev/null +++ b/set-footprints-from-pcb.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +""" +Set all schematic footprints from the PCB file, where the components footprints +are known. The footprints were exported once from the PCB using the menu option + + File -> Archive Footprints -> Create New Library and Archive Footprints + +and the fp-lib-table was set up to map the name 'Cryptech_Alpha_Footprints' to +the archive directory (rev03-KiCad/footprints.pretty). +""" + +import os +import sys +import pprint + +import pcbnew + +def add_footprints(fn_in, fn_out, comp): + in_ = open(fn_in) + out = open(fn_out, 'w') + curr = None + c = 0 + print('Adding footprints to {}'.format(fn_in)) + for line in in_.readlines(): + c += 1 + if line.startswith('L '): + curr = line.split(' ')[-1] + while curr[-1] == '\n': + curr = curr[:-1] + if line.startswith('F 2 ""'): + if curr in comp: + fp = comp[curr] + line = 'F 2 "Cryptech_Alpha_Footprints:{}{}'.format(fp, line[5:]) + if line.endswith('0000 C CNN\n'): + # don't show the footprint in the schematics + line = line[:-8] + '1' + line[-7:] + #print('{}: line {} {} fp {}'.format(fn_in, c, curr, fp)) + else: + if not curr.startswith('#PWR'): + sys.stderr.write('Footprint for {} not known\n'.format(curr)) + out.write(line) + return True + +def main(pcb, schemas): + print("Loading PCB '{}'".format(pcb)) + board = pcbnew.LoadBoard(pcb) + comp = {} + for mod in board.GetModules(): + ref = mod.GetReference() + fp = mod.GetFPID().GetLibItemName() + if ref in comp: + sys.stderr.write('{} already in comp ({} -> {})!\n'.format(ref, comp[ref], fp)) + comp[ref] = str(fp) + + #print(pprint.pformat(comp)) + + for this in schemas: + if add_footprints(this, this + '.tmp', comp): + os.rename(this + '.tmp', this) + + return True + + +if __name__ == '__main__': + try: + if len(sys.argv) == 0: + sys.stderr.write('Syntax: set-footprints-from-pcb.py *.kicad_pcb *.sch\n') + sys.exit(1) + pcb = [x for x in sys.argv if x.endswith('.kicad_pcb')][0] + schemas = [x for x in sys.argv if x.endswith('.sch')] + res = main(pcb, schemas) + if res: + sys.exit(0) + sys.exit(1) + except KeyboardInterrupt: + pass |