Skip to content

Instantly share code, notes, and snippets.

@urish
Created January 6, 2020 15:36
Show Gist options
  • Save urish/9c5b4aea6362da086541be14acdf0f72 to your computer and use it in GitHub Desktop.
Save urish/9c5b4aea6362da086541be14acdf0f72 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# coding=utf8
#
# Simple script to scale a KiCad footprint
# Usage:
# python kicad-resize-footprint.py <input.kicad_mod> <output.kicad_mod> <scale>
#
# Where scale is how much to scale (1 = 100%)
#
# Copyright (C) 2020, Uri Shaked.
import sys
import re
scale = float(sys.argv[3])
def scalexy(val):
x = float(val.group(1)) * scale
y = float(val.group(2)) * scale
return '(xy {} {})'.format(x, y)
with open(sys.argv[1], 'r') as in_file, open(sys.argv[2], 'w', newline='') as out_file:
for line in in_file:
line = re.sub(r'\(xy ([0-9-.]+) ([0-9-.]+)\)', scalexy, line)
out_file.write(line)
@InputBlackBoxOutput
Copy link

InputBlackBoxOutput commented Sep 12, 2022

Hi @mohammadsdtmnd,

I created a web application implementing this script using Streamlit. You can use it by going to the following URL:
https://inputblackboxoutput-kicad-footprint-resizer-app-bj5g74.streamlitapp.com/

Have fun!

@ThisIsRobokitty
Copy link

Hi @mohammadsdtmnd,

I created a web application implementing this script using Streamlit. You can use it by going to the following URL: https://inputblackboxoutput-kicad-footprint-resizer-app-bj5g74.streamlitapp.com/

Have fun!

I tried the python script and the web version. Neither make a single line change. I am giving it a schematic file straight from KiCad's default files. I'll repeat just in-case. NEITHER the web version or the python version work for a default KiCad .kicad_mod footprint. If you want to know, it was a DIP-28 footprint. I tried an SSOP-28 as well which had the same result.

@ThisIsRobokitty
Copy link

the kicad files don't seem to use "xy" before the coords, so I made some light changes that worked for me (start, end, width, size). Wasn't elegant, but it did the trick for me.

@InputBlackBoxOutput
Copy link

InputBlackBoxOutput commented Dec 4, 2022

I looked into the .kicad_mod files. Apparently the script provided above can only resize footprints generated by the image converter tool in KiCAD. I did not notice it till now since I mostly used this tool only for custom image footprints generated by the latter mentioned tool.

The following script will most likely rescale all footprints

import sys
import re


def scale_start(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(start {x} {y})'


def scale_mid(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(mid {x} {y})'


def scale_center(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(center {x} {y})'


def scale_end(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(end {x} {y})'


def scale_width(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(width {x} {y})'


def scale_size(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(size {x} {y})'


def scale_xy(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(xy {x} {y})'


def scale_at(val):
    x = float(val.group(1)) * scale
    y = float(val.group(2)) * scale
    return f'(at {x} {y}'


def scale_drill(val):
    x = float(val.group(1)) * scale
    return f'(drill {x})'


def scale_width(val):
    x = float(val.group(1)) * scale
    return f'(width {x})'


def scale_thickness(val):
    x = float(val.group(1)) * scale
    return f'(thickness {x})'


def process_line(line):
    line = re.sub(r'\(start ([0-9-.]+) ([0-9-.]+)\)', scale_start, line)
    line = re.sub(r'\(mid ([0-9-.]+) ([0-9-.]+)\)', scale_mid, line)
    line = re.sub(r'\(center ([0-9-.]+) ([0-9-.]+)\)', scale_center, line)
    line = re.sub(r'\(end ([0-9-.]+) ([0-9-.]+)\)', scale_end, line)
    line = re.sub(r'\(width ([0-9-.]+) ([0-9-.]+)\)', scale_width, line)
    line = re.sub(r'\(size ([0-9-.]+) ([0-9-.]+)\)', scale_size, line)
    line = re.sub(r'\(xy ([0-9-.]+) ([0-9-.]+)\)', scale_xy, line)
    line = re.sub(r'\(at ([0-9-.]+) ([0-9-.]+)', scale_at, line)
    line = re.sub(r'\(drill ([0-9-.]+)\)', scale_drill, line)
    line = re.sub(r'\(width ([0-9-.]+)\)', scale_width, line)
    line = re.sub(r'\(thickness ([0-9-.]+)\)', scale_thickness, line)

    return line


if __name__ == "__main__":
    scale = float(sys.argv[3])
    with open(sys.argv[1], 'r') as in_file, open(sys.argv[2], 'w', newline='') as out_file:
        for line in in_file:
            out_file.write(process_line(line))

PS: Thanks @ThisIsRobokitty for finding out the issue and recommending the solution

@InputBlackBoxOutput
Copy link

InputBlackBoxOutput commented Dec 5, 2022

I have also modified the Streamlit web application hosted at the following URL:

https://kicad-footprint-resizer.streamlit.app/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment