from pathlib import Path from xml.dom import minidom from xml.etree import ElementTree from ee.kicad import sch_fact_types from ee.kicad.read_schematic import read_schematics from ee.kicad.to_bom import to_bom, to_bom_xml from ee.part import PartDb, save_db, Part from ee.xml import types __all__ = [ "make_bom", ] def work(sch, out: Path, new_mode, pretty): def strip(s): s = (s or "").strip() return None if len(s) == 0 else s if not new_mode: bom = to_bom_xml(sch) xml = ElementTree.tostring(bom, encoding="unicode") if pretty: xml = minidom.parseString(xml).toprettyxml(indent=" ") print(xml) else: parts = PartDb() components = to_bom(sch, require_ref=False) for c in components: xml = types.Part() part = Part(xml) if c.has_ref_num: part.add_schematic_reference(c.ref) value = strip(c.value) if value: part.facts.add(sch_fact_types.value, value) if c.symbol.library: part.facts.add(sch_fact_types.symbol_library, c.symbol.library) part.facts.add(sch_fact_types.symbol_name, c.symbol.name) footprint = strip(c.footprint) if footprint: part.facts.add(sch_fact_types.footprint, footprint) i = footprint.find(":") if i >= 0: lib, footprint = footprint.split(":") part.facts.add(sch_fact_types.footprint_library, lib) part.facts.add(sch_fact_types.footprint_name, footprint) for f in c.named_fields: if f.value is not None and len(f.value): part.facts.add(sch_fact_types.field, "{}:{}".format(f.name, f.value)) parts.add_entry(part, True) save_db(out, parts) def make_bom(sch_file: Path, out_dir: Path, new_mode: bool, pretty: bool): sch = read_schematics(str(sch_file)) work(sch, out_dir, new_mode, pretty)