Skip to content

Multi file guide

This guide shows how to use CompositeView class in par with GerberFile class to operate on multiple Gerber files at once.

Creating CompositeView instance

CompositeView class is a thin wrapper around multiple GerberFile objects. Usually it does not make sense to operate on all files from project at once, since rendering top layers on top of bottom ones will produce hard to understand images. It makes more sense to render them separately. Therefore CompositeView is a way to group files that should be rendered together, for example all parts of top of PCB, silkscreen, solder mask, top copper etc. while parts of bottom of PCB are grouped in separate CompositeView, so do internal layers.

CompositeView constructor accepts multiple GerberFile objects as arguments. First GerberFile is the bottom most, usually copper layer, and last GerberFile is top most, usually silk screen. Here is an example:

create_project.py
from pygerber.examples import ExamplesEnum, load_example
from pygerber.gerber.api import CompositeView, GerberFile, FileTypeEnum


project = CompositeView(
    [
        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,
        ),
    ],
)
print(project)
$ python create_project.py
CompositeView((GerberFile(source='@string', sha256='415f75c8fb8ffa30a1c272241e1048186af9d686cc58588c96af859acffc3f0e'), GerberFile(source='@string', sha256='f6cf84f51a7330d9195ea53164e4db7bac0415db5780df3f53e1d2986c85c725'), GerberFile(source='@string', sha256='0957b0e6c778e58507ba848249589bcbfba3a2b7e28c65ab5f07fe08f154d52a'), GerberFile(source='@string', sha256='66e189271f75224c5aa37e1ecf716e0a90f28560f54a49861b21420133c3c530')))

Rendering raster images

Image rendering API of CompositeView object is similar to rendering API of GerberFile object. CompositeView object exposes render_with_pillow() method which renders all files into single image.

CompositeView automatically aligns all images during grouped rendering, determines how big final image has to be to fit all images and merges them into single image. It is still possible to retrieve individual images from result returned by rendering methods.

render_project.py
from pygerber.examples import ExamplesEnum, load_example
from pygerber.gerber.api import CompositeView, GerberFile


project = CompositeView(
    [
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Cu)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Mask)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Paste)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Silkscreen)),
    ],
)
image = project.render_with_pillow()
image.get_image().save("output.png")

render_project

Image presented above is a result of rendering Gerber files from this project: github.com/AntonioMR/ATMEGA328-Motor-Board

Accessing individual images

After rendering a image with CompositeView object you can still access individual images by using get_sub_images() method available on CompositePillowImage object returned by render_with_pillow() method.

render_project.py
from pygerber.examples import ExamplesEnum, load_example
from pygerber.gerber.api import CompositeView, GerberFile


project = CompositeView(
    [
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Cu)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Mask)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Paste)),
        GerberFile.from_str(load_example(ExamplesEnum.simple_2layer_F_Silkscreen)),
    ],
)
image = project.render_with_pillow()
image.get_sub_images()[0].get_image().save("output.png")

render_project

Further reading

Check out full reference of pygerber.gerber.api.