Created
February 3, 2015 23:21
-
-
Save opalenic/c48d35ed91c18d5c5f6e to your computer and use it in GitHub Desktop.
Gerber file pad shrinker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import re | |
subtract = float(sys.argv[1]) / 1000 | |
mode = 'IN' | |
with open(sys.argv[2], "r") as f: | |
for line in f: | |
mode_match = re.match(r'%MO(?P<mode>IN|MM)*%', line) | |
match = re.match(r'^%ADD(?P<aperture_num>[0-9]{2,})(?P<shape>[CROP]),(?P<tail>.*)\*%', line) | |
if mode_match: | |
new_mode = mode_match.group('mode') | |
if mode == 'IN': | |
if new_mode == 'MM': | |
subtract *= 25.4 | |
elif mode == 'MM': | |
if new_mode == 'IN': | |
subtract /= 25.4 | |
sys.stdout.write(line) | |
elif match: | |
shape = match.group('shape') | |
tail = match.group('tail') | |
aperture_num = match.group('aperture_num') | |
dimensions = re.findall(r'[0-9]*\.?[0-9]+', tail) | |
shrink = [] | |
grow = [] | |
keep = [] | |
if shape == 'C': | |
shrink = dimensions[:1] | |
grow = dimensions[1:] | |
elif shape == 'R': | |
shrink = dimensions[:2] | |
grow = dimensions[2:] | |
elif shape == 'O': | |
shrink = dimensions[:2] | |
grow = dimensions[2:] | |
elif shape == 'P': | |
shrink = dimensions[:1] | |
keep = dimensions[1:3] | |
if len(dimensions) > 3: | |
grow = dimensions[3:] | |
else: | |
sys.stderr.write("Unknown aperture type: " + line) | |
sys.stdout.write(line) | |
continue | |
out = [] | |
out.extend(map(lambda e: "{:.4f}".format(float(e) - subtract).rstrip('0').rstrip('.'), shrink)) | |
out.extend(keep) | |
out.extend(map(lambda e: "{:.4f}".format(float(e) + subtract).rstrip('0').rstrip('.'), grow)) | |
sys.stdout.write("%ADD{}{},{}*%\n".format(aperture_num, shape, "X".join(out))) | |
else: | |
sys.stdout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment