Skip to main content

Parsing, formatting and rendering toolkit for Gerber X3 file format

Project description

PyGerber

GitHub release GitHub Release Date - Published_At PyPI release PyPI Downloads Supported Python versions Supported Python implementations license_mit coverage GitHub tag checks state Pull requests Open issues GitHub repo size GitHub code size in bytes GitHub Repo stars Poetry Code Style

PyGerber is a Python implementation of Gerber X3/X2 format. It is based on Ucamco's The Gerber Layer Format Specification. Revision 2023.03 (Available on Ucamco's webpage and in this repository). The goal of this project is to provide support for wide variety of Gerber-like syntaxes, with support for most of deprecated features along with support for modern ones.

Target set of tools:

  • Tokenizer
  • Parser
  • Rasterized 2D rendering engine (With Pillow)
  • Vector 2D rendering engine (With drawsvg)
  • Model 3D rendering engine (With Blender)
  • Formatter
  • Linter (eg. deprecated syntax detection)

You can view progress of development in Gerber features support section down below. All Gerber source files which can be redistributed under MIT license and included in this repository for testing purposes will be greatly appreciated.

Syntax feature requests

All deprecated features (Mainly those from X2 format) are considered optional and priority to implement them will be assigned based on number of requests form community.

If You needs support for syntax features which are not mentioned in The Gerber Layer Format Specification. Revision 2023.03 (Available on Ucamco's webpage and in this repository) and this feature is not already listed in Support paragraph, please open a new Feature request issue.

Feature request Issue should contain:

  • detailed description how requested feature works,
  • code samples for testing the feature,
  • reference images (only applies to features changing image look).

Requests which don't comply with those guidelines will be considered low priority.

Installation

PyGerber can be installed with pip from PyPI:

pip install pygerber

Alternatively, it is also possible to install it directly from repository:

pip install git+https://github.com/Argmaster/pygerber

Usage

PyGerber offers a high-level API that simplifies the process of rendering Gerber files. Whether you're looking to save the rendered output to a file or directly into a buffer, PyGerber has got you covered.

  • The Layer Class: At its core, the Layer class stands for a single Gerber source file, complete with its associated PyGerber configuration.

    Important Layer class represents any Gerber file, not layer of PCB. For example, silkscreen Gerber file will require one instance of Layer, paste mask will require another one, copper top yet another, etc.

  • Configuration Flexibility: The configuration possibilities you get with a Layer are driven by the backend you choose to render your source file.

  • Selecting a Backend: PyGerber provides specialized subclasses of the Layer class each tied to one rendering backend. For instance, if you're aiming for 2D rasterized images, Rasterized2DLayer is your go-to choice.

  • Output Types: Keep in mind, the type of your output file is closely tied to the backend you select.For 2D rasterized rendering all formats supported by Pillow are accepted.

Rasterized render from file

from pygerber.gerberx3.api import (
      ColorScheme,
      Rasterized2DLayer,
      Rasterized2DLayerParams,
)

# Path to Gerber source file.
source_path = "main_cu.grb"

Rasterized2DLayer(
      options=Rasterized2DLayerParams(
            source_path=source_path,
            colors=ColorScheme.COPPER_ALPHA,
      ),
).render().save("output.png")

Example code above creates Rasterized2DLayer object, renders it with rasterized 2D backend and saves it as PNG image. Use of Rasterized2DLayer and Rasterized2DLayerOptions classes implicitly use 2D rasterized backend. To use different rendering backend with high level API, user must pick different Layer and LayerOptions subclasses. For other backends see Target set of tools section, note that only checked ones are available.

source_path option accepts str or Path pointing to local Gerber file. No special file extension is required, content is blindly loaded from specified file, so it's user responsibility to provide correct path. There are also source_code and source_buffer parameters which allow for use of raw str or bytes objects (first one) and StringIO and BytesIO or file descriptors (second one). source_code, source_buffer and source_path are mutually exclusive.

ColorScheme is a class which describes what colors should be used for rendering different parts of image. Additionally it has a few static members which contain predefined colors schemes for frequently used layer types. It is not required to use predefined schemes, creating and passing custom ColorScheme object should work perfectly fine.

Pattern of using <Class> and <Class>Options, like above, is used in many places in PyGerber. When initializing object like Rasterized2DLayer it is only valid to pass Rasterized2DLayerOptions to constructor. Passing LayerOptions or Vectorized2DLayer will cause undefined behavior, most likely yielding no result or raising exception.

Rasterized render from string

from pygerber.gerberx3.api import (
      ColorScheme,
      Rasterized2DLayer,
      Rasterized2DLayerParams,
)

source_code = """
%FSLAX26Y26*%
%MOMM*%
%ADD100R,1.5X1.0X0.5*%
%ADD200C,1.5X1.0*%
%ADD300O,1.5X1.0X0.6*%
%ADD400P,1.5X3X5.0*%
D100*
X0Y0D03*
D200*
X0Y2000000D03*
D300*
X2000000Y0D03*
D400*
X2000000Y2000000D03*
M02*
"""

Rasterized2DLayer(
      options=Rasterized2DLayerParams(
            source_code=source_code,
            colors=ColorScheme.SILK,
            dpi=3000,
      ),
).render().save("output.png")

Code above renders following image:

Documentation

Official documentations is hosted on Github Pages and can be found here.

Gerber features support

This section outlines the support for various Gerber format features in PyGerber's core components: Tokenizer, Parser, and Rasterized2DBackend. We use checkboxes to indicate which features are currently implemented. Checked boxes represent supported features, while unchecked boxes denote features still under development or not available.

Tokenizer

Supported Gerber X3 features:

  • G04 - Comment - A human readable comment, does not affect the image.
  • MO - Mode - Sets the unit to mm or inch.
  • FS - Format specification - Sets the coordinate format, e.g. the number of decimals.
  • FS (Deprecated modes)
  • AD - Aperture define - Defines a template-based aperture, assigns a D code to it.
  • AM - Aperture macro - Defines a macro aperture template.
  • Dnn (nn≥10) - Sets the current aperture to D code nn.
  • D01 - Plot operation - Outside a region statement D01 creates a draw or arc object with the current aperture. Inside it adds a draw/arc segment to the contour under construction. The current point is moved to draw/arc end point after the creation of the draw/arc.
  • D02 - Move operation - D02 moves the current point to the coordinate in the command. It does not create an object.
  • D03 - Flash operation - Creates a flash object with the current aperture. The current point is moved to the flash point.
  • G01 - Sets linear/circular mode to linear.
  • G02 - Sets linear/circular mode to clockwise circular.
  • G03 - Sets linear/circular mode to counterclockwise circular.
  • G75 - A G75 must be called before creating the first arc.
  • LP - Load polarity - Loads the polarity object transformation parameter.
  • LM - Load mirroring - Loads the mirror object transformation parameter.
  • LR - Load rotation - Loads the rotation object transformation parameter.
  • LS - Load scaling - Loads the scale object transformation parameter.
  • G36 - Starts a region statement which creates a region by defining its contours.
  • G37 - Ends the region statement.
  • AB - Aperture block - Opens a block aperture statement and assigns its aperture number or closes a block aperture statement.
  • SR - Step and repeat - Open or closes a step and repeat statement.
  • TF - Attribute on file - Set a file attribute.
  • TA - Attribute on aperture - Add an aperture attribute to the dictionary or modify it.
  • TO - Attribute on object - Add an object attribute to the dictionary or modify it.
  • TD - Attribute delete - Delete one or all attributes in the dictionary.
  • M02 - End of file.

Supported DEPRECATED Gerber features:

  • G54 - Select aperture - This historic code optionally precedes an aperture selection Dnn command. It has no effect. Sometimes used. Deprecated in 2012.

  • G55 - Prepare for flash - This historic code optionally precedes D03 code. It has no effect. Very rarely used nowadays. Deprecated in 2012.

  • G70 - Set the 'Unit' to inch - These historic codes perform a function handled by the MO command. See 4.2.1. Sometimes used. Deprecated in 2012.

  • G71 - Set the 'Unit' to mm - This is part of the historic codes that perform a function handled by the MO command.

  • G90 - Set the 'Coordinate format' to 'Absolute notation' - These historic codes perform a function handled by the FS command. Very rarely used nowadays. Deprecated in 2012.

  • G91 - Set the 'Coordinate format' to 'Incremental notation' - Part of the historic codes handled by the FS command.

    • Important: Incremental notation itself is not supported and is not planned due to lack of test assets and expected complications during implementation.
  • G74 - Sets single quadrant mode - Rarely used, and then typically without effect. Deprecated in 2020. (Spec. 8.1.10)

  • M00 - Program stop - This historic code has the same effect as M02. Very rarely, if ever, used nowadays. Deprecated in 2012.

  • M01 - Optional stop - This historic code has no effect. Very rarely, if ever, used nowadays. Deprecated in 2012.

  • IP - Sets the 'Image polarity' graphics state parameter - This command has no effect in CAD to CAM workflows. Sometimes used, and then usually as %IPPOS*% to confirm the default and then it then has no effect. Deprecated in 2013. (Spec. 8.1.4)

  • AS - Sets the 'Axes correspondence' graphics state parameter - Deprecated in 2013. Rarely used nowadays. (Spec. 8.1.2)

  • IR - Sets 'Image rotation' graphics state parameter - Deprecated in 2013. Rarely used nowadays. (Spec. 8.1.5)

  • MI - Sets 'Image mirroring' graphics state parameter (Spec. 8.1.7)

  • OF - Sets 'Image offset' graphics state parameter (Spec. 8.1.8)

  • SF - Sets 'Scale factor' graphics state parameter (Spec. 8.1.9)

  • IN - Sets the name of the file image. Has no effect. It is comment. Sometimes used. Deprecated in 2013. (Spec. 8.1.3)

  • LN - Loads a name. Has no effect. It is a comment. Sometimes used. Deprecated in 2013. (Spec. 8.1.6)

  • Combining G01/G02/G03 and D01 in a single command. (Spec 8.3.1)

  • Coordinate Data without Operation Code. (Spec 8.3.2)

  • Style Variations in Command Codes. (Spec 8.3.3)

  • Deprecated usage of SR. (Spec 8.3.4)

  • Deprecated Attribute Values. (Spec 8.4)

  • Format Specification (FS) Options (Trailing Zero Omission, Incremental Notation). (Spec. 8.2)

    • Important: Incremental notation itself is not supported and is not planned due to lack of test assets and expected complications during implementation.
  • Rectangular Hole in Standard Apertures (Spec. 8.2.2)

  • Draws and Arcs with Rectangular Apertures (Spec. 8.2.3)

  • Macro Primitive Code 2, Vector Line (Spec. 8.2.4)

  • Macro Primitive Code 22, Lower Left Line (Spec. 8.2.5)

  • Macro Primitive Code 6, Moiré (Spec. 8.2.6)

Parser

Supported Gerber X3 features:

  • MO - Mode - Sets the unit to mm or inch.
  • FS - Format specification - Sets the coordinate format, e.g. the number of decimals.
  • AD - Aperture define - Defines a template-based aperture, assigns a D code to it.
    • Define circle.
    • Define rectangle.
    • Define obround.
    • Define polygon.
    • Define macro.
  • AM - Aperture macro - Defines a macro aperture template.
  • Dnn (nn≥10) - Sets the current aperture to D code nn.
  • D01 - Plot operation - Outside a region statement D01 creates a draw or arc object with the current aperture.
  • D01 - Plot operation - Inside region statement adds a draw/arc segment to the contour under construction. The current point is moved to draw/arc end point after the creation of the draw/arc.
  • D02 - Move operation - D02 moves the current point to the coordinate in the command. It does not create an object.
  • D03 - Flash operation - Creates a flash object with the current aperture. The current point is moved to the flash point.
  • G01 - Sets linear/circular mode to linear.
  • G02 - Sets linear/circular mode to clockwise circular.
  • G03 - Sets linear/circular mode to counterclockwise circular.
  • LP - Load polarity - Loads the polarity object transformation parameter.
  • LM - Load mirroring - Loads the mirror object transformation parameter.
  • LR - Load rotation - Loads the rotation object transformation parameter.
  • LS - Load scaling - Loads the scale object transformation parameter.
  • G36 - Starts a region statement which creates a region by defining its contours.
  • G37 - Ends the region statement.
  • AB - Aperture block - Opens a block aperture statement and assigns its aperture number or closes a block aperture statement.
  • SR - Step and repeat - Open or closes a step and repeat statement.
  • TF - Attribute on file - Set a file attribute.
  • TA - Attribute on aperture - Add an aperture attribute to the dictionary or modify it.
  • TO - Attribute on object - Add an object attribute to the dictionary or modify it.
  • TD - Attribute delete - Delete one or all attributes in the dictionary.
  • M02 - End of file.

Supported DEPRECATED Gerber features:

  • G54 - Select aperture - This historic code optionally precedes an aperture selection Dnn command. It has no effect. Sometimes used. Deprecated in 2012.

  • G55 - Prepare for flash - This historic code optionally precedes D03 code. It has no effect. Very rarely used nowadays. Deprecated in 2012.

  • G70 - Set the 'Unit' to inch - These historic codes perform a function handled by the MO command. See 4.2.1. Sometimes used. Deprecated in 2012.

  • G71 - Set the 'Unit' to mm - This is part of the historic codes that perform a function handled by the MO command.

  • G90 - Set the 'Coordinate format' to 'Absolute notation' - These historic codes perform a function handled by the FS command. Very rarely used nowadays. Deprecated in 2012.

  • G91 - Set the 'Coordinate format' to 'Incremental notation' - Part of the historic codes handled by the FS command.

    • Important: Incremental notation itself is not supported and is not planned due to lack of test assets and expected complications during implementation.
  • G74 - Sets single quadrant mode - Rarely used, and then typically without effect. Deprecated in 2020. (Spec. 8.1.10)

  • M00 - Program stop - This historic code has the same effect as M02. Very rarely, if ever, used nowadays. Deprecated in 2012.

  • M01 - Optional stop - This historic code has no effect. Very rarely, if ever, used nowadays. Deprecated in 2012.

  • IP - Sets the 'Image polarity' graphics state parameter - This command has no effect in CAD to CAM workflows. Sometimes used, and then usually as %IPPOS*% to confirm the default and then it then has no effect. Deprecated in 2013. (Spec. 8.1.4)

  • AS - Sets the 'Axes correspondence' graphics state parameter - Deprecated in 2013. Rarely used nowadays. (Spec. 8.1.2)

  • IR - Sets 'Image rotation' graphics state parameter - Deprecated in 2013. Rarely used nowadays. (Spec. 8.1.5)

  • MI - Sets 'Image mirroring' graphics state parameter (Spec. 8.1.7)

  • OF - Sets 'Image offset' graphics state parameter (Spec. 8.1.8)

  • SF - Sets 'Scale factor' graphics state parameter (Spec. 8.1.9)

  • IN - Sets the name of the file image. Has no effect. It is comment. Sometimes used. Deprecated in 2013. (Spec. 8.1.3)

  • LN - Loads a name. Has no effect. It is a comment. Sometimes used. Deprecated in 2013. (Spec. 8.1.6)

  • Combining G01/G02/G03/G70/G71 and D01 in a single command. (Spec 8.3.1)

  • Combining G01/G02/G03/G70/G71 and D02 in a single command.

  • Combining G01/G02/G03/G70/G71 and D03 in a single command.

  • Coordinate Data without Operation Code. (Spec 8.3.2)

  • Style Variations in Command Codes. (Spec 8.3.3)

  • Deprecated usage of SR. (Spec 8.3.4)

  • Deprecated Attribute Values. (Spec 8.4)

  • Format Specification (FS) Options (Trailing Zero Omission, Incremental Notation). (Spec. 8.2)

    • Important: Incremental notation itself is not supported and is not planned due to lack of test assets and expected complications during implementation.
  • Rectangular Hole in Standard Apertures (Spec. 8.2.2)

  • Draws and Arcs with Rectangular Apertures (Spec. 8.2.3)

  • Macro Primitive Code 2, Vector Line (Spec. 8.2.4)

  • Macro Primitive Code 22, Lower Left Line (Spec. 8.2.5)

  • Macro Primitive Code 6, Moiré (Spec. 8.2.6)

Rasterized2DBackend

Supported Gerber X3 features:

  • Aperture definition with circle
  • Aperture definition with rectangle
  • Aperture definition with obround
  • Aperture definition with polygon
  • Aperture definition with macro
  • Block aperture definition
  • Draw flash with circle aperture
  • Draw flash with rectangle aperture
  • Draw flash with obround aperture
  • Draw flash with polygon aperture
  • Draw flash with macro aperture
  • Draw flash with block aperture
  • Draw line
  • Draw clockwise arc
  • Draw counterclockwise arc
  • Global mirroring
  • Global rotation
  • Global scaling
  • Create region

Supported DEPRECATED Gerber features:

  • Image polarity
  • Image rotation
  • Image mirroring

IMPORTANT This feature list is incomplete, it will get longer over time ...

Development

To quickly set up development environment, first you have to install poetry globally:

pip install poetry

Afterwards you will be able to create development virtual environment:

poetry shell

Then You have to install dependencies into this environment:

poetry install

And pre-commit hooks:

poe install-hooks

Now you are good to go. Whenever you commit changes, pre-commit hooks will be invoked. If they fail or change files, you will have to re-add changes and commit again.

Build from source

To build PyGerber from source You have to set up Development environment first. Make sure you have poetry environment activated with:

poetry shell

With environment active it should be possible to build wheel and source distribution with:

poetry build

Check dist directory within current working directory, pygerber-x.y.z.tar.gz and pygerber-x.y.z-py3-none-any.whl should be there.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygerber-2.0.1.tar.gz (65.4 kB view details)

Uploaded Source

Built Distribution

pygerber-2.0.1-py3-none-any.whl (106.2 kB view details)

Uploaded Python 3

File details

Details for the file pygerber-2.0.1.tar.gz.

File metadata

  • Download URL: pygerber-2.0.1.tar.gz
  • Upload date:
  • Size: 65.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for pygerber-2.0.1.tar.gz
Algorithm Hash digest
SHA256 a833e7ee6c6153053f02db8f58d809daf600f5ba2632a8d6fb0dcd42eda1315b
MD5 977b69cbe8f83b337a725bee2fb6d23c
BLAKE2b-256 1b97614b4eeef35d8dc8225e4caf465a97b947044a13ff778cc97ee167230ba5

See more details on using hashes here.

Provenance

File details

Details for the file pygerber-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: pygerber-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 106.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for pygerber-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09d13730a27ee69c99437ab96665d5f685c193a233fe59155fb407d3a575ec2a
MD5 17ad47232855cbcf592e0a474168524d
BLAKE2b-256 e85099a2f17a07ad5069a18de2b0acda2c78c524d9ba346a1bb27235db4ab3c7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page