summaryrefslogtreecommitdiff
path: root/gerber/excellon.py
diff options
context:
space:
mode:
authorHamilton Kibbe <hamilton.kibbe@gmail.com>2014-10-06 08:33:53 -0400
committerHamilton Kibbe <hamilton.kibbe@gmail.com>2014-10-06 08:33:53 -0400
commit597427d785d6f44348fe15631f2c184504195fb0 (patch)
tree1ce13f04f3d2d23ff6059acc2bc464a16d46a9c6 /gerber/excellon.py
parent0b8e2e4b8b552e90d55eabe39aefba0b5b3daef5 (diff)
downloadgerbonara-597427d785d6f44348fe15631f2c184504195fb0.tar.gz
gerbonara-597427d785d6f44348fe15631f2c184504195fb0.tar.bz2
gerbonara-597427d785d6f44348fe15631f2c184504195fb0.zip
add excellon statements
Diffstat (limited to 'gerber/excellon.py')
-rwxr-xr-xgerber/excellon.py128
1 files changed, 20 insertions, 108 deletions
diff --git a/gerber/excellon.py b/gerber/excellon.py
index 7c7d0c6..6ae182b 100755
--- a/gerber/excellon.py
+++ b/gerber/excellon.py
@@ -22,6 +22,7 @@ Excellon module
This module provides Excellon file classes and parsing utilities
"""
import re
+from .excellon_statements import *
from .utils import parse_gerber_value
from .cnc import CncFile, FileSettings
@@ -74,108 +75,6 @@ class ExcellonFile(CncFile):
ctx.dump(filename)
-class ExcellonTool(object):
- """ Excellon Tool class
-
- Parameters
- ----------
- settings : FileSettings (dict-like)
- File-wide settings.
-
- kwargs : dict-like
- Tool settings from the excellon statement. Valid keys are:
- diameter : Tool diameter [expressed in file units]
- rpm : Tool RPM
- feed_rate : Z-axis tool feed rate
- retract_rate : Z-axis tool retraction rate
- max_hit_count : Number of hits allowed before a tool change
- depth_offset : Offset of tool depth from tip of tool.
-
- Attributes
- ----------
- number : integer
- Tool number from the excellon file
-
- diameter : float
- Tool diameter in file units
-
- rpm : float
- Tool RPM
-
- feed_rate : float
- Tool Z-axis feed rate.
-
- retract_rate : float
- Tool Z-axis retract rate
-
- depth_offset : float
- Offset of depth measurement from tip of tool
-
- max_hit_count : integer
- Maximum number of tool hits allowed before a tool change
-
- hit_count : integer
- Number of tool hits in excellon file.
- """
-
- @classmethod
- def from_line(cls, line, settings):
- """ Create a Tool from an excellon gile tool definition line.
-
- Parameters
- ----------
- line : string
- Tool definition line from an excellon file.
-
- settings : FileSettings (dict-like)
- Excellon file-wide settings
-
- Returns
- -------
- tool : Tool
- An ExcellonTool representing the tool defined in `line`
- """
- commands = re.split('([BCFHSTZ])', line)[1:]
- commands = [(command, value) for command, value in pairwise(commands)]
- args = {}
- format = settings['format']
- zero_suppression = settings['zero_suppression']
- for cmd, val in commands:
- if cmd == 'B':
- args['retract_rate'] = parse_gerber_value(val, format, zero_suppression)
- elif cmd == 'C':
- args['diameter'] = parse_gerber_value(val, format, zero_suppression)
- elif cmd == 'F':
- args['feed_rate'] = parse_gerber_value(val, format, zero_suppression)
- elif cmd == 'H':
- args['max_hit_count'] = parse_gerber_value(val, format, zero_suppression)
- elif cmd == 'S':
- args['rpm'] = 1000 * parse_gerber_value(val, format, zero_suppression)
- elif cmd == 'T':
- args['number'] = int(val)
- elif cmd == 'Z':
- args['depth_offset'] = parse_gerber_value(val, format, zero_suppression)
- return cls(settings, **args)
-
- def __init__(self, settings, **kwargs):
- self.number = kwargs.get('number')
- self.feed_rate = kwargs.get('feed_rate')
- self.retract_rate = kwargs.get('retract_rate')
- self.rpm = kwargs.get('rpm')
- self.diameter = kwargs.get('diameter')
- self.max_hit_count = kwargs.get('max_hit_count')
- self.depth_offset = kwargs.get('depth_offset')
- self.units = settings.get('units', 'inch')
- self.hit_count = 0
-
- def _hit(self):
- self.hit_count += 1
-
- def __repr__(self):
- unit = 'in.' if self.units == 'inch' else 'mm'
- return '<ExcellonTool %d: %0.3f%s dia.>' % (self.number, self.diameter, unit)
-
-
class ExcellonParser(object):
""" Excellon File Parser
"""
@@ -186,6 +85,7 @@ class ExcellonParser(object):
self.zero_suppression = 'trailing'
self.format = (2, 5)
self.state = 'INIT'
+ self.statements = []
self.tools = {}
self.hits = []
self.active_tool = None
@@ -206,15 +106,23 @@ class ExcellonParser(object):
def _parse(self, line):
if 'M48' in line:
+ self.statements.append(HeaderBeginStmt())
self.state = 'HEADER'
- if 'G00' in line:
- self.state = 'ROUT'
+ elif line[0] == '%':
+ self.statements.append(RewindStopStmt())
+ if self.state == 'HEADER':
+ self.state = 'DRILL'
- if 'G05' in line:
- self.state = 'DRILL'
+ elif 'M95' in line:
+ self.statements.append(HeaderEndStmt())
+ if self.state == 'HEADER':
+ self.state = 'DRILL'
+
+ elif 'G00' in line:
+ self.state = 'ROUT'
- elif line[0] == '%' and self.state == 'HEADER':
+ elif 'G05' in line:
self.state = 'DRILL'
if 'INCH' in line or line.strip() == 'M72':
@@ -279,11 +187,15 @@ class ExcellonParser(object):
notation=self.notation)
-
def pairwise(iterator):
+ """ Iterate over list taking two elements at a time.
+
+ e.g. [1, 2, 3, 4, 5, 6] ==> [(1, 2), (3, 4), (5, 6)]
+ """
itr = iter(iterator)
while True:
yield tuple([itr.next() for i in range(2)])
+
if __name__ == '__main__':
p = parser()