import sys import argparse import csv from . import mk_parents from ..kicad import pcb, parse_ref from .._utils import run_filters parser = argparse.ArgumentParser(description="Create a pick and place file from a KiCAD schematic") parser.add_argument("--kicad-pcb", required=True, dest="pcb", metavar="PCB", help="The pcb to read") parser.add_argument("--out", metavar="FILE", help="The output file") args = parser.parse_args() pcb = pcb.parse(args.pcb) def run(stream): rows = [] for m in pcb.modules: row = {} ref_fp_text = next(m.filter_fp_text(kind = "reference"), None) row["ref"] = ref= ref_fp_text and ref_fp_text.value row["r"] = parse_ref(ref) value_fp_text = next(m.filter_fp_text(kind = "value"), None) row["value"] = value_fp_text and value_fp_text.value (row["library"], row["footprint"]) = m.footprint.split(":") (row["x"], row["y"], row["rot"]) = m.at if m.layer == "F.Cu": row["side"] = "top" elif m.layer == "B.Cu": row["side"] = "bottom" else: row["side"] = None rows.append(row) rows = sorted(rows, key=lambda row: row["r"]) out = csv.writer(stream) out.writerow(["ref", "value", "library", "footprint", "x", "y", "rotation", "side"]) for row in rows: del row["r"] out.writerow(row.values()) if args.out: mk_parents(args.out) with open(args.out, "w") as f: run(f) else: run(sys.stdout)