import logging import os.path from pathlib import Path from typing import Mapping from configclass import Config import ee.kicad import ee.kicad.pcb from ee.doit import DoItConfig logger = logging.getLogger(__name__) _config_template = Config({ "sch": None, "kicad_pcb": None, "gerber_dir": None, "gerber_zip": None, }) doit_config = DoItConfig() _config = None # type: Mapping[str, str] def init(**kwargs): global _config _config = _config_template.make(kwargs) def task_kicad_gerber(): kicad_pcb = _config["kicad_pcb"] gerber_dir = _config["gerber_dir"] gerber_zip = _config["gerber_zip"] gerber_zip = gerber_zip or "{}.zip".format(gerber_dir) # logger.info("gerber_zip={}".format(gerber_zip)) eg = next((p for p in (os.path.join(p, "export_gerber.py") for p in ee.kicad.__path__) if os.path.isfile(p)), None) if not eg: raise Exception("Could not find export_gerber.py") # TODO: replace with python mkdir = "mkdir -p {}".format(gerber_dir) export_gerber = " ".join([ eg, "--pcb", kicad_pcb, "--output-directory", gerber_dir, "--protel-extensions", ]) def make_zip(): import zipfile from pathlib import Path with zipfile.ZipFile(gerber_zip, "w") as z: for p in Path(gerber_dir).iterdir(): if not p.is_file(): continue z.write(p, arcname=p.relative_to(gerber_dir)) return { "targets": [gerber_zip], "actions": [mkdir, export_gerber, make_zip], "file_dep": [kicad_pcb], } def task_kicad_sch_to_data_set(): out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_sch_to_data_set) sch = _config["sch"] def action(): from ee.kicad.model import ComponentField with doit_config.dsm.create_rw(out_data_set, clean=True) as ds: schematics = ee.kicad.read_schematics(sch) for c in [c for c in schematics.components]: o = ds.create_object("kicad-schematic-component", c.timestamp) o.set("ref", c.ref) o.set("ref-type", c.ref_type) if c.has_ref_num: o.set("ref-num", str(c.ref_num)) o.set("value", c.value) if c.footprint: o.set("footprint", c.footprint) for f in c.fields: if f.value and f.name not in ComponentField.names: o.set("field-{}".format(f.name), str(f.value)) return { "file_dep": [Path(sch)] + [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } doit_config.set_data_sets_for(task_kicad_sch_to_data_set, "kicad-sch") def task_kicad_pcb_to_data_set(): kicad_pcb = _config["kicad_pcb"] out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_pcb_to_data_set) def action(): from ee.kicad.pcb import KicadPcb, Module logger.debug("Parsing PCB {}".format(kicad_pcb)) with doit_config.dsm.create_rw(out_data_set, clean=True) as ds: # [ds.delete(o) for o in ds.items(object_type="kicad-pcb-component")] pcb = ee.kicad.pcb.parse(kicad_pcb) # type: KicadPcb for _m in pcb.modules: m = _m # type: Module o = ds.create_object("kicad-pcb-component", m.tstamp) ref_text = next((t for t in m.fp_texts if t.kind == "reference"), None) o.set("ref", ref_text.value) x, y, rot = m.at o.set("placement-x", x) o.set("placement-y", y) o.set("placement-rotation", rot) o.set("layer", m.layer) return { "file_dep": [Path(kicad_pcb)] + [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } doit_config.set_data_sets_for(task_kicad_pcb_to_data_set, "kicad-pcb") def task_kicad_create_component_data_set(): out_data_set, in_data_sets = doit_config.data_sets_for(task_kicad_create_component_data_set) def action(): in_ds = doit_config.dsm.load_data_sets(in_data_sets) # for o in in_ds.items(): # logger.info("item: {}/{}".format(o.object_type.name, o.key)) def map_footprint(footprint): for o in in_ds.items(): if not o.object_type.name == "kicad-footprint-mapping": continue common = o.get("common") if common: return common return footprint with doit_config.dsm.create_rw(out_data_set, clean=True) as output: kicad_sch = [o for o in in_ds.items() if o.object_type.name == "kicad-schematic-component"] logger.info("processing {} kicad-sch".format(len(kicad_sch))) ignored_ref_types = {"#PWR", "#FLG"} for sch in kicad_sch: ref = sch.get("ref") ref_num = sch.get("ref-num") if not ref or not ref_num: logger.debug("Missing ref or ref-num") continue ref_type = sch.get("ref-type") if not ref_type: logger.debug("Missing ref-type") continue if ref_type in ignored_ref_types: continue c = output.create_object("component", ref) c.set("ref", ref) c.set("ref-num", ref_num) c.set("ref-type", ref_type) fp = sch.get("footprint") if fp: fp = map_footprint(fp) c.set("footprint", fp) c.set("mpn", sch.get("field-mpn")) c.set("distributor", sch.get("field-distributor")) def pcb_match(o): return o.object_type.name == "kicad-pcb-component" and \ o.get("ref") == ref pcb = [o for o in in_ds.items() if pcb_match(o)] if not pcb: logger.info("Could not find PCB component for {}".format(ref)) # TODO: check that the SCH and PCB footprint are the same # c.set("footprint", pcb.) return { "file_dep": [doit_config.dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], "targets": [doit_config.dsm.cookie_for_ds(out_data_set)], } doit_config.set_data_sets_for(task_kicad_create_component_data_set, "components", "kicad-sch", "kicad-pcb") __all__ = [ init.__name__, task_kicad_create_component_data_set.__name__, task_kicad_gerber.__name__, task_kicad_pcb_to_data_set.__name__, task_kicad_sch_to_data_set.__name__, ]