diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ee/kicad/model.py | 43 | ||||
-rw-r--r-- | src/ee/kicad/to_bom.py | 8 |
2 files changed, 44 insertions, 7 deletions
diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index f44868b..7baa4a0 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -1,5 +1,8 @@ +import re from typing import List +from functools import total_ordering + class Position(object): def __init__(self, x, y): @@ -8,11 +11,11 @@ class Position(object): @property def x(self): - return _x + return self._x @property def y(self): - return _y + return self._y class ComponentField(object): @@ -41,6 +44,7 @@ class ComponentField(object): return self._position +@total_ordering class Component(object): def __init__(self, position, timestamp, library, name, unit, ref, fields): self._position = position @@ -51,6 +55,25 @@ class Component(object): self._ref = ref self._fields = fields # type List[ComponentField] + def __eq__(self, o: object) -> bool: + other = o # type Component + return isinstance(o, Component) and self.ref == other.ref + + def __lt__(self, o: object) -> bool: + if not isinstance(o, Component): + return True + + other = o # type Component + + if self.has_ref_num and other.has_ref_num: + return (self.ref_type, self.ref_num) < (other.ref_type, other.ref_num) + else: + # raise Exception("fail, ref={}".format(self.ref)) + return self.ref < other.ref + + def __hash__(self) -> int: + return self._ref.__hash__() + @property def unit(self): return self._unit @@ -60,6 +83,20 @@ class Component(object): return self._ref @property + def has_ref_num(self): + return not self._ref.endswith("?") + + @property + def ref_type(self) -> str: + r = re.compile("([^0-9]+)(.+)") + return r.split(self._ref)[1] + + @property + def ref_num(self) -> int: + r = re.compile("([^0-9]+)(.+)") + return int(r.split(self._ref)[2]) + + @property def value(self): return self._fields[1].value @@ -113,7 +150,7 @@ class Schematic(object): # Getters @property - def components(self): + def components(self) -> List[Component]: a = [] for s in self._sheets: for c in s.components: diff --git a/src/ee/kicad/to_bom.py b/src/ee/kicad/to_bom.py index ca2221e..634547a 100644 --- a/src/ee/kicad/to_bom.py +++ b/src/ee/kicad/to_bom.py @@ -1,8 +1,9 @@ -from xml.etree.ElementTree import Element, ElementTree +from xml.etree.ElementTree import Element from ee.kicad.model import * __all__ = ["to_bom"] + def simple_element(parent, e, text): if len(text) == 0: return @@ -28,7 +29,7 @@ def comp(c: Component) -> Element: fields = Element("fields") [fields.append(field(f)) for f in c.fields if f.index > 3] if len(fields) > 0: - comp.append(fields) + comp.append(fields) return comp @@ -41,7 +42,6 @@ def to_bom(schematic: Schematic) -> Element: components = Element("components") root.append(components) - [components.append(comp(c)) for c in schematic.components] - + [components.append(comp(c)) for c in sorted(schematic.components) if c.ref_type != "#PWR"] return root |