from pathlib import Path from ee.part import PartDb, load_db, save_db, Part, fact_keys from ee.xml import types, uris __all__ = ["pn_part_search_list"] ignored_part_classes = [ "mechanical" ] def valid_pns(part: Part): """Check if the part has any MPNs or SPNs""" return len(part.get_mpns()) > 0 or len(part.get_spns()) > 0 def get_value(part: Part): """Check if the part has a value and it is not a resistor, capacitor or inductor. Their value is not useful for a part number-based lookup""" value = part.find_fact(uris.make_fact_key("value")) if value is None: return typ = part.find_fact(uris.make_fact_key("type")) if typ is None: return # if type is Zener, it's value could be a voltage return value.valueProp if typ.valueProp not in (uris.RESISTOR, uris.CAPACITOR, uris.INDUCTOR) else None def pn_part_search_list(in_path: Path, out_path: Path, supplier: str): in_parts = load_db(in_path) out_parts = PartDb() count = 0 skipped = list() for xml in in_parts.iterparts(): count += 1 part = Part(xml) refs = [ref.referenceProp for ref in part.get_schematic_references()] part_class = part.find_fact(fact_keys.part_class) if part_class: if part_class.valueProp in ignored_part_classes: skipped.append(refs) continue if not valid_pns(part): value = get_value(part) if not value: skipped.append(refs) continue # Use the value of the component as a part number to search for xml.referencesProp.part_number.append(types.PartNumber(value=value)) new_part = types.Part() new_part.referencesProp = xml.referencesProp out_parts.add_entry(new_part, True) if len(skipped) > 0: print("Skipped {} of {} parts".format(len(skipped), count)) save_db(out_path, out_parts)