New Design Review SpecLib/BOM scripts/spectrum_bom_general.py

52 lines
1.6 KiB
Python

# BOM generation script for KiCAD (6+)
# Last update: 26/03/2025
# Author: Jorian Zwerver
# This scripts formats the BOM to the format of SPECTRUM
"""
@package
Generate a comma delimited list (csv file type) in SPECTRUM style.
Components are sorted by reference and grouped by value.
Fields are (if exist)
'References', 'Value', 'MPN', 'Manufacturer', 'Description', 'Farnell', 'Mouser', 'Digikey', 'LCSC', 'Quantity'
Command line:
python "pathToFile/SpecLib/BOM scripts/spectrum_bom_general.py" "%I" "%O_BOM_GENERAL.csv"
"""
# Import libraries
from libs import kicad_netlist_reader
import csv
import sys
net = kicad_netlist_reader.netlist(sys.argv[1])
try:
f = open(sys.argv[2], 'w')
except IOError:
e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr)
f = sys.stdout
# Generate new CSV file with header
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
out.writerow(['References', 'Value', 'MPN', 'Manufacturer', 'Description', 'Farnell', 'Mouser', 'Digikey', 'LCSC', 'Quantity'])
grouped = net.groupComponents()
for group in grouped:
refs = ", ".join(component.getRef() for component in group)
component = group[0]
out.writerow([
refs,
component.getValue(),
component.getField('MPN'),
component.getField('Manufacturer'),
component.getDescription(),
component.getField('Farnell'),
component.getField('Mouser'),
component.getField('Digikey'),
component.getField('LCSC'),
len(group)
])