コード例 #1
0
 def parse_label(self, label):
     """ Extract a label. """
     x = int(label.get('x'))
     y = int(label.get('y'))
     text = label.get('text')
     align = label.get('align')
     rotation = float(label.get('rotation'))
     parsed_label = Label(x, y, text, align, rotation)
     parsed_label.styles = label.get('styles') or {}
     return parsed_label
コード例 #2
0
 def parse_label(self, label):
     """ Extract a label. """
     x = int(label.get('x'))
     y = int(label.get('y'))
     text = label.get('text')
     font_size = label.get('font_size')
     font_family = label.get('font_family')
     align = label.get('align')
     baseline = label.get('baseline')
     rotation = float(label.get('rotation'))
     parsed_label = Label(x, y, text, font_size, font_family, align, baseline, rotation)
     parsed_label.styles = label.get('styles') or {}
     return parsed_label
コード例 #3
0
 def parse_label(self, label):
     """ Extract a label. """
     if label is None:
         return None
     x = int(label.get("x"))
     y = int(label.get("y"))
     text = label.get("text")
     font_size = label.get("font_size")
     font_family = label.get("font_family")
     align = label.get("align")
     baseline = label.get("baseline")
     rotation = float(label.get("rotation"))
     parsed_label = Label(x, y, text, font_size, font_family, align, baseline, rotation)
     parsed_label.styles = label.get("styles") or {}
     return parsed_label
コード例 #4
0
 def parse_label(self, label):
     """ Extract a label. """
     if label is None:
         return None
     x = int(label.get('x'))
     y = int(label.get('y'))
     text = label.get('text')
     font_size = label.get('font_size')
     font_family = label.get('font_family')
     align = label.get('align')
     baseline = label.get('baseline')
     rotation = float(label.get('rotation'))
     parsed_label = Label(x, y, text, font_size, font_family, align,
                          baseline, rotation)
     parsed_label.styles = label.get('styles') or {}
     return parsed_label
コード例 #5
0
    def test_get_pin_line(self):
        """
        The get_pin_line returns the correct string for a kicad pin.
        """

        writer = KiCAD()

        pin = Pin('1', (-300, 100), (-600, 100))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 1 -6667 1111 3333 R 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('1', (300, 100), (600, 100))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 1 6667 1111 3333 L 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, -1300), (0, -1500))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 2 0 -16667 2222 U 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, 1300), (0, 1500))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 2 0 16667 2222 D 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, 1300), (0, 1500), Label(0, 0, 'name', 'center', 0))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X name 2 0 16667 2222 D 60 60 %(unit)d %(convert)d B\n')
コード例 #6
0
 def test_create_new_label(self):
     """ Test the creation of a new empty label. """
     lbl = Label(0, 1, 'abc', 'left', 2)
     assert lbl.x == 0
     assert lbl.y == 1
     assert lbl.text == 'abc'
     assert lbl.align == 'left'
     assert lbl.rotation == 2
コード例 #7
0
 def parse_label(self, args):
     """ Returns a parsed label. """
     # So far, only seen it for labelling pins, in the symmbol files
     # at least.
     x, y, _pts, rot, _anchor, _scope, _vis, inv, text = args.split()
     if inv == '1':
         # cheap-o overbar
         text = '/' + text
     return ('label', Label(int(x), int(y), text, 'left', int(rot) * 0.5))
コード例 #8
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'))
            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
コード例 #9
0
 def parse_text(self, args):
     """ Parses a text label and returns as a Shape.Label. """
     x, y, _size, rot, _anchor, text = args.split(' ', 5)
     # TODO sort out alignment
     rot, _flip = self.rot_and_flip(rot)
     return ('shape', Label(int(x),
                            int(y),
                            text,
                            align='left',
                            rotation=rot))
コード例 #10
0
 def test_pin_label_bounds(self):
     '''Test bounds() for a pin with a label'''
     lab = Label(0, 0, 'foo', align='left', rotation=0)
     mkbounds(lab, 1, 3, 2, 6)
     pin = Pin(0, Point(2, 2), Point(4, 3), lab)
     top_left, bottom_right = pin.bounds()
     self.assertEqual(top_left.x, 1)
     self.assertEqual(top_left.y, 2)
     self.assertEqual(bottom_right.x, 4)
     self.assertEqual(bottom_right.y, 6)
コード例 #11
0
    def parse_t_line(self, parts):
        """ Parse a T (Text) line """
        angle, x, y = [int(i) for i in parts[1:4]]
        angle = angle / 1800.0
        text = parts[8].replace('~', ' ')

        if len(parts) >= 12:
            align = {'C': 'center', 'L': 'left', 'R': 'right'}.get(parts[11])
        else:
            align = 'left'

        return Label(make_length(x), make_length(y), text, align, angle)
