From c96bc5755a68d8e317d96be31b330cc0b626c194 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 9 Sep 2017 22:10:07 +0200 Subject: to_bom: A utility to create a KiCAD-like xml Element of a KiCAD schematic. --- src/ee/kicad/__init__.py | 2 ++ src/ee/kicad/model.py | 14 +++++++++++++- src/ee/kicad/to_bom.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ test/test_read_schematic.py | 19 ++++++++++++++++-- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/ee/kicad/to_bom.py diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index ab0dede..c4c3af5 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -1,6 +1,7 @@ import re from ee import EeException from ee.kicad.read_schematic import read_schematic +from ee.kicad.to_bom import to_bom from .model import * __all__ = [ @@ -9,4 +10,5 @@ __all__ = [ "Component", "Schematic", "read_schematic", + "to_bom", ] diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index 3c6001c..255fad5 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -14,13 +14,17 @@ class Position(object): return _y class ComponentField(object): - names = ["Name", "Reference", "Value", "Footprint", "Datasheet"] + names = ["Reference", "Value", "Footprint", "Datasheet"] def __init__(self, index, name, value, position): self._index = index self._name = name if index >= len(ComponentField.names) else ComponentField.names[index] self._value = value self._position = position + @property + def index(self): + return self._index + @property def name(self): return self._name @@ -51,6 +55,14 @@ class Component(object): def ref(self): return self._ref + @property + def value(self): + return self._fields[1].value + + @property + def footprint(self): + return self._fields[2].value + @property def fields(self) -> List[ComponentField]: return list(self._fields) diff --git a/src/ee/kicad/to_bom.py b/src/ee/kicad/to_bom.py new file mode 100644 index 0000000..ca2221e --- /dev/null +++ b/src/ee/kicad/to_bom.py @@ -0,0 +1,47 @@ +from xml.etree.ElementTree import Element, ElementTree +from ee.kicad.model import * + +__all__ = ["to_bom"] + +def simple_element(parent, e, text): + if len(text) == 0: + return + element = Element(e) + element.text = text + parent.append(element) + + +def field(f: ComponentField) -> Element: + field = Element("field", {"name": f.name}) + field.text = f.value + return field + + +def comp(c: Component) -> Element: + comp = Element("comp", {"ref": c.ref}) + + simple_element(comp, "value", c.value) + simple_element(comp, "footprint", c.footprint) + # + # + # 561E4EAA + fields = Element("fields") + [fields.append(field(f)) for f in c.fields if f.index > 3] + if len(fields) > 0: + comp.append(fields) + + return comp + + +def to_bom(schematic: Schematic) -> Element: + root = Element("export") + root.attrib["version"] = "D" + design = Element("design") + root.append(design) + + components = Element("components") + root.append(components) + [components.append(comp(c)) for c in schematic.components] + + + return root diff --git a/test/test_read_schematic.py b/test/test_read_schematic.py index 8edda3b..6d1add7 100644 --- a/test/test_read_schematic.py +++ b/test/test_read_schematic.py @@ -1,7 +1,7 @@ import pytest import sys import os.path -from ee.kicad import read_schematic +#from ee.kicad import read_schematic, to_bom from ee import kicad import ee @@ -26,17 +26,32 @@ def dump_schema(sch): for f in c.fields: print(" {}={}".format(f.name, f.value)) +def dump_bom(sch): + from xml.etree import ElementTree + from xml.dom import minidom + + print("---") + bom = kicad.to_bom(sch) + s = ElementTree.tostring(bom, encoding='unicode') + print(s) + xmlstr = minidom.parseString(s).toprettyxml(indent=" ") + print(xmlstr) + print("---") + def load(path): p = basedir + "/parser/" + path print("p={}".format(p)) - return read_schematic(p) + sch = kicad.read_schematic(p) + return sch def test_demo_1(): sch = load("parser-demo-1.sch") dump_schema(sch) + dump_bom(sch) r101 = sch.get_component("R101") assert r101 def test_sch(): sch = load("foo.sch") dump_schema(sch) + dump_bom(sch) -- cgit v1.2.3