예제 #1
0
    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)
예제 #2
0
    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]
예제 #3
0
    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')
예제 #4
0
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)
예제 #5
0
 def setUp(self):
     """ Setup the test case. """
     self.poly = Polygon()