Skip to content

Script to query Gerber file outer dimensions

by Layne on April 2, 2013

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: ./ 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]
    import re
    filename = sys.argv[1]
    xmin = None
    xmax = None
    ymin = None
    ymax = None
    for line in file(filename):
        results ="^X([\d-]+)Y([\d-]+)", line.strip())
        if results:
            x = int(
            y = int(
            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__":

From → Uncategorized

4 Comments Leave one →
  1. José Daniel Muñoz Frías permalink

    For the people that use the S.I: I have added the following line:
    print ” (%.4f, %.4f) mm” % (w / 10000.0*25.4 , h / 10000.0*25.4)

    just at the end, after the line:
    print ” (%.4f, %.4f) inches” % (w / 10000.0, h / 10000.0)

    Maybe you find it useful

    Best regards


  2. Thank you José, that is a very helpful addition! I have edited the script listing above to include it. Thanks again!

  3. Jan permalink

    The script doesn’t work with negative X-values. This regex fixes it for me: “^X([\d-]+)Y([\d-]+)”

  4. Thanks for the heads-up, I’ve fixed it.

Leave a Reply

Note: HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS