From e34e1078b67f43be9b678a67cf30d3c53fdea171 Mon Sep 17 00:00:00 2001 From: Hamilton Kibbe Date: Sun, 26 Apr 2015 02:58:12 -0400 Subject: Refactor primitive unit conversion and add regression coverage to tests --- gerber/primitives.py | 360 ++++++++++++---------------------------- gerber/tests/test_primitives.py | 334 ++++++++++++++++++++++++++++++++++--- 2 files changed, 414 insertions(+), 280 deletions(-) (limited to 'gerber') diff --git a/gerber/primitives.py b/gerber/primitives.py index 4c027d2..bdd49f7 100644 --- a/gerber/primitives.py +++ b/gerber/primitives.py @@ -40,6 +40,7 @@ class Primitive(object): self.level_polarity = level_polarity self.rotation = rotation self.units = units + self._to_convert = list() def bounding_box(self): """ Calculate bounding box @@ -52,10 +53,39 @@ class Primitive(object): 'implemented in subclass') def to_inch(self): - pass + if self.units == 'metric': + self.units = 'inch' + for attr, value in [(attr, getattr(self, attr)) for attr in self._to_convert]: + if hasattr(value, 'to_inch'): + value.to_inch() + else: + try: + if len(value) > 1: + if isinstance(value[0], tuple): + setattr(self, attr, [tuple(map(inch, point)) for point in value]) + else: + setattr(self, attr, tuple(map(inch, value))) + except: + if value is not None: + setattr(self, attr, inch(value)) + def to_metric(self): - pass + if self.units == 'inch': + self.units = 'metric' + for attr, value in [(attr, getattr(self, attr)) for attr in self._to_convert]: + if hasattr(value, 'to_metric'): + value.to_metric() + else: + try: + if len(value) > 1: + if isinstance(value[0], tuple): + setattr(self, attr, [tuple(map(metric, point)) for point in value]) + else: + setattr(self, attr, tuple(map(metric, value))) + except: + if value is not None: + setattr(self, attr, metric(value)) def offset(self, x_offset=0, y_offset=0): pass @@ -72,6 +102,7 @@ class Line(Primitive): self.start = start self.end = end self.aperture = aperture + self._to_convert = ['start', 'end', 'aperture'] @property def angle(self): @@ -141,20 +172,6 @@ class Line(Primitive): return (start_ll, start_lr, start_ur, end_ur, end_ul, end_ll) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.aperture.to_inch() - self.start = tuple(map(inch, self.start)) - self.end = tuple(map(inch, self.end)) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.aperture.to_metric() - self.start = tuple(map(metric, self.start)) - self.end = tuple(map(metric, self.end)) - def offset(self, x_offset=0, y_offset=0): self.start = tuple(map(add, self.start, (x_offset, y_offset))) self.end = tuple(map(add, self.end, (x_offset, y_offset))) @@ -170,6 +187,7 @@ class Arc(Primitive): self.center = center self.direction = direction self.aperture = aperture + self._to_convert = ['start', 'end', 'center', 'aperture'] @property def radius(self): @@ -236,22 +254,6 @@ class Arc(Primitive): max_y = max(y) + self.aperture.radius return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.aperture.to_inch() - self.start = tuple(map(inch, self.start)) - self.end = tuple(map(inch, self.end)) - self.center = tuple(map(inch, self.center)) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.aperture.to_metric() - self.start = tuple(map(metric, self.start)) - self.end = tuple(map(metric, self.end)) - self.center = tuple(map(metric, self.center)) - def offset(self, x_offset=0, y_offset=0): self.start = tuple(map(add, self.start, (x_offset, y_offset))) self.end = tuple(map(add, self.end, (x_offset, y_offset))) @@ -266,6 +268,7 @@ class Circle(Primitive): validate_coordinates(position) self.position = position self.diameter = diameter + self._to_convert = ['position', 'diameter'] @property def radius(self): @@ -279,20 +282,6 @@ class Circle(Primitive): max_y = self.position[1] + self.radius return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - if self.position is not None: - self.position = tuple(map(inch, self.position)) - self.diameter = inch(self.diameter) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - if self.position is not None: - self.position = tuple(map(metric, self.position)) - self.diameter = metric(self.diameter) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -306,13 +295,8 @@ class Ellipse(Primitive): self.position = position self.width = width self.height = height - # Axis-aligned width and height - ux = (self.width / 2.) * math.cos(math.radians(self.rotation)) - uy = (self.width / 2.) * math.sin(math.radians(self.rotation)) - vx = (self.height / 2.) * math.cos(math.radians(self.rotation) + (math.pi / 2.)) - vy = (self.height / 2.) * math.sin(math.radians(self.rotation) + (math.pi / 2.)) - self._abs_width = 2 * math.sqrt((ux * ux) + (vx * vx)) - self._abs_height = 2 * math.sqrt((uy * uy) + (vy * vy)) + self._to_convert = ['position', 'width', 'height'] + @property def bounding_box(self): @@ -322,23 +306,21 @@ class Ellipse(Primitive): max_y = self.position[1] + (self._abs_height / 2.0) return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + ux = (self.width / 2.) * math.cos(math.radians(self.rotation)) + vx = (self.height / 2.) * math.cos(math.radians(self.rotation) + (math.pi / 2.)) + return 2 * math.sqrt((ux * ux) + (vx * vx)) + + @property + def _abs_height(self): + uy = (self.width / 2.) * math.sin(math.radians(self.rotation)) + vy = (self.height / 2.) * math.sin(math.radians(self.rotation) + (math.pi / 2.)) + return 2 * math.sqrt((uy * uy) + (vy * vy)) + class Rectangle(Primitive): """ @@ -349,11 +331,8 @@ class Rectangle(Primitive): self.position = position self.width = width self.height = height - # Axis-aligned width and height - self._abs_width = (math.cos(math.radians(self.rotation)) * self.width + - math.sin(math.radians(self.rotation)) * self.height) - self._abs_height = (math.cos(math.radians(self.rotation)) * self.height + - math.sin(math.radians(self.rotation)) * self.width) + self._to_convert = ['position', 'width', 'height'] + @property def lower_left(self): @@ -373,23 +352,18 @@ class Rectangle(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + return (math.cos(math.radians(self.rotation)) * self.width + + math.sin(math.radians(self.rotation)) * self.height) + @property + def _abs_height(self): + return (math.cos(math.radians(self.rotation)) * self.height + + math.sin(math.radians(self.rotation)) * self.width) + class Diamond(Primitive): """ @@ -400,11 +374,7 @@ class Diamond(Primitive): self.position = position self.width = width self.height = height - # Axis-aligned width and height - self._abs_width = (math.cos(math.radians(self.rotation)) * self.width + - math.sin(math.radians(self.rotation)) * self.height) - self._abs_height = (math.cos(math.radians(self.rotation)) * self.height + - math.sin(math.radians(self.rotation)) * self.width) + self._to_convert = ['position', 'width', 'height'] @property def lower_left(self): @@ -424,23 +394,18 @@ class Diamond(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + return (math.cos(math.radians(self.rotation)) * self.width + + math.sin(math.radians(self.rotation)) * self.height) + @property + def _abs_height(self): + return (math.cos(math.radians(self.rotation)) * self.height + + math.sin(math.radians(self.rotation)) * self.width) + class ChamferRectangle(Primitive): """ @@ -453,11 +418,7 @@ class ChamferRectangle(Primitive): self.height = height self.chamfer = chamfer self.corners = corners - # Axis-aligned width and height - self._abs_width = (math.cos(math.radians(self.rotation)) * self.width + - math.sin(math.radians(self.rotation)) * self.height) - self._abs_height = (math.cos(math.radians(self.rotation)) * self.height + - math.sin(math.radians(self.rotation)) * self.width) + self._to_convert = ['position', 'width', 'height', 'chamfer'] @property def lower_left(self): @@ -477,25 +438,17 @@ class ChamferRectangle(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - self.chamfer = inch(self.chamfer) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - self.chamfer = metric(self.chamfer) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + return (math.cos(math.radians(self.rotation)) * self.width + + math.sin(math.radians(self.rotation)) * self.height) + @property + def _abs_height(self): + return (math.cos(math.radians(self.rotation)) * self.height + + math.sin(math.radians(self.rotation)) * self.width) class RoundRectangle(Primitive): """ @@ -508,11 +461,7 @@ class RoundRectangle(Primitive): self.height = height self.radius = radius self.corners = corners - # Axis-aligned width and height - self._abs_width = (math.cos(math.radians(self.rotation)) * self.width + - math.sin(math.radians(self.rotation)) * self.height) - self._abs_height = (math.cos(math.radians(self.rotation)) * self.height + - math.sin(math.radians(self.rotation)) * self.width) + self._to_convert = ['position', 'width', 'height', 'radius'] @property def lower_left(self): @@ -532,25 +481,17 @@ class RoundRectangle(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - self.radius = inch(self.radius) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - self.radius = metric(self.radius) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + return (math.cos(math.radians(self.rotation)) * self.width + + math.sin(math.radians(self.rotation)) * self.height) + @property + def _abs_height(self): + return (math.cos(math.radians(self.rotation)) * self.height + + math.sin(math.radians(self.rotation)) * self.width) class Obround(Primitive): """ @@ -561,11 +502,7 @@ class Obround(Primitive): self.position = position self.width = width self.height = height - # Axis-aligned width and height - self._abs_width = (math.cos(math.radians(self.rotation)) * self.width + - math.sin(math.radians(self.rotation)) * self.height) - self._abs_height = (math.cos(math.radians(self.rotation)) * self.height + - math.sin(math.radians(self.rotation)) * self.width) + self._to_convert = ['position', 'width', 'height'] @property def lower_left(self): @@ -607,23 +544,17 @@ class Obround(Primitive): self.height) return {'circle1': circle1, 'circle2': circle2, 'rectangle': rect} - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) + @property + def _abs_width(self): + return (math.cos(math.radians(self.rotation)) * self.width + + math.sin(math.radians(self.rotation)) * self.height) + @property + def _abs_height(self): + return (math.cos(math.radians(self.rotation)) * self.height + + math.sin(math.radians(self.rotation)) * self.width) class Polygon(Primitive): """ @@ -634,6 +565,7 @@ class Polygon(Primitive): self.position = position self.sides = sides self.radius = radius + self._to_convert = ['position', 'radius'] @property def bounding_box(self): @@ -643,18 +575,6 @@ class Polygon(Primitive): max_y = self.position[1] + self.radius return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.radius = inch(self.radius) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.radius = metric(self.radius) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -665,6 +585,7 @@ class Region(Primitive): def __init__(self, points, **kwargs): super(Region, self).__init__(**kwargs) self.points = points + self._to_convert = ['points'] @property def bounding_box(self): @@ -675,16 +596,6 @@ class Region(Primitive): max_y = max(y_list) return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.points = [tuple(map(inch, point)) for point in self.points] - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.points = [tuple(map(metric, point)) for point in self.points] - def offset(self, x_offset=0, y_offset=0): self.points = [tuple(map(add, point, (x_offset, y_offset))) for point in self.points] @@ -698,6 +609,7 @@ class RoundButterfly(Primitive): validate_coordinates(position) self.position = position self.diameter = diameter + self._to_convert = ['position', 'diameter'] @property def radius(self): @@ -711,18 +623,6 @@ class RoundButterfly(Primitive): max_y = self.position[1] + self.radius return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.diameter = inch(self.diameter) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.diameter = metric(self.diameter) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -735,6 +635,7 @@ class SquareButterfly(Primitive): validate_coordinates(position) self.position = position self.side = side + self._to_convert = ['position', 'side'] @property @@ -745,18 +646,6 @@ class SquareButterfly(Primitive): max_y = self.position[1] + (self.side / 2.) return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.side = inch(self.side) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.side = metric(self.side) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -782,6 +671,7 @@ class Donut(Primitive): # Hexagon self.width = 0.5 * math.sqrt(3.) * outer_diameter self.height = outer_diameter + self._to_convert = ['position', 'width', 'height', 'inner_diameter', 'outer_diameter'] @property def lower_left(self): @@ -801,24 +691,6 @@ class Donut(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.width = inch(self.width) - self.height = inch(self.height) - self.inner_diameter = inch(self.inner_diameter) - self.outer_diameter = inch(self.outer_diameter) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.width = metric(self.width) - self.height = metric(self.height) - self.inner_diameter = metric(self.inner_diameter) - self.outer_diameter = metric(self.outer_diameter) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -834,6 +706,7 @@ class SquareRoundDonut(Primitive): raise ValueError('Outer diameter must be larger than inner diameter.') self.inner_diameter = inner_diameter self.outer_diameter = outer_diameter + self._to_convert = ['position', 'inner_diameter', 'outer_diameter'] @property def lower_left(self): @@ -851,20 +724,6 @@ class SquareRoundDonut(Primitive): max_y = self.upper_right[1] return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.inner_diameter = inch(self.inner_diameter) - self.outer_diameter = inch(self.outer_diameter) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.inner_diameter = metric(self.inner_diameter) - self.outer_diameter = metric(self.outer_diameter) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) @@ -877,6 +736,7 @@ class Drill(Primitive): validate_coordinates(position) self.position = position self.diameter = diameter + self._to_convert = ['position', 'diameter'] @property def radius(self): @@ -890,18 +750,6 @@ class Drill(Primitive): max_y = self.position[1] + self.radius return ((min_x, max_x), (min_y, max_y)) - def to_inch(self): - if self.units == 'metric': - self.units = 'inch' - self.position = tuple(map(inch, self.position)) - self.diameter = inch(self.diameter) - - def to_metric(self): - if self.units == 'inch': - self.units = 'metric' - self.position = tuple(map(metric, self.position)) - self.diameter = metric(self.diameter) - def offset(self, x_offset=0, y_offset=0): self.position = tuple(map(add, self.position, (x_offset, y_offset))) diff --git a/gerber/tests/test_primitives.py b/gerber/tests/test_primitives.py index c438735..67c7822 100644 --- a/gerber/tests/test_primitives.py +++ b/gerber/tests/test_primitives.py @@ -77,18 +77,18 @@ def test_line_vertices(): def test_line_conversion(): c = Circle((0, 0), 25.4, units='metric') l = Line((2.54, 25.4), (254.0, 2540.0), c, units='metric') - + # No effect l.to_metric() assert_equal(l.start, (2.54, 25.4)) assert_equal(l.end, (254.0, 2540.0)) assert_equal(l.aperture.diameter, 25.4) - + l.to_inch() assert_equal(l.start, (0.1, 1.0)) assert_equal(l.end, (10.0, 100.0)) assert_equal(l.aperture.diameter, 1.0) - + # No effect l.to_inch() assert_equal(l.start, (0.1, 1.0)) @@ -97,19 +97,19 @@ def test_line_conversion(): c = Circle((0, 0), 1.0, units='inch') l = Line((0.1, 1.0), (10.0, 100.0), c, units='inch') - + # No effect l.to_inch() assert_equal(l.start, (0.1, 1.0)) assert_equal(l.end, (10.0, 100.0)) assert_equal(l.aperture.diameter, 1.0) - - + + l.to_metric() assert_equal(l.start, (2.54, 25.4)) assert_equal(l.end, (254.0, 2540.0)) assert_equal(l.aperture.diameter, 25.4) - + #No effect l.to_metric() assert_equal(l.start, (2.54, 25.4)) @@ -180,6 +180,21 @@ def test_arc_bounds(): def test_arc_conversion(): c = Circle((0, 0), 25.4, units='metric') a = Arc((2.54, 25.4), (254.0, 2540.0), (25400.0, 254000.0),'clockwise', c, units='metric') + + #No effect + a.to_metric() + assert_equal(a.start, (2.54, 25.4)) + assert_equal(a.end, (254.0, 2540.0)) + assert_equal(a.center, (25400.0, 254000.0)) + assert_equal(a.aperture.diameter, 25.4) + + a.to_inch() + assert_equal(a.start, (0.1, 1.0)) + assert_equal(a.end, (10.0, 100.0)) + assert_equal(a.center, (1000.0, 10000.0)) + assert_equal(a.aperture.diameter, 1.0) + + #no effect a.to_inch() assert_equal(a.start, (0.1, 1.0)) assert_equal(a.end, (10.0, 100.0)) @@ -220,14 +235,31 @@ def test_circle_bounds(): def test_circle_conversion(): c = Circle((2.54, 25.4), 254.0, units='metric') + c.to_metric() #shouldn't do antyhing + assert_equal(c.position, (2.54, 25.4)) + assert_equal(c.diameter, 254.) + + c.to_inch() + assert_equal(c.position, (0.1, 1.)) + assert_equal(c.diameter, 10.) + + #no effect c.to_inch() - c.to_inch() #shouldn't do anything assert_equal(c.position, (0.1, 1.)) assert_equal(c.diameter, 10.) + c = Circle((0.1, 1.0), 10.0, units='inch') + #No effect c.to_inch() + assert_equal(c.position, (0.1, 1.)) + assert_equal(c.diameter, 10.) + c.to_metric() + assert_equal(c.position, (2.54, 25.4)) + assert_equal(c.diameter, 254.) + + #no effect c.to_metric() assert_equal(c.position, (2.54, 25.4)) assert_equal(c.diameter, 254.) @@ -261,16 +293,38 @@ def test_ellipse_bounds(): def test_ellipse_conversion(): e = Ellipse((2.54, 25.4), 254.0, 2540., units='metric') + + #No effect e.to_metric() + assert_equal(e.position, (2.54, 25.4)) + assert_equal(e.width, 254.) + assert_equal(e.height, 2540.) + e.to_inch() + assert_equal(e.position, (0.1, 1.)) + assert_equal(e.width, 10.) + assert_equal(e.height, 100.) + + #No effect e.to_inch() assert_equal(e.position, (0.1, 1.)) assert_equal(e.width, 10.) assert_equal(e.height, 100.) e = Ellipse((0.1, 1.), 10.0, 100., units='inch') + + #no effect e.to_inch() + assert_equal(e.position, (0.1, 1.)) + assert_equal(e.width, 10.) + assert_equal(e.height, 100.) + e.to_metric() + assert_equal(e.position, (2.54, 25.4)) + assert_equal(e.width, 254.) + assert_equal(e.height, 2540.) + + # No effect e.to_metric() assert_equal(e.position, (2.54, 25.4)) assert_equal(e.width, 254.) @@ -307,15 +361,33 @@ def test_rectangle_bounds(): def test_rectangle_conversion(): r = Rectangle((2.54, 25.4), 254.0, 2540.0, units='metric') + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + r.to_inch() assert_equal(r.position, (0.1, 1.0)) assert_equal(r.width, 10.0) assert_equal(r.height, 100.0) + r = Rectangle((0.1, 1.0), 10.0, 100.0, units='inch') r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + r.to_metric() assert_equal(r.position, (2.54,25.4)) assert_equal(r.width, 254.0) @@ -352,12 +424,34 @@ def test_diamond_bounds(): def test_diamond_conversion(): d = Diamond((2.54, 25.4), 254.0, 2540.0, units='metric') + + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.width, 254.0) + assert_equal(d.height, 2540.0) + + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.width, 10.0) + assert_equal(d.height, 100.0) + d.to_inch() assert_equal(d.position, (0.1, 1.0)) assert_equal(d.width, 10.0) assert_equal(d.height, 100.0) d = Diamond((0.1, 1.0), 10.0, 100.0, units='inch') + + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.width, 10.0) + assert_equal(d.height, 100.0) + + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.width, 254.0) + assert_equal(d.height, 2540.0) + d.to_metric() assert_equal(d.position, (2.54, 25.4)) assert_equal(d.width, 254.0) @@ -398,6 +492,19 @@ def test_chamfer_rectangle_bounds(): def test_chamfer_rectangle_conversion(): r = ChamferRectangle((2.54, 25.4), 254.0, 2540.0, 0.254, (True, True, False, False), units='metric') + + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + assert_equal(r.chamfer, 0.254) + + r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + assert_equal(r.chamfer, 0.01) + r.to_inch() assert_equal(r.position, (0.1, 1.0)) assert_equal(r.width, 10.0) @@ -405,6 +512,18 @@ def test_chamfer_rectangle_conversion(): assert_equal(r.chamfer, 0.01) r = ChamferRectangle((0.1, 1.0), 10.0, 100.0, 0.01, (True, True, False, False), units='inch') + r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + assert_equal(r.chamfer, 0.01) + + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + assert_equal(r.chamfer, 0.254) + r.to_metric() assert_equal(r.position, (2.54,25.4)) assert_equal(r.width, 254.0) @@ -446,6 +565,19 @@ def test_round_rectangle_bounds(): def test_round_rectangle_conversion(): r = RoundRectangle((2.54, 25.4), 254.0, 2540.0, 0.254, (True, True, False, False), units='metric') + + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + assert_equal(r.radius, 0.254) + + r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + assert_equal(r.radius, 0.01) + r.to_inch() assert_equal(r.position, (0.1, 1.0)) assert_equal(r.width, 10.0) @@ -453,6 +585,19 @@ def test_round_rectangle_conversion(): assert_equal(r.radius, 0.01) r = RoundRectangle((0.1, 1.0), 10.0, 100.0, 0.01, (True, True, False, False), units='inch') + + r.to_inch() + assert_equal(r.position, (0.1, 1.0)) + assert_equal(r.width, 10.0) + assert_equal(r.height, 100.0) + assert_equal(r.radius, 0.01) + + r.to_metric() + assert_equal(r.position, (2.54,25.4)) + assert_equal(r.width, 254.0) + assert_equal(r.height, 2540.0) + assert_equal(r.radius, 0.254) + r.to_metric() assert_equal(r.position, (2.54,25.4)) assert_equal(r.width, 254.0) @@ -510,12 +655,38 @@ def test_obround_subshapes(): def test_obround_conversion(): o = Obround((2.54,25.4), 254.0, 2540.0, units='metric') + + #No effect + o.to_metric() + assert_equal(o.position, (2.54, 25.4)) + assert_equal(o.width, 254.0) + assert_equal(o.height, 2540.0) + + o.to_inch() + assert_equal(o.position, (0.1, 1.0)) + assert_equal(o.width, 10.0) + assert_equal(o.height, 100.0) + + #No effect o.to_inch() assert_equal(o.position, (0.1, 1.0)) assert_equal(o.width, 10.0) assert_equal(o.height, 100.0) o= Obround((0.1, 1.0), 10.0, 100.0, units='inch') + + #No effect + o.to_inch() + assert_equal(o.position, (0.1, 1.0)) + assert_equal(o.width, 10.0) + assert_equal(o.height, 100.0) + + o.to_metric() + assert_equal(o.position, (2.54, 25.4)) + assert_equal(o.width, 254.0) + assert_equal(o.height, 2540.0) + + #No effect o.to_metric() assert_equal(o.position, (2.54, 25.4)) assert_equal(o.width, 254.0) @@ -554,11 +725,33 @@ def test_polygon_bounds(): def test_polygon_conversion(): p = Polygon((2.54, 25.4), 3, 254.0, units='metric') + + #No effect + p.to_metric() + assert_equal(p.position, (2.54, 25.4)) + assert_equal(p.radius, 254.0) + + p.to_inch() + assert_equal(p.position, (0.1, 1.0)) + assert_equal(p.radius, 10.0) + + #No effect p.to_inch() assert_equal(p.position, (0.1, 1.0)) assert_equal(p.radius, 10.0) p = Polygon((0.1, 1.0), 3, 10.0, units='inch') + + #No effect + p.to_inch() + assert_equal(p.position, (0.1, 1.0)) + assert_equal(p.radius, 10.0) + + p.to_metric() + assert_equal(p.position, (2.54, 25.4)) + assert_equal(p.radius, 254.0) + + #No effect p.to_metric() assert_equal(p.position, (2.54, 25.4)) assert_equal(p.radius, 254.0) @@ -623,15 +816,37 @@ def test_round_butterfly_ctor_validation(): assert_raises(TypeError, RoundButterfly, (3,4,5), 5) def test_round_butterfly_conversion(): - b = RoundButterfly((2.54, 25.4), 254.0, units='metric') - b.to_inch() - assert_equal(b.position, (0.1, 1.0)) - assert_equal(b.diameter, 10.0) + b = RoundButterfly((2.54, 25.4), 254.0, units='metric') + + #No Effect + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.diameter, (254.0)) + + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.diameter, 10.0) + + #No effect + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.diameter, 10.0) - b = RoundButterfly((0.1, 1.0), 10.0, units='inch') - b.to_metric() - assert_equal(b.position, (2.54, 25.4)) - assert_equal(b.diameter, (254.0)) + b = RoundButterfly((0.1, 1.0), 10.0, units='inch') + + #No effect + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.diameter, 10.0) + + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.diameter, (254.0)) + + #No Effect + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.diameter, (254.0)) def test_round_butterfly_offset(): b = RoundButterfly((0, 0), 1) @@ -672,15 +887,37 @@ def test_square_butterfly_bounds(): assert_array_almost_equal(ybounds, (-1, 1)) def test_squarebutterfly_conversion(): - b = SquareButterfly((2.54, 25.4), 254.0, units='metric') - b.to_inch() - assert_equal(b.position, (0.1, 1.0)) - assert_equal(b.side, 10.0) + b = SquareButterfly((2.54, 25.4), 254.0, units='metric') + + #No effect + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.side, (254.0)) + + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.side, 10.0) + + #No effect + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.side, 10.0) - b = SquareButterfly((0.1, 1.0), 10.0, units='inch') - b.to_metric() - assert_equal(b.position, (2.54, 25.4)) - assert_equal(b.side, (254.0)) + b = SquareButterfly((0.1, 1.0), 10.0, units='inch') + + #No effect + b.to_inch() + assert_equal(b.position, (0.1, 1.0)) + assert_equal(b.side, 10.0) + + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.side, (254.0)) + + #No effect + b.to_metric() + assert_equal(b.position, (2.54, 25.4)) + assert_equal(b.side, (254.0)) def test_square_butterfly_offset(): b = SquareButterfly((0, 0), 1) @@ -717,12 +954,38 @@ def test_donut_bounds(): def test_donut_conversion(): d = Donut((2.54, 25.4), 'round', 254.0, 2540.0, units='metric') + + #No effect + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.inner_diameter, 254.0) + assert_equal(d.outer_diameter, 2540.0) + + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.inner_diameter, 10.0) + assert_equal(d.outer_diameter, 100.0) + + #No effect d.to_inch() assert_equal(d.position, (0.1, 1.0)) assert_equal(d.inner_diameter, 10.0) assert_equal(d.outer_diameter, 100.0) d = Donut((0.1, 1.0), 'round', 10.0, 100.0, units='inch') + + #No effect + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.inner_diameter, 10.0) + assert_equal(d.outer_diameter, 100.0) + + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.inner_diameter, 254.0) + assert_equal(d.outer_diameter, 2540.0) + + #No effect d.to_metric() assert_equal(d.position, (2.54, 25.4)) assert_equal(d.inner_diameter, 254.0) @@ -763,11 +1026,34 @@ def test_drill_bounds(): def test_drill_conversion(): d = Drill((2.54, 25.4), 254., units='metric') + + #No effect + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.diameter, 254.0) + d.to_inch() assert_equal(d.position, (0.1, 1.0)) assert_equal(d.diameter, 10.0) + + #No effect + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.diameter, 10.0) + d = Drill((0.1, 1.0), 10., units='inch') + + #No effect + d.to_inch() + assert_equal(d.position, (0.1, 1.0)) + assert_equal(d.diameter, 10.0) + + d.to_metric() + assert_equal(d.position, (2.54, 25.4)) + assert_equal(d.diameter, 254.0) + + #No effect d.to_metric() assert_equal(d.position, (2.54, 25.4)) assert_equal(d.diameter, 254.0) -- cgit