From 670d3fbbd7ebfb69bd223ac30b73ec47b195b380 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Tue, 3 Mar 2015 03:41:55 -0300 Subject: Add aperture macro parsing and evaluation. Aperture macros can get complex with arithmetical operations, variables and variables substitution. Current pcb-tools code just read each macro block as an independent unit, this cannot deal with variables that get changed after used. This patch splits the task in two: first we parse all macro content and creates a bytecode representation of all operations. This bytecode representation will be executed when an AD command is issues passing the required parameters. Parsing is heavily based on gerbv using a Shunting Yard approach to math parsing. Integration with rs274x.py code is not finished as I need to figure out how to integrate the final macro primitives with the graphical primitives already in use. --- gerber/rs274x.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'gerber/rs274x.py') diff --git a/gerber/rs274x.py b/gerber/rs274x.py index c5c89fb..69485a8 100644 --- a/gerber/rs274x.py +++ b/gerber/rs274x.py @@ -189,6 +189,7 @@ class GerberParser(object): self.statements = [] self.primitives = [] self.apertures = {} + self.macros = {} self.current_region = None self.x = 0 self.y = 0 @@ -392,6 +393,12 @@ class GerberParser(object): width = modifiers[0][0] height = modifiers[0][1] aperture = Obround(position=None, width=width, height=height) + elif shape == 'P': + # FIXME: not supported yet? + pass + else: + aperture = self.macros[shape].evaluate(modifiers) + self.apertures[d] = aperture def _evaluate_mode(self, stmt): @@ -414,6 +421,8 @@ class GerberParser(object): self.image_polarity = stmt.ip elif stmt.param == "LP": self.level_polarity = stmt.lp + elif stmt.param == "AM": + self.macros[stmt.name] = stmt elif stmt.param == "AD": self._define_aperture(stmt.d, stmt.shape, stmt.modifiers) @@ -449,9 +458,14 @@ class GerberParser(object): primitive = copy.deepcopy(self.apertures[self.aperture]) # XXX: temporary fix because there are no primitives for Macros and Polygon if primitive is not None: - primitive.position = (x, y) - primitive.level_polarity = self.level_polarity - self.primitives.append(primitive) + # XXX: just to make it easy to spot + if isinstance(primitive, type([])): + print primitive[0].to_gerber() + else: + primitive.position = (x, y) + primitive.level_polarity = self.level_polarity + self.primitives.append(primitive) + self.x, self.y = x, y def _evaluate_aperture(self, stmt): -- cgit From a13b981c1c2ea9ede39e9821d9ba818566f044de Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Thu, 5 Mar 2015 14:43:30 -0300 Subject: Fix tests for macros with no variables. All AM*Primitive classes now handles float for all but the code modifiers. This simplifies the reading/parsing. --- gerber/rs274x.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gerber/rs274x.py') diff --git a/gerber/rs274x.py b/gerber/rs274x.py index 69485a8..d2844c9 100644 --- a/gerber/rs274x.py +++ b/gerber/rs274x.py @@ -397,7 +397,7 @@ class GerberParser(object): # FIXME: not supported yet? pass else: - aperture = self.macros[shape].evaluate(modifiers) + aperture = self.macros[shape].build(modifiers) self.apertures[d] = aperture -- cgit From adc1ff6d72ac1201517fffcd8e377768be022e91 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Silva Date: Thu, 5 Mar 2015 14:58:36 -0300 Subject: Fix for py3 --- gerber/rs274x.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gerber/rs274x.py') diff --git a/gerber/rs274x.py b/gerber/rs274x.py index d2844c9..a3a27e9 100644 --- a/gerber/rs274x.py +++ b/gerber/rs274x.py @@ -460,7 +460,7 @@ class GerberParser(object): if primitive is not None: # XXX: just to make it easy to spot if isinstance(primitive, type([])): - print primitive[0].to_gerber() + print(primitive[0].to_gerber()) else: primitive.position = (x, y) primitive.level_polarity = self.level_polarity -- cgit