def make_shapes_for_poly(self, poly): """ Generate openjson shapes for an eaglexml polygon. """ # TODO: handle curves opoly = Polygon() for vertex in poly.vertex: opoly.add_point(self.make_length(vertex.x), self.make_length(vertex.y)) yield opoly
def parse_shape(self, shape): """ Extract a shape. """ # pylint: disable=R0914 # pylint: disable=R0911 typ = shape.get('type') if 'rectangle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) return Rectangle(x, y, width, height) elif 'rounded_rectangle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) radius = int(shape.get('radius')) return RoundedRectangle(x, y, width, height, radius) elif 'arc' == typ: x = int(shape.get('x')) y = int(shape.get('y')) start_angle = float(shape.get('start_angle')) end_angle = float(shape.get('end_angle')) radius = int(shape.get('radius')) return Arc(x, y, start_angle, end_angle, radius) elif 'circle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) radius = int(shape.get('radius')) return Circle(x, y, radius) elif 'label' == typ: x = int(shape.get('x')) y = int(shape.get('y')) rotation = float(shape.get('rotation')) text = shape.get('text') align = shape.get('align') return Label(x, y, text, align, rotation) elif 'line' == typ: p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) return Line(p1, p2) elif 'polygon' == typ: poly = Polygon() for point in shape.get('points'): poly.add_point(self.parse_point(point)) return poly elif 'bezier' == typ: control1 = self.parse_point(shape.get('control1')) control2 = self.parse_point(shape.get('control2')) p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) return BezierCurve(control1, control2, p1, p2)
def test_polygon(self): """ Polygons are output correctly. """ writer = KiCAD() poly = Polygon() poly.add_point(0, 0) poly.add_point(0, 10) poly.add_point(10, 10) poly.add_point(10, 0) line = writer.get_shape_line(poly) self.assertEqual( line, 'P 5 %(unit)d %(convert)d 0 0 0 0 111 111 111 111 0 0 0 N\n')
def test_polygon(self): """ Convert polygon shape """ poly = Polygon() poly.add_point(0, 0) poly.add_point(0, 10) poly.add_point(10, 10) poly.add_point(10, 0) writer = Specctra() obj = writer._convert_shape(poly) self.assertEqual( to_string(writer, obj), '( (polygon signal 10.416667 0.000000 0.000000 0.000000 104.166667 104.166667 104.166667 104.166667 0.000000) )' )
def parse_polygon(self, poly): """ Parse a polygon element """ shape = Polygon() for point in poly.get('points', '').split(): if point: x, y = point.split(',') shape.add_point(make_x(x, self.svg_mult), make_y(y, self.svg_mult)) if shape.points: shape.add_point(shape.points[0].x, shape.points[0].y) return [shape]
def test_polygon(self): """ Convert polygon shape """ poly = Polygon() poly.add_point(0, 0) poly.add_point(0, 10) poly.add_point(10, 10) poly.add_point(10, 0) writer = Specctra() obj = writer._convert_shape(poly) self.assertEqual( to_string(writer, obj), '( (polygon signal 10.416667 0.000000 0.000000 0.000000 104.166667 104.166667 104.166667 104.166667 0.000000) )')
def test_polygon(self): """ Polygons are output correctly. """ writer = KiCAD() poly = Polygon() poly.add_point(0, 0) poly.add_point(0, 10) poly.add_point(10, 10) poly.add_point(10, 0) line = writer.get_shape_line(poly) self.assertEqual(line, 'P 5 %(unit)d %(convert)d 0 0 0 0 111 111 111 111 0 0 0 N\n')
def _convert_shapes(self, shapes, center=(0, 0), absolute=False): """ Convert shapes """ result = [] def fix_point(point): x, y = (point[0] + center[0], point[1] + center[1]) if absolute: # freerouter often creates points outside boundary, fix it if x > self.max_x: x = self.min_x + x - self.max_x elif x < self.min_x: x = self.max_x - x - self.min_x if y > self.max_y: y = self.min_y + y - self.max_y elif y < self.min_y: y = self.max_y - y - self.min_y return (x, y) for shape in shapes: if isinstance(shape, specctraobj.PolylinePath): points = [ fix_point(self.to_pixels(point)) for point in shape.vertex ] result.extend( self._convert_path(self.to_pixels(shape.aperture_width), points)) elif isinstance(shape, specctraobj.Path): points = [ fix_point(self.to_pixels(point)) for point in shape.vertex ] # Path has connected start and end points if points[0] != points[-1] and len(points) != 2: points.append(points[0]) result.extend( self._convert_path(self.to_pixels(shape.aperture_width), points)) elif isinstance(shape, specctraobj.Polygon): points = [ fix_point(self.to_pixels(point)) for point in shape.vertex ] points = [Point(point[0], point[1]) for point in points] result.append(Polygon(points)) elif isinstance(shape, specctraobj.Rectangle): x1, y1 = self.to_pixels(shape.vertex1) x2, y2 = self.to_pixels(shape.vertex2) width, height = abs(x1 - x2), abs(y1 - y2) x1, y1 = fix_point((min(x1, x2), max(y1, y2))) result.append(Rectangle(x1, y1, width, height)) elif isinstance(shape, specctraobj.Circle): point = fix_point(self.to_pixels(shape.vertex)) result.append( Circle(point[0], point[1], self.to_pixels(shape.diameter / 2.0))) return result
def test_polygon(self): """ Convert polygon shape """ poly = Polygon() poly.add_point(0, 0) poly.add_point(0, 10) poly.add_point(10, 10) poly.add_point(10, 0) writer = Specctra() writer.resolution = specctraobj.Resolution() writer.resolution.unit = 'mil' writer.resolution.resolution = 10 obj = writer._convert_shape(poly) self.assertEqual( to_string(writer, obj), '( (polygon signal 10.416667 0.000000 0.000000 0.000000 104.166667 104.166667' + ' 104.166667 104.166667 0.000000) )')
def parse_shape(self, shape): """ Extract a shape. """ # pylint: disable=R0914 # pylint: disable=R0911 typ = shape.get('type') if 'rectangle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) parsed_shape = Rectangle(x, y, width, height) elif 'rounded_rectangle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) radius = int(shape.get('radius')) parsed_shape = RoundedRectangle(x, y, width, height, radius) elif 'arc' == typ: x = int(shape.get('x')) y = int(shape.get('y')) start_angle = float(shape.get('start_angle')) end_angle = float(shape.get('end_angle')) radius = int(shape.get('radius')) parsed_shape = Arc(x, y, start_angle, end_angle, radius) elif 'circle' == typ: x = int(shape.get('x')) y = int(shape.get('y')) radius = int(shape.get('radius')) parsed_shape = Circle(x, y, radius) elif 'label' == typ: x = int(shape.get('x')) y = int(shape.get('y')) rotation = float(shape.get('rotation')) text = shape.get('text') align = shape.get('align') parsed_shape = Label(x, y, text, align, rotation) elif 'line' == typ: p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) parsed_shape = Line(p1, p2) elif 'polygon' == typ: parsed_shape = Polygon() for point in shape.get('points'): parsed_shape.add_point(self.parse_point(point)) elif 'bezier' == typ: control1 = self.parse_point(shape.get('control1')) control2 = self.parse_point(shape.get('control2')) p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) parsed_shape = BezierCurve(control1, control2, p1, p2) parsed_shape.styles = shape.get('styles') or {} parsed_shape.attributes = shape.get('attributes') or {} return parsed_shape
def instantiate(self, values): """ Return a core.layout.Primitive with a set of fixed shapes given a dict mapping variable numbers to values, or None if there is no corresponding shape. """ shape_type = self.shape_type mods = [m.evaluate(values) for m in self.modifiers] if shape_type == 'ignore': return None is_additive = True if shape_type in ('moire', 'thermal') \ else bool(mods[0]) rotation = 0 if shape_type in ('circle', 'moire', 'thermal') \ else mods[-1]/180 if shape_type == 'circle': shape = Circle(x=mods[2], y=mods[3], radius=mods[1] / 2) elif shape_type == 'line-vector': shape, rotation = self._vector_to_rect(mods, rotation) elif shape_type == 'line-center': shape = Rectangle(x=mods[3] - mods[1] / 2, y=mods[4] + mods[2] / 2, width=mods[1], height=mods[2]) elif shape_type == 'line-lower-left': shape = Rectangle(x=mods[3], y=mods[4] + mods[2], width=mods[1], height=mods[2]) elif shape_type == 'outline': points = [ Point(mods[i], mods[i + 1]) for i in range(2, len(mods[:-1]), 2) ] shape = Polygon(points) elif shape_type == 'polygon': shape = RegularPolygon(x=mods[2], y=mods[3], outer=mods[4], vertices=mods[1]) elif shape_type == 'moire': mods[8] = 2 - mods[8] / 180 shape = Moire(*mods[0:9]) elif shape_type == 'thermal': mods[5] = 2 - mods[5] / 180 shape = Thermal(*mods[0:6]) return Primitive(is_additive, rotation, shape)
class PolygonTests(unittest.TestCase): """ The tests of the core module polygon shape """ def setUp(self): """ Setup the test case. """ self.poly = Polygon() def tearDown(self): """ Teardown the test case. """ del self.poly def test_create_new_polygon(self): """ Test the creation of a new empty polygon. """ assert len(self.poly.points) == 0 def test_empty_min_point(self): '''Test Polygon.min_point() for a Polygon with no points at all''' self.assertEqual(self.poly.min_point().x, 0) self.assertEqual(self.poly.min_point().y, 0) def test_empty_max_point(self): '''Test Polygon.max_point() for a Polygon with no points at all''' self.assertEqual(self.poly.max_point().x, 0) self.assertEqual(self.poly.max_point().y, 0) def test_min_point(self): '''Test Polygon.min_point() for complex case''' for _xy in [(1, 3), (3, 7), (4, 3), (3, -2)]: self.poly.add_point(_xy) self.assertEqual(self.poly.min_point().x, 1) self.assertEqual(self.poly.min_point().y, -2) def test_max_point(self): '''Test Polygon.max_point() for complex case''' for _xy in [(1, 3), (3, 7), (4, 3), (3, -2)]: self.poly.add_point(_xy) self.assertEqual(self.poly.max_point().x, 4) self.assertEqual(self.poly.max_point().y, 7)
def parse_shape(self, shape): """ Extract a shape. """ # pylint: disable=R0914 # pylint: disable=R0911 rotation = shape.get('rotation', 0.0) flip_horizontal = shape.get('flip_horizontal', False) shape_type = shape.get('type') if 'rectangle' == shape_type: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) parsed_shape = Rectangle(x, y, width, height) elif 'rounded_rectangle' == shape_type: x = int(shape.get('x')) y = int(shape.get('y')) height = int(shape.get('height')) width = int(shape.get('width')) radius = int(shape.get('radius')) parsed_shape = RoundedRectangle(x, y, width, height, radius) elif 'arc' == shape_type: x = int(shape.get('x')) y = int(shape.get('y')) start_angle = float(shape.get('start_angle')) end_angle = float(shape.get('end_angle')) radius = int(shape.get('radius')) parsed_shape = Arc(x, y, start_angle, end_angle, radius) elif 'circle' == shape_type: x = int(shape.get('x')) y = int(shape.get('y')) radius = int(shape.get('radius')) parsed_shape = Circle(x, y, radius) elif 'label' == shape_type: parsed_shape = self.parse_label(shape) elif 'line' == shape_type: p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) parsed_shape = Line(p1, p2) elif 'polygon' == shape_type: parsed_shape = Polygon() for point in shape.get('points'): parsed_shape.add_point(self.parse_point(point)) elif 'bezier' == shape_type: control1 = self.parse_point(shape.get('control1')) control2 = self.parse_point(shape.get('control2')) p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) parsed_shape = BezierCurve(control1, control2, p1, p2) elif 'rounded_segment' == shape_type: p1 = self.parse_point(shape.get('p1')) p2 = self.parse_point(shape.get('p2')) width = int(shape.get('width')) parsed_shape = RoundedSegment(p1, p2, width) parsed_shape.rotation = rotation parsed_shape.flip_horizontal = flip_horizontal parsed_shape.styles = shape.get('styles') or {} parsed_shape.attributes = shape.get('attributes') or {} return parsed_shape
def setUp(self): """ Setup the test case. """ self.poly = Polygon()