aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-09 22:10:07 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-09 22:10:07 +0200
commitc96bc5755a68d8e317d96be31b330cc0b626c194 (patch)
treebc35085a635aab4116ed4de611efa529902a5c2b
parent542ec502c9b9619c005bc73e562229e4547ab90b (diff)
downloadee-python-c96bc5755a68d8e317d96be31b330cc0b626c194.tar.gz
ee-python-c96bc5755a68d8e317d96be31b330cc0b626c194.tar.bz2
ee-python-c96bc5755a68d8e317d96be31b330cc0b626c194.tar.xz
ee-python-c96bc5755a68d8e317d96be31b330cc0b626c194.zip
to_bom: A utility to create a KiCAD-like xml Element of a KiCAD schematic.
-rw-r--r--src/ee/kicad/__init__.py2
-rw-r--r--src/ee/kicad/model.py14
-rw-r--r--src/ee/kicad/to_bom.py47
-rw-r--r--test/test_read_schematic.py19
4 files changed, 79 insertions, 3 deletions
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,7 +14,7 @@ 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]
@@ -22,6 +22,10 @@ class ComponentField(object):
self._position = position
@property
+ def index(self):
+ return self._index
+
+ @property
def name(self):
return self._name
@@ -52,6 +56,14 @@ class Component(object):
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)
+ # <libsource lib="A64-OlinuXino_Rev_C-cache" part="C"/>
+ # <sheetpath names="/" tstamps="/"/>
+ # <tstamp>561E4EAA</tstamp>
+ 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)