aboutsummaryrefslogtreecommitdiff
path: root/src/ee/kicad
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 /src/ee/kicad
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.
Diffstat (limited to 'src/ee/kicad')
-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
3 files changed, 62 insertions, 1 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