コード例 #12
0
    def make_body_from_symbol(self, lib, symbol_name):
        """ Construct an openjson Body from an eagle symbol in a library. """

        body = Body()

        symbol = [
            s for s in get_subattr(lib, 'symbols.symbol')
            if s.name == symbol_name
        ][0]

        for wire in symbol.wire:
            body.add_shape(
                Line((self.make_length(wire.x1), self.make_length(wire.y1)),
                     (self.make_length(wire.x2), self.make_length(wire.y2))))

        for rect in symbol.rectangle:
            x = self.make_length(rect.x1)
            y = self.make_length(rect.y1)
            width = self.make_length(rect.x2) - x
            height = self.make_length(rect.y2) - y
            body.add_shape(Rectangle(x, y + height, width, height))

        pin_map = {}

        for pin in symbol.pin:
            connect_point = (self.make_length(pin.x), self.make_length(pin.y))
            null_point = self.get_pin_null_point(connect_point, pin.length,
                                                 pin.rot)
            label = self.get_pin_label(pin, null_point)
            pin_map[pin.name] = Pin(pin.name, null_point, connect_point, label)
            body.add_pin(pin_map[pin.name])

        ann_map = {}

        for text in symbol.text:
            x = self.make_length(text.x)
            y = self.make_length(text.y)
            content = '' if text.valueOf_ is None else text.valueOf_
            rotation = self.make_angle('0' if text.rot is None else text.rot)
            if content == '>NAME':
                ann_map['name'] = Annotation(content, x, y, rotation, 'true')
            elif content == '>VALUE':
                ann_map['value'] = Annotation(content, x, y, rotation, 'true')
            else:
                body.add_shape(Label(x, y, content, 'left', rotation))

        return body, pin_map, ann_map
コード例 #13
0
    def test_get_pin_line(self):
        """
        The get_pin_line returns the correct string for a kicad pin.
        """

        writer = KiCAD()

        pin = Pin('1', (-300, 100), (-600, 100))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 1 ' 
            + str(int(-600 / MULT)) + ' ' 
            + str(int(100 / MULT)) + ' ' 
            + str(int(300 / MULT)) + ' ' 
            + 'R 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('1', (300, 100), (600, 100))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 1 '
            + str(int(600 / MULT)) + ' '
            + str(int(100 / MULT)) + ' '
            + str(int(300 / MULT)) + ' '
            + 'L 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, -1300), (0, -1500))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 2 0 '
            + str(int(-1500 / MULT)) + ' '
            + str(int(200 / MULT)) + ' U 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, 1300), (0, 1500))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X ~ 2 0 '
            + str(int(1500 / MULT)) + ' '
            + str(int(200 / MULT)) + ' D 60 60 %(unit)d %(convert)d B\n')

        pin = Pin('2', (0, 1300), (0, 1500),
                  Label(0, 0, 'name', align='center', rotation=0))
        line = writer.get_pin_line(pin)
        self.assertEqual(
            line, 'X name 2 0 '
            + str(int(1500 / MULT)) + ' '
            + str(int(200 / MULT)) + ' D 60 60 %(unit)d %(convert)d B\n')
コード例 #14
0
    def parse_x_line(self, parts):
        """ Parse an X (Pin) line """
        name, num, direction = parts[1], parts[2], parts[6]
        p2x, p2y, pinlen = int(parts[3]), int(parts[4]), int(parts[5])

        if direction == 'U':  # up
            p1x = p2x
            p1y = p2y + pinlen
            label_x = p2x - 20
            label_y = p2y + int(pinlen / 2)
            label_rotation = 1.5
        elif direction == 'D':  # down
            p1x = p2x
            p1y = p2y - pinlen
            label_x = p2x - 20
            label_y = p2y - int(pinlen / 2)
            label_rotation = 1.5
        elif direction == 'L':  # left
            p1x = p2x - pinlen
            p1y = p2y
            label_x = p2x - int(pinlen / 2)
            label_y = p2y + 20
            label_rotation = 0
        elif direction == 'R':  # right
            p1x = p2x + pinlen
            p1y = p2y
            label_x = p2x + int(pinlen / 2)
            label_y = p2y + 20
            label_rotation = 0
        else:
            raise ValueError('unexpected pin direction', direction)

        if name == '~':
            label = None
        else:
            label = Label(label_x,
                          label_y,
                          name,
                          align='center',
                          rotation=label_rotation)

        return Pin(num, (p1x, p1y), (p2x, p2y), label)
