diff options
-rw-r--r-- | gerber/excellon_statements.py | 73 | ||||
-rw-r--r-- | gerber/tests/test_excellon_statements.py | 23 | ||||
-rw-r--r-- | gerber/tests/test_gerber_statements.py | 3 |
3 files changed, 73 insertions, 26 deletions
diff --git a/gerber/excellon_statements.py b/gerber/excellon_statements.py index caf1626..dbd807a 100644 --- a/gerber/excellon_statements.py +++ b/gerber/excellon_statements.py @@ -102,26 +102,42 @@ class ExcellonTool(ExcellonStatement): commands = re.split('([BCFHSTZ])', line)[1:] commands = [(command, value) for command, value in pairwise(commands)] args = {} - format = settings['format'] + nformat = 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) + args['retract_rate'] = parse_gerber_value(val, nformat, zero_suppression) elif cmd == 'C': - args['diameter'] = parse_gerber_value(val, format, zero_suppression) + args['diameter'] = parse_gerber_value(val, nformat, zero_suppression) elif cmd == 'F': - args['feed_rate'] = parse_gerber_value(val, format, zero_suppression) + args['feed_rate'] = parse_gerber_value(val, nformat, zero_suppression) elif cmd == 'H': - args['max_hit_count'] = parse_gerber_value(val, format, zero_suppression) + args['max_hit_count'] = parse_gerber_value(val, nformat, zero_suppression) elif cmd == 'S': - args['rpm'] = 1000 * parse_gerber_value(val, format, zero_suppression) + args['rpm'] = 1000 * parse_gerber_value(val, nformat, zero_suppression) elif cmd == 'T': args['number'] = int(val) elif cmd == 'Z': - args['depth_offset'] = parse_gerber_value(val, format, zero_suppression) + args['depth_offset'] = parse_gerber_value(val, nformat, zero_suppression) return cls(settings, **args) + @classmethod def from_dict(cls, settings, tool_dict): + """ Create an ExcellonTool from a dict. + + Parameters + ---------- + settings : FileSettings (dict-like) + Excellon File-wide settings + + tool_dict : dict + Excellon tool parameters as a dict + + Returns + ------- + tool : ExcellonTool + An ExcellonTool initialized with the parameters in tool_dict. + """ return cls(settings, tool_dict) def __init__(self, settings, **kwargs): @@ -168,6 +184,18 @@ class ToolSelectionStmt(ExcellonStatement): @classmethod def from_excellon(cls, line): + """ Create a ToolSelectionStmt from an excellon file line. + + Parameters + ---------- + line : string + Line from an Excellon file + + Returns + ------- + tool_statement : ToolSelectionStmt + ToolSelectionStmt representation of `line.` + """ line = line.strip()[1:] compensation_index = None tool = int(line[:2]) @@ -177,7 +205,8 @@ class ToolSelectionStmt(ExcellonStatement): def __init__(self, tool, compensation_index=None): tool = int(tool) - compensation_index = int(compensation_index) if compensation_index else None + compensation_index = (int(compensation_index) if compensation_index + is not None else None) self.tool = tool self.compensation_index = compensation_index @@ -191,16 +220,16 @@ class ToolSelectionStmt(ExcellonStatement): class CoordinateStmt(ExcellonStatement): @classmethod - def from_excellon(cls, line, format=(2, 5), zero_suppression='trailing'): + def from_excellon(cls, line, nformat=(2, 5), zero_suppression='trailing'): x = None y = None if line[0] == 'X': splitline = line.strip('X').split('Y') - x = parse_gerber_value(splitline[0].strip(), format, zero_suppression) + x = parse_gerber_value(splitline[0].strip(), nformat, zero_suppression) if len(splitline) == 2: - y = parse_gerber_value(splitline[1].strip(), format, zero_suppression) + y = parse_gerber_value(splitline[1].strip(), nformat, zero_suppression) else: - y = parse_gerber_value(line.strip(' Y'), format, zero_suppression) + y = parse_gerber_value(line.strip(' Y'), nformat, zero_suppression) return cls(x, y) def __init__(self, x=None, y=None): @@ -270,6 +299,7 @@ class EndOfProgramStmt(ExcellonStatement): stmt += 'Y%s' % write_gerber_value(self.y) return stmt + class UnitStmt(ExcellonStatement): @classmethod @@ -284,10 +314,11 @@ class UnitStmt(ExcellonStatement): def to_excellon(self): stmt = '%s,%s' % ('INCH' if self.units == 'inch' else 'METRIC', - 'LZ' if self.zero_suppression == 'trailing' + 'LZ' if self.zero_suppression == 'trailing' else 'TZ') return stmt + class IncrementalModeStmt(ExcellonStatement): @classmethod @@ -327,14 +358,14 @@ class FormatStmt(ExcellonStatement): fmt = int(line.split(',')[1]) return cls(fmt) - def __init__(self, format=1): - format = int(format) - if format not in [1, 2]: + def __init__(self, nformat=1): + nformat = int(nformat) + if nformat not in [1, 2]: raise ValueError('Valid formats are 1 or 2') - self.format = format + self.nformat = nformat def to_excellon(self): - return 'FMAT,%d' % self.format + return 'FMAT,%d' % self.n class LinkToolStmt(ExcellonStatement): @@ -379,9 +410,7 @@ class UnknownStmt(ExcellonStatement): def to_excellon(self): return self.stmt - - - + def pairwise(iterator): """ Iterate over list taking two elements at a time. @@ -389,4 +418,4 @@ def pairwise(iterator): """ itr = iter(iterator) while True: - yield tuple([itr.next() for i in range(2)])
\ No newline at end of file + yield tuple([itr.next() for i in range(2)]) diff --git a/gerber/tests/test_excellon_statements.py b/gerber/tests/test_excellon_statements.py index 3a10153..49207d3 100644 --- a/gerber/tests/test_excellon_statements.py +++ b/gerber/tests/test_excellon_statements.py @@ -20,7 +20,7 @@ def test_ExcellonTool_factory(): def test_ExcellonTool_dump(): - """ Test ExcellonTool to_gerber method + """ Test ExcellonTool to_excellon() """ exc_lines = ['T1F00S00C0.01200', 'T2F00S00C0.01500', 'T3F00S00C0.01968', 'T4F00S00C0.02800', 'T5F00S00C0.03300', 'T6F00S00C0.03800', @@ -30,4 +30,23 @@ def test_ExcellonTool_dump(): for line in exc_lines: tool = ExcellonTool.from_excellon(line, settings) assert_equal(tool.to_excellon(), line) -
\ No newline at end of file + + +def test_ToolSelectionStmt_factory(): + """ Test ToolSelectionStmt factory method + """ + stmt = ToolSelectionStmt.from_excellon('T01') + assert_equal(stmt.tool, 1) + assert_equal(stmt.compensation_index, None) + stmt = ToolSelectionStmt.from_excellon('T0223') + assert_equal(stmt.tool, 2) + assert_equal(stmt.compensation_index, 23) + + +def test_ToolSelectionStmt_dump(): + """ Test ToolSelectionStmt to_excellon() + """ + lines = ['T01', 'T0223', 'T10', 'T09', 'T0000'] + for line in lines: + stmt = ToolSelectionStmt.from_excellon(line) + assert_equal(stmt.to_excellon(), line) diff --git a/gerber/tests/test_gerber_statements.py b/gerber/tests/test_gerber_statements.py index 7495ba7..7c01130 100644 --- a/gerber/tests/test_gerber_statements.py +++ b/gerber/tests/test_gerber_statements.py @@ -90,7 +90,7 @@ def test_IPParamStmt_dump(): def test_OFParamStmt_factory(): """ Test OFParamStmt factory correctly handles parameters """ - stmt = {'param': 'OF', 'a': '0.1234567', 'b':'0.1234567'} + stmt = {'param': 'OF', 'a': '0.1234567', 'b': '0.1234567'} of = OFParamStmt.from_dict(stmt) assert_equal(of.a, 0.1234567) assert_equal(of.b, 0.1234567) @@ -102,4 +102,3 @@ def test_OFParamStmt_dump(): stmt = {'param': 'OF', 'a': '0.1234567', 'b': '0.1234567'} of = OFParamStmt.from_dict(stmt) assert_equal(of.to_gerber(), '%OFA0.123456B0.123456*%') - |