Skip to content

Instantly share code, notes, and snippets.

@smurfix
Last active December 10, 2021 11:57
Show Gist options
  • Save smurfix/ccf5719b7704521a8b3249bf8aaa3204 to your computer and use it in GitHub Desktop.
Save smurfix/ccf5719b7704521a8b3249bf8aaa3204 to your computer and use it in GitHub Desktop.
Kicad scale footprint
#!/usr/bin/python3
from sexpdata import dump,load,Symbol, dumps as ds
import click
import sys
skip = set(("rotate",))
scaled = set(("xy","xyz","at","size","start","end"))
scaled_width = set(("thickness","width"))
def resize(s, scale):
if isinstance(s[0], Symbol):
sn = s[0].value()
if sn in skip:
return
if sn in scaled:
for i in range(1,len(s)):
if isinstance(s[i],(int,float)):
s[i] = round(s[i] * scale, 10)
# this avoids nonsense outut like "1.23400000002"
for m in s:
if isinstance(m,list):
resize(m, scale)
@click.command()
@click.option("-s","--scale", type=float,default=1, help="Scaling factor")
@click.option("-S","--scale-width", is_flag=True, help="Affect line widths")
@click.option("-T","--scale-text", is_flag=True, help="Affect text")
@click.option("-i","--in", "_in", type=click.File("r"), default=sys.stdin,
help="File to read. Defaults to stdin")
@click.option("-o","--out", "_out", type=click.File("w"), default=sys.stdout,
help="File to write. Defaults to stdout")
def main(scale,scale_width,scale_text, _in,_out):
"""
Rescale a KiCAD footprint.
"""
global scaled
if not scale_text:
skip.add("font")
if scale_width:
scaled |= scaled_width
s=load(_in)
resize(s,scale)
dump(s,_out)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment