2013-05-26 04:36:44 +00:00
|
|
|
#
|
|
|
|
# Example python script to generate a BOM from a KiCad generic netlist
|
|
|
|
#
|
|
|
|
# Example: Tab delimited list (The same as std output) Ungrouped
|
|
|
|
#
|
|
|
|
|
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:
|
2013-09-01 17:49:01 +00:00
|
|
|
print(__file__, ":", e, file=sys.stderr)
|
2013-05-26 04:36:44 +00:00
|
|
|
f = stdout
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
utf8row.append( str(col).encode('utf8') )
|
|
|
|
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)] )
|
2013-09-19 23:50:30 +00:00
|
|
|
writerow( out, ['Ref', 'Value', 'Part', 'Documentation', '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(),
|
2013-05-26 04:36:44 +00:00
|
|
|
c.getDatasheet(), c.getDescription(), c.getField("Vendor")])
|