From 84bfd34e918251ff82f4b3818bc6268feab72efe Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Thu, 9 Oct 2014 09:51:29 -0400 Subject: Add mode statement parsing --- gerber/gerber.py | 24 ++++++++++++++++++------ gerber/gerber_statements.py | 6 +++--- gerber/render/render.py | 14 ++++++++++++-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/gerber/gerber.py b/gerber/gerber.py index 07ecd78..c59d871 100644 --- a/gerber/gerber.py +++ b/gerber/gerber.py @@ -164,6 +164,9 @@ class GerberParser(object): EOF_STMT = re.compile(r"(?PM02)\*") + REGION_MODE_STMT = re.compile(r'(?PG3[67])\*') + QUAD_MODE_STMT = re.compile(r'(?PG7[45])\*') + def __init__(self): self.settings = FileSettings() self.statements = [] @@ -206,12 +209,21 @@ class GerberParser(object): while did_something and len(line) > 0: did_something = False - # region mode - #if 'G36' in line or 'G37' in line: - # yield RegionModeStmt.from_gerber(line) - # did_something = True - # line = '' - # continue + # Region Mode + (mode, r) = self._match_one(self.REGION_MODE_STMT, line) + if mode: + yield RegionModeStmt.from_gerber(line) + line = r + did_something = True + continue + + # Quadrant Mode + (mode, r) = self._match_one(self.QUAD_MODE_STMT, line) + if mode: + yield QuadrantModeStmt.from_gerber(line) + line = r + did_something = True + continue # coord (coord, r) = self._match_one(self.COORD_STMT, line) diff --git a/gerber/gerber_statements.py b/gerber/gerber_statements.py index 90952b2..76a6f0c 100644 --- a/gerber/gerber_statements.py +++ b/gerber/gerber_statements.py @@ -600,7 +600,7 @@ class QuadrantModeStmt(Statement): else cls('multi-quadrant')) def __init__(self, mode): - super(QuadrantModeStmt, self).__init__('Quadrant Mode') + super(QuadrantModeStmt, self).__init__('QuadrantMode') mode = mode.lower() if mode not in ['single-quadrant', 'multi-quadrant']: raise ValueError('Quadrant mode must be "single-quadrant" \ @@ -611,7 +611,7 @@ class QuadrantModeStmt(Statement): return 'G74*' if self.mode == 'single-quadrant' else 'G75*' class RegionModeStmt(Statement): - + @classmethod def from_gerber(cls, line): line = line.strip() @@ -620,7 +620,7 @@ class RegionModeStmt(Statement): return (cls('on') if line[:3] == 'G36' else cls('off')) def __init__(self, mode): - super(RegionModeStmt, self).__init__('Region Mode') + super(RegionModeStmt, self).__init__('RegionMode') mode = mode.lower() if mode not in ['on', 'off']: raise ValueError('Valid modes are "on" or "off"') diff --git a/gerber/render/render.py b/gerber/render/render.py index e7ec6ee..e91c71e 100644 --- a/gerber/render/render.py +++ b/gerber/render/render.py @@ -16,8 +16,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ..gerber_statements import ( - CommentStmt, UnknownStmt, EofStmt, ParamStmt, CoordStmt, ApertureStmt +from ..gerber_statements import (CommentStmt, UnknownStmt, EofStmt, ParamStmt, + CoordStmt, ApertureStmt, RegionModeStmt, + QuadrantModeStmt, ) @@ -111,9 +112,18 @@ class GerberContext(object): elif isinstance(stmt, ApertureStmt): self._evaluate_aperture(stmt) + elif isinstance(stmt, (RegionModeStmt, QuadrantModeStmt)): + self._evaluate_mode(stmt) + else: raise Exception("Invalid statement to evaluate") + def _evaluate_mode(self, stmt): + if stmt.type == 'RegionMode': + self.region_mode = stmt.mode + elif stmt.type == 'QuadrantMode': + self.quadrant_mode = stmt.mode + def _evaluate_param(self, stmt): if stmt.param == "FS": self.set_coord_format(stmt.zero_suppression, stmt.format, -- cgit