From 41f9475b132001d52064392057e376c6423c33dc Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Mon, 9 Feb 2015 17:39:24 -0500 Subject: Tests and bugfixes --- gerber/am_statements.py | 6 ++ gerber/gerber_statements.py | 47 ++++++--- gerber/tests/resources/top_copper.GTL | 2 +- gerber/tests/test_gerber_statements.py | 177 ++++++++++++++++++++++++++++++--- 4 files changed, 202 insertions(+), 30 deletions(-) diff --git a/gerber/am_statements.py b/gerber/am_statements.py index 0e27623..dc97dfa 100644 --- a/gerber/am_statements.py +++ b/gerber/am_statements.py @@ -820,5 +820,11 @@ class AMUnsupportPrimitive(AMPrimitive): def __init__(self, primitive): self.primitive = primitive + def to_inch(self): + pass + + def to_metric(self): + pass + def to_gerber(self, settings=None): return self.primitive diff --git a/gerber/gerber_statements.py b/gerber/gerber_statements.py index 48d5d93..1401345 100644 --- a/gerber/gerber_statements.py +++ b/gerber/gerber_statements.py @@ -262,19 +262,19 @@ class ADParamStmt(ParamStmt): self.d = d self.shape = shape if modifiers is not None: - self.modifiers = [[float(x) for x in m.split("X")] for m in modifiers.split(",") if len(m)] + self.modifiers = [tuple([float(x) for x in m.split("X")]) for m in modifiers.split(",") if len(m)] else: self.modifiers = [] def to_inch(self): - self.modifiers = [[x / 25.4 for x in modifier] for modifier in self.modifiers] + self.modifiers = [tuple([x / 25.4 for x in modifier]) for modifier in self.modifiers] def to_metric(self): - self.modifiers = [[x * 25.4 for x in modifier] for modifier in self.modifiers] + self.modifiers = [tuple([x * 25.4 for x in modifier]) for modifier in self.modifiers] def to_gerber(self, settings=None): if len(self.modifiers): - return '%ADD{0}{1},{2}*%'.format(self.d, self.shape, ','.join(['X'.join(["%.4f" % x for x in modifier]) for modifier in self.modifiers])) + return '%ADD{0}{1},{2}*%'.format(self.d, self.shape, ','.join(['X'.join(["%.4g" % x for x in modifier]) for modifier in self.modifiers])) else: return '%ADD{0}{1}*%'.format(self.d, self.shape) @@ -326,16 +326,30 @@ class AMParamStmt(ParamStmt): def _parsePrimitives(self, macro): primitives = [] - for primitive in macro.split('*'): + for primitive in macro.strip('%\n').split('*'): # Couldn't find anything explicit about leading whitespace in the spec... - primitive = primitive.lstrip() - if primitive[0] == '0': - primitives.append(AMCommentPrimitive.from_gerber(primitive)) - if primitive[0] == '4': - primitives.append(AMOutlinePrimitive.from_gerber(primitive)) - else: - primitives.append(AMUnsupportPrimitive.from_gerber(primitive)) - + primitive = primitive.strip(' *%\n') + if len(primitive): + if primitive[0] == '0': + primitives.append(AMCommentPrimitive.from_gerber(primitive)) + elif primitive[0] == '1': + primitives.append(AMCirclePrimitive.from_gerber(primitive)) + elif primitive[0:2] in ('2,', '20'): + primitives.append(AMVectorLinePrimitive.from_gerber(primitive)) + elif primitive[0:2] == '21': + primitives.append(AMCenterLinePrimitive.from_gerber(primitive)) + elif primitive[0:2] == '22': + primitives.append(AMLowerLeftLinePrimitive.from_gerber(primitive)) + elif primitive[0] == '4': + primitives.append(AMOutlinePrimitive.from_gerber(primitive)) + elif primitive[0] == '5': + primitives.append(AMPolygonPrimitive.from_gerber(primitive)) + elif primitive[0] =='6': + primitives.append(AMMoirePrimitive.from_gerber(primitive)) + elif primitive[0] == '7': + primitives.append(AMThermalPrimitive.from_gerber(primitive)) + else: + primitives.append(AMUnsupportPrimitive.from_gerber(primitive)) return primitives def to_inch(self): @@ -465,7 +479,8 @@ class IRParamStmt(ParamStmt): """ @classmethod def from_dict(cls, stmt_dict): - return cls(**stmt_dict) + angle = int(stmt_dict['angle']) + return cls(stmt_dict['param'], angle) def __init__(self, param, angle): """ Initialize IRParamStmt class @@ -639,9 +654,9 @@ class SFParamStmt(ParamStmt): def __str__(self): scale_factor = '' if self.a is not None: - scale_factor += ('X: %f' % self.a) + scale_factor += ('X: %g' % self.a) if self.b is not None: - scale_factor += ('Y: %f' % self.b) + scale_factor += ('Y: %g' % self.b) return ('' % scale_factor) diff --git a/gerber/tests/resources/top_copper.GTL b/gerber/tests/resources/top_copper.GTL index 6d382c0..b49f7e7 100644 --- a/gerber/tests/resources/top_copper.GTL +++ b/gerber/tests/resources/top_copper.GTL @@ -6,7 +6,7 @@ G75* %LPD*% G04This is a comment,:* %AMOC8* -5,1,8,0,0,1.08239X$1,22.5* +5,1,8,0,0,1.08239,22.5* % %ADD10C,0.0000*% %ADD11R,0.0260X0.0800*% diff --git a/gerber/tests/test_gerber_statements.py b/gerber/tests/test_gerber_statements.py index e797d5a..0875b57 100644 --- a/gerber/tests/test_gerber_statements.py +++ b/gerber/tests/test_gerber_statements.py @@ -159,6 +159,31 @@ def test_IPParamStmt_dump(): ip = IPParamStmt.from_dict(stmt) assert_equal(ip.to_gerber(), '%IPNEG*%') +def test_IPParamStmt_string(): + stmt = {'param': 'IP', 'ip': 'POS'} + ip = IPParamStmt.from_dict(stmt) + assert_equal(str(ip), '') + + stmt = {'param': 'IP', 'ip': 'NEG'} + ip = IPParamStmt.from_dict(stmt) + assert_equal(str(ip), '') + +def test_IRParamStmt_factory(): + stmt = {'param': 'IR', 'angle': '45'} + ir = IRParamStmt.from_dict(stmt) + assert_equal(ir.param, 'IR') + assert_equal(ir.angle, 45) + +def test_IRParamStmt_dump(): + stmt = {'param': 'IR', 'angle': '45'} + ir = IRParamStmt.from_dict(stmt) + assert_equal(ir.to_gerber(), '%IR45*%') + +def test_IRParamStmt_string(): + stmt = {'param': 'IR', 'angle': '45'} + ir = IRParamStmt.from_dict(stmt) + assert_equal(str(ir), '') + def test_OFParamStmt_factory(): """ Test OFParamStmt factory @@ -195,6 +220,24 @@ def test_OFParamStmt_string(): of = OFParamStmt.from_dict(stmt) assert_equal(str(of), '') +def test_SFParamStmt_factory(): + stmt = {'param': 'SF', 'a': '1.4', 'b': '0.9'} + sf = SFParamStmt.from_dict(stmt) + assert_equal(sf.param, 'SF') + assert_equal(sf.a, 1.4) + assert_equal(sf.b, 0.9) + +def test_SFParamStmt_dump(): + stmt = {'param': 'SF', 'a': '1.4', 'b': '0.9'} + sf = SFParamStmt.from_dict(stmt) + assert_equal(sf.to_gerber(), '%SFA1.4B0.9*%') + +def test_SFParamStmt_string(): + stmt = {'param': 'SF', 'a': '1.4', 'b': '0.9'} + sf = SFParamStmt.from_dict(stmt) + assert_equal(str(sf), '') + + def test_LPParamStmt_factory(): """ Test LPParamStmt factory """ @@ -231,6 +274,75 @@ def test_LPParamStmt_string(): assert_equal(str(lp), '') +def test_AMParamStmt_factory(): + name = 'DONUTVAR' + macro = ( +'''0 Test Macro. * +1,1,1.5,0,0* +20,1,0.9,0,0.45,12,0.45,0* +21,1,6.8,1.2,3.4,0.6,0* +22,1,6.8,1.2,0,0,0* +4,1,4,0.1,0.1,0.5,0.1,0.5,0.5,0.1,0.5,0.1,0.1,0* +5,1,8,0,0,8,0* +6,0,0,5,0.5,0.5,2,0.1,6,0* +7,0,0,7,6,0.2,0* +8,THIS IS AN UNSUPPORTED PRIMITIVE* +''') + s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + assert_equal(len(s.primitives), 10) + assert_true(isinstance(s.primitives[0], AMCommentPrimitive)) + assert_true(isinstance(s.primitives[1], AMCirclePrimitive)) + assert_true(isinstance(s.primitives[2], AMVectorLinePrimitive)) + assert_true(isinstance(s.primitives[3], AMCenterLinePrimitive)) + assert_true(isinstance(s.primitives[4], AMLowerLeftLinePrimitive)) + assert_true(isinstance(s.primitives[5], AMOutlinePrimitive)) + assert_true(isinstance(s.primitives[6], AMPolygonPrimitive)) + assert_true(isinstance(s.primitives[7], AMMoirePrimitive)) + assert_true(isinstance(s.primitives[8], AMThermalPrimitive)) + assert_true(isinstance(s.primitives[9], AMUnsupportPrimitive)) + +def testAMParamStmt_conversion(): + name = 'POLYGON' + macro = '5,1,8,25.4,25.4,25.4,0*%' + s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.to_inch() + assert_equal(s.primitives[0].position, (1., 1.)) + assert_equal(s.primitives[0].diameter, 1.) + + macro = '5,1,8,1,1,1,0*%' + s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.to_metric() + assert_equal(s.primitives[0].position, (25.4, 25.4)) + assert_equal(s.primitives[0].diameter, 25.4) + +def test_AMParamStmt_dump(): + name = 'POLYGON' + macro = '5,1,8,25.4,25.4,25.4,0*%' + s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + assert_equal(s.to_gerber(), '%AMPOLYGON*5,1,8,25.4,25.4,25.4,0.0*%') + +def test_AMParamStmt_string(): + name = 'POLYGON' + macro = '5,1,8,25.4,25.4,25.4,0*%' + s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + assert_equal(str(s), '') + +def test_ASParamStmt_factory(): + stmt = {'param': 'AS', 'mode': 'AXBY'} + s = ASParamStmt.from_dict(stmt) + assert_equal(s.param, 'AS') + assert_equal(s.mode, 'AXBY') + +def test_ASParamStmt_dump(): + stmt = {'param': 'AS', 'mode': 'AXBY'} + s = ASParamStmt.from_dict(stmt) + assert_equal(s.to_gerber(), '%ASAXBY*%') + +def test_ASParamStmt_string(): + stmt = {'param': 'AS', 'mode': 'AXBY'} + s = ASParamStmt.from_dict(stmt) + assert_equal(str(s), '') + def test_INParamStmt_factory(): """ Test INParamStmt factory """ @@ -238,7 +350,6 @@ def test_INParamStmt_factory(): inp = INParamStmt.from_dict(stmt) assert_equal(inp.name, 'test') - def test_INParamStmt_dump(): """ Test INParamStmt to_gerber() """ @@ -246,6 +357,10 @@ def test_INParamStmt_dump(): inp = INParamStmt.from_dict(stmt) assert_equal(inp.to_gerber(), '%INtest*%') +def test_INParamStmt_string(): + stmt = {'param': 'IN', 'name': 'test'} + inp = INParamStmt.from_dict(stmt) + assert_equal(str(inp), '') def test_LNParamStmt_factory(): """ Test LNParamStmt factory @@ -254,7 +369,6 @@ def test_LNParamStmt_factory(): lnp = LNParamStmt.from_dict(stmt) assert_equal(lnp.name, 'test') - def test_LNParamStmt_dump(): """ Test LNParamStmt to_gerber() """ @@ -262,6 +376,10 @@ def test_LNParamStmt_dump(): lnp = LNParamStmt.from_dict(stmt) assert_equal(lnp.to_gerber(), '%LNtest*%') +def test_LNParamStmt_string(): + stmt = {'param': 'LN', 'name': 'test'} + lnp = LNParamStmt.from_dict(stmt) + assert_equal(str(lnp), '') def test_comment_stmt(): """ Test comment statement @@ -270,28 +388,24 @@ def test_comment_stmt(): assert_equal(stmt.type, 'COMMENT') assert_equal(stmt.comment, 'A comment') - def test_comment_stmt_dump(): """ Test CommentStmt to_gerber() """ stmt = CommentStmt('A comment') assert_equal(stmt.to_gerber(), 'G04A comment*') - def test_eofstmt(): """ Test EofStmt """ stmt = EofStmt() assert_equal(stmt.type, 'EOF') - def test_eofstmt_dump(): """ Test EofStmt to_gerber() """ stmt = EofStmt() assert_equal(stmt.to_gerber(), 'M02*') - def test_quadmodestmt_factory(): """ Test QuadrantModeStmt.from_gerber() """ @@ -390,12 +504,50 @@ def test_ADParamStmt_factory(): assert_equal(ad.d, 1) assert_equal(ad.shape, 'R') +def test_ADParamStmt_conversion(): + stmt = {'param': 'AD', 'd': 0, 'shape': 'C', 'modifiers': '25.4X25.4,25.4X25.4'} + ad = ADParamStmt.from_dict(stmt) + ad.to_inch() + assert_equal(ad.modifiers[0], (1., 1.)) + assert_equal(ad.modifiers[1], (1., 1.)) + + stmt = {'param': 'AD', 'd': 0, 'shape': 'C', 'modifiers': '1X1,1X1'} + ad = ADParamStmt.from_dict(stmt) + ad.to_metric() + assert_equal(ad.modifiers[0], (25.4, 25.4)) + assert_equal(ad.modifiers[1], (25.4, 25.4)) + +def test_ADParamStmt_dump(): + stmt = {'param': 'AD', 'd': 0, 'shape': 'C'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(ad.to_gerber(), '%ADD0C*%') + stmt = {'param': 'AD', 'd': 0, 'shape': 'C', 'modifiers': '1X1,1X1'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(ad.to_gerber(), '%ADD0C,1X1,1X1*%') + +def test_ADPamramStmt_string(): + stmt = {'param': 'AD', 'd': 0, 'shape': 'C'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(str(ad), '') + + stmt = {'param': 'AD', 'd': 0, 'shape': 'R'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(str(ad), '') + + stmt = {'param': 'AD', 'd': 0, 'shape': 'O'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(str(ad), '') + + stmt = {'param': 'AD', 'd': 0, 'shape': 'test'} + ad = ADParamStmt.from_dict(stmt) + assert_equal(str(ad), '') + def test_MIParamStmt_factory(): stmt = {'param': 'MI', 'a': 1, 'b': 1} mi = MIParamStmt.from_dict(stmt) assert_equal(mi.a, 1) assert_equal(mi.b, 1) - + def test_MIParamStmt_dump(): stmt = {'param': 'MI', 'a': 1, 'b': 1} mi = MIParamStmt.from_dict(stmt) @@ -406,12 +558,12 @@ def test_MIParamStmt_dump(): stmt = {'param': 'MI', 'b': 1} mi = MIParamStmt.from_dict(stmt) assert_equal(mi.to_gerber(), '%MIA0B1*%') - + def test_MIParamStmt_string(): stmt = {'param': 'MI', 'a': 1, 'b': 1} mi = MIParamStmt.from_dict(stmt) assert_equal(str(mi), '') - + stmt = {'param': 'MI', 'b': 1} mi = MIParamStmt.from_dict(stmt) assert_equal(str(mi), '') @@ -430,7 +582,6 @@ def test_coordstmt_ctor(): assert_equal(cs.i, 0.2) assert_equal(cs.j, 0.3) assert_equal(cs.op, 'D01') - - - - \ No newline at end of file + + + -- cgit