API V1 Reference (legacy)¶
Deprecation warning
This is a legacy Gerber API, it will be removed in PyGerber 3.0.
api
¶
pygerber.gerberx3.api
module provides simple, high-level API for rendering
Gerber X3/X2 files.
ColorScheme
¶
Bases: FrozenGeneralModel
Set of colors which should be used for rendering.
ColorScheme class contains set of colors which should be used for different parts of rendered image. At the same time it also works as a container for predefined color schemes commonly used for parts of PCB.
Predefined colors
All predefined colors have two variants - normal one and one with "*_ALPHA" suffix. Those without suffix have solid background and are not intended for constructing multi-layer images out of them ie. they are not suitable for rendering a project consisting of separate copper, silk, pase mask and composing them into single image. For cases when rendered images are intended for stacking "*_ALPHA" schemes should be used, as background and transparent parts of image will be truly transparent.
Source code in src\pygerber\backend\rasterized_2d\color_scheme.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
SILK
class-attribute
¶
Default color of silk layer.
This schema provided non-transparent background, which results in images which can not be used for stacking on top of other layers, as they would completely obscure them.
SILK_ALPHA
class-attribute
¶
Default color of silk layer with alpha channel.
This schema provides transparent background. Images using this schema can be stacked on top of each other without obscuring layers below.
COPPER
class-attribute
¶
Default color of copper layer.
This schema provided non-transparent background, which results in images which can not be used for stacking on top of other layers, as they would completely obscure them.
COPPER_ALPHA
class-attribute
¶
Default color of copper layer with alpha channel.
This schema provides transparent background. Images using this schema can be stacked on top of each other without obscuring layers below.
PASTE_MASK
class-attribute
¶
Default color of paste mask layer.
This schema provided non-transparent background, which results in images which can not be used for stacking on top of other layers, as they would completely obscure them.
PASTE_MASK_ALPHA
class-attribute
¶
Default color of paste mask layer with alpha channel.
This schema provides transparent background. Images using this schema can be stacked on top of each other without obscuring layers below.
SOLDER_MASK
class-attribute
¶
Default color of solder mask layer.
This schema provided non-transparent background, which results in images which can not be used for stacking on top of other layers, as they would completely obscure them.
SOLDER_MASK_ALPHA
class-attribute
¶
Default color of solder mask layer with alpha channel.
This schema provides transparent background. Images using this schema can be stacked on top of each other without obscuring layers below.
DEFAULT_GRAYSCALE
class-attribute
¶
Default color scheme for files which were not assigned other color scheme.
debug_1_color
class-attribute
instance-attribute
¶
Color used for debug elements.
debug_2_color
class-attribute
instance-attribute
¶
Color used for debug elements.
get_grayscale_to_rgba_color_map
¶
Return grayscale to RGBA color map.
Source code in src\pygerber\backend\rasterized_2d\color_scheme.py
RGBA
¶
Bases: FrozenGeneralModel
Representation of RGBA color.
Source code in src\pygerber\common\rgba.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
from_hex
classmethod
¶
Build RGBA color object from hexadecimal string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
String containing color value. Accepted formats are |
required |
Returns:
Type | Description |
---|---|
RGBA
|
Color built from hexadecimal values. |
Source code in src\pygerber\common\rgba.py
from_rgba
classmethod
¶
Build RGBA color object from reg, green, blue and alpha integer values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
int
|
Red chanel value as integer from 0 to 255, inclusive. |
required |
g |
int
|
Green chanel value as integer from 0 to 255, inclusive. |
required |
b |
int
|
Blue chanel value as integer from 0 to 255, inclusive. |
required |
a |
int
|
Alpha chanel value as integer from 0 to 255, inclusive., by default 0xFF |
255
|
Returns:
Type | Description |
---|---|
Self
|
Color built from r, g, b, a values. |
Source code in src\pygerber\common\rgba.py
from_hsv
classmethod
¶
Build RGBA color object from hue, saturation, value and alpha.
For extended information refer to Wikipedia: https://en.wikipedia.org/wiki/HSL_and_HSV
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
int
|
Hue of color, integer in range 0 to 360 inclusive. |
required |
s |
float
|
Saturation of color, float in range 0.0 to 100.0 inclusive. |
required |
v |
float
|
Value of color, float in range 0.0 to 100.0 inclusive. |
required |
a |
int
|
Alpha of color, int in range 0 to 255 inclusive. |
255
|
Returns:
Type | Description |
---|---|
Self
|
Color built from h, s, v, a values. |
Source code in src\pygerber\common\rgba.py
as_rgba_int
¶
as_rgb_int
¶
as_rgba_float
¶
Return RGBA color as tuple of floats in range 0.0 to 1.0 inclusive.
Source code in src\pygerber\common\rgba.py
to_hex
¶
Return color as hexadecimal string.
Eg. #FF0000FF
for red color.
GerberX3APIError
¶
Bases: Exception
Base class for API errors.
Exceptions derived from this exception are exclusively raised in PyGerber's Gerber
X3 high level API. This exception can be used in
try: ... except GerberX3APIError: ...
block to catch all exceptions
raised by this API while allowing other exceptions to interrupt execution.
Source code in src\pygerber\gerberx3\api\_errors.py
MutuallyExclusiveViolationError
¶
Bases: GerberX3APIError
Raised when two or more of mutually exclusive parameters are provided.
LayerParams
class accepts three mutually exclusive fields, source_path
,
source_code
and source_buffer
for providing source code to Layer
.
When more than one of those options is set, this exception will be raised.
Source code in src\pygerber\gerberx3\api\_errors.py
RenderingResultNotReadyError
¶
Bases: GerberX3APIError
Raised when RenderingResult is requested before it was rendered.
Layer.get_rendering_result()
method can only be called after Layer.render()
.
Breaking this rule will cause this exception to be raised.
Source code in src\pygerber\gerberx3\api\_errors.py
Layer
¶
Representation of Gerber X3 image layer.
This is only abstract base class, please use one of its subclasses with rendering system guarantees.
Source code in src\pygerber\gerberx3\api\_layers.py
__init__
¶
Create PCB layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options |
LayerParams
|
Configuration of layer. |
required |
Source code in src\pygerber\gerberx3\api\_layers.py
render
¶
Render layer image.
Source code in src\pygerber\gerberx3\api\_layers.py
get_rendering_result
¶
Return result of rendering Gerber file.
Source code in src\pygerber\gerberx3\api\_layers.py
LayerParams
¶
Bases: BaseModel
Parameters for Layer object.
source_path
, source_code
and source_buffer
are mutually exclusive.
When more than one of them is provided to constructor,
MutuallyExclusiveViolationError will be raised.
Source code in src\pygerber\gerberx3\api\_layers.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
source_path
class-attribute
instance-attribute
¶
Path to source file containing Gerber code. It will be automatically loaded
from local storage, when provided. Mutually exclusive with source_code
and
source_buffer
.
source_code
class-attribute
instance-attribute
¶
Gerber source code. Mutually exclusive with source_path
and source_buffer
.
source_buffer
class-attribute
instance-attribute
¶
Buffer containing Gerber source code. Buffer pointer should be at the
beginning of the buffer. Mutually exclusive with source_path
and
source_code
.
parser_error
class-attribute
instance-attribute
¶
Callback function or rule describing how to treat errors during parsing.
encoding
class-attribute
instance-attribute
¶
Encoding of code, used when loading from file, decoding source_code
provided as bytes and reading source_buffer
provided as BytesIO.
draw_region_outlines
class-attribute
instance-attribute
¶
When drawing regions, after filling region, draw also outline of region with apertures used for region outlines. This behavior is not expected by KiCAD by default but may be useful in some scenarios.
get_source_code
¶
Return source code of layer.
LayerProperties
¶
Properties of layer retrieved from Gerber source code.
Source code in src\pygerber\gerberx3\api\_layers.py
target_bounding_box
instance-attribute
¶
Bounding box of rendering target. May differ from coordinates used in Gerber file as it uses rendering target coordinate space.
target_coordinate_origin
instance-attribute
¶
Offset of origin of coordinate system used by rendering target. Bottom left corner of coordinate space of rendering target.
gerber_bounding_box
instance-attribute
¶
Bounding box of drawing area in Gerber file coordinate space.
gerber_coordinate_origin
instance-attribute
¶
Origin of coordinate space of Gerber file. Equivalent to bottom left corner of
gerber_bounding_box
.
Can be useful to determine how to align multiple Gerber files by calculating how their coordinate origins are positioned in relation to each other.
__init__
¶
__init__(
target_bounding_box: BoundingBox,
target_coordinate_origin: Vector2D,
gerber_bounding_box: BoundingBox,
gerber_coordinate_origin: Vector2D,
) -> None
Initialize layer properties.
Source code in src\pygerber\gerberx3\api\_layers.py
Rasterized2DLayer
¶
Bases: Layer
Representation of Gerber X3 rasterized 2D image layer.
Rasterized images can be saved in any image format supported by Pillow library. For full list of supported formats please refer to Pillow documentation.
Source code in src\pygerber\gerberx3\api\_layers.py
__init__
¶
Initialize Layer object.
Source code in src\pygerber\gerberx3\api\_layers.py
Rasterized2DLayerParams
¶
Bases: LayerParams
Parameters for Layer with 2D rendering.
source_path
, source_code
and source_buffer
are mutually exclusive.
When more than one of them is provided to constructor,
MutuallyExclusiveViolationError will be raised.
Source code in src\pygerber\gerberx3\api\_layers.py
debug_dump_apertures
class-attribute
instance-attribute
¶
Debug option - dump aperture images to files in given directory.
debug_include_extra_padding
class-attribute
instance-attribute
¶
Debug option - include large extra padding on all rendering targets to simplify tracking of mispositioned draw commands.
debug_include_bounding_boxes
class-attribute
instance-attribute
¶
Debug option - include bounding boxes as square outlines on drawing targets to simplify tracking of miscalculated bounding boxes.
RenderingResult
¶
Result of rendering of layer.
Source code in src\pygerber\gerberx3\api\_layers.py
__init__
¶
Initialize rendering result object.
save
¶
Save result to specified file or buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dest |
Path | str | BytesIO
|
Write target. |
required |
**options |
Any
|
Extra parameters which will be passed to saving implementation.
When dest is BytesIO or alike, |
{}
|
Source code in src\pygerber\gerberx3\api\_layers.py
ApertureNotDefinedError
¶
Bases: ParserError
Raised when undefined aperture is selected.
Source code in src\pygerber\gerberx3\parser\errors.py
ApertureNotSelectedError
¶
Bases: ParserError
Raised when attempting to use aperture without selecting it first.
Source code in src\pygerber\gerberx3\parser\errors.py
CoordinateFormatNotSetError
¶
Bases: ParserError
Raised when coordinate parser is requested before coordinate format was set.
Source code in src\pygerber\gerberx3\parser\errors.py
ExitParsingProcessInterrupt
¶
IncrementalCoordinatesNotSupportedError
¶
Bases: ParserError
Raised when incremental coordinates are selected. (Spec. 8.2.1.2).
Source code in src\pygerber\gerberx3\parser\errors.py
InvalidCoordinateLengthError
¶
Bases: ParserError
Raised when coordinate string is too long.
Source code in src\pygerber\gerberx3\parser\errors.py
OnUpdateDrawingStateError
¶
Bases: ParserError
Raised when parser encounters fatal failure from non-parser specific exception during call to .update_drawing_state() call.
Source code in src\pygerber\gerberx3\parser\errors.py
ParserError
¶
Bases: ValueError
Base class for parser errors.
Exceptions derived from this exception are exclusively raised in PyGerber's Gerber
X3 Parser. This exception can be used in
try: ... except ParserError: ...
block to catch all exceptions
raised by Parser while allowing other exceptions to interrupt execution.
Source code in src\pygerber\gerberx3\parser\errors.py
ParserFatalError
¶
Bases: ParserError
Raised when parser encounters fatal failure from non-parser specific exception.
Source code in src\pygerber\gerberx3\parser\errors.py
UnitNotSetError
¶
Bases: ParserError
Raised when operation which requires units to be set is executed before units are set.
Source code in src\pygerber\gerberx3\parser\errors.py
UnsupportedCoordinateTypeError
¶
Bases: ParserError
Raised for unsupported coordinate types.
Source code in src\pygerber\gerberx3\parser\errors.py
ZeroOmissionNotSupportedError
¶
Bases: ParserError
Raised when incremental coordinates are selected. (Spec. 8.2.1.2).
Source code in src\pygerber\gerberx3\parser\errors.py
ParserOnErrorAction
¶
Bases: Enum
Possible error actions.
Source code in src\pygerber\gerberx3\parser\parser.py
Ignore
class-attribute
instance-attribute
¶
Ignore parser errors. Errors which occurred will not be signaled. May yield unexpected results for broken files, with missing draw commands or even more significant errors.
Warn
class-attribute
instance-attribute
¶
Warn on parser error. Parser will log warning message about what went wrong. Best for supporting wide range of files without silently ignoring errors in code.
Raise
class-attribute
instance-attribute
¶
Raise exception whenever parser encounters error. Will completely break out of parsing process, making it impossible to render slightly malformed files.