|
| 1 | +# Sorted and Grouped HTML BOM |
| 2 | +# |
| 3 | +""" |
| 4 | + @package |
| 5 | + Generate a HTML BOM list. |
| 6 | + Components are sorted by ref and grouped by value |
| 7 | + Fields are (if exist) |
| 8 | + Ref, Quantity, Value, Part, Datasheet, Description, Vendor, Farnel Ref |
| 9 | +
|
| 10 | + Command line: |
| 11 | + python "pathToFile/bom_html_grouped_by_value.py" "%I" "%O.html" |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import print_function |
| 15 | + |
| 16 | +# Import the KiCad python helper module and the csv formatter |
| 17 | +import kicad_netlist_reader |
| 18 | +import sys |
| 19 | + |
| 20 | +# Start with a basic html template |
| 21 | +html = """ |
| 22 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 23 | + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 24 | +<html xmlns="http://www.w3.org/1999/xhtml"> |
| 25 | + <head> |
| 26 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 27 | + </head> |
| 28 | + <style> |
| 29 | + table, th, td { |
| 30 | + border: 1px solid black; |
| 31 | + border-collapse: collapse; |
| 32 | + } |
| 33 | + </style> |
| 34 | + <body> |
| 35 | + <p><!--COMPCOUNT--></p> |
| 36 | + <table> |
| 37 | + <!--TABLEROW--> |
| 38 | + </table> |
| 39 | + </body> |
| 40 | +</html> |
| 41 | + """ |
| 42 | + |
| 43 | +# Generate an instance of a generic netlist, and load the netlist tree from |
| 44 | +# the command line option. If the file doesn't exist, execution will stop |
| 45 | +net = kicad_netlist_reader.netlist(sys.argv[1]) |
| 46 | + |
| 47 | +# Open a file to write to, if the file cannot be opened output to stdout |
| 48 | +# instead |
| 49 | +try: |
| 50 | + f = open(sys.argv[2], 'w') |
| 51 | +except IOError: |
| 52 | + e = "Can't open output file for writing: " + sys.argv[2] |
| 53 | + print(__file__, ":", e, file=sys.stderr) |
| 54 | + f = sys.stdout |
| 55 | + |
| 56 | +components = net.getInterestingComponents() |
| 57 | + |
| 58 | +# Output a set of rows for a header providing general information |
| 59 | +html = html.replace('<!--COMPCOUNT-->', "<b>Component Count:</b>" + \ |
| 60 | + str(len(components))) |
| 61 | + |
| 62 | +row = "<tr><th style='width:640px'>Ref</th>" |
| 63 | +row += "<th>Qnty</th>" |
| 64 | +row += "<th>Value</th>" + "<th>Footprint</th>" |
| 65 | +row += "<th>Description</th>" |
| 66 | +row += "<th>Farnell Ref</th></tr>" |
| 67 | + |
| 68 | +html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->") |
| 69 | + |
| 70 | +# Get all of the components in groups of matching parts + values |
| 71 | +# (see kicad_netlist_reader.py) |
| 72 | +grouped = net.groupComponents(components) |
| 73 | + |
| 74 | +# Output all of the component information |
| 75 | +for group in grouped: |
| 76 | + refs = "" |
| 77 | + |
| 78 | + # Add the reference of every component in the group and keep a reference |
| 79 | + # to the component so that the other data can be filled in once per group |
| 80 | + for component in group: |
| 81 | + if len(refs) > 0: |
| 82 | + refs += ", " |
| 83 | + refs += component.getRef() |
| 84 | + c = component |
| 85 | + |
| 86 | + row = "<tr><td>" + refs +"</td><td>" + str(len(group)) |
| 87 | + row += "</td><td>" + c.getValue() |
| 88 | + row += "</td><td>" + c.getFootprint() |
| 89 | + row += "</td><td>" + c.getDescription() |
| 90 | + row += "</td><td>" + c.getField("Farnell Ref") + "</td></tr>" |
| 91 | + |
| 92 | + html = html.replace('<!--TABLEROW-->', row + "<!--TABLEROW-->") |
| 93 | + |
| 94 | +# Print the formatted html to the file |
| 95 | +print(html, file=f) |
0 commit comments