From 094cf41e7bf66fb2be97c070d604b8b5e36f4869 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Mon, 31 Aug 2020 20:43:52 -0300 Subject: [PATCH] Added support for Italic and Bold in Text* elements. --- kibot/kicad/v5_sch.py | 49 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index aea5bb1f..1f01c0a5 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -986,29 +986,58 @@ class SchematicConnection(object): c.y = int(m.group(2)) return c + def write(self, f): + f.write('{} ~ {} {}\n'.format(['NoConn', 'Connection'][self.connect], self.x, self.y)) + class SchematicText(object): label_re = re.compile(r'Text\s+(Notes|HLabel|GLabel|Label)\s+(-?\d+)\s+(-?\d+)\s+(\d)\s+(\d+)\s+(\S+)') + TYPES = ['Notes', 'HLabel', 'GLabel', 'Label'] def __init__(self): super().__init__() @staticmethod def load(f, line): - m = SchematicText.label_re.match(line) - if not m: - raise SchFileError('Malformed text', line, f) + gs = _split_space(line) + c = len(gs) + if c < 6 or gs[0] != 'Text' or gs[1] not in SchematicText.TYPES: + raise SchFileError('Malformed `Text`', line, f) text = SchematicText() - gs = m.groups() - text.type = gs[0] - text.x = int(gs[1]) - text.y = int(gs[2]) - text.orient = int(gs[3]) - text.size = int(gs[4]) - text.shape = gs[5] + text.type = gs[1] + try: + text.x = int(gs[2]) + text.y = int(gs[3]) + text.orient = int(gs[4]) + text.size = int(gs[5]) + offset = 6 + text.shape = None + if gs[1][0] in 'GH': + if c < 7: + raise SchFileError('Missing `Text` shape', line, f) + text.shape = gs[6] + offset += 1 + # New versions adds Italics and Bold, in a different way of course + text.italic = False + if c > offset: + text.italic = gs[offset] == 'Italic' + offset += 1 + text.thickness = 0 + if c > offset: + text.thickness = int(gs[offset]) + offset += 1 + except ValueError: + raise SchFileError('Not a number in `Text`', line, f) text.text = f.get_line() return text + def write(self, f): + f.write('Text {} {} {} {} {}'.format(self.type, self.x, self.y, self.orient, self.size)) + if self.type[0] in 'GH': + f.write(' '+self.shape) + f.write(' {} {}\n'.format(['~', 'Italic'][self.italic], self.thickness)) + f.write(self.text+'\n') + class SchematicWire(object): WIRE = 0