diff options
Diffstat (limited to 'src/ee/kicad')
-rw-r--r-- | src/ee/kicad/__init__.py | 3 | ||||
-rw-r--r-- | src/ee/kicad/model.py | 80 | ||||
-rw-r--r-- | src/ee/kicad/read_schematic.py | 35 |
3 files changed, 92 insertions, 26 deletions
diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index f431e13..1aac33d 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -1,7 +1,7 @@ from typing import Any from ee import EeException -from ee.kicad.read_schematic import read_schematic +from ee.kicad.read_schematic import read_schematic, read_schematics from ee.kicad.to_bom import to_bom, to_bom_xml from .model import * @@ -11,6 +11,7 @@ __all__ = [ "Component", "Schematic", "read_schematic", + "read_schematics", "to_bom", "to_bom_xml", "to_pandas", diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index 7d9a437..eacf8e2 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -133,16 +133,17 @@ class Component(object): class Sheet(object): - def __init__(self): - self._components = [] - pass + def __init__(self, name, path): + self._name = name + self._path = path @property - def components(self): - return frozenset(self._components) + def path(self): + return self._path - def add_component(self, component): - self._components.append(component) + @property + def name(self): + return self._name class Library(object): @@ -153,12 +154,8 @@ class Library(object): class Schematic(object): def __init__(self): self._libraries = set() - self._sheets = [Sheet()] - pass - - @property - def first_sheet(self): - return self._sheets[0] + self._sheets = [] + self._components = [] @property def libraries(self): @@ -167,18 +164,61 @@ class Schematic(object): def add_library(self, library): self._libraries.add(Library(library)) - # Getters @property - def components(self) -> List[Component]: - a = [] - for s in self._sheets: - for c in s.components: - a.append(c) - return a + def sheets(self): + return frozenset(self._sheets) + + def add_sheet(self, sheet): + self._sheets.append(sheet) + + @property + def components(self): + return frozenset(self._components) + + def add_component(self, component): + self._components.append(component) def get_component(self, ref, unit=1): + c = find_component(self, ref, unit) + + if c: + return c + + raise KeyError("No such component: {}".format(ref)) + + def find_component(self, ref, unit=1): for c in self.components: if c.ref == ref and unit == unit: return c +class Schematics(object): + def __init__(self, schematics): + self._schematics = schematics + + @property + def schematics(self) -> List[Schematic]: + return list(self._schematics) + + @property + def components(self) -> List[Component]: + cs = [] + for s in self._schematics: + for c in s.components: + cs.append(c) + return cs + + def get_component(self, ref, unit=1): + for s in self._schematics: + c = s.find_component(ref, unit) + if c: + return c + raise KeyError("No such component: {}".format(ref)) + + @property + def libraries(self) -> List[Library]: + ls = [] + for s in self._schematics: + for l in s.libraries: + ls.append(l) + return ls diff --git a/src/ee/kicad/read_schematic.py b/src/ee/kicad/read_schematic.py index e70d1a7..ed5ce21 100644 --- a/src/ee/kicad/read_schematic.py +++ b/src/ee/kicad/read_schematic.py @@ -1,16 +1,42 @@ import shlex from ee import EeException from ee.kicad.model import * +import os.path +def read_schematics(path): + def read(path): + schematic = read_schematic(path) + schematics = [schematic] + for sheet in schematic.sheets: + p = os.path.join(os.path.dirname(path), sheet.path) + print("Loading {} from {}, path: {}".format(sheet.name, sheet.path, p)) + children = read(p) + schematics.extend(children) + return schematics + + return Schematics(read(path)) + def read_schematic(path): schematic = Schematic() - sheet = None def descr_section(lines): # print("descr_section: len={}".format(len(lines))) pass + def sheet_section(lines): + + for line in lines: + parts = shlex.split(line) + if len(parts) < 2: + continue + if parts[0] == "F0": + name = parts[1] + elif parts[0] == "F1": + path = parts[1] + + schematic.add_sheet(Sheet(name, path)) + def comp_section(lines): # print("comp_section: len={}".format(len(lines))) timestamp = None @@ -65,16 +91,13 @@ def read_schematic(path): else: raise EeException("Bad component field: '{}'".format(line)) - sheet.add_component(Component(position, timestamp, library, name, unit, ref, fields)) + schematic.add_component(Component(position, timestamp, library, name, unit, ref, fields)) def load(f): header = f.readline() if "EESchema Schematic File Version" not in header: raise EeException("Not a KiCAD schematic file.") - nonlocal sheet - sheet = schematic.first_sheet - section_name = None section = [] @@ -93,6 +116,8 @@ def read_schematic(path): comp_section(section) elif section_name == "$Descr": descr_section(section) + elif section_name == "$Sheet": + sheet_section(section) else: if line == "$EndSCHEMATIC": seen_end = True |