import re import ee.kicad.model import ee.kicad.sch_fact_types as kicad_ft from ee import EeVal from ee.kicad import sch_fact_types from ee.part import Part from ee.part import common_fact_types from ee.xml import uris def part_type_from_footprint(lib): if lib == "Capacitor_SMD": return uris.CAPACITOR elif lib == "Resistor_SMD": return uris.RESISTOR elif lib == "Diode_SMD": return uris.DIODE elif lib == "Inductor_SMD": return uris.INDUCTOR elif lib == "Crystal": return uris.CRYSTAL def part_type_from_ref_type(ref_type): if ref_type == "C": return uris.CAPACITOR elif ref_type == "R": return uris.RESISTOR elif ref_type == "D": return uris.DIODE elif ref_type == "L": return uris.INDUCTOR elif ref_type == "X": return uris.CRYSTAL elif ref_type == "Q": return uris.TRANSISTOR # noinspection PyUnusedLocal def part_type_strategy(**kwargs): def on_part(part: Part) -> Part: pt = None fp_lib = part.facts.get_value(kicad_ft.footprint_library) if fp_lib is not None: pt = part_type_from_footprint(fp_lib) ref = part.get_only_schematic_reference() if ref: ref_type, ref_num = ee.kicad.model.split_ref(ref.referenceProp) if ref_type: pt = part_type_from_ref_type(ref_type) if pt is not None: part.facts.add(common_fact_types.ee_component_type, pt) return part return on_part # noinspection PyUnusedLocal def fix_value_strategy(**kwargs): def on_part(part: Part) -> Part: ref = part.get_only_schematic_reference() if not ref: return part ref_type, ref_num = ee.kicad.model.split_ref(ref.referenceProp) if not ref_num: return part v = part.facts.get_value(sch_fact_types.value) if not v: return part symbol_name = part.facts.get_value(sch_fact_types.symbol_name) # if ref_type in ("D", "R", "L", "C") and v == symbol_name: # part.remove_fact(uris.make_fact_key("value")) if ref_type == "R": ee_value = EeVal.try_parse(v) if ee_value: part.facts.add(common_fact_types.resistance, str(ee_value.value)) if ref_type == "C": ee_value = EeVal.try_parse(v) if ee_value: part.facts.add(common_fact_types.capacitance, str(ee_value.value)) if ref_type == "L": ee_value = EeVal.try_parse(v) if ee_value: part.facts.add(common_fact_types.inductance, str(ee_value.value)) # if ref_type == "Q" and v == symbol_name and re.match("^Q_[NP]MOS_[DSG]{3}$", symbol_name): # part.remove_fact(uris.make_fact_key("value")) return part return on_part # noinspection PyUnusedLocal def mpn_strategy(**kwargs): def on_part(part: Part) -> Part: for field in part.facts.all(sch_fact_types.field): k, v = re.split(":", field.value, 1) if k.lower() == "mpn": part.add_mpn(v) return part return on_part # noinspection PyUnusedLocal def map_footprint(footprint_mappings=None, **kwargs): # noinspection PyProtectedMember from ee.part._utils import map_footprint return map_footprint(footprint_mappings, "kicad-to-common", kicad_ft.footprint) def default(**kwargs): function_factories = [ fix_value_strategy, mpn_strategy, part_type_strategy, map_footprint, ] functions = [factory(**kwargs) for factory in function_factories] functions = [f for f in functions if f is not None] def on_part(part: Part) -> Part: for f in functions: part = f(part) return part return on_part