Beispiel #1
0
    def test_is_valid_path(self):
        """ Tests if SBody objects contain valid paths."""
        shapes = [
            shape.Line((10, 10), (50, 10)),  #L 500,100
            shape.BezierCurve((70, 10), (80, 30), (50, 10),
                              (80, 40)),  #C 700,100 800,300 800,400
            shape.BezierCurve((80, 50), (70, 70), (80, 40),
                              (50, 70)),  #C 800,500 700,700 500,700
            shape.Line((50, 70), (10, 70)),  #L 100,700
        ]

        body = components.SBody()
        body.shapes = shapes
        self.assertTrue(self.geda_writer.is_valid_path(body))

        body.add_shape(shape.Line((10, 70), (10, 10)))
        self.assertTrue(self.geda_writer.is_valid_path(body))

        shapes = [
            shape.Line((10, 10), (50, 10)),  #L 500,100
            shape.BezierCurve((70, 10), (80, 30), (50, 10),
                              (80, 40)),  #C 700,100 800,300 800,400
            shape.Line((50, 70), (10, 70)),  #L 100,700
        ]
        body.shapes = shapes
        self.assertFalse(self.geda_writer.is_valid_path(body))

        body.add_shape(shape.Circle(0, 0, 10))
        self.assertFalse(self.geda_writer.is_valid_path(body))
Beispiel #2
0
    def test_create_path(self):
        """ Test creating path commands from SBody objects. """
        shapes = [
            shape.Line((10, 10), (50, 10)),
            shape.BezierCurve((70, 10), (80, 30), (50, 10), (80, 40)),
            shape.BezierCurve((80, 50), (70, 70), (80, 40), (50, 70)),
            shape.Line((50, 70), (10, 70)),
        ]

        self.assertEquals(self.geda_writer._create_path(shapes), [
            'H 3 10 0 0 -1 -1 0 -1 -1 -1 -1 -1 5',
            'M 100,100',
            'L 500,100',
            'C 700,100 800,300 800,400',
            'C 800,500 700,700 500,700',
            'L 100,700',
        ])

        shapes.append(shape.Line((10, 70), (10, 10)))

        self.assertEquals(self.geda_writer._create_path(shapes), [
            'H 3 10 0 0 -1 -1 0 -1 -1 -1 -1 -1 6',
            'M 100,100',
            'L 500,100',
            'C 700,100 800,300 800,400',
            'C 800,500 700,700 500,700',
            'L 100,700',
            'z',
        ])
Beispiel #3
0
    def test_convert_line(self):
        """ Tests converting Line objects to line commands. """
        line = shape.Line((0, 0), (0, 50))
        command = self.geda_writer._convert_line(line)
        self.assertEquals(command, ['L 0 0 0 500 3 10 0 0 -1 -1'])

        line = shape.Line((20, 40), (-20, 40))
        command = self.geda_writer._convert_line(line)
        self.assertEquals(command, ['L 200 400 -200 400 3 10 0 0 -1 -1'])

        line = shape.Line((20, 40), (-30, 50))
        command = self.geda_writer._convert_line(line)
        self.assertEquals(command, ['L 200 400 -300 500 3 10 0 0 -1 -1'])
Beispiel #4
0
 def test_generating_geda_commands_for_toplevel_shapes(self):
     design = Design()
     design.shapes = [
         shape.Line((0, 0), (0, 50)),
         shape.Circle(0, 0, 300),
     ]
     design.pins = [
         components.Pin('E', (0, 0), (0, 30)),
         components.Pin('E', (0, 0), (0, 30)),
     ]
     commands = self.geda_writer.generate_body_commands(design)
     ## default pins require 6 commands, shapes require 1 command
     self.assertEquals(len(commands), 2 * 6 + 2 * 1)
    def _parse_L(self, stream, params):
        """ Creates a Line object from the parameters in *params*. All
            style related parameters are ignored.
            Returns a Line object.
        """
        line_x1 = params['x1']
        line_x2 = params['x2']

        if self._is_mirrored_command(params):
            line_x1 = 0 - params['x1']
            line_x2 = 0 - params['x2']

        line = shape.Line(
            self.conv_coords(line_x1, params['y1']),
            self.conv_coords(line_x2, params['y2']),
        )
        ## store style data for line in 'style' dict
        self._save_parameters_to_object(line, params)
        return line
    def _parse_H(self, stream, params):
        """ Parses a SVG-like path provided path into a list
            of simple shapes. The gEDA formats allows only line
            and curve segments with absolute coordinates. Hence,
            shapes are either Line or BezierCurve objects.
            The method processes the stream data according to
            the number of lines in *params*.
            Returns a list of Line and BezierCurve shapes.
        """
        params['extra_id'] = self.path_counter.next()
        num_lines = params['num_lines']
        mirrored = self._is_mirrored_command(params)
        command = stream.readline().strip().split(self.DELIMITER)

        if command[0] != 'M':
            raise GEDAError('found invalid path in gEDA file')

        def get_coords(string, mirrored):
            """ Get coordinates from string with comma-sparated notation."""
            x, y = [int(value) for value in string.strip().split(',')]

            if mirrored:
                x = -x

            return (self.x_to_px(x), self.y_to_px(y))

        shapes = []
        current_pos = initial_pos = (get_coords(command[1], mirrored))

        ## loop over the remaining lines of commands (after 'M')
        for _ in range(num_lines - 1):
            command = stream.readline().strip().split(self.DELIMITER)

            ## draw line from current to given position
            if command[0] == 'L':
                assert (len(command) == 2)
                end_pos = get_coords(command[1], mirrored)

                shape_ = shape.Line(current_pos, end_pos)
                current_pos = end_pos

            ## draw curve from current to given position
            elif command[0] == 'C':
                assert (len(command) == 4)
                control1 = get_coords(command[1], mirrored)
                control2 = get_coords(command[2], mirrored)
                end_pos = get_coords(command[3], mirrored)

                shape_ = shape.BezierCurve(control1, control2, current_pos,
                                           end_pos)
                current_pos = end_pos

            ## end of sub-path, straight line from current to initial position
            elif command[0] in ['z', 'Z']:
                shape_ = shape.Line(current_pos, initial_pos)

            else:
                raise GEDAError("invalid command type in path '%s'" %
                                command[0])

            ## store style parameters in shape's style dict
            self._save_parameters_to_object(shape_, params)
            shapes.append(shape_)

        return shapes