Skip to content

🥞 Multi file guide

Welcome to the multi file guide. This guide shows how to use CompositeView class API to handle multiple Gerber files at once. CompositeView relies on GerberFile instances to provide access to individual Gerber files, hence It might be useful if you take a look onto Single file guide before continuing with this guide.

Creating CompositeView instance

CompositeView class is a thin wrapper around multiple GerberFile objects which can be imported from pygerber.gerber.api module. You may have noticed that the name of the class is weirdly generic, instead of being something like Project or alike. Well, it turns out that usually it does not make sense to operate on all files from project at once, for example, rendering top layers on top of bottom ones will produce hard to understand images. Therefore PyGerbers CompositeView class takes an aproach of group files that should be rendered together instead of accumulating all the files from the project. With that logic in mind, we would use one instance of CompositeView to represent top layer of the design, with silkscreen, solder mask, top copper etc. while parts of bottom of PCB are grouped in separate CompositeView instance.

CompositeView constructor accepts multiple GerberFile objects as arguments. Order of files affects order of rendering to shared image. First GerberFile is the bottom most, usually copper layer, will be covered with all other layers, and last GerberFile is top most, usually silk screen, will not be obscured by any layer. 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.