← Back to team overview

kicad-developers team mailing list archive

Re: OT: Extracting board dimension from a Gerber board outline file.

 

This is a little python script I use to determine the size of a PCB
based on the PCB_Edges.gbr file generated by kicad. No guarantees that
it'll work on every gerber, but it's worked great so far for me.

#!/usr/bin/env python

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 "  (%.2f, %.2f) inches" % (w / 10000.0, h / 10000.0)

Good luck, hope this helps!

-Matthew Beckler


On 01/21/2012 06:12 AM, David J S Briscoe wrote:
> Hi,
> I need to produce a small application that can examine the contents of a board outline gerber file produced by PCBNEW.
> The application needs to read the gerber file and extract the X and Y co-ordinates and spit out the actual board size.
> Is it possible to extract a board size from the following Gerber file syntax for example?
> 
> 
> G04 (created by PCBNEW-RS274X (2011-07-08 BZR 3044)-stable) date 01/11/2011 11:39:11*
> 
> G01*
> 
> G70*
> 
> G90*
> 
> %MOIN*%
> 
> G04 Gerber Fmt 3.4, Leading zero omitted, Abs format*
> 
> %FSLAX34Y34*%
> 
> G04 APERTURE LIST*
> 
> %ADD10C,0.006000*%
> 
> %ADD11C,0.015000*%
> 
> G04 APERTURE END LIST*
> 
> G54D10*
> 
> G54D11*
> 
> X46170Y-34610D02*
> 
> X46170Y-22790D01*
> 
> X63890Y-34610D02*
> 
> X46170Y-34610D01*
> 
> X63890Y-22790D02*
> 
> X63890Y-34610D01*
> 
> X46170Y-22790D02*
> 
> X63890Y-22790D01*
> 
> M02*
> 
> 
> 
> Which lines describe the actual top-left,top-right, bottom-left and bottom-right X and Y co-ordinates. All I need to end up with is a set of numbers from which I can calculate the actual length and width of a PCB (in inches (MILS) or mm). Can someone point me in the right direction? I would be very grateful for any assistance offered.Thanks.
> 
> 
> 
> David.
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Mailing list: https://launchpad.net/~kicad-developers
> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~kicad-developers
> More help   : https://help.launchpad.net/ListHelp


References