Script to query Gerber file outer dimensions
At Wayne & Layne, we design a lot of circuit boards. During the request-for-quote (RFQ) process for getting a PCB fabricated, the outside dimensions of the PCB are an important driver of the overall per-unit price. As part of the W&L “prepare PCB for RFQ” process, we have a little script that uses the PCB’s board outline Gerber file to determine the dimensions of the PCB’s bounding box.
Pass in the filename of the PCB “board outline” Gerber file, and it will print out the dimensions in both the original units (decimills for Gerbers exported from Kicad) as well as inches (again, assuming decimills). It does this by analyzing all the lines of the gerber file, and determining the minimum and maximum in both x and y directions. The script is based on a little chunk of code written by @laen on Twitter (who makes no claim to the code, and also runs the most excellent OSH Park PCB service). We’re releasing this script into the Public Domain, use it however you like.
#!/usr/bin/env python # 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 sys.exit() import re filename = sys.argv 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()