KiCad/bom_csv_Adcis.py
2021-07-01 14:07:23 +02:00

183 lines
5.2 KiB
Python

"""
@package
Generate a colon delimited list (csv file type).
Components are sorted by ref and grouped by value with same footprint
Fields are (if exist)
'Ref', 'Quantity', 'Value', 'Part, Package'
Command line:
python "pathToFile/bom_csv_Adcis.py" "%I" "%O"
"""
# Import the KiCad python helper module and the csv formatter
import kicad_netlist_reader
import csv
import sys
# Generate an instance of a generic netlist, and load the netlist tree from
# the command line option. If the file doesn't exist, execution will stop
net = kicad_netlist_reader.netlist(sys.argv[1])
# Open a file to write to, if the file cannot be opened output to stdout
# instead
try:
f = open(sys.argv[2]+'-BOM.csv', 'w')
except IOError:
e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr)
f = sys.stdout
try:
if sys.argv[3] == 'osaz':
try:
osaz = open(sys.argv[2]+'-Osazeni.csv', 'w')
out_osaz = csv.writer(osaz, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
out_osaz.writerow(['Ref', 'Part', 'Quantity'])
except IOError:
e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr)
f = sys.stdout
except:
pass
# Create a new csv writer object to use as the output formatter
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
# Output a set of rows for a header providing general information
out.writerow(['Date:', net.getDate()])
out.writerow(['Tool:', net.getTool()])
out.writerow(['Component Count:', len(net.components)])
out.writerow([''])
out.writerow(['Ref', 'Quantity', 'Value', 'Part', 'Package'])
# Get all of the components in groups of matching parts + values
# (see ky_generic_netlist_reader.py)
grouped = net.groupComponents()
# Output all of the component information
for group in grouped:
refs = ""
# Add the reference of every component in the group and keep a reference
# to the component so that the other data can be filled in once per group
for component in group:
refs += component.getRef() + ", "
c = component
refs = refs[:-2]
prt_a = c.getValue()
prt_b = c.getFootprint()
prt_c = c.getPartName()
prefix = ''
part = ''
name = ''
footprint = ''
package = ''
if 'Tantalum' in prt_b:
prefix = 'CTS '
part = 'tantal'
prt_a = prt_a.replace('F','')
elif 'C' == prt_c:
prefix = 'CK '
part = 'c'
prt_a = prt_a.replace('F','')
elif 'CP' == prt_c:
if prefix != 'CTS ':
prefix = 'CK '
part = 'elit'
prt_a = prt_a.replace('F','')
elif 'R' == prt_c:
prefix = 'R '
part = 'r'
elif ('D' == prt_c) or (prt_c == 'LED'):
part = 'd'
elif 'L' == prt_c:
part = 'l'
else:
prefix = ''
if part != 'l':
prt_a = prt_a.upper()
if (prt_c == 'C') or (prt_c == 'CP'):
if 'U' in prt_a:
prt_a = prt_a.replace('U','M')
if part == 'tantal':
if 'Kemet' in prt_b:
x = prt_b.split('Kemet-')
footprint = x[1][0]
package = footprint
elif part == 'c':
x = prt_b.split('_')
footprint = x[2]
package = footprint
elif part == 'r':
x = prt_b.split('_')
package = x[2]
if '%' in prt_a:
y = prt_a.split(' ')
prt_a = y[0]
footprint = x[2]+' '+y[1]
else:
footprint = x[2]+' 5%'
elif part == 'elit':
x = prt_b.split('_')
footprint = x[3]+'/'+x[4]
package = footprint
else:
footprint = ''
name = prt_a
if 'Connector' in prt_b:
x = prt_b.split(':')
name = x[1]
if 'PinHeader' in prt_b:
x = prt_c.split('_')
if len(x) > 1:
x = x[1].split('x')
if x[0][0] == '0':
x[0] = x[0].replace('0','',1)
if x[1][0] == '0':
x[1] = x[1].replace('0','',1)
x[1] = str(int(x[0])*int(x[1]))
name = 'S'+x[0]+'G'+x[1]
else:
name = prt_c
if part == 'd':
name = prt_a
x = prt_b.split(':')
x = x[1].split('_')
footprint = x[1]
package = x[1]
if 'Package' in prt_b:
x = prt_b.split(':')
x = x[1].split('_')
package = x[0]
if 'Crystal' in prt_b:
x = prt_b.split('_')
package = x[1]
part = prefix+name+' '+footprint
try:
if c.getField('Rozpiska') != '':
part = c.getField('Rozpiska')
except:
pass
# Fill in the component groups common data
if ('TP' not in refs) and ('NC ' not in prt_a):
out.writerow([refs, len(group), c.getValue(), part, package])
try:
if sys.argv[3] == 'osaz':
out_osaz.writerow([refs, part, len(group)])
except:
pass