2D rendering API¶
In general 2D rendering is faster and more reliable, as Gerber format was not created with generating 3D models in mind. Therefore 2D rendering was first one developed and currently has most extensive support for Gerber language features.
Single-file 2D rendering¶
Rendering single Gerber source file and saving it to a file can be done the following way:
# -*- coding: utf-8 -*-
from pygerber.API2D import render_file
pillow_image = render_file("tests/gerber/s5.grb")
pillow_image.save("render.png")
Also, even though it takes only 2 lines of code, you can reduce it to one function call:
# -*- coding: utf-8 -*-
from pygerber.API2D import render_file_and_save
render_file_and_save("tests/gerber/s5.grb", "render.png")
Multi-file file 2D rendering¶
Rendering multiple files from one project is a bit more complicated as it involves creating some sort of configuration, in form of a standalone file or a dictionary generated on a fly.
Both files and dictionaries share data format and capabilities, which you can learn more about from Specfile for 2D rendering chapter.
Example usage of dictionary configuration looks like this:
# -*- coding: utf-8 -*-
from pygerber.API2D import render_from_spec
render_from_spec(
{
"dpi": 600,
"image_padding": 0,
"ignore_deprecated": True,
"layers": [
{
"file_path": "tests/gerber/set/top_copper.grb",
"colors": {
"dark": [40, 143, 40, 255],
"clear": [60, 181, 60, 255],
},
},
{
"file_path": "tests/gerber/set/top_solder_mask.grb",
"colors": "solder_mask",
},
{"file_path": "tests/gerber/set/top_paste_mask.grb"},
{
"file_path": ".tests/gerber/set/top_silk.grb",
"colors": "silk",
},
],
}
)
As It was mentioned above, configuration can be also created in form of standalone so called specfile, and then it can be used following way:
# -*- coding: utf-8 -*-
from pygerber.API2D import render_from_yaml
render_from_yaml("tests/gerber/pillow/specfile.yaml")
And coresponding source of YAML specfile looks like this:
dpi: 600
ignore_deprecated: yes
image_padding: 0
layers:
- file_path: ./tests/gerber/set/top_copper.grb
colors:
dark: [40, 143, 40, 255]
clear: [60, 181, 60, 255]
- file_path: ./tests/gerber/set/top_solder_mask.grb
colors: solder_mask
- file_path: ./tests/gerber/set/top_paste_mask.grb
- file_path: ./tests/gerber/set/top_silk.grb
colors: silk
Specfiles can be created using JSON, YAML and TOML however functions for
each one of the those languages differs only by function name suffix. We
have render_from_json for JSON, render_from_yaml for
YAML and render_from_toml for TOML, so there is no reason to
show separate examples for each.