コード例 #15
0
    def make_body_from_symbol(self, lib, symbol_name, pin_number_lookup):
        """ Construct an openjson SBody from an eagle symbol in a library. """

        body = SBody()

        symbol = [
            s for s in get_subattr(lib, 'symbols.symbol')
            if s.name == symbol_name
        ][0]

        for wire in symbol.wire:
            body.add_shape(self.make_shape_for_wire(wire))

        for rect in symbol.rectangle:
            rotation = make_angle('0' if rect.rot is None else rect.rot)
            x1, y1 = rotate_point(
                (self.make_length(rect.x1), self.make_length(rect.y1)),
                rotation)
            x2, y2 = rotate_point(
                (self.make_length(rect.x2), self.make_length(rect.y2)),
                rotation)
            ux, uy = min(x1, x2), max(y1, y2)
            lx, ly = max(x1, x2), min(y1, y2)
            body.add_shape(Rectangle(ux, uy, lx - ux, uy - ly))

        for poly in symbol.polygon:
            map(body.add_shape, self.make_shapes_for_poly(poly))

        for circ in symbol.circle:
            body.add_shape(self.make_shape_for_circle(circ))

        pin_map = {}

        for pin in symbol.pin:
            connect_point = (self.make_length(pin.x), self.make_length(pin.y))
            null_point = self.get_pin_null_point(connect_point, pin.length,
                                                 pin.rot)
            label = self.get_pin_label(pin, null_point)
            pin_map[pin.name] = Pin(pin_number_lookup(pin.name), null_point,
                                    connect_point, label)
            if pin.direction:
                pin_map[pin.name].add_attribute('eaglexml_direction',
                                                pin.direction)
            if pin.visible:
                pin_map[pin.name].add_attribute('eaglexml_visible',
                                                pin.visible)
            body.add_pin(pin_map[pin.name])

        ann_map = {}

        for text in symbol.text:
            x = self.make_length(text.x)
            y = self.make_length(text.y)
            content = '' if text.valueOf_ is None else text.valueOf_
            rotation = make_angle('0' if text.rot is None else text.rot)
            align = 'right' if is_mirrored(text.rot) else 'left'
            if rotation == 0.5:
                rotation = 1.5
            if content.lower() == '>name':
                ann_map['name'] = Annotation(content, x, y, rotation, 'true')
            elif content.lower() == '>value':
                ann_map['value'] = Annotation(content, x, y, rotation, 'true')
            else:
                body.add_shape(
                    Label(x, y, content, align=align, rotation=rotation))

        return body, pin_map, ann_map
コード例 #16
0
        if rotation == 0.0:
            oy = -3
        elif rotation == 0.5:
            rotation = 1.5
            align = 'right'
            ox = 3
        elif rotation == 1.0:
            rotation = 0.0
            align = 'right'
            oy = -3
        elif rotation == 1.5:
            ox = 3

        return Label(null_x + ox,
                     null_y + oy,
                     pin.name,
                     align=align,
                     rotation=rotation)

    def make_component_instances(self, root):
        """ Construct openjson component instances from an eagle model. """

        parts = dict(
            (p.name, p)
            for p in get_subattr(root, 'drawing.schematic.parts.part', ()))

        for sheet in get_subattr(root, 'drawing.schematic.sheets.sheet', ()):
            for instance in get_subattr(sheet, 'instances.instance', ()):
                inst = self.ensure_component_instance(parts, instance)
                self.set_symbol_attribute(instance, inst)
コード例 #17
0
 def fake_label(args):
     return ('label', Label(1, 2, args, 'left', 0))
コード例 #18
0
 def fake_label(args):
     return ('label', Label(1, 2, args, align='left', rotation=0))
コード例 #19
0
 def parse_text(self, args):
     """ Parses a text label and returns as a Shape.Label. """
     x, y, _size, _rot, _anchor, text = args.split(' ', 5)
     # TODO sort out rotation, alignment
     return ('shape', Label(int(x), int(y), text, 'left', 0))
コード例 #20
0
        distance = int(self.SCALE * (len(pin.name) * 10) / 2)

        if pin.rot == 'R90':
            x, y = (null_x, null_y + distance)
            rotation = 1.5
        elif pin.rot == 'R180':
            x, y = (null_x - distance, null_y)
            rotation = 0.0
        elif pin.rot == 'R270':
            x, y = (null_x, null_y - distance)
            rotation = 0.5
        else:
            x, y = (null_x + distance, null_y)
            rotation = 0.0

        return Label(x, y, pin.name, 'center', rotation)

    def make_component_instances(self, root):
        """ Construct openjson component instances from an eagle model. """

        parts = dict(
            (p.name, p)
            for p in get_subattr(root, 'drawing.schematic.parts.part', ()))

        for sheet in get_subattr(root, 'drawing.schematic.sheets.sheet', ()):
            for instance in get_subattr(sheet, 'instances.instance', ()):
                inst = self.ensure_component_instance(parts, instance)
                self.set_symbol_attribute(instance, inst)

    def ensure_component_instance(self, parts, instance):
        """ Ensure there is a component instance for an eagle instance. """