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: ./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
    with open(filename, 'r') as fid:
        for line in fid:
            results = re.search("^X(\d+)Y([\d-]+)", line)
            if results:
                x = int(results.group(1))
                y = int(results.group(2))
                if not xmin or x < xmin:
                    xmin = x
                if not ymin or y < ymin:
                    ymin = y
                if not xmax or x > xmax:
                    xmax = x
                if not ymax or y > ymax:
                    ymax = y
    print "Board dimensions:"
    w = xmax - xmin
    h = ymax - ymin
    print " ", (w, h), "original units"
    print "  (%.4f, %.4f) inches" % (w / 10000.0, h / 10000.0)
    print "  (%.4f, %.4f) mm" % (w / 10000.0*25.4 , h / 10000.0*25.4)
 
 
if __name__ == "__main__":
    main()

From → Uncategorized

2 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

    Daniel

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

Leave a Reply

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

Subscribe to this comment feed via RSS