summaryrefslogtreecommitdiff
path: root/gerber/render
diff options
context:
space:
mode:
authorHamilton Kibbe <ham@hamiltonkib.be>2014-10-13 12:38:57 -0400
committerHamilton Kibbe <ham@hamiltonkib.be>2014-10-13 12:38:57 -0400
commit6adcdbae5fc06959203761616e778ba4594475cc (patch)
tree1b878d7fe63645899a0d631849596b70bd002b07 /gerber/render
parent152fca07685d6f96f5e5bad723f1f62de99d8b7d (diff)
parent8c5c7ec8bbc8a074884ef04b566f9c0ecd6e78bb (diff)
downloadgerbonara-6adcdbae5fc06959203761616e778ba4594475cc.tar.gz
gerbonara-6adcdbae5fc06959203761616e778ba4594475cc.tar.bz2
gerbonara-6adcdbae5fc06959203761616e778ba4594475cc.zip
Merge branch 'master' of https://github.com/hamiltonkibbe/gerber-tools
Diffstat (limited to 'gerber/render')
-rw-r--r--gerber/render/render.py42
-rw-r--r--gerber/render/svgwrite_backend.py29
2 files changed, 60 insertions, 11 deletions
diff --git a/gerber/render/render.py b/gerber/render/render.py
index e76aed1..f7e4485 100644
--- a/gerber/render/render.py
+++ b/gerber/render/render.py
@@ -83,6 +83,9 @@ class GerberContext(object):
background_color : tuple (<float>, <float>, <float>)
Color of the background. Used when exposing areas in 'clear' level
polarity mode. Format is the same as for `color`.
+
+ alpha : float
+ Rendering opacity. Between 0.0 (transparent) and 1.0 (opaque.)
"""
def __init__(self):
self.settings = {}
@@ -96,11 +99,12 @@ class GerberContext(object):
self.level_polarity = 'dark'
self.region_mode = 'off'
self.quadrant_mode = 'multi-quadrant'
-
+ self.step_and_repeat = (1, 1, 0, 0)
self.color = (0.7215, 0.451, 0.200)
self.drill_color = (0.25, 0.25, 0.25)
self.background_color = (0.0, 0.0, 0.0)
-
+ self.alpha = 1.0
+
def set_format(self, settings):
""" Set source file format.
@@ -260,6 +264,19 @@ class GerberContext(object):
"""
self.background_color = color
+ def set_alpha(self, alpha):
+ """ Set layer rendering opacity
+
+ .. note::
+ Not all backends/rendering devices support this parameter.
+
+ Parameters
+ ----------
+ alpha : float
+ Rendering opacity. must be between 0.0 (transparent) and 1.0 (opaque)
+ """
+ self.alpha = alpha
+
def resolve(self, x, y):
""" Resolve missing x or y coordinates in a coordinate command.
@@ -415,6 +432,12 @@ class GerberContext(object):
"""
pass
+ def region_contour(self, x, y):
+ pass
+
+ def fill_region(self):
+ pass
+
def evaluate(self, stmt):
""" Evaluate Gerber statement and update image accordingly.
@@ -450,7 +473,7 @@ class GerberContext(object):
def _evaluate_mode(self, stmt):
if stmt.type == 'RegionMode':
if self.region_mode == 'on' and stmt.mode == 'off':
- self._fill_region()
+ self.fill_region()
self.region_mode = stmt.mode
elif stmt.type == 'QuadrantMode':
self.quadrant_mode = stmt.mode
@@ -460,11 +483,11 @@ class GerberContext(object):
self.set_coord_format(stmt.zero_suppression, stmt.format,
stmt.notation)
self.set_coord_notation(stmt.notation)
- elif stmt.param == "MO:":
+ elif stmt.param == "MO":
self.set_coord_unit(stmt.mode)
- elif stmt.param == "IP:":
+ elif stmt.param == "IP":
self.set_image_polarity(stmt.ip)
- elif stmt.param == "LP:":
+ elif stmt.param == "LP":
self.set_level_polarity(stmt.lp)
elif stmt.param == "AD":
self.define_aperture(stmt.d, stmt.shape, stmt.modifiers)
@@ -477,7 +500,10 @@ class GerberContext(object):
self.direction = ('clockwise' if stmt.function in ('G02', 'G2')
else 'counterclockwise')
if stmt.op == "D01":
- self.stroke(stmt.x, stmt.y, stmt.i, stmt.j)
+ if self.region_mode == 'on':
+ self.region_contour(stmt.x, stmt.y)
+ else:
+ self.stroke(stmt.x, stmt.y, stmt.i, stmt.j)
elif stmt.op == "D02":
self.move(stmt.x, stmt.y)
elif stmt.op == "D03":
@@ -486,5 +512,3 @@ class GerberContext(object):
def _evaluate_aperture(self, stmt):
self.set_aperture(stmt.d)
- def _fill_region(self):
- pass
diff --git a/gerber/render/svgwrite_backend.py b/gerber/render/svgwrite_backend.py
index 7570c84..78961da 100644
--- a/gerber/render/svgwrite_backend.py
+++ b/gerber/render/svgwrite_backend.py
@@ -117,17 +117,25 @@ class GerberSvgContext(GerberContext):
self.apertures = {}
self.dwg = svgwrite.Drawing()
+ self.dwg.transform = 'scale 1 -1'
self.background = False
+ self.region_path = None
def set_bounds(self, bounds):
xbounds, ybounds = bounds
size = (SCALE * (xbounds[1] - xbounds[0]), SCALE * (ybounds[1] - ybounds[0]))
if not self.background:
+ self.dwg = svgwrite.Drawing(viewBox='%f, %f, %f, %f' % (SCALE*xbounds[0], -SCALE*ybounds[1],size[0], size[1]))
self.dwg.add(self.dwg.rect(insert=(SCALE * xbounds[0],
-SCALE * ybounds[1]),
- size=size, fill="black"))
+ size=size, fill=convert_color(self.background_color)))
self.background = True
+ def set_alpha(self, alpha):
+ super(GerberSvgContext, self).set_alpha(alpha)
+ import warnings
+ warnings.warn('SVG output does not support transparency')
+
def define_aperture(self, d, shape, modifiers):
aperture = None
if shape == 'C':
@@ -173,7 +181,8 @@ class GerberSvgContext(GerberContext):
ap = self.apertures.get(self.aperture, None)
if ap is None:
return
- color = (convert_color(self.color) if self.level_polarity == 'dark'
+
+ color = (convert_color(self.color) if self.level_polarity == 'dark'
else convert_color(self.background_color))
for shape in ap.flash(self, x, y, color):
self.dwg.add(shape)
@@ -185,5 +194,21 @@ class GerberSvgContext(GerberContext):
fill=convert_color(self.drill_color))
self.dwg.add(hit)
+ def region_contour(self, x, y):
+ super(GerberSvgContext, self).region_contour(x, y)
+ x, y = self.resolve(x, y)
+ color = (convert_color(self.color) if self.level_polarity == 'dark'
+ else convert_color(self.background_color))
+ if self.region_path is None:
+ self.region_path = self.dwg.path(d = 'M %f, %f' %
+ (self.x*SCALE, -self.y*SCALE),
+ fill = color, stroke = 'none')
+ self.region_path.push('L %f, %f' % (x*SCALE, -y*SCALE))
+ self.move(x, y, resolve=False)
+
+ def fill_region(self):
+ self.dwg.add(self.region_path)
+ self.region_path = None
+
def dump(self, filename):
self.dwg.saveas(filename)