GerberFileReader is a Java class that provides methods to read and parse a Gerber file into a format suitable for additional processing by a Java program. It does not generate plots per se, but creates a list of graphical objects based on Java's Shape geometry along with meta-data that can be used to easily create plots and/or extract various information from the Gerber file.
GerberFileReader is intended to be fully compliant with Ucamco's Gerber Layer Format Specification Revision 2024.05. In addition, it supports many deprecated Gerber commands so it should be able to read many older Gerber files as well. See the Limitations section below for a complete list of deprecated commands that are NOT supported.
Note: GerberFileReader is not intended to be a complete Gerber syntax checker. While it will catch many such errors, it is not guaranteed to catch every possible one. If you have doubts of the validity of a Gerber file, run it through Ucamco's online Reference Gerber Viewer as it has a complete syntax checker built-in.
- Previously, GerberFileReader ignored all G04 comment content. It now correctly processes Standard Comments used to define attributes, see the Gerber Layer Format Specification Revision 2024.05, section 5.1.1 Comment attributes
- Standard attributes now have their own classes defined in the standardAttributes package so developers no longer need to understand their detailed Gerber syntax in order to process their content, see the Gerber Layer Format Specification Revision 2024.05, section 5.6 Standard Attributes. The unit tests have been updated to take advantage of this and they also provide a good example of how to use these new classes.
- GerberFileReader now generates an MD5 signature as the Gerber file is parsed. Once parsing of the file has successfully completed, the signature can be obtained by calling the getActualMD5Signature() method. This can be compared to the expected MD5 signature (obtained from the optional FileMD5 Standard Attribute) to guard against inadvertent file modifications.
For Maven users, simply add the following dependency to your pom:
<dependency>
<groupId>io.github.tonyluken</groupId>
<artifactId>GerberFileReader</artifactId>
<version>1.1.0</version>
</dependency>
Other users can download jar files here.
GerberFileReader is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
GerberFileReader is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Java 11 JDK, or later must be installed in order to compile the code.
GerberFileReader includes a number of test Gerber files, many of which were inspired by the examples in the Gerber Layer Format Specification. Others were developed to cover specific features of the specification that were not covered elsewhere. Gerber files generated with KiCad7 for a sample board design are also included. For each of these Gerber files, a unit test verifies the image generated by using GerberFileReader matches with the image from a screen capture of Ucamco's online Reference Gerber Viewer.
Two additional unit tests verify that the netlist and pick-and-place data extracted from the sample board design using GerberFileReader matches with the netlist and pick-and-place data exported directly by KiCad.
The unit tests are a good starting point to see how to use GerberFileReader. In addition, a full feature GUI to view Gerber files which uses GerberFileReader is available here.
The following deprecated Gerber command is not supported, any usage results in an exception:
G91 - Set the Coordinate format to Incremental notation
The following deprecated Gerber commands are are only supported if they confirm the default state, any other usage results in an exception:
The following deprecated Gerber command operations are not supported, any usage results in an exception:
Rectangular Hole in Standard Apertures
Draws and Arcs with Rectangular Apertures (Note, straight line draws with rectangular apertures are supported)
This is rather esoteric and probably would impact very few, if any, current users of Gerber files but it is worth mentioning. The Gerber Layer Format Specification as currently written (Revision 2024.05) is rather vague with respect to how Aperture Attributes and Block Apertures work together. For instance, the AB (Aperture Block) command creates an aperture and adds it to the Aperture Dictionary. However it is not clear exactly when Aperture Attributes get attached to the created aperture (as the contents of the Attribute Dictionary can change during the block definition). Currently, Gerber File Reader attaches all Aperture Attributes that are in the Attribute Dictionary at the time the Aperture Block is first opened, i.e., when the %ABD...*% command is encountered. Any changes to the Attribute Dictionary that occur within the block definition are not reflected in the Aperture Attributes attached to the block. Furthermore, whenever the block aperture is flashed, its attached aperture attributes are attached to all graphical objects created by the flash.
Similarly, since the SR (Step and Repeat) command effectively creates a Block Aperture (although it doesn't get added to the Aperture Dictionary), GerberFileReader attaches all Aperture Attributes in the Attribute Dictionary to the block at the opening of the SR block, i.e., when the %SRX...Y...I...J...*% command is encountered. Which in turn means those Aperture Attributes get attached to all objects created by each repeat of the block.
In my view, these are consistent with how Aperture Attributes work with the standard and macro apertures as well as regions. Of course, this is all subject to change should it be clarified in future revisions of the Gerber Layer Format Specification.
If you suspect GerberFileReader is not handling some aspect of the Gerber syntax correctly, please verify the Gerber file is in fact correct by first testing it with Ucamco's online Reference Gerber Viewer. If it looks correct there, then open an Issue with a detailed description of the problem and attach the Gerber file exhibiting the problem.
In early 2022 I became aware that Gerber files were no longer just a graphical description of a PCB's layers but could also contain information related to the PCB's design such as component placement data needed by pick-and-place machines to assemble the board (for more details see GerberX3). Since I am a contributor towards OpenPnP, an open source software for running pick-and-place machines, I became interested in adding support to OpenPnP to read Gerber files in order to extract the placement data - hence, the development of GerberFileReader. I initially began with a fork of Wayne Holder's GerberPlot. But, as my changes became more extensive and because I was really aiming to develop a stand alone library that could be utilized by other applications, it made sense to break this out as a separate project with its own repository.