def main(): oparser = initOptions() (opts, args) = oparser.parse_args() fn = opts.macrosch outfn = opts.schout xcopies = opts.xclones ycopies = opts.yclones xsize = opts.xsize ysize = opts.ysize if (fn == None) or (outfn == None): oparser.exit('File names must be specified, --help for help') if xcopies + ycopies == 0: oparser.exit('Specify more than zero copies in X and/or Y dimensions') if (xcopies > 0) and (xsize == 0): oparser.exit( 'Specify xsize of a clone in magic KiCad units, e.g. 8000; --help for help' ) if (ycopies > 0) and (ysize == 0): oparser.exit( 'Specify ysize of a clone in magic KiCad units, e.g. 2000; --help for help' ) print 'Going to make %s copies along X axis with spacing of %s' % (xcopies, xsize) print 'Going to make %s copies along Y axis with spacing of %s' % (ycopies, ysize) fn = FileAccess(fn) sch = SchFile(fn) newitems = [] for item in sch.items: clown = -1 for y in xrange(ycopies + 1): if y == ycopies: continue for x in xrange(xcopies + 1): clown = clown + 1 ii = CloneItem(item).items[0] newRefDes(item, ii, x, y + 1, xsize, ysize, clown) newitems.append(ii) sch.items = sch.items + newitems print 'Saving result to ', outfn output = FileAccess(outfn) output.write(sch)
def main(): oparser = initOptions() (opts, args) = oparser.parse_args() fn = opts.macrosch outfn = opts.schout xcopies = opts.xclones ycopies = opts.yclones xsize = opts.xsize ysize = opts.ysize if (fn == None) or (outfn == None): oparser.exit('File names must be specified, --help for help') if xcopies + ycopies == 0: oparser.exit('Specify more than zero copies in X and/or Y dimensions') if (xcopies > 0) and (xsize == 0): oparser.exit('Specify xsize of a clone in magic KiCad units, e.g. 8000; --help for help') if (ycopies > 0) and (ysize == 0): oparser.exit('Specify ysize of a clone in magic KiCad units, e.g. 2000; --help for help') print 'Going to make %s copies along X axis with spacing of %s' % (xcopies,xsize) print 'Going to make %s copies along Y axis with spacing of %s' % (ycopies,ysize) fn = FileAccess(fn) sch = SchFile(fn) newitems = [] for item in sch.items: clown = -1 for y in xrange(ycopies+1): if y == ycopies: continue for x in xrange(xcopies+1): clown = clown + 1 ii = CloneItem(item).items[0] newRefDes(item, ii, x, y + 1, xsize, ysize, clown) newitems.append(ii) sch.items = sch.items + newitems print 'Saving result to ', outfn output = FileAccess(outfn) output.write(sch)
I had a schematic where my resistor parts had the values in the footprint field (because I incorrectly defined the library part). So this script replaces 'res' fields with the footprint fields. ''' import os import glob import find_kipy from kipy.fileobjs import SchFile from kipy.utility import FileAccess root ='.' for fn in glob.glob(os.path.join(root, '*.sch')): fn = FileAccess(fn) sch = SchFile(fn) changed = False for item in sch.items: if not isinstance(item, sch.Component): continue fields = item.fields if fields[1][0] == 'RES': fields[1] = fields[2] fields[2] = None changed = True if changed: fn.write(sch)
def main(): templateX = 20 oparser = initOptions() (opts, args) = oparser.parse_args() schfn = opts.macrosch brdfn = opts.brdin outfn = opts.brdout if (schfn == None) or (brdfn == None) or (outfn == None): oparser.exit('All three command-line arguments must be specified') sch = SchFile(FileAccess(schfn)) brd = BrdFile(FileAccess(brdfn)) print 'hello.jpg' # build a list of modules that are supposed to be placed in the template originalModRefs = [] clonedModRefs = [] for schi in sch.items: if schi.__class__ == Component: originalModRefs.append(schi.refdes) for suff in xrange(100): clonedModRefs.append(subRefDes(schi.refdes, suff)) print 'originalModRefs=', len(originalModRefs) print 'clonedModRefs=', len(clonedModRefs) templateMods = [] relocateMods = [] tracks = [] for placed in brd.items: if placed.__class__ == Module: if placed.refdes in originalModRefs: templateMods.append(placed) elif placed.refdes in clonedModRefs: relocateMods.append(placed) elif placed.__class__ == Track: track = placed elif placed.__class__ == CZoneOutline: if placed.layer == 25: print 'Found zone that defines the tracks to clone' zone = placed print 'templateMods=', len(templateMods) print 'relocateMods=', len(relocateMods) print 'tracks=', len(tracks) topleft = [10000,10000] bottomright = [-1, -1] for c in zone.corners: if c[0] < topleft[0]: topleft[0] = c[0] if c[1] < topleft[1]: topleft[1] = c[1] if c[0] > bottomright[0]: bottomright[0] = c[0] if c[1] > bottomright[1]: bottomright[1] = c[1] print 'Found clone zone: (%s,%s)-(%s,%s)' % tuple(topleft + bottomright) templateX = (bottomright[0]-topleft[0]) print 'Using width %s as X offset for the clones' % templateX nclones = 0 nclonesmax = 20 for relocate in relocateMods: for template in templateMods: for x in xrange(nclonesmax): refdes = subRefDes(template.refdes, x) if refdes == relocate.refdes: #print 'refdes=', relocate.refdes if (x+1) > nclones: nclones = x + 1 move(relocate, template.xpos + (x+1)*templateX, template.ypos, template.orientation) print 'nclones=', nclones newpos = [] newdes = [] for po,de in zip(track.po,track.de): newpos.append(po) newdes.append(de) if (po.xstart > topleft[0]) and (po.ystart > topleft[1]) and \ (po.xend < bottomright[0]) and (po.yend < bottomright[1]): for x in xrange(nclones): newdes.append(de.clone()) newpo = po.clone() newpo.xstart = po.xstart + (x+1)*templateX newpo.xend = po.xend + (x+1)*templateX newpos.append(newpo) track.po = newpos track.de = newdes print 'Saving output to ', outfn output = FileAccess(outfn) output.write(brd)
I had a schematic where my resistor parts had the values in the footprint field (because I incorrectly defined the library part). So this script replaces 'res' fields with the footprint fields. ''' import os import glob import find_kipy from kipy.fileobjs import SchFile from kipy.utility import FileAccess root = '.' for fn in glob.glob(os.path.join(root, '*.sch')): fn = FileAccess(fn) sch = SchFile(fn) changed = False for item in sch.items: if not isinstance(item, sch.Component): continue fields = item.fields if fields[1][0] == 'RES': fields[1] = fields[2] fields[2] = None changed = True if changed: fn.write(sch)
def main(): templateX = 20 oparser = initOptions() (opts, args) = oparser.parse_args() schfn = opts.macrosch brdfn = opts.brdin outfn = opts.brdout if (schfn == None) or (brdfn == None) or (outfn == None): oparser.exit('All three command-line arguments must be specified') sch = SchFile(FileAccess(schfn)) brd = BrdFile(FileAccess(brdfn)) print 'hello.jpg' # build a list of modules that are supposed to be placed in the template originalModRefs = [] clonedModRefs = [] for schi in sch.items: if schi.__class__ == Component: originalModRefs.append(schi.refdes) for suff in xrange(100): clonedModRefs.append(subRefDes(schi.refdes, suff)) print 'originalModRefs=', len(originalModRefs) print 'clonedModRefs=', len(clonedModRefs) templateMods = [] relocateMods = [] tracks = [] for placed in brd.items: if placed.__class__ == Module: if placed.refdes in originalModRefs: templateMods.append(placed) elif placed.refdes in clonedModRefs: relocateMods.append(placed) elif placed.__class__ == Track: track = placed elif placed.__class__ == CZoneOutline: if placed.layer == 25: print 'Found zone that defines the tracks to clone' zone = placed print 'templateMods=', len(templateMods) print 'relocateMods=', len(relocateMods) print 'tracks=', len(tracks) topleft = [10000, 10000] bottomright = [-1, -1] for c in zone.corners: if c[0] < topleft[0]: topleft[0] = c[0] if c[1] < topleft[1]: topleft[1] = c[1] if c[0] > bottomright[0]: bottomright[0] = c[0] if c[1] > bottomright[1]: bottomright[1] = c[1] print 'Found clone zone: (%s,%s)-(%s,%s)' % tuple(topleft + bottomright) templateX = (bottomright[0] - topleft[0]) print 'Using width %s as X offset for the clones' % templateX nclones = 0 nclonesmax = 20 for relocate in relocateMods: for template in templateMods: for x in xrange(nclonesmax): refdes = subRefDes(template.refdes, x) if refdes == relocate.refdes: #print 'refdes=', relocate.refdes if (x + 1) > nclones: nclones = x + 1 move(relocate, template.xpos + (x + 1) * templateX, template.ypos, template.orientation) print 'nclones=', nclones newpos = [] newdes = [] for po, de in zip(track.po, track.de): newpos.append(po) newdes.append(de) if (po.xstart > topleft[0]) and (po.ystart > topleft[1]) and \ (po.xend < bottomright[0]) and (po.yend < bottomright[1]): for x in xrange(nclones): newdes.append(de.clone()) newpo = po.clone() newpo.xstart = po.xstart + (x + 1) * templateX newpo.xend = po.xend + (x + 1) * templateX newpos.append(newpo) track.po = newpos track.de = newdes print 'Saving output to ', outfn output = FileAccess(outfn) output.write(brd)
#!/usr/bin/env python2.6 ''' Strip the dates out of schematic and library files for easier interoperability with subversion. Just run this before using subversion diff or checkin, and unchanged files will remain unchanged. ''' import os import glob import find_kipy from kipy.fileobjs import LibFile, SchFile from kipy.utility import FileAccess root ='.' for fn in glob.glob(os.path.join(root, '*-cache.lib')): fn = FileAccess(fn) lib = LibFile(fn) lib.topline = lib.topline.split(' Date: ')[0].strip() fn.write(lib) for fn in glob.glob(os.path.join(root, '*.sch')): fn = FileAccess(fn) sch = SchFile(fn) sch.page_header.linetext = sch.page_header.linetext.split(' date ')[0].strip() fn.write(sch)