Files

copied
Last update 4 years 8 months by Cliff Brake
Fileskicad-tools
..
__pycache__
bom_csv_grouped_by_value_with_fp.py
bom_csv_sorted_by_ref.py
kicad_bom_seeedstudio.py
kicad_netlist_reader.py
kicad_bom_seeedstudio.py
#!/usr/bin/env python3 import csv import sys import xml.etree.ElementTree as ET ### Natural key sorting for orders like : C1, C5, C10, C12 ... (instead of C1, C10, C12, C5...) # http://stackoverflow.com/a/5967539 import re def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments) ''' return [ atoi(c) for c in re.split('(\d+)', text) ] ### def parse_kicad_xml(input_file): """Parse the KiCad XML file and look for the part designators as done in the case of the official KiCad Open Parts Library: * OPL parts are designated with "SKU" (preferred) * other parts are designated with "MPN" """ components = {} parts = {} missing = [] tree = ET.parse(input_file) root = tree.getroot() for f in root.findall('./components/'): ref = f.attrib['ref'] opl, mpn, pn, datasheet = None, None, None, None fields = f.find('fields') datasheetF = f.find('datasheet') if datasheetF is not None: datasheet = datasheetF.text if fields is not None: for x in fields: if x.attrib['name'].upper() == 'SKU': opl = x.text elif x.attrib['name'].upper() == 'MPN': mpn = x.text if opl: pn = opl elif mpn: pn = mpn else: missing += [ref] continue components[ref] = pn if components[ref] not in parts: parts[pn] = {'datasheet':datasheet, 'refs':[]} parts[pn]['refs']+= [ref] return parts, missing def write_bom_seeed(output_file_slug, parts): """Write the BOM according to the Seeed Studio Fusion PCBA template available at: https://statics3.seeedstudio.com/assets/file/fusion/bom_template_2016-08-18.csv ``` Part/Designator,Manufacture Part Number/Seeed SKU,Quantity C1,RHA,1 "D1,D2",CC0603KRX7R9BB102,2 ``` The output is a CSV file at the `output_file_slug`.csv location. """ field_names = ['Designator', 'Manufacture Part Number or Seeed SKU', 'Qty', 'Link'] with open("{}.csv".format(output_file_slug), 'w') as csvfile: bomwriter = csv.DictWriter(csvfile, fieldnames=field_names, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) bomwriter.writeheader() for pn in sorted(parts.keys()): part = parts[pn] refs = sorted(part['refs'], key=natural_keys) refsSorted = ",".join(refs) bomwriter.writerow({'Designator': refsSorted, 'Manufacture Part Number or Seeed SKU': pn, 'Qty': len(refs), 'Link': part['datasheet']}) if __name__ == "__main__": input_file = sys.argv[1] output_file = sys.argv[2] parts, missing = parse_kicad_xml(input_file) write_bom_seeed(output_file, parts) if len(missing) > 0: print("** Warning **: there were parts with missing SKU/MFP") print(missing)
Report a bug