Skip to content

Gerber code generation

Overview

This section describes how to generate Gerber code with use of the pygerber.builder.gerber module.

All of the code building functionality is provided within GerberX3Builder class available in that module.

For reference of tools available in that module check out this reference page.

Creating pads

To generate Gerber code you need to create an instance of GerberX3Builder class, afterwards you can use new_pad() method to create a new pad shape which can be added to the image with add_pad() method.

example.py
from pygerber.builder.gerber import GerberX3Builder


builder = GerberX3Builder()

d10 = builder.new_pad().circle(diameter=0.5)
builder.add_pad(d10, (2, 1))

code = builder.get_code()
print(code.dumps())
$ python example.py
%FSLAX46Y46*%
%MOMM*%
%ADD10C,0.5*%
D10*
X2000000Y1000000D03*
M02*

This is the rendered result of the example presented above:


Embedded Image


Adding traces

You can also add traces to the image with new_trace() method and add_arc_trace() method.

example.py
from pygerber.builder.gerber import GerberX3Builder


builder = GerberX3Builder()

d10 = builder.new_pad().circle(diameter=0.5)
builder.add_pad(d10, (2, 1))
builder.add_trace(0.1, (0, 0), (0, 1))
builder.add_trace(0.1, (0, 1), (2, 1))

code = builder.get_code()
print(code.dumps())
$ python example.py
%FSLAX46Y46*%
%MOMM*%
%ADD10C,0.5*%
%ADD11C,0.1*%
D10*
X2000000Y1000000D03*
D11*
X0Y0D02*
X0Y1000000D01*
X2000000Y1000000D01*
M02*

This is the rendered result of the example presented above:


Embedded Image


Using objects as locations

Commands creating graphical elements, like add_pad() or add_trace() return special Draw objects (PadDraw or TraceDraw respectively) which can be used as locations for new_pad() or new_trace() methods. This way you don not have to retype coordinates for draws which are connected to previous objects.

example.py
from pygerber.builder.gerber import GerberX3Builder


builder = GerberX3Builder()

d10 = builder.new_pad().circle(diameter=0.5)

pad_0 = builder.add_pad(d10, (0, 0))
pad_1 = builder.add_pad(d10, (2, 1))

trace_0 = builder.add_trace(0.1, pad_0, (0, 1))
builder.add_trace(0.1, trace_0, pad_1)


code = builder.get_code()
print(code.dumps())
$ python example.py
%FSLAX46Y46*%
%MOMM*%
%ADD10C,0.5*%
%ADD11C,0.1*%
D10*
X0Y0D03*
X2000000Y1000000D03*
D11*
X0Y0D02*
X0Y1000000D01*
X2000000Y1000000D01*
M02*

This is the rendered result of the example presented above:


Embedded Image


Creating custom pads

You can create custom pads by using custom() method of object returned by new_pad() method. Custom pads are built with use of methods available on object returned by custom(). Method calls can (and should) be chained. When you are done adding elements to the pad, you should call create() method to finish the modification of custom pad and create concrete object representing it. After calling create(), you cannot and new elements to that custom pad.

Custom pad is used in the same way as any other pad, to add it to the image you should use add_pad() method.

custom_pad_example.py
from pygerber.builder.gerber import GerberX3Builder


builder = GerberX3Builder()

d10 = (
    builder.new_pad()
    .custom()
    .add_circle(0.2, (1, 1))
    .add_circle(0.2, (-1, 1))
    .add_circle(0.2, (1, -1))
    .add_circle(0.2, (-1, -1))
    .add_vector_line(0.15, (1, 1), (-1, -1))
    .add_vector_line(0.15, (-1, 1), (1, -1))
    .create()
)
builder.add_pad(d10, (0, 0))
builder.add_pad(d10, (3, 0))
builder.add_pad(d10, (6, 0))

code = builder.get_code()
print(code.dumps())
$ python custom_pad_example.py
%FSLAX46Y46*%
%MOMM*%
%AMM0*
1,1.0,0.2,1.0,1.0,0.0*
1,1.0,0.2,-1.0,1.0,0.0*
1,1.0,0.2,1.0,-1.0,0.0*
1,1.0,0.2,-1.0,-1.0,0.0*
20,1.0,0.15,1.0,1.0,-1.0,-1.0,0.0*
20,1.0,0.15,-1.0,1.0,1.0,-1.0,0.0*%
%ADD10M0*%
D10*
X0Y0D03*
X3000000Y0D03*
X6000000Y0D03*
M02*

This is the rendered result of the example presented above:


Embedded Image