From 736b7b25f913d21578e26940265b20acabf62cd9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 9 Sep 2017 21:04:08 +0200 Subject: o Splitting out read_schematic into its own file. --- src/ee/kicad/read_schematic.py | 129 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/ee/kicad/read_schematic.py (limited to 'src/ee/kicad/read_schematic.py') diff --git a/src/ee/kicad/read_schematic.py b/src/ee/kicad/read_schematic.py new file mode 100644 index 0000000..6fa9686 --- /dev/null +++ b/src/ee/kicad/read_schematic.py @@ -0,0 +1,129 @@ +import shlex +from ee.kicad.model import * + +def read_schematic(path): + schematic = Schematic() + sheet = None + + def descr_section(lines): + print("descr_section: len={}".format(len(lines))) + pass + + def comp_section(lines): + print("comp_section: len={}".format(len(lines))) + timestamp = None + position = None + library = None + name = None + unit = None + fields = [] + + extra_lines = 0 + for line in lines: + if line.startswith((' ', '\t')): + parts = line.strip().split(" ") + if extra_lines == 0: + pass + else: + # rotation/mirroring, x1, y1, x2, y2 + pass + extra_lines += 1 + else: + parts = shlex.split(line) + if len(parts) < 3: + raise EeException("Bad component line: {}".format(line)) + + if parts[0] == "L" and len(parts) == 3: + name = parts[1] + ref = parts[2] + elif parts[0] == "U" and len(parts) == 4: + unit = int(parts[1]) + # parts[2] == body style + timestamp = parts[3] + elif parts[0] == "P" and len(parts) == 3: + position = Position(int(parts[1]), int(parts[2])) + elif parts[0] == "F" and len(parts) >= 10: + oritentation = parts[3] + pos = Position(int(parts[4]), int(parts[5])) + value = parts[2] + size = parts[6] + attributes = parts[7] + justify = parts[8] # One of L, R, C + textAttrs = parts[9] # tree characters, + # 0: T=top justify, B=bottom justify, C=center + # 1: I=italics, N=normal + # 2: B=bold, N=normal + field_name = parts[10] if len(parts) > 10 else None + idx = int(parts[1]) + if len(fields) != idx: + raise EeException("Bad index: {}, expected={}. component={}, line={}".format(idx, len(fields), ref, line)) + fields.append(ComponentField(idx, field_name, value, pos)) + else: + raise EeException("Bad component field: '{}'".format(line)) + + sheet.add_component(Component(position, timestamp, library, name, unit, ref, fields)) + + with open(path) as f: + header = f.readline() + if not "EESchema Schematic File Version" in header: + raise EeException("Not a KiCAD schematic file.") + + sheet = schematic.first_sheet + + section_name = None + section = [] + + seen_end = False + while not seen_end: + line = f.readline() + if not line: + break + + line = line.rstrip() + + if section_name: + if line.startswith("$End"): + print("END SECTION: {}".format(section_name)) + if section_name == "$Comp": + comp_section(section) + elif section_name == "$Descr": + descr_section(section) + else: + if line == "$EndSCHEMATIC": + seen_end = True + break + section_name = None + else: + section.append(line) + else: + parts = line.split(" ") + if line.startswith("EELAYER "): + pass # Legacy, ignored + elif line.startswith("LIBS:"): + schematic.add_library(line[5:]) + elif line.startswith("$"): + section_name = parts[0] + print("SECTION: {}".format(section_name)) + section = [] + elif line == "Entry Wire Line": + f.readline() # ignore the next line for now + elif line.startswith("Text Label "): + f.readline() # ignore the next line for now + elif line.startswith("Text Notes "): + f.readline() # ignore the next line for now + elif line.startswith("NoConn "): + pass + elif parts[0:3] == ["Wire", "Notes", "Line"]: + f.readline() # ignore the next line for now + elif parts[0:3] == ["Wire", "Wire", "Line"]: + f.readline() # ignore the next line for now + elif parts[0:3] == ["Wire", "Bus", "Line"]: + f.readline() # ignore the next line for now + elif parts[0:2] == ["Connection", "~"]: + pass + else: + print("line={}, len={}, wat={}".format(line, len(line), parts[0:3])) + raise EeException("Bad line: {}".format(line)) + + return schematic + -- cgit v1.2.3