diff options
author | Hamilton Kibbe <hamilton.kibbe@gmail.com> | 2015-12-22 02:45:48 -0500 |
---|---|---|
committer | Hamilton Kibbe <hamilton.kibbe@gmail.com> | 2015-12-22 02:47:23 -0500 |
commit | 6f876edd09d9b81649691e529f85653f14b8fd1c (patch) | |
tree | 808e3ea789816452f52e834d4c5c1744bdee6541 /gerber/render/cairo_backend.py | |
parent | af5541ac93b222c05229ee05c9def8dbae5f6e25 (diff) | |
download | gerbonara-6f876edd09d9b81649691e529f85653f14b8fd1c.tar.gz gerbonara-6f876edd09d9b81649691e529f85653f14b8fd1c.tar.bz2 gerbonara-6f876edd09d9b81649691e529f85653f14b8fd1c.zip |
Add PCB interface
this incorporates some of @chintal's layers.py changes
PCB.from_directory() simplifies loading of multiple gerbers
the PCB() class should be pretty helpful going forward...
the context classes could use some cleaning up, although I'd like to wait until the freecad stuff gets merged, that way we can try to refactor the context base to support more use cases
Diffstat (limited to 'gerber/render/cairo_backend.py')
-rw-r--r-- | gerber/render/cairo_backend.py | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/gerber/render/cairo_backend.py b/gerber/render/cairo_backend.py index 8283ae0..7acf29a 100644 --- a/gerber/render/cairo_backend.py +++ b/gerber/render/cairo_backend.py @@ -17,7 +17,7 @@ import cairocffi as cairo
-from operator import mul
+from operator import mul, div
import math
import tempfile
@@ -39,16 +39,16 @@ class GerberCairoContext(GerberContext): self.bg = False
self.mask = None
self.mask_ctx = None
- self.origin_in_pixels = None
- self.size_in_pixels = None
+ self.origin_in_inch = None
+ self.size_in_inch = None
- def set_bounds(self, bounds):
+ def set_bounds(self, bounds, new_surface=False):
origin_in_inch = (bounds[0][0], bounds[1][0])
size_in_inch = (abs(bounds[0][1] - bounds[0][0]), abs(bounds[1][1] - bounds[1][0]))
size_in_pixels = map(mul, size_in_inch, self.scale)
- self.origin_in_pixels = tuple(map(mul, origin_in_inch, self.scale)) if self.origin_in_pixels is None else self.origin_in_pixels
- self.size_in_pixels = size_in_pixels if self.size_in_pixels is None else self.size_in_pixels
- if self.surface is None:
+ self.origin_in_inch = origin_in_inch if self.origin_in_inch is None else self.origin_in_inch
+ self.size_in_inch = size_in_inch if self.size_in_inch is None else self.size_in_inch
+ if (self.surface is None) or new_surface:
self.surface_buffer = tempfile.NamedTemporaryFile()
self.surface = cairo.SVGSurface(self.surface_buffer, size_in_pixels[0], size_in_pixels[1])
self.ctx = cairo.Context(self.surface)
@@ -61,6 +61,36 @@ class GerberCairoContext(GerberContext): self.mask_ctx.scale(1, -1)
self.mask_ctx.translate(-(origin_in_inch[0] * self.scale[0]), (-origin_in_inch[1]*self.scale[0]) - size_in_pixels[1])
+ def render_layers(self, layers, filename):
+ """ Render a set of layers
+ """
+ self.set_bounds(layers[0].bounds, True)
+ self._paint_background(True)
+ for layer in layers:
+ self._render_layer(layer)
+ self.dump(filename)
+
+ @property
+ def origin_in_pixels(self):
+ return tuple(map(mul, self.origin_in_inch, self.scale)) if self.origin_in_inch is not None else (0.0, 0.0)
+
+ @property
+ def size_in_pixels(self):
+ return tuple(map(mul, self.size_in_inch, self.scale)) if self.size_in_inch is not None else (0.0, 0.0)
+
+ def _render_layer(self, layer):
+ self.color = layer.settings.color
+ self.alpha = layer.settings.alpha
+ self.invert = layer.settings.invert
+ if layer.settings.mirror:
+ raise Warning('mirrored layers aren\'t supported yet...')
+ if self.invert:
+ self._clear_mask()
+ for p in layer.primitives:
+ self.render(p)
+ if self.invert:
+ self._render_mask()
+
def _render_line(self, line, color):
start = map(mul, line.start, self.scale)
end = map(mul, line.end, self.scale)
@@ -178,12 +208,13 @@ class GerberCairoContext(GerberContext): self._render_circle(circle, color)
def _render_test_record(self, primitive, color):
- self.ctx.select_font_face('monospace', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
- self.ctx.set_font_size(200)
- self._render_circle(Circle(primitive.position, 0.01), color)
+ position = tuple(map(add, primitive.position, self.origin_in_inch))
+ self.ctx.select_font_face('monospace', cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
+ self.ctx.set_font_size(13)
+ self._render_circle(Circle(position, 0.015), color)
self.ctx.set_source_rgb(*color)
self.ctx.set_operator(cairo.OPERATOR_OVER if primitive.level_polarity == "dark" else cairo.OPERATOR_CLEAR)
- self.ctx.move_to(*[self.scale[0] * (coord + 0.01) for coord in primitive.position])
+ self.ctx.move_to(*[self.scale[0] * (coord + 0.015) for coord in position])
self.ctx.scale(1, -1)
self.ctx.show_text(primitive.net_name)
self.ctx.scale(1, -1)
@@ -196,14 +227,15 @@ class GerberCairoContext(GerberContext): def _render_mask(self):
self.ctx.set_operator(cairo.OPERATOR_OVER)
ptn = cairo.SurfacePattern(self.mask)
- ptn.set_matrix(cairo.Matrix(xx=1.0, yy=-1.0, x0=-self.origin_in_pixels[0], y0=self.size_in_pixels[1] + self.origin_in_pixels[1]))
+ ptn.set_matrix(cairo.Matrix(xx=1.0, yy=-1.0, x0=-self.origin_in_pixels[0],
+ y0=self.size_in_pixels[1] + self.origin_in_pixels[1]))
self.ctx.set_source(ptn)
self.ctx.paint()
- def _paint_background(self):
- if not self.bg:
+ def _paint_background(self, force=False):
+ if (not self.bg) or force:
self.bg = True
- self.ctx.set_source_rgba(*self.background_color)
+ self.ctx.set_source_rgba(*self.background_color, alpha=1.0)
self.ctx.paint()
def dump(self, filename):
|