import uuid from pathlib import Path from uuid import UUID 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, uris __all__ = [ "make_bom", ] def work(sch, out: Path, project_uuid: UUID, 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) count = 0 for c in components: count = count + 1 if c.has_ref_num: uri_ref = c.ref else: uri_ref = c.timestamp xml = types.Part(uri=uris.make_schematic_part_uri(project_uuid, uri_ref)) 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.is_custom and f.value is not None and len(f.value) and f.value != "~": part.facts.add(sch_fact_types.make_custom_field_key(f.name), f.value) parts.add_entry(part, True) save_db(out, parts) def make_bom(sch_file: Path, out_dir: Path, project_uuid: UUID, new_mode: bool, pretty: bool): sch = read_schematics(str(sch_file)) work(sch, out_dir, project_uuid, new_mode, pretty)