diff options
Diffstat (limited to 'gerber')
-rw-r--r-- | gerber/am_read.py | 17 | ||||
-rw-r--r-- | gerber/am_statements.py | 16 | ||||
-rw-r--r-- | gerber/gerber_statements.py | 27 | ||||
-rw-r--r-- | gerber/rs274x.py | 2 | ||||
-rw-r--r-- | gerber/tests/test_gerber_statements.py | 16 |
5 files changed, 45 insertions, 33 deletions
diff --git a/gerber/am_read.py b/gerber/am_read.py index d1201b2..ade4389 100644 --- a/gerber/am_read.py +++ b/gerber/am_read.py @@ -32,6 +32,7 @@ class Token: LEFT_PARENS = "(" RIGHT_PARENS = ")" EQUALS = "=" + EOF = "EOF" def token_to_opcode(token): @@ -71,7 +72,8 @@ class Scanner: def peek(self): if not self.eof(): return self.buff[self.n] - return "" + + return Token.EOF def ungetc(self): if self.n > 0: @@ -104,6 +106,11 @@ class Scanner: return s.strip() +def print_instructions(instructions): + for opcode, argument in instructions: + print "%s %s" % (OpCode.str(opcode), str(argument) if argument is not None else "") + + def read_macro(macro): instructions = [] @@ -185,9 +192,10 @@ def read_macro(macro): elif c == "0": if is_primitive and not found_primitive_code: instructions.append((OpCode.PUSH, scanner.readstr("*"))) + found_primitive_code = True else: # decimal or integer disambiguation - if scanner.peek() not in '.': + if scanner.peek() not in '.' or scanner.peek() == Token.EOF: instructions.append((OpCode.PUSH, 0)) elif c in "123456789.": @@ -207,7 +215,7 @@ def read_macro(macro): instructions.append((token_to_opcode(pop()), None)) # at end, we either have a primitive or a equation - if is_primitive: + if is_primitive and found_primitive_code: instructions.append((OpCode.PRIM, primitive_code)) if is_equation: @@ -221,8 +229,7 @@ if __name__ == '__main__': instructions = read_macro(sys.argv[1]) print "insructions:" - for opcode, argument in instructions: - print "%s %s" % (OpCode.str(opcode), str(argument) if argument is not None else "") + print_instructions(instructions) print "eval:" for primitive in eval_macro(instructions): diff --git a/gerber/am_statements.py b/gerber/am_statements.py index c514ad7..38f4d71 100644 --- a/gerber/am_statements.py +++ b/gerber/am_statements.py @@ -160,7 +160,7 @@ class AMCirclePrimitive(AMPrimitive): def from_gerber(cls, primitive): modifiers = primitive.strip(' *').split(',') code = int(modifiers[0]) - exposure = 'on' if modifiers[1].strip() == '1' else 'off' + exposure = 'on' if float(modifiers[1]) == 1 else 'off' diameter = float(modifiers[2]) position = (float(modifiers[3]), float(modifiers[4])) return cls(code, exposure, diameter, position) @@ -233,7 +233,7 @@ class AMVectorLinePrimitive(AMPrimitive): def from_gerber(cls, primitive): modifiers = primitive.strip(' *').split(',') code = int(modifiers[0]) - exposure = 'on' if modifiers[1].strip() == '1' else 'off' + exposure = 'on' if float(modifiers[1]) == 1 else 'off' width = float(modifiers[2]) start = (float(modifiers[3]), float(modifiers[4])) end = (float(modifiers[5]), float(modifiers[6])) @@ -318,8 +318,8 @@ class AMOutlinePrimitive(AMPrimitive): modifiers = primitive.strip(' *').split(",") code = int(modifiers[0]) - exposure = "on" if modifiers[1].strip() == "1" else "off" - n = int(modifiers[2]) + exposure = "on" if float(modifiers[1]) == 1 else "off" + n = int(float(modifiers[2])) start_point = (float(modifiers[3]), float(modifiers[4])) points = [] for i in range(n): @@ -405,7 +405,7 @@ class AMPolygonPrimitive(AMPrimitive): def from_gerber(cls, primitive): modifiers = primitive.strip(' *').split(",") code = int(modifiers[0]) - exposure = "on" if modifiers[1].strip() == "1" else "off" + exposure = "on" if float(modifiers[1]) == 1 else "off" vertices = int(float(modifiers[2])) position = (float(modifiers[3]), float(modifiers[4])) try: @@ -508,7 +508,7 @@ class AMMoirePrimitive(AMPrimitive): diameter = float(modifiers[3]) ring_thickness = float(modifiers[4]) gap = float(modifiers[5]) - max_rings = int(modifiers[6]) + max_rings = int(float(modifiers[6])) crosshair_thickness = float(modifiers[7]) crosshair_length = float(modifiers[8]) rotation = float(modifiers[9]) @@ -690,7 +690,7 @@ class AMCenterLinePrimitive(AMPrimitive): def from_gerber(cls, primitive): modifiers = primitive.strip(' *').split(",") code = int(modifiers[0]) - exposure = 'on' if modifiers[1].strip() == '1' else 'off' + exposure = 'on' if float(modifiers[1]) == 1 else 'off' width = float(modifiers[2]) height = float(modifiers[3]) center= (float(modifiers[4]), float(modifiers[5])) @@ -772,7 +772,7 @@ class AMLowerLeftLinePrimitive(AMPrimitive): def from_gerber(cls, primitive): modifiers = primitive.strip(' *').split(",") code = int(modifiers[0]) - exposure = 'on' if modifiers[1].strip() == '1' else 'off' + exposure = 'on' if float(modifiers[1]) == 1 else 'off' width = float(modifiers[2]) height = float(modifiers[3]) lower_left = (float(modifiers[4]), float(modifiers[5])) diff --git a/gerber/gerber_statements.py b/gerber/gerber_statements.py index 19c7138..99672de 100644 --- a/gerber/gerber_statements.py +++ b/gerber/gerber_statements.py @@ -350,31 +350,30 @@ class AMParamStmt(ParamStmt): def read(self, macro): return read_macro(macro) - def evaluate(self, modifiers=[]): - primitives = [] + def build(self, modifiers=[[]]): + self.primitives = [] + for primitive in eval_macro(self.instructions, modifiers[0]): if primitive[0] == '0': - primitives.append(AMCommentPrimitive.from_gerber(primitive)) + self.primitives.append(AMCommentPrimitive.from_gerber(primitive)) elif primitive[0] == '1': - primitives.append(AMCirclePrimitive.from_gerber(primitive)) + self.primitives.append(AMCirclePrimitive.from_gerber(primitive)) elif primitive[0:2] in ('2,', '20'): - primitives.append(AMVectorLinePrimitive.from_gerber(primitive)) + self.primitives.append(AMVectorLinePrimitive.from_gerber(primitive)) elif primitive[0:2] == '21': - primitives.append(AMCenterLinePrimitive.from_gerber(primitive)) + self.primitives.append(AMCenterLinePrimitive.from_gerber(primitive)) elif primitive[0:2] == '22': - primitives.append(AMLowerLeftLinePrimitive.from_gerber(primitive)) + self.primitives.append(AMLowerLeftLinePrimitive.from_gerber(primitive)) elif primitive[0] == '4': - primitives.append(AMOutlinePrimitive.from_gerber(primitive)) + self.primitives.append(AMOutlinePrimitive.from_gerber(primitive)) elif primitive[0] == '5': - primitives.append(AMPolygonPrimitive.from_gerber(primitive)) + self.primitives.append(AMPolygonPrimitive.from_gerber(primitive)) elif primitive[0] =='6': - primitives.append(AMMoirePrimitive.from_gerber(primitive)) + self.primitives.append(AMMoirePrimitive.from_gerber(primitive)) elif primitive[0] == '7': - primitives.append(AMThermalPrimitive.from_gerber(primitive)) + self.primitives.append(AMThermalPrimitive.from_gerber(primitive)) else: - primitives.append(AMUnsupportPrimitive.from_gerber(primitive)) - - return primitives + self.primitives.append(AMUnsupportPrimitive.from_gerber(primitive)) def to_inch(self): for primitive in self.primitives: 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 diff --git a/gerber/tests/test_gerber_statements.py b/gerber/tests/test_gerber_statements.py index 04358eb..9032268 100644 --- a/gerber/tests/test_gerber_statements.py +++ b/gerber/tests/test_gerber_statements.py @@ -333,6 +333,7 @@ def test_AMParamStmt_factory(): 8,THIS IS AN UNSUPPORTED PRIMITIVE* ''') s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.build() assert_equal(len(s.primitives), 10) assert_true(isinstance(s.primitives[0], AMCommentPrimitive)) assert_true(isinstance(s.primitives[1], AMCirclePrimitive)) @@ -347,29 +348,34 @@ def test_AMParamStmt_factory(): def testAMParamStmt_conversion(): name = 'POLYGON' - macro = '5,1,8,25.4,25.4,25.4,0*%' + macro = '5,1,8,25.4,25.4,25.4,0*' s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.build() 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*%' + macro = '5,1,8,1,1,1,0*' s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.build() 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*%' + macro = '5,1,8,25.4,25.4,25.4,0*' s = AMParamStmt.from_dict({'param': 'AM', 'name': name, 'macro': macro }) + s.build() + 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*%' + 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), '<Aperture Macro POLYGON: 5,1,8,25.4,25.4,25.4,0*%>') + s.build() + assert_equal(str(s), '<Aperture Macro POLYGON: 5,1,8,25.4,25.4,25.4,0*>') def test_ASParamStmt_factory(): stmt = {'param': 'AS', 'mode': 'AXBY'} |