Skip to content

Instantly share code, notes, and snippets.

@pklaus
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/397451e8cc9336b96c47 to your computer and use it in GitHub Desktop.
Save pklaus/397451e8cc9336b96c47 to your computer and use it in GitHub Desktop.
Script to query Gerber file outer dimensions
#!/usr/bin/env python
# From <https://www.wayneandlayne.com/blog/2013/04/02/script-to-query-gerber-file-outer-dimensions/>
# Gerber query script
# Usage: ./gerber_query.py board_edges.gbr
# Written by Matthew Beckler for Wayne and Layne, LLC
# Based on a script from @laen
# Released into the Public Domain. Have fun
def main():
import sys
if len(sys.argv) < 2:
print "Usage: %s gerberfile" % sys.argv[0]
sys.exit()
import re
filename = sys.argv[1]
xmin = None
xmax = None
ymin = None
ymax = None
for line in file(filename):
results = re.search("^X([\d-]+)Y([\d-]+)", line.strip())
if results:
x = int(results.group(1))
y = int(results.group(2))
xmin = min(xmin, x) if xmin else x
xmax = max(xmax, x) if xmax else x
ymin = min(ymin, y) if ymin else y
ymax = max(ymax, y) if ymax else y
print "Board dimensions:"
w = xmax - xmin
h = ymax - ymin
w_in = w / 10000.0
h_in = h / 10000.0
w_mm = w_in * 25.4
h_mm = h_in * 25.4
print " (%d, %d) original units" % (w, h)
print " (%.4f, %.4f) inches" % (w_in, h_in)
print " (%.4f, %.4f) mm" % (w_mm, h_mm)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment