2013-05-26 04:36:44 +00:00
|
|
|
#
|
|
|
|
# Example python script to generate a BOM from a KiCad generic netlist
|
|
|
|
#
|
2014-12-03 16:22:06 +00:00
|
|
|
"""
|
|
|
|
@package
|
2015-02-28 16:56:09 +00:00
|
|
|
Generate a BOM list file.
|
|
|
|
Components are sorted by ref
|
2014-12-03 16:22:06 +00:00
|
|
|
One component per line
|
|
|
|
Fields are (if exist)
|
2015-03-02 15:59:03 +00:00
|
|
|
Ref, Quantity, value, Part, footprint, Description, Vendor
|
2015-02-28 16:56:09 +00:00
|
|
|
Fields are separated by tabs
|
2014-12-03 16:22:06 +00:00
|
|
|
"""
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2013-09-01 17:49:01 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
# Import the KiCad python helper module and the csv formatter
|
2013-09-01 17:49:01 +00:00
|
|
|
import kicad_netlist_reader
|
2013-05-26 04:36:44 +00:00
|
|
|
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
|
2013-09-01 17:49:01 +00:00
|
|
|
net = kicad_netlist_reader.netlist(sys.argv[1])
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
# Open a file to write to, if the file cannot be opened output to stdout
|
|
|
|
# instead
|
|
|
|
try:
|
|
|
|
f = open(sys.argv[2], 'w')
|
|
|
|
except IOError:
|
2014-12-03 16:22:06 +00:00
|
|
|
e = "Can't open output file for writing: " + sys.argv[2]
|
|
|
|
print(__file__, ":", e, sys.stderr)
|
|
|
|
f = sys.stdout
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
# Create a new csv writer object to use as the output formatter, although we
|
|
|
|
# are created a tab delimited list instead!
|
2013-06-14 14:59:52 +00:00
|
|
|
out = csv.writer(f, lineterminator='\n', delimiter='\t', quoting=csv.QUOTE_NONE)
|
2013-05-26 04:36:44 +00:00
|
|
|
|
2013-09-19 23:50:30 +00:00
|
|
|
# override csv.writer's writerow() to support utf8 encoding:
|
|
|
|
def writerow( acsvwriter, columns ):
|
|
|
|
utf8row = []
|
|
|
|
for col in columns:
|
2015-02-28 16:56:09 +00:00
|
|
|
txt=str(col);
|
2015-06-26 17:52:49 +00:00
|
|
|
utf8row.append( txt )
|
2013-09-19 23:50:30 +00:00
|
|
|
acsvwriter.writerow( utf8row )
|
|
|
|
|
2013-09-20 15:11:45 +00:00
|
|
|
components = net.getInterestingComponents()
|
|
|
|
|
2013-05-26 04:36:44 +00:00
|
|
|
# Output a field delimited header line
|
2013-09-19 23:50:30 +00:00
|
|
|
writerow( out, ['Source:', net.getSource()] )
|
|
|
|
writerow( out, ['Date:', net.getDate()] )
|
|
|
|
writerow( out, ['Tool:', net.getTool()] )
|
2013-09-20 15:11:45 +00:00
|
|
|
writerow( out, ['Component Count:', len(components)] )
|
2015-03-02 15:59:03 +00:00
|
|
|
writerow( out, ['Ref', 'Value', 'Part', 'Footprint', 'Description', 'Vendor'] )
|
2013-05-26 04:36:44 +00:00
|
|
|
|
|
|
|
# Output all of the component information
|
2013-09-01 17:49:01 +00:00
|
|
|
for c in components:
|
2013-09-19 23:50:30 +00:00
|
|
|
writerow( out, [c.getRef(), c.getValue(), c.getLibName() + ":" + c.getPartName(),
|
2015-03-02 15:59:03 +00:00
|
|
|
c.getFootprint(), c.getDescription(), c.getField("Vendor")])
|