API V1 Usage (legacy)¶
Deprecation warning
This is a legacy Gerber API, it will be removed in PyGerber 3.0.
PyGerber offers simple high-level API for rendering Gerber files. All necessary tools
can be imported from pygerber.gerberx3.api module. See
module reference for full object
list.
Important
API of pygerber.gerberx3.api module is guaranteed to remain unchanged between
patches and minor releases of PyGerber, whereas other modules, even those contained
deeper in pygerber.gerberx3.api like gerber.gerberx3.api._layers, may change at
any time.
Bug
Please report any objects which
have to be imported from other places than pygerber.gerberx3.api for high level
API, it's typechecking or error handling to work. Such situations are considered a
bug.
Overview¶
-
Layerclass and it's subclasses in PyGerber API represent a Gerber file. It's completely different meaning than in PCB design, much closer to what a image layer is in image manipulation software, like Gimp.Layerclass itself it not a functional, only it's subclasses can be used to instruct PyGerber how to render Gerber file. Based on whatLayersubclass is used, different output image types can be obtained. For exampleRasterized2DLayerwill allow for generating rasterized images and saving them as JPEG, PNG, TIFF and other image formats. -
LayerParamsclass and its subclasses are intended to be used to configure the correspondingLayerclasses. For example, when usingRasterized2DLayerfor rendering, it is only valid to passRasterized2DLayerParamsto constructor. Passing incorrectLayerParamssubclass will result inTypeError. -
RenderingResultclass is returned fromrender()method ofLayerinstance. It provides simple interface for saving rendering output. Different output formats will be available depending on the layer type used. ForRasterized2DLayerlist of supported output formats is equivalent to list of formats supported by Pillow library. It can be found in Pillow documentation.
Usage¶
Below we provided an example of simple API use.
We have following Gerber file:
| render_copper_from_path.grb | |
|---|---|
Which should result in simple image:
To achieve such result with PyGerber, first we have to import all necessary classes from
pygerber.gerberx3.api module:
We will be using Rasterized2DLayer, as we want to create a PNG image.
Rasterized2DLayerParams will be necessary to specify path to source file and image
color scheme, declared with ColorScheme:
ColorScheme creation.
ColorScheme.COPPER_ALPHA is a predefined color scheme, equivalent can be created
manually:
ColorScheme.COPPER_ALPHA = ColorScheme(
background_color=RGBA.from_rgba(0, 0, 0, 0),
clear_color=RGBA.from_rgba(60, 181, 60, 255),
solid_color=RGBA.from_rgba(40, 143, 40, 255),
clear_region_color=RGBA.from_rgba(60, 181, 60, 255),
solid_region_color=RGBA.from_rgba(40, 143, 40, 255),
)
See reference for all possible ways of creating
RGBA color objects
and ColorSchema color schema objects.
Afterwards we can create a Rasterized2DLayer object. Remember to provide previously
constructed Rasterized2DLayerParams instance to constructor:
Now we can use render() method of Rasterized2DLayer instance to create
RenderingResult:
Then we can call save() method on RenderingResult to save rendered image to drive:
Alternatively you can save image to BytesIO:
More examples¶
Below are few more examples showing how to provide Gerber code to Layer by different
ways, however they are all equivalent.
Load from file path¶
Read from buffer¶
Read from string¶
Obtaining layer properties¶
I some cases it may be useful to obtain information about layer which was rendered. For example origin of coordinate system of image can be useful for aligning multiple layers on top of each other, or for other similar transformations.
Those information can be extracted from RenderingResult object, returned from
Layer.render() method. RenderingResult object has get_properties() method which
returns LayerProperties object which contains all the necessary data to determine
coordinate origins and bounding boxes of layer.
Further reading¶
To further extend your knowledge about how to use PyGerber you could read
pygerber.gerberx3.api module reference
or see Gerber Advanced API