From 4565712869ef4269d62de593a245ca8d001c4ea9 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 7 Oct 2021 11:13:12 +0200 Subject: WIP --- gerbonara/gerber/am_read.py | 2 +- gerbonara/gerber/am_statements.py | 143 +++++++------------ gerbonara/gerber/gerber_statements.py | 99 ++++++++----- gerbonara/gerber/panelize/__init__.py | 4 +- gerbonara/gerber/panelize/am_expression.py | 155 ++++++++++++-------- gerbonara/gerber/panelize/am_primitive.py | 49 ------- gerbonara/gerber/panelize/common.py | 17 +-- gerbonara/gerber/panelize/composition.py | 8 +- gerbonara/gerber/panelize/excellon.py | 13 +- gerbonara/gerber/panelize/gerber_statements.py | 66 --------- gerbonara/gerber/panelize/rs274x.py | 0 gerbonara/gerber/primitives.py | 61 -------- gerbonara/gerber/rs274x.py | 23 ++- gerbonara/gerber/tests/panelize/test_dxf.py | 2 + gerbonara/gerber/tests/panelize/test_rs274x.py | 23 ++- gerbonara/gerber/tests/panelize/test_utility.py | 15 -- gerbonara/gerber/tests/test_primitives.py | 181 ++++++++++++------------ gerbonara/gerber/tests/test_rs274x.py | 2 +- 18 files changed, 351 insertions(+), 512 deletions(-) delete mode 100644 gerbonara/gerber/panelize/rs274x.py diff --git a/gerbonara/gerber/am_read.py b/gerbonara/gerber/am_read.py index 4aff00b..475caad 100644 --- a/gerbonara/gerber/am_read.py +++ b/gerbonara/gerber/am_read.py @@ -29,7 +29,7 @@ class Token: # compatibility as many gerber writes do use non compliant X MULT = ("x", "X") DIV = "/" - OPERATORS = (ADD, SUB, MULT[0], MULT[1], DIV) + OPERATORS = (ADD, SUB, *MULT, DIV) LEFT_PARENS = "(" RIGHT_PARENS = ")" EQUALS = "=" diff --git a/gerbonara/gerber/am_statements.py b/gerbonara/gerber/am_statements.py index 31c0ae4..803a123 100644 --- a/gerbonara/gerber/am_statements.py +++ b/gerbonara/gerber/am_statements.py @@ -56,7 +56,7 @@ class AMPrimitive(object): TypeError, ValueError """ - def __init__(self, code, exposure=None): + def __init__(self, code, exposure=None, rotation=AMConstantExpression(0)): VALID_CODES = (0, 1, 2, 4, 5, 6, 7, 20, 21, 22, 9999) if not isinstance(code, int): raise TypeError('Aperture Macro Primitive code must be an integer') @@ -67,12 +67,30 @@ class AMPrimitive(object): raise ValueError('Exposure must be either on or off') self.code = code self.exposure = exposure.lower() if exposure is not None else None + self.rotation = rotation - def to_inch(self): - raise NotImplementedError('Subclass must implement `to-inch`') + def rotate(self, angle, center=None): + self.rotation = AMOperatorExpression(AMOperatorExpression.ADD, + self.rotation, + AMConstantExpression(float(angle))) + self.rotation = self.rotation.optimize() - def to_metric(self): - raise NotImplementedError('Subclass must implement `to-metric`') + #def to_inch(self): + # raise NotImplementedError('Subclass must implement to_inch') + + #def to_metric(self): + # raise NotImplementedError('Subclass must implement to_metric') + + #def to_gerber(self, settings=None): + # raise NotImplementedError('Subclass must implement to_gerber') + + #def to_instructions(self): + # raise NotImplementedError('Subclass must implement to_instructions') + + #def to_primitive(self, units): + # """ Return a Primitive instance based on the specified macro params. + # """ + # raise NotImplementedError('Subclass must implement to_primitive') @property def _level_polarity(self): @@ -80,11 +98,6 @@ class AMPrimitive(object): return 'clear' return 'dark' - def to_primitive(self, units): - """ Return a Primitive instance based on the specified macro params. - """ - print('Rendering {}s is not supported yet.'.format(str(self.__class__))) - def __eq__(self, other): return self.__dict__ == other.__dict__ @@ -126,7 +139,7 @@ class AMCommentPrimitive(AMPrimitive): def __init__(self, code, comment): if code != 0: raise ValueError('Not a valid Aperture Macro Comment statement') - super(AMCommentPrimitive, self).__init__(code) + super().__init__(code) self.comment = comment.strip(' *') def to_inch(self): @@ -136,7 +149,7 @@ class AMCommentPrimitive(AMPrimitive): pass def to_gerber(self, settings=None): - return '0 %s *' % self.comment + return f'0 {self.comment} *' def to_primitive(self, units): """ @@ -144,6 +157,9 @@ class AMCommentPrimitive(AMPrimitive): """ return None + def to_instructions(self): + return [(OpCode.PUSH, self.comment), (OpCode.PRIM, self.code)] + def __str__(self): return '' % self.comment @@ -210,12 +226,9 @@ class AMCirclePrimitive(AMPrimitive): self.position = tuple([metric(x) for x in self.position]) def to_gerber(self, settings=None): - data = dict(code=self.code, - exposure='1' if self.exposure == 'on' else 0, - diameter=self.diameter, - x=self.position[0], - y=self.position[1]) - return '{code},{exposure},{diameter},{x},{y}*'.format(**data) + exposure = 1 if self.exposure == 'on' else 0 + x, y = self.position + return f'{self.code},{exposure},{self.diameter},{x},{y}*' def to_primitive(self, units): return Circle((self.position), self.diameter, units=units, level_polarity=self._level_polarity) @@ -298,16 +311,10 @@ class AMVectorLinePrimitive(AMPrimitive): self.end = tuple([metric(x) for x in self.end]) def to_gerber(self, settings=None): - fmtstr = '{code},{exp},{width},{startx},{starty},{endx},{endy},{rotation}*' - data = dict(code=self.code, - exp=1 if self.exposure == 'on' else 0, - width=self.width, - startx=self.start[0], - starty=self.start[1], - endx=self.end[0], - endy=self.end[1], - rotation=self.rotation) - return fmtstr.format(**data) + exp = 1 if self.exposure == 'on' else 0 + start_x, start_y = self.start + end_x, end_y = self.end + return f'{self.code},{exp},{self.width},{start_x},{start_y},{end_x},{end_y},{self.rotation}*' def to_primitive(self, units): """ @@ -422,15 +429,10 @@ class AMOutlinePrimitive(AMPrimitive): self.points = tuple([(metric(x), metric(y)) for x, y in self.points]) def to_gerber(self, settings=None): - data = dict( - code=self.code, - exposure="1" if self.exposure == "on" else "0", - n_points=len(self.points), - start_point="%.6g,%.6g" % self.start_point, - points=",\n".join(["%.6g,%.6g" % point for point in self.points]), - rotation=str(self.rotation) - ) - return "{code},{exposure},{n_points},{start_point},{points},{rotation}*".format(**data) + exposure = 1 if self.exposure == 'on' else 0 + x0, y0 = self.start_point + points = ",\n".join([ f'{x:.6f},{y:.6f}' for x, y in self.points ]) + return f'{self.code},{exposure},{len(self.points)},{x0:.6f},{y0:.6f},{points},{self.rotation}*' def to_primitive(self, units): """ @@ -535,16 +537,9 @@ class AMPolygonPrimitive(AMPrimitive): self.diameter = metric(self.diameter) def to_gerber(self, settings=None): - data = dict( - code=self.code, - exposure="1" if self.exposure == "on" else "0", - vertices=self.vertices, - position="%.4g,%.4g" % self.position, - diameter='%.4g' % self.diameter, - rotation=str(self.rotation) - ) - fmt = "{code},{exposure},{vertices},{position},{diameter},{rotation}*" - return fmt.format(**data) + exposure = 1 if self.exposure == 'on' else 0 + x, y = self.position + return f'{self.code},{exposure},{self.vertices},{x:.4f},{y:.4f},{self.diameter:.4f},{self.rotation}*' def to_primitive(self, units): return Polygon(self.position, self.vertices, self.diameter / 2.0, 0, rotation=math.radians(self.rotation), units=units, level_polarity=self._level_polarity) @@ -646,19 +641,8 @@ class AMMoirePrimitive(AMPrimitive): def to_gerber(self, settings=None): - data = dict( - code=self.code, - position="%.4g,%.4g" % self.position, - diameter=self.diameter, - ring_thickness=self.ring_thickness, - gap=self.gap, - max_rings=self.max_rings, - crosshair_thickness=self.crosshair_thickness, - crosshair_length=self.crosshair_length, - rotation=self.rotation - ) - fmt = "{code},{position},{diameter},{ring_thickness},{gap},{max_rings},{crosshair_thickness},{crosshair_length},{rotation}*" - return fmt.format(**data) + x, y = self.position + return f'{self.code},{x:.4f},{y:.4f},{self.diameter},{self.ring_thickness},{self.gap},{self.max_rings},{self.crosshair_thickness},{self.crosshair_length},{self.rotation}*' def to_primitive(self, units): #raise NotImplementedError() @@ -739,16 +723,8 @@ class AMThermalPrimitive(AMPrimitive): self.gap = metric(self.gap) def to_gerber(self, settings=None): - data = dict( - code=self.code, - position="%.4g,%.4g" % self.position, - outer_diameter=self.outer_diameter, - inner_diameter=self.inner_diameter, - gap=self.gap, - rotation=self.rotation - ) - fmt = "{code},{position},{outer_diameter},{inner_diameter},{gap},{rotation}*" - return fmt.format(**data) + x, y = self.position + return f'{self.code},{x:.4f},{y:.4f},{self.outer_diameter},{self.inner_diameter},{self.gap},{self.rotation}*' def _approximate_arc_cw(self, start_angle, end_angle, radius, center): """ @@ -909,16 +885,9 @@ class AMCenterLinePrimitive(AMPrimitive): self.height = metric(self.height) def to_gerber(self, settings=None): - data = dict( - code=self.code, - exposure = '1' if self.exposure == 'on' else '0', - width = self.width, - height = self.height, - center="%.4g,%.4g" % self.center, - rotation=self.rotation - ) - fmt = "{code},{exposure},{width},{height},{center},{rotation}*" - return fmt.format(**data) + exposure = 1 if self.exposure == 'on' else 0 + x, y = self.center + return f'{self.code},{exposure},{self.width},{self.height},{x:.4f},{y:.4f},{self.rotation}*' def to_primitive(self, units): @@ -1015,16 +984,9 @@ class AMLowerLeftLinePrimitive(AMPrimitive): self.height = metric(self.height) def to_gerber(self, settings=None): - data = dict( - code=self.code, - exposure = '1' if self.exposure == 'on' else '0', - width = self.width, - height = self.height, - lower_left="%.4g,%.4g" % self.lower_left, - rotation=self.rotation - ) - fmt = "{code},{exposure},{width},{height},{lower_left},{rotation}*" - return fmt.format(**data) + exposure = 1 if self.exposure == 'on' else 0 + x, y = self.lower_left + return f'{self.code},{exposure},{self.width},{self.height},{x:.4f},{y:.4f},{self.rotation}*' class AMUnsupportPrimitive(AMPrimitive): @@ -1044,3 +1006,4 @@ class AMUnsupportPrimitive(AMPrimitive): def to_gerber(self, settings=None): return self.primitive + diff --git a/gerbonara/gerber/gerber_statements.py b/gerbonara/gerber/gerber_statements.py index 28f5e81..22766f8 100644 --- a/gerbonara/gerber/gerber_statements.py +++ b/gerbonara/gerber/gerber_statements.py @@ -391,10 +391,10 @@ class AMParamStmt(ParamStmt): """ @classmethod - def from_dict(cls, stmt_dict): - return cls(**stmt_dict) + def from_dict(cls, stmt_dict, units): + return cls(**stmt_dict, units=units) - def __init__(self, param, name, macro): + def __init__(self, param, name, macro, units): """ Initialize AMParamStmt class Parameters @@ -417,41 +417,66 @@ class AMParamStmt(ParamStmt): ParamStmt.__init__(self, param) self.name = name self.macro = macro - - self.instructions = self.read(macro) - self.primitives = [] + self.units = units + self.primitives = list(AMParamStmt._parse_primitives(self.instructions)) def read(self, macro): return read_macro(macro) - def build(self, modifiers=[[]]): - self.primitives = [] - - for primitive in eval_macro(self.instructions, modifiers[0]): - if primitive[0] == '0': - self.primitives.append(AMCommentPrimitive.from_gerber(primitive)) - elif primitive[0] == '1': - self.primitives.append(AMCirclePrimitive.from_gerber(primitive)) - elif primitive[0:2] in ('2,', '20'): - self.primitives.append(AMVectorLinePrimitive.from_gerber(primitive)) - elif primitive[0:2] == '21': - self.primitives.append(AMCenterLinePrimitive.from_gerber(primitive)) - elif primitive[0:2] == '22': - self.primitives.append(AMLowerLeftLinePrimitive.from_gerber(primitive)) - elif primitive[0] == '4': - self.primitives.append(AMOutlinePrimitive.from_gerber(primitive)) - elif primitive[0] == '5': - self.primitives.append(AMPolygonPrimitive.from_gerber(primitive)) - elif primitive[0] == '6': - self.primitives.append(AMMoirePrimitive.from_gerber(primitive)) - elif primitive[0] == '7': - self.primitives.append( - AMThermalPrimitive.from_gerber(primitive)) + @classmethod + def _parse_primitives(kls, instructions): + classes = { + 0: AMCommentPrimitive, + 1: AMCirclePrimitive, + 2: AMVectorLinePrimitive, + 20: AMVectorLinePrimitive, + 21: AMCenterLinePrimitive, + 4: AMOutlinePrimitive, + 5: AMPolygonPrimitive, + 6: AMMoirePrimitive, + 7: AMThermalPrimitive, + } + + for code, modifiers in eval_macro(instructions): + if code < 0: + yield AMVariableDef(-code, modifiers[0]) else: - self.primitives.append( - AMUnsupportPrimitive.from_gerber(primitive)) + primitive = classes[code] + yield primitive.from_modifiers(code, modifiers) + + @classmethod + def circle(cls, name, units): + return cls('AM', name, '1,1,$1,0,0,0*1,0,$2,0,0,0', units) - return AMGroup(self.primitives, stmt=self, units=self.units) + @classmethod + def rectangle(cls, name, units): + return cls('AM', name, '21,1,$1,$2,0,0,0*1,0,$3,0,0,0', units) + + @classmethod + def landscape_obround(cls, name, units): + return cls( + 'AM', name, + '$4=$1-$2*' + '$5=$1-$4*' + '21,1,$5,$2,0,0,0*' + '1,1,$4,$4/2,0,0*' + '1,1,$4,-$4/2,0,0*' + '1,0,$3,0,0,0', units) + + @classmethod + def portrate_obround(cls, name, units): + return cls( + 'AM', name, + '$4=$2-$1*' + '$5=$2-$4*' + '21,1,$1,$5,0,0,0*' + '1,1,$4,0,$4/2,0*' + '1,1,$4,0,-$4/2,0*' + '1,0,$3,0,0,0', units) + + @classmethod + def polygon(cls, name, units): + return cls('AM', name, '5,1,$2,0,0,$1,$3*1,0,$4,0,0,0', units) def to_inch(self): if self.units == 'metric': @@ -466,15 +491,19 @@ class AMParamStmt(ParamStmt): primitive.to_metric() def to_gerber(self, settings=None): - return '%AM{0}*{1}%'.format(self.name, "".join([primitive.to_gerber() for primitive in self.primitives])) + primitive_defs = '\n'.join(primitive.to_gerber() for primitive in self.primitives) + return f'%AM{self.name}*\n{primitive_defs}%' + + def rotate(self, angle, center=None): + for primitive_def in self.primitives: + primitive_def.rotate(angle, center) def __str__(self): return '' % (self.name, self.macro) class ASParamStmt(ParamStmt): - """ AS - Axis Select. (Deprecated) - """ + """ AS - Axis Select. (Deprecated) """ @classmethod def from_dict(cls, stmt_dict): param = stmt_dict.get('param') diff --git a/gerbonara/gerber/panelize/__init__.py b/gerbonara/gerber/panelize/__init__.py index a3de2c1..645b632 100644 --- a/gerbonara/gerber/panelize/__init__.py +++ b/gerbonara/gerber/panelize/__init__.py @@ -3,6 +3,6 @@ # Copyright 2019 Hiroshi Murayama -from .common import read, loads, rectangle +from .common import read, loads # , rectangle from .composition import GerberComposition, DrillComposition -from .dxf import DxfFile +# from .dxf import DxfFile diff --git a/gerbonara/gerber/panelize/am_expression.py b/gerbonara/gerber/panelize/am_expression.py index 9e19f71..a0a4114 100644 --- a/gerbonara/gerber/panelize/am_expression.py +++ b/gerbonara/gerber/panelize/am_expression.py @@ -3,6 +3,8 @@ # Copyright 2019 Hiroshi Murayama +import operator + from ..utils import * from ..am_eval import OpCode from ..am_statements import * @@ -20,21 +22,19 @@ class AMExpression(object): return self def optimize(self): - pass + return self def to_inch(self): - return AMOperatorExpression(AMOperatorExpression.DIV, self, - AMConstantExpression(MILLIMETERS_PER_INCH)) + return AMOperatorExpression.div(self, MILLIMETERS_PER_INCH) def to_metric(self): - return AMOperatorExpression(AMOperatorExpression.MUL, self, - AMConstantExpression(MILLIMETERS_PER_INCH)) + return AMOperatorExpression.mul(self, MILLIMETERS_PER_INCH) - def to_gerber(self, settings=None): - pass + #def to_gerber(self, settings=None): + # pass - def to_instructions(self): - pass + #def to_instructions(self): + # pass class AMConstantExpression(AMExpression): def __init__(self, value): @@ -45,14 +45,35 @@ class AMConstantExpression(AMExpression): def value(self): return self._value - def optimize(self): - return self - + def __float__(self): + return float(self._value) + + @staticmethod + def _amex_val(other): + return float(other) if isinstance(other, AMConstantExpression) else other + + def __eq__(self, val): + return self._value == AMConstantExpression._amex_val(val) + + def __ne__(self, val): + return self._value != AMConstantExpression._amex_val(val) + + def __lt__(self, val): + return self._value < AMConstantExpression._amex_val(val) + + def __gt__(self, val): + return self._value > AMConstantExpression._amex_val(val) + + def __le__(self, val): + return self._value <= AMConstantExpression._amex_val(val) + + def __ge__(self, val): + return self._value >= AMConstantExpression._amex_val(val) + def to_gerber(self, settings=None): if isinstance(self._value, str): return self._value - gerber = '%.6g' % self._value - return '%.6f' % self._value if 'e' in gerber else gerber + return f'{self.value:.6f}'.rstrip('0').rstrip('.') def to_instructions(self): return [(OpCode.PUSH, self._value)] @@ -62,76 +83,90 @@ class AMVariableExpression(AMExpression): super(AMVariableExpression, self).__init__(AMExpression.VARIABLE) self.number = number - def optimize(self): - return self - def to_gerber(self, settings=None): - return '$%d' % self.number + return f'${self.number}' def to_instructions(self): return (OpCode.LOAD, self.number) class AMOperatorExpression(AMExpression): - ADD = '+' - SUB = '-' - MUL = 'X' - DIV = '/' - def __init__(self, op, lvalue, rvalue): super(AMOperatorExpression, self).__init__(AMExpression.OPERATOR) self.op = op - self.lvalue = lvalue - self.rvalue = rvalue + self.lvalue = AMConstantExpression(lvalue) if isinstance(lvalue, (int, float)) else lvalue + self.rvalue = AMConstantExpression(rvalue) if isinstance(rvalue, (int, float)) else rvalue + + @classmethod + def add(kls, lvalue, rvalue): + return kls(operator.add, lvalue, rvalue) + + @classmethod + def sub(kls, lvalue, rvalue): + return kls(operator.sub, lvalue, rvalue) + + @classmethod + def mul(kls, lvalue, rvalue): + return kls(operator.mul, lvalue, rvalue) + + @classmethod + def div(kls, lvalue, rvalue): + return kls(operator.truediv, lvalue, rvalue) def optimize(self): - self.lvalue = self.lvalue.optimize() - self.rvalue = self.rvalue.optimize() - - if isinstance(self.lvalue, AMConstantExpression) and isinstance(self.rvalue, AMConstantExpression): - lvalue = float(self.lvalue.value) - rvalue = float(self.rvalue.value) - value = lvalue + rvalue if self.op == self.ADD else \ - lvalue - rvalue if self.op == self.SUB else \ - lvalue * rvalue if self.op == self.MUL else \ - lvalue / rvalue if self.op == self.DIV else None - return AMConstantExpression(value) - elif self.op == self.ADD: - if self.rvalue.value == 0: - return self.lvalue - elif self.lvalue.value == 0: - return self.rvalue - elif self.op == self.SUB: - if self.rvalue.value == 0: - return self.lvalue - elif self.lvalue.value == 0 and isinstance(self.rvalue, AMConstantExpression): - return AMConstantExpression(-self.rvalue.value) - elif self.op == self.MUL: - if self.rvalue.value == 1: - return self.lvalue - elif self.lvalue.value == 1: - return self.rvalue - elif self.lvalue == 0 or self.rvalue == 0: + l = self.lvalue = self.lvalue.optimize() + r = self.rvalue = self.rvalue.optimize() + + if isinstance(l, AMConstantExpression) and isinstance(r, AMConstantExpression): + return AMConstantExpression(self.op(float(r), float(l))) + + elif self.op == operator.ADD: + if r == 0: + return l + elif l == 0: + return r + + elif self.op == operator.SUB: + if r == 0: + return l + elif l == 0 and isinstance(r, AMConstantExpression): + return AMConstantExpression(-float(r)) + + elif self.op == operator.MUL: + if r == 1: + return l + elif l == 1: + return r + elif l == 0 or r == 0: return AMConstantExpression(0) - elif self.op == self.DIV: - if self.rvalue.value == 1: + + elif self.op == operator.TRUEDIV: + if r == 1: return self.lvalue - elif self.lvalue.value == 0: + elif l == 0: return AMConstantExpression(0) return self def to_gerber(self, settings=None): - return '(%s)%s(%s)' % (self.lvalue.to_gerber(settings), self.op, self.rvalue.to_gerber(settings)) + lval = self.lvalue.to_gerber(settings) + rval = self.rvalue.to_gerber(settings)) + op = {AMOperatorExpression.ADD: '+', + AMOperatorExpression.SUB: '-', + AMOperatorExpression.MUL: 'x', + AMOperatorExpression.DIV: '/'} [self.op] + return '(' + lval + op + rval + ')' def to_instructions(self): for i in self.lvalue.to_instructions(): yield i + for i in self.rvalue.to_instructions(): yield i - op = OpCode.ADD if self.op == self.ADD else\ - OpCode.SUB if self.op == self.SUB else\ - OpCode.MUL if self.op == self.MUL else\ - OpCode.DIV + + op = {AMOperatorExpression.ADD: OpCode.ADD, + AMOperatorExpression.SUB: OpCode.SUB, + AMOperatorExpression.MUL: OpCode.MUL, + AMOperatorExpression.DIV: OpCode.DIV} [self.op] yield (op, None) def eval_macro(instructions): diff --git a/gerbonara/gerber/panelize/am_primitive.py b/gerbonara/gerber/panelize/am_primitive.py index 123f030..5d8458b 100644 --- a/gerbonara/gerber/panelize/am_primitive.py +++ b/gerbonara/gerber/panelize/am_primitive.py @@ -9,31 +9,6 @@ from ..am_eval import OpCode from .am_expression import eval_macro, AMConstantExpression, AMOperatorExpression -class AMPrimitiveDef(AMPrimitive): - def __init__(self, code, exposure=None, rotation=None): - super(AMPrimitiveDef, self).__init__(code, exposure) - if not rotation: - rotation = AMConstantExpression(0) - self.rotation = rotation - - def rotate(self, angle, center=None): - self.rotation = AMOperatorExpression(AMOperatorExpression.ADD, - self.rotation, - AMConstantExpression(float(angle))) - self.rotation = self.rotation.optimize() - - def to_inch(self): - pass - - def to_metric(self): - pass - - def to_gerber(self, settings=None): - pass - - def to_instructions(self): - pass - class AMCommentPrimitiveDef(AMPrimitiveDef): @classmethod def from_modifiers(cls, code, modifiers): @@ -42,12 +17,6 @@ class AMCommentPrimitiveDef(AMPrimitiveDef): def __init__(self, code, comment): super(AMCommentPrimitiveDef, self).__init__(code) self.comment = comment - - def to_gerber(self, settings=None): - return '%d %s*' % (self.code, self.comment.to_gerber()) - - def to_instructions(self): - return [(OpCode.PUSH, self.comment), (OpCode.PRIM, self.code)] class AMCirclePrimitiveDef(AMPrimitiveDef): @classmethod @@ -428,21 +397,3 @@ class AMVariableDef(object): def rotate(self, angle, center=None): pass -def to_primitive_defs(instructions): - classes = { - 0: AMCommentPrimitiveDef, - 1: AMCirclePrimitiveDef, - 2: AMVectorLinePrimitiveDef, - 20: AMVectorLinePrimitiveDef, - 21: AMCenterLinePrimitiveDef, - 4: AMOutlinePrimitiveDef, - 5: AMPolygonPrimitiveDef, - 6: AMMoirePrimitiveDef, - 7: AMThermalPrimitiveDef, - } - for code, modifiers in eval_macro(instructions): - if code < 0: - yield AMVariableDef(-code, modifiers[0]) - else: - primitive = classes[code] - yield primitive.from_modifiers(code, modifiers) diff --git a/gerbonara/gerber/panelize/common.py b/gerbonara/gerber/panelize/common.py index ac25138..aa3ba0e 100644 --- a/gerbonara/gerber/panelize/common.py +++ b/gerbonara/gerber/panelize/common.py @@ -10,9 +10,7 @@ from ..utils import detect_file_format from .. import rs274x from .. import ipc356 -from . import rs274x as ex_rs274x from . import excellon -from . import dxf def read(filename, format=None): with open(filename, 'r') as f: @@ -21,14 +19,11 @@ def read(filename, format=None): def loads(data, filename=None, format=None): - if os.path.splitext(filename if filename else '')[1].lower() == '.dxf': - return dxf.loads(data, filename) + # if os.path.splitext(filename if filename else '')[1].lower() == '.dxf': + # return dxf.loads(data, filename) fmt = detect_file_format(data) - if fmt == 'rs274x': - file = ex_rs274x.loads(data, filename=filename) - return ex_rs274x.GerberFile.from_gerber_file(file) - elif fmt == 'excellon': + if fmt == 'excellon': return excellon.loads(data, filename=filename, format=format) elif fmt == 'ipc_d_356': return ipc356.loads(data, filename=filename) @@ -36,6 +31,6 @@ def loads(data, filename=None, format=None): raise ParseError('Unable to detect file format') -def rectangle(width, height, left=0, bottom=0, units='metric', draw_mode=None, filename=None): - return dxf.DxfFile.rectangle( - width, height, left, bottom, units, draw_mode, filename) +# def rectangle(width, height, left=0, bottom=0, units='metric', draw_mode=None, filename=None): +# return dxf.DxfFile.rectangle( +# width, height, left, bottom, units, draw_mode, filename) diff --git a/gerbonara/gerber/panelize/composition.py b/gerbonara/gerber/panelize/composition.py index 619a0cf..a30f959 100644 --- a/gerbonara/gerber/panelize/composition.py +++ b/gerbonara/gerber/panelize/composition.py @@ -8,9 +8,9 @@ from ..cam import FileSettings from ..gerber_statements import EofStmt from ..excellon_statements import * from ..excellon import DrillSlot, DrillHit -from . import rs274x +from .. import rs274x from . import excellon -from . import dxf +# from . import dxf class Composition(object): def __init__(self, settings = None, comments = None): @@ -29,8 +29,8 @@ class GerberComposition(Composition): def merge(self, file): if isinstance(file, rs274x.GerberFile): self._merge_gerber(file) - elif isinstance(file, dxf.DxfFile): - self._merge_dxf(file) +# elif isinstance(file, dxf.DxfFile): +# self._merge_dxf(file) else: raise Exception('unsupported file type') diff --git a/gerbonara/gerber/panelize/excellon.py b/gerbonara/gerber/panelize/excellon.py index ae0b68e..e6cfcd0 100644 --- a/gerbonara/gerber/panelize/excellon.py +++ b/gerbonara/gerber/panelize/excellon.py @@ -13,8 +13,7 @@ from ..excellon_statements import ExcellonStatement, UnitStmt, CoordinateStmt, U RetractWithClampingStmt, RetractWithoutClampingStmt, \ EndOfProgramStmt from ..cam import FileSettings -from ..utils import inch, metric, write_gerber_value, parse_gerber_value -from .utility import rotate +from ..utils import inch, metric, write_gerber_value, parse_gerber_value, rotate_point def loads(data, filename=None, settings=None, tools=None, format=None): if not settings: @@ -221,7 +220,7 @@ class DrillHitEx(DrillHit): self.position = tuple(map(metric, self.position)) def rotate(self, angle, center=(0, 0)): - self.position = rotate(*self.position, angle, center) + self.position = rotate_point(self.position, angle, center) def to_excellon(self, settings): return CoordinateStmtEx(*self.position).to_excellon(settings) @@ -236,8 +235,8 @@ class DrillSlotEx(DrillSlot): self.end = tuple(map(metric, self.end)) def rotate(self, angle, center=(0,0)): - self.start = rotate(*self.start, angle, center) - self.end = rotate(*self.end, angle, center) + self.start = rotate_point(self.start, angle, center) + self.end = rotate_point(self.end, angle, center) def to_excellon(self, settings): return SlotStmt(*self.start, *self.end).to_excellon(settings) @@ -295,9 +294,9 @@ class DrillRout(object): def rotate(self, angle, center=(0, 0)): for node in self.nodes: - node.position = rotate(*node.position, angle, center) + node.position = rotate_point(node.position, angle, center) if node.center_offset is not None: - node.center_offset = rotate(*node.center_offset, angle, (0., 0.)) + node.center_offset = rotate_point(node.center_offset, angle, (0., 0.)) class UnitStmtEx(UnitStmt): @classmethod diff --git a/gerbonara/gerber/panelize/gerber_statements.py b/gerbonara/gerber/panelize/gerber_statements.py index 875d656..208660e 100644 --- a/gerbonara/gerber/panelize/gerber_statements.py +++ b/gerbonara/gerber/panelize/gerber_statements.py @@ -7,72 +7,6 @@ from ..gerber_statements import AMParamStmt, ADParamStmt from ..utils import inch, metric from .am_primitive import to_primitive_defs -class AMParamStmtEx(AMParamStmt): - @classmethod - def from_stmt(cls, stmt): - return cls(stmt.param, stmt.name, stmt.macro, stmt.units) - - @classmethod - def circle(cls, name, units): - return cls('AM', name, '1,1,$1,0,0,0*1,0,$2,0,0,0', units) - - @classmethod - def rectangle(cls, name, units): - return cls('AM', name, '21,1,$1,$2,0,0,0*1,0,$3,0,0,0', units) - - @classmethod - def landscape_obround(cls, name, units): - return cls( - 'AM', name, - '$4=$1-$2*' - '$5=$1-$4*' - '21,1,$5,$2,0,0,0*' - '1,1,$4,$4/2,0,0*' - '1,1,$4,-$4/2,0,0*' - '1,0,$3,0,0,0', units) - - @classmethod - def portrate_obround(cls, name, units): - return cls( - 'AM', name, - '$4=$2-$1*' - '$5=$2-$4*' - '21,1,$1,$5,0,0,0*' - '1,1,$4,0,$4/2,0*' - '1,1,$4,0,-$4/2,0*' - '1,0,$3,0,0,0', units) - - @classmethod - def polygon(cls, name, units): - return cls('AM', name, '5,1,$2,0,0,$1,$3*1,0,$4,0,0,0', units) - - def __init__(self, param, name, macro, units): - super(AMParamStmtEx, self).__init__(param, name, macro) - self.units = units - self.primitive_defs = list(to_primitive_defs(self.instructions)) - - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - for p in self.primitive_defs: - p.to_inch() - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - for p in self.primitive_defs: - p.to_metric() - - def to_gerber(self, settings = None): - def plist(): - for p in self.primitive_defs: - yield p.to_gerber(settings) - return "%%AM%s*\n%s%%" % (self.name, '\n'.join(plist())) - - def rotate(self, angle, center=None): - for primitive_def in self.primitive_defs: - primitive_def.rotate(angle, center) - class ADParamStmtEx(ADParamStmt): GEOMETRIES = { 'C': [0,1], diff --git a/gerbonara/gerber/panelize/rs274x.py b/gerbonara/gerber/panelize/rs274x.py deleted file mode 100644 index e69de29..0000000 diff --git a/gerbonara/gerber/primitives.py b/gerbonara/gerber/primitives.py index fa08fef..afe6ec4 100644 --- a/gerbonara/gerber/primitives.py +++ b/gerbonara/gerber/primitives.py @@ -1448,67 +1448,6 @@ class Region(Primitive): for p in self.primitives: p.offset(x_offset, y_offset) - -class RoundButterfly(Primitive): - """ A circle with two diagonally-opposite quadrants removed - """ - - def __init__(self, position, diameter, **kwargs): - super(RoundButterfly, self).__init__(**kwargs) - validate_coordinates(position) - self.position = position - self.diameter = diameter - self._to_convert = ['position', 'diameter'] - - # TODO This does not reset bounding box correctly - - @property - def flashed(self): - return True - - @property - def radius(self): - return self.diameter / 2. - - @property - def bounding_box(self): - if self._bounding_box is None: - min_x = self.position[0] - self.radius - max_x = self.position[0] + self.radius - min_y = self.position[1] - self.radius - max_y = self.position[1] + self.radius - self._bounding_box = ((min_x, min_y), (max_x, max_y)) - return self._bounding_box - - -class SquareButterfly(Primitive): - """ A square with two diagonally-opposite quadrants removed - """ - - def __init__(self, position, side, **kwargs): - super(SquareButterfly, self).__init__(**kwargs) - validate_coordinates(position) - self.position = position - self.side = side - self._to_convert = ['position', 'side'] - - # TODO This does not reset bounding box correctly - - @property - def flashed(self): - return True - - @property - def bounding_box(self): - if self._bounding_box is None: - min_x = self.position[0] - (self.side / 2.) - max_x = self.position[0] + (self.side / 2.) - min_y = self.position[1] - (self.side / 2.) - max_y = self.position[1] + (self.side / 2.) - self._bounding_box = ((min_x, min_y), (max_x, max_y)) - return self._bounding_box - - class Donut(Primitive): """ A Shape with an identical concentric shape removed from its center """ diff --git a/gerbonara/gerber/rs274x.py b/gerbonara/gerber/rs274x.py index b659f20..1d946e9 100644 --- a/gerbonara/gerber/rs274x.py +++ b/gerbonara/gerber/rs274x.py @@ -124,11 +124,10 @@ class GerberFile(CamFile): self.context.normalize_coordinates(stmt) if isinstance(stmt, AMParamStmt): - for mdef in stmts: - self.aperture_macros[mdef.name] = mdef + self.aperture_macros[stmt.name] = stmt elif isinstance(stmt, ADParamStmt): - self.aperture_defs.extend(stmts) + self.aperture_defs.append(stmt) else: # ignore FS, MO, AS, IN, IP, IR, MI, OF, SF, LN statements @@ -138,7 +137,7 @@ class GerberFile(CamFile): if isinstance(stmt, (CommentStmt, EofStmt)): continue - self.main_statements.extend(stmts) + self.main_statements.append(stmt) if self.context.angle != 0: self.rotate(self.context.angle) # TODO is this correct/useful? @@ -246,16 +245,16 @@ class GerberFile(CamFile): return next(f'{prefix}_{i}' for i in count() if f'{prefix}_{i}' not in self.aperture_macros) rect = free_name('MACR') - self.aperture_macros[rect] = AMParamStmtEx.rectangle(rect, self.units) + self.aperture_macros[rect] = AMParamStmt.rectangle(rect, self.units) obround_landscape = free_name('MACLO') - self.aperture_macros[obround_landscape] = AMParamStmtEx.landscape_obround(obround_landscape, self.units) + self.aperture_macros[obround_landscape] = AMParamStmt.landscape_obround(obround_landscape, self.units) obround_portrait = free_name('MACPO') - self.aperture_macros[obround_portrait] = AMParamStmtEx.portrait_obround(obround_portrait, self.units) + self.aperture_macros[obround_portrait] = AMParamStmt.portrait_obround(obround_portrait, self.units) polygon = free_name('MACP') - self.aperture_macros[polygon] = AMParamStmtEx.polygon(polygon, self.units) + self.aperture_macros[polygon] = AMParamStmt.polygon(polygon, self.units) for statement in self.aperture_defs: if isinstance(statement, ADParamStmt): @@ -298,7 +297,7 @@ class GerberParser(object): IR = r"(?PIR)(?P{number})".format(number=NUMBER) MI = r"(?PMI)(A(?P0|1))?(B(?P0|1))?" OF = r"(?POF)(A(?P{decimal}))?(B(?P{decimal}))?".format(decimal=DECIMAL) - SF = r"(?PSF)(A(?P{decimal}))?(B(?P{decimal}))?".format(decimal=cls.DECIMAL) + SF = r"(?PSF)(A(?P{decimal}))?(B(?P{decimal}))?".format(decimal=DECIMAL) LN = r"(?PLN)(?P.*)" DEPRECATED_UNIT = re.compile(r'(?PG7[01])\*') DEPRECATED_FORMAT = re.compile(r'(?PG9[01])\*') @@ -466,9 +465,7 @@ class GerberParser(object): elif param["param"] == "AD": yield ADParamStmt.from_dict(param) elif param["param"] == "AM": - stmt = AMParamStmt.from_dict(param) - stmt.units = self.settings.units - yield stmt + yield AMParamStmt.from_dict(param, units=self.settings.units) elif param["param"] == "OF": yield OFParamStmt.from_dict(param) elif param["param"] == "IF": @@ -925,7 +922,7 @@ class GerberContext(FileSettings): self.x, self.y = 0, 0 def update_from_statement(self, stmt): - elif isinstance(stmt, MIParamStmt): + if isinstance(stmt, MIParamStmt): self.mirror = (stmt.a, stmt.b) elif isinstance(stmt, OFParamStmt): diff --git a/gerbonara/gerber/tests/panelize/test_dxf.py b/gerbonara/gerber/tests/panelize/test_dxf.py index dfd59d2..39c3842 100644 --- a/gerbonara/gerber/tests/panelize/test_dxf.py +++ b/gerbonara/gerber/tests/panelize/test_dxf.py @@ -8,10 +8,12 @@ import tempfile from pathlib import Path from contextlib import contextmanager import unittest + from ... import panelize from ...utils import inch, metric +@unittest.skip class TestExcellon(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/gerbonara/gerber/tests/panelize/test_rs274x.py b/gerbonara/gerber/tests/panelize/test_rs274x.py index 85748a3..067717d 100644 --- a/gerbonara/gerber/tests/panelize/test_rs274x.py +++ b/gerbonara/gerber/tests/panelize/test_rs274x.py @@ -8,7 +8,7 @@ import tempfile from pathlib import Path from contextlib import contextmanager import unittest -from ... import panelize +from ...rs274x import read class TestRs274x(unittest.TestCase): @classmethod @@ -26,41 +26,50 @@ class TestRs274x(unittest.TestCase): actual = tmp_out.read() expected = (self.EXPECTSDIR / reference_fn).read_bytes() + + print('==== ACTUAL ===') + print(actual.decode()) + print() + print() + print('==== EXPECTED ===') + print(expected.decode()) + print() + print() self.assertEqual(actual, expected) def test_save(self): with self._check_result('RS2724x_save.gtl') as outfile: - gerber = panelize.read(self.METRIC_FILE) + gerber = read(self.METRIC_FILE) gerber.write(outfile) def test_to_inch(self): with self._check_result('RS2724x_to_inch.gtl') as outfile: - gerber = panelize.read(self.METRIC_FILE) + gerber = read(self.METRIC_FILE) gerber.to_inch() gerber.format = (2,5) gerber.write(outfile) def test_to_metric(self): with self._check_result('RS2724x_to_metric.gtl') as outfile: - gerber = panelize.read(self.INCH_FILE) + gerber = read(self.INCH_FILE) gerber.to_metric() gerber.format = (3, 4) gerber.write(outfile) def test_offset(self): with self._check_result('RS2724x_offset.gtl') as outfile: - gerber = panelize.read(self.METRIC_FILE) + gerber = read(self.METRIC_FILE) gerber.offset(11, 5) gerber.write(outfile) def test_rotate(self): with self._check_result('RS2724x_rotate.gtl') as outfile: - gerber = panelize.read(self.METRIC_FILE) + gerber = read(self.METRIC_FILE) gerber.rotate(20, (10,10)) gerber.write(outfile) def test_single_quadrant(self): with self._check_result('RS2724x_single_quadrant.gtl') as outfile: - gerber = panelize.read(self.SQ_FILE) + gerber = read(self.SQ_FILE) gerber.write(outfile) diff --git a/gerbonara/gerber/tests/panelize/test_utility.py b/gerbonara/gerber/tests/panelize/test_utility.py index b32af8b..f632d90 100644 --- a/gerbonara/gerber/tests/panelize/test_utility.py +++ b/gerbonara/gerber/tests/panelize/test_utility.py @@ -9,21 +9,6 @@ from ...panelize.utility import * from math import sqrt class TestUtility(unittest.TestCase): - def test_rotate(self): - x0, y0 = (10, 0) - - x1, y1 = rotate(x0, y0, 60, (0, 0)) - self.assertAlmostEqual(x1, 5) - self.assertAlmostEqual(y1, 10 * sqrt(3) / 2) - - x1, y1 = rotate(x0, y0, 180, (5, 0)) - self.assertAlmostEqual(x1, 0) - self.assertAlmostEqual(y1, 0) - - x1, y1 = rotate(x0, y0, -90, (10, 5)) - self.assertAlmostEqual(x1, 5) - self.assertAlmostEqual(y1, 5) - def test_is_equal_value(self): a = 10.0001 b = 10.01 diff --git a/gerbonara/gerber/tests/test_primitives.py b/gerbonara/gerber/tests/test_primitives.py index ad5b34f..d4ee653 100644 --- a/gerbonara/gerber/tests/test_primitives.py +++ b/gerbonara/gerber/tests/test_primitives.py @@ -49,10 +49,10 @@ def test_line_bounds(): """ Test Line primitive bounding box calculation """ cases = [ - ((0, 0), (1, 1), ((-1, 2), (-1, 2))), - ((-1, -1), (1, 1), ((-2, 2), (-2, 2))), - ((1, 1), (-1, -1), ((-2, 2), (-2, 2))), - ((-1, 1), (1, -1), ((-2, 2), (-2, 2))), + ((0, 0), (1, 1), ((-1, -1), (2, 2))), + ((-1, -1), (1, 1), ((-2, -2), (2, 2))), + ((1, 1), (-1, -1), ((-2, -2), (2, 2))), + ((-1, 1), (1, -1), ((-2, -2), (2, 2))), ] c = Circle((0, 0), 2) @@ -64,10 +64,10 @@ def test_line_bounds(): # Test a non-square rectangle r = Rectangle((0, 0), 3, 2) cases = [ - ((0, 0), (1, 1), ((-1.5, 2.5), (-1, 2))), - ((-1, -1), (1, 1), ((-2.5, 2.5), (-2, 2))), - ((1, 1), (-1, -1), ((-2.5, 2.5), (-2, 2))), - ((-1, 1), (1, -1), ((-2.5, 2.5), (-2, 2))), + ((0, 0), (1, 1), ((-1.5, -1), (2.5, 2))), + ((-1, -1), (1, 1), ((-2.5, -2), (2.5, 2))), + ((1, 1), (-1, -1), ((-2.5, -2), (2.5, 2))), + ((-1, 1), (1, -1), ((-2.5, -2), (2.5, 2))), ] for start, end, expected in cases: l = Line(start, end, r) @@ -197,17 +197,17 @@ def test_arc_bounds(): """ Test Arc primitive bounding box calculation """ cases = [ - ((1, 0), (0, 1), (0, 0), "clockwise", ((-1.5, 1.5), (-1.5, 1.5))), - ((1, 0), (0, 1), (0, 0), "counterclockwise", ((-0.5, 1.5), (-0.5, 1.5))), - ((0, 1), (-1, 0), (0, 0), "clockwise", ((-1.5, 1.5), (-1.5, 1.5))), - ((0, 1), (-1, 0), (0, 0), "counterclockwise", ((-1.5, 0.5), (-0.5, 1.5))), - ((-1, 0), (0, -1), (0, 0), "clockwise", ((-1.5, 1.5), (-1.5, 1.5))), - ((-1, 0), (0, -1), (0, 0), "counterclockwise", ((-1.5, 0.5), (-1.5, 0.5))), - ((0, -1), (1, 0), (0, 0), "clockwise", ((-1.5, 1.5), (-1.5, 1.5))), - ((0, -1), (1, 0), (0, 0), "counterclockwise", ((-0.5, 1.5), (-1.5, 0.5))), + (( 1, 0), ( 0, 1), (0, 0), "clockwise", ((-1.5, -1.5), (1.5, 1.5))), + (( 1, 0), ( 0, 1), (0, 0), "counterclockwise", ((-0.5, -0.5), (1.5, 1.5))), + (( 0, 1), (-1, 0), (0, 0), "clockwise", ((-1.5, -1.5), (1.5, 1.5))), + (( 0, 1), (-1, 0), (0, 0), "counterclockwise", ((-1.5, -0.5), (0.5, 1.5))), + ((-1, 0), ( 0, -1), (0, 0), "clockwise", ((-1.5, -1.5), (1.5, 1.5))), + ((-1, 0), ( 0, -1), (0, 0), "counterclockwise", ((-1.5, -1.5), (0.5, 0.5))), + (( 0, -1), ( 1, 0), (0, 0), "clockwise", ((-1.5, -1.5), (1.5, 1.5))), + (( 0, -1), ( 1, 0), (0, 0), "counterclockwise", ((-0.5, -1.5), (1.5, 0.5))), # Arcs with the same start and end point render a full circle - ((1, 0), (1, 0), (0, 0), "clockwise", ((-1.5, 1.5), (-1.5, 1.5))), - ((1, 0), (1, 0), (0, 0), "counterclockwise", ((-1.5, 1.5), (-1.5, 1.5))), + (( 1, 0), ( 1, 0), (0, 0), "clockwise", ((-1.5, -1.5), (1.5, 1.5))), + (( 1, 0), ( 1, 0), (0, 0), "counterclockwise", ((-1.5, -1.5), (1.5, 1.5))), ] for start, end, center, direction, bounds in cases: c = Circle((0, 0), 1) @@ -219,17 +219,17 @@ def test_arc_bounds_no_aperture(): """ Test Arc primitive bounding box calculation ignoring aperture """ cases = [ - ((1, 0), (0, 1), (0, 0), "clockwise", ((-1.0, 1.0), (-1.0, 1.0))), - ((1, 0), (0, 1), (0, 0), "counterclockwise", ((0.0, 1.0), (0.0, 1.0))), - ((0, 1), (-1, 0), (0, 0), "clockwise", ((-1.0, 1.0), (-1.0, 1.0))), + ((1, 0), (0, 1), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))), + ((1, 0), (0, 1), (0, 0), "counterclockwise", ((0.0, 0.0), (1.0, 1.0))), + ((0, 1), (-1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))), ((0, 1), (-1, 0), (0, 0), "counterclockwise", ((-1.0, 0.0), (0.0, 1.0))), - ((-1, 0), (0, -1), (0, 0), "clockwise", ((-1.0, 1.0), (-1.0, 1.0))), - ((-1, 0), (0, -1), (0, 0), "counterclockwise", ((-1.0, 0.0), (-1.0, 0.0))), - ((0, -1), (1, 0), (0, 0), "clockwise", ((-1.0, 1.0), (-1.0, 1.0))), - ((0, -1), (1, 0), (0, 0), "counterclockwise", ((-0.0, 1.0), (-1.0, 0.0))), + ((-1, 0), (0, -1), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))), + ((-1, 0), (0, -1), (0, 0), "counterclockwise", ((-1.0, -1.0), (0.0, 0.0))), + ((0, -1), (1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))), + ((0, -1), (1, 0), (0, 0), "counterclockwise", ((-0.0, -1.0), (1.0, 0.0))), # Arcs with the same start and end point render a full circle - ((1, 0), (1, 0), (0, 0), "clockwise", ((-1.0, 1.0), (-1.0, 1.0))), - ((1, 0), (1, 0), (0, 0), "counterclockwise", ((-1.0, 1.0), (-1.0, 1.0))), + ((1, 0), (1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))), + ((1, 0), (1, 0), (0, 0), "counterclockwise", ((-1.0, -1.0), (1.0, 1.0))), ] for start, end, center, direction, bounds in cases: c = Circle((0, 0), 1) @@ -317,7 +317,7 @@ def test_circle_bounds(): """ Test Circle bounding box calculation """ c = Circle((1, 1), 2) - assert c.bounding_box == ((0, 2), (0, 2)) + assert c.bounding_box == ((0, 0), (2, 2)) def test_circle_conversion(): @@ -419,13 +419,13 @@ def test_ellipse_bounds(): """ Test ellipse bounding box calculation """ e = Ellipse((2, 2), 4, 2) - assert e.bounding_box == ((0, 4), (1, 3)) + assert e.bounding_box == ((0, 1), (4, 3)) e = Ellipse((2, 2), 4, 2, rotation=90) - assert e.bounding_box == ((1, 3), (0, 4)) + assert e.bounding_box == ((1, 0), (3, 4)) e = Ellipse((2, 2), 4, 2, rotation=180) - assert e.bounding_box == ((0, 4), (1, 3)) + assert e.bounding_box == ((0, 1), (4, 3)) e = Ellipse((2, 2), 4, 2, rotation=270) - assert e.bounding_box == ((1, 3), (0, 4)) + assert e.bounding_box == ((1, 0), (3, 4)) def test_ellipse_conversion(): @@ -501,13 +501,13 @@ def test_rectangle_bounds(): """ Test rectangle bounding box calculation """ r = Rectangle((0, 0), 2, 2) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = r.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) r = Rectangle((0, 0), 2, 2, rotation=45) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-math.sqrt(2), math.sqrt(2))) - pytest.approx(ybounds, (-math.sqrt(2), math.sqrt(2))) + bounds = r.bounding_box + pytest.approx(bounds[0], (-math.sqrt(2), -math.sqrt(2))) + pytest.approx(bounds[1], (math.sqrt(2), math.sqrt(2))) def test_rectangle_vertices(): @@ -650,13 +650,13 @@ def test_diamond_bounds(): """ Test diamond bounding box calculation """ d = Diamond((0, 0), 2, 2) - xbounds, ybounds = d.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = d.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) d = Diamond((0, 0), math.sqrt(2), math.sqrt(2), rotation=45) - xbounds, ybounds = d.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = d.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) def test_diamond_conversion(): @@ -724,13 +724,13 @@ def test_chamfer_rectangle_bounds(): """ Test chamfer rectangle bounding box calculation """ r = ChamferRectangle((0, 0), 2, 2, 0.2, (True, True, False, False)) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = r.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) r = ChamferRectangle((0, 0), 2, 2, 0.2, (True, True, False, False), rotation=45) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-math.sqrt(2), math.sqrt(2))) - pytest.approx(ybounds, (-math.sqrt(2), math.sqrt(2))) + bounds = r.bounding_box + pytest.approx(bounds[0], (-math.sqrt(2), -math.sqrt(2))) + pytest.approx(bounds[1], (math.sqrt(2), math.sqrt(2))) def test_chamfer_rectangle_conversion(): @@ -849,13 +849,13 @@ def test_round_rectangle_bounds(): """ Test round rectangle bounding box calculation """ r = RoundRectangle((0, 0), 2, 2, 0.2, (True, True, False, False)) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = r.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) r = RoundRectangle((0, 0), 2, 2, 0.2, (True, True, False, False), rotation=45) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (-math.sqrt(2), math.sqrt(2))) - pytest.approx(ybounds, (-math.sqrt(2), math.sqrt(2))) + bounds = r.bounding_box + pytest.approx(bounds[0], (-math.sqrt(2), -math.sqrt(2))) + pytest.approx(bounds[1], (math.sqrt(2), math.sqrt(2))) def test_round_rectangle_conversion(): @@ -927,13 +927,13 @@ def test_obround_bounds(): """ Test obround bounding box calculation """ o = Obround((2, 2), 2, 4) - xbounds, ybounds = o.bounding_box - pytest.approx(xbounds, (1, 3)) - pytest.approx(ybounds, (0, 4)) + bounds = o.bounding_box + pytest.approx(bounds[0], (1, 0)) + pytest.approx(bounds[1], (3, 4)) o = Obround((2, 2), 4, 2) - xbounds, ybounds = o.bounding_box - pytest.approx(xbounds, (0, 4)) - pytest.approx(ybounds, (1, 3)) + bounds = o.bounding_box + pytest.approx(bounds[0], (0, 1)) + pytest.approx(bounds[1], (4, 3)) def test_obround_orientation(): @@ -1020,13 +1020,13 @@ def test_polygon_bounds(): """ Test polygon bounding box calculation """ p = Polygon((2, 2), 3, 2, 0) - xbounds, ybounds = p.bounding_box - pytest.approx(xbounds, (0, 4)) - pytest.approx(ybounds, (0, 4)) + bounds = p.bounding_box + pytest.approx(bounds[0], (0, 0)) + pytest.approx(bounds[0], (4, 4)) p = Polygon((2, 2), 3, 4, 0) - xbounds, ybounds = p.bounding_box - pytest.approx(xbounds, (-2, 6)) - pytest.approx(ybounds, (-2, 6)) + bounds = p.bounding_box + pytest.approx(bounds[0], (-2, -2)) + pytest.approx(bounds[1], (6, 6)) def test_polygon_conversion(): @@ -1098,9 +1098,9 @@ def test_region_bounds(): Line((0, 1), (0, 0), apt), ) r = Region(lines) - xbounds, ybounds = r.bounding_box - pytest.approx(xbounds, (0, 1)) - pytest.approx(ybounds, (0, 1)) + bounds = r.bounding_box + pytest.approx(bounds[0], (0, 0)) + pytest.approx(bounds[1], (1, 1)) def test_region_offset(): @@ -1183,9 +1183,9 @@ def test_round_butterfly_bounds(): """ Test RoundButterfly bounding box calculation """ b = RoundButterfly((0, 0), 2) - xbounds, ybounds = b.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = b.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) def test_square_butterfly_ctor(): @@ -1209,9 +1209,9 @@ def test_square_butterfly_bounds(): """ Test SquareButterfly bounding box calculation """ b = SquareButterfly((0, 0), 2) - xbounds, ybounds = b.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = b.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) def test_squarebutterfly_conversion(): @@ -1282,9 +1282,9 @@ def test_donut_ctor_validation(): def test_donut_bounds(): d = Donut((0, 0), "round", 0.0, 2.0) - xbounds, ybounds = d.bounding_box - assert xbounds == (-1.0, 1.0) - assert ybounds == (-1.0, 1.0) + bounds = d.bounding_box + assert bounds[0] == (-1.0, -1.0) + assert bounds[1] == (1.0, 1.0) def test_donut_conversion(): @@ -1355,13 +1355,13 @@ def test_drill_ctor_validation(): def test_drill_bounds(): d = Drill((0, 0), 2) - xbounds, ybounds = d.bounding_box - pytest.approx(xbounds, (-1, 1)) - pytest.approx(ybounds, (-1, 1)) + bounds = d.bounding_box + pytest.approx(bounds[0], (-1, -1)) + pytest.approx(bounds[1], (1, 1)) d = Drill((1, 2), 2) - xbounds, ybounds = d.bounding_box - pytest.approx(xbounds, (0, 2)) - pytest.approx(ybounds, (1, 3)) + bounds = d.bounding_box + pytest.approx(bounds[0], (0, 1)) + pytest.approx(bounds[1], (2, 3)) def test_drill_conversion(): @@ -1418,12 +1418,13 @@ def test_slot_bounds(): """ Test Slot primitive bounding box calculation """ cases = [ - ((0, 0), (1, 1), ((-1, 2), (-1, 2))), - ((-1, -1), (1, 1), ((-2, 2), (-2, 2))), - ((1, 1), (-1, -1), ((-2, 2), (-2, 2))), - ((-1, 1), (1, -1), ((-2, 2), (-2, 2))), + (( 0, 0), ( 1, 1), ((-1, -1), (2, 2))), + ((-1, -1), ( 1, 1), ((-2, -2), (2, 2))), + (( 1, 1), (-1, -1), ((-2, -2), (2, 2))), + ((-1, 1), ( 1, -1), ((-2, -2), (2, 2))), ] for start, end, expected in cases: s = Slot(start, end, 2.0) assert s.bounding_box == expected + diff --git a/gerbonara/gerber/tests/test_rs274x.py b/gerbonara/gerber/tests/test_rs274x.py index e7baf11..e430f36 100644 --- a/gerbonara/gerber/tests/test_rs274x.py +++ b/gerbonara/gerber/tests/test_rs274x.py @@ -23,7 +23,7 @@ def test_read(): def test_multiline_read(): multiline = read(MULTILINE_READ_FILE) assert isinstance(multiline, GerberFile) - assert 10 == len(multiline.statements) + assert 11 == len(multiline.statements) def test_comments_parameter(): -- cgit