Parsing, formatting and rendering toolkit for Gerber X3 file format
Project description
PyGerber
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
- Optimizer
- Introspection API
- Rasterized 2D rendering engine (With Pillow)
- Vector 2D rendering engine (With drawsvg)
- Model 3D rendering engine (With Blender)
- Formatter
- Linter (eg. deprecated syntax detection)
- Gerber X3/X2 Language Server (with
language-server
extras)
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.
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
Language Server
Since release 2.1.0 PyGerber now provides Gerber X3/X2 Language Server with
LSP support. It can be enabled
by installing PyGerber extras set language-server
with following command:
pip install pygerber[language-server]
Afterwards you can use pygerber is-language-server-available
to check if language
server was correctly enabled. Please report all issues in
PyGerber Issues section.
Command line usage
After installing pygerber
, depending on your environment, it should become available
in your command line:
pygerber --version
Output should be similar to one below ⇩, where x.y.z
should match version of
PyGerber installed.
$ pygerber --version
pygerber, version x.y.z
Use --help
to display help messages with lists of subcommands and subcommand options:
pygerber raster-2d --help
To render 2D PNG image of some gerber file you can simply use:
pygerber raster-2d gerber-source.grb
Image will be saved to output.png
in current working directory.
Programmatic usage
JPG
PyGerber can be used programmatically to render Gerber files. Below is an minimalistic example of how to render one of the example files included with PyGerber release to JPEG image:
from pygerber.examples import ExamplesEnum, get_example_path
from pygerber.gerberx3.api.v2 import GerberFile
GerberFile.from_file(
get_example_path(ExamplesEnum.UCAMCO_ex_2_Shapes),
).parse().render_raster("output.jpg")
Running code above will create output.jpg
file in current working directory which
should look like this:
PNG
It is also possible to render Gerber files to PNG with custom resolution and different color schemes:
from pygerber.examples import ExamplesEnum, get_example_path
from pygerber.gerberx3.api.v2 import ColorScheme, GerberFile, PixelFormatEnum
GerberFile.from_file(
get_example_path(ExamplesEnum.ShapeFlashes),
).parse().render_raster(
"output.png",
dpmm=100,
color_scheme=ColorScheme.COPPER_ALPHA,
pixel_format=PixelFormatEnum.RGBA,
)
Code above renders following image:
SVG
Finally you can also create SVG files with PyGerber:
from pygerber.examples import ExamplesEnum, load_example
from pygerber.gerberx3.api.v2 import GerberFile
source_code = load_example(ExamplesEnum.UCAMCO_ex_2_Shapes)
GerberFile.from_str(source_code).parse().render_svg("output.svg")
Multiple layers
PyGerber can also render multiple layers to single image. Below is an example of how to
render four layers to single PNG image with use of Project
class:
from pygerber.examples import ExamplesEnum, load_example
from pygerber.gerberx3.api.v2 import FileTypeEnum, GerberFile, Project
Project(
[
GerberFile.from_str(
load_example(ExamplesEnum.simple_2layer_F_Cu),
FileTypeEnum.COPPER,
),
GerberFile.from_str(
load_example(ExamplesEnum.simple_2layer_F_Mask),
FileTypeEnum.MASK,
),
GerberFile.from_str(
load_example(ExamplesEnum.simple_2layer_F_Paste),
FileTypeEnum.PASTE,
),
GerberFile.from_str(
load_example(ExamplesEnum.simple_2layer_F_Silkscreen),
FileTypeEnum.SILK,
),
],
).parse().render_raster("output.png", dpmm=40)
Here is the result:
Advanced usage
Additionally to examples presented above which use high level API, PyGerber provides low level API which allows to directly access PyGerber internals and change behavior of parser, tokenizer and renderers. This can be used for code introspection and potentially other purposed. Check out documentation for more information.
Documentation
Official documentations is hosted on Github Pages and can be found here.
Gerber features support
Please refer to documentation for
for detailed list of features which are supported/not supported by each tool.
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.08
(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.
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.
Gerber reference archive
This repository contains also archival reference files. Although new specs contain dedicated changelog section it may still be helpful in some rare cases to look through old Gerber specs. Archival files can be found here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pygerber-2.3.0.tar.gz
.
File metadata
- Download URL: pygerber-2.3.0.tar.gz
- Upload date:
- Size: 157.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67759934fe35c3d5185efc1c1efcf4062f4201d3cc98f5da65bad09542ec4a53 |
|
MD5 | 15c37cf7ae9f2506bb012d45ed57dcd7 |
|
BLAKE2b-256 | 647dffa1c279b5b62fc627a0e3589da6df6e14a4bbefc16b4773556e674f8cb0 |
Provenance
File details
Details for the file pygerber-2.3.0-py3-none-any.whl
.
File metadata
- Download URL: pygerber-2.3.0-py3-none-any.whl
- Upload date:
- Size: 273.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2994b115b233927f4752a4c0faf3311e5b965a8d48dcc3df6d7b5661e7179640 |
|
MD5 | 666f1c041d424e0125e343a2dcafe91e |
|
BLAKE2b-256 | e3fff104c0d0f1b80b323a6f926b64ad804d5f4c6346329a33d76a017148b069 |