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], require_ref=True) -> Iterable[Component]: bad_ref_types = ("#PWR", "#FLG") cs = [] for c in sorted(schematic.components): if require_ref and not c.has_ref_num: continue if c.has_ref_num and c.ref_type in bad_ref_types: continue cs.append(c) return cs 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