From 9eba62ef1d6b4896de693976116f69a9692332d9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 20 May 2019 15:27:38 +0200 Subject: ee: o Adding FactType as a smaller wrapper around the fact uri. o Adding ee.part.Facts, used as Part.facts o Renaming 'type' uri to 'ee-component-type'. kicad-make-bom: Removing strategy functionality, replaced with part-apply-function. Moving default strategy contents into ee.kicad.functions. --- src/ee/kicad/functions.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/ee/kicad/functions.py (limited to 'src/ee/kicad/functions.py') diff --git a/src/ee/kicad/functions.py b/src/ee/kicad/functions.py new file mode 100644 index 0000000..39614b6 --- /dev/null +++ b/src/ee/kicad/functions.py @@ -0,0 +1,105 @@ +import re + +import ee.kicad.model +import ee.kicad.sch_fact_types as kicad_ft +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 + + +def part_type_strategy(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 + + +def fix_value_strategy(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 == "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 + + +def mpn_strategy(part: Part) -> Part: + for field in part.facts.all(sch_fact_types.field): + + k, v = re.split(":", field.value, 1) + if k == "mpn": + part.add_mpn(v) + + return part + + +def default(part: Part) -> Part: + functions = [ + fix_value_strategy, + mpn_strategy, + part_type_strategy, + ] + + for f in functions: + part = f(part) + + return part -- cgit v1.2.3