from typing import Iterable, Union from xml.etree.ElementTree import Element from ee.kicad.model import * __all__ = ["to_bom", "to_bom_xml"] def simple_element(parent, e, text): if not text or 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: Union[Schematic, Schematics]) -> Iterable[Component]: return [c for c in sorted(schematic.components) if c.has_ref_num and c.ref_type != "#PWR" and c.ref_type != "#FLG"] def to_bom_xml(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 to_bom(schematic)] return root