# -*- coding: utf-8 -*- from bomlib.component import ColumnList BG_GEN = "#E6FFEE" BG_KICAD = "#FFE6B3" BG_USER = "#E6F9FF" BG_EMPTY = "#FF8080" def bgColor(col): """ Return a background color for a given column title """ # Auto-generated columns if col in ColumnList._COLUMNS_GEN: return BG_GEN # KiCad protected columns elif col in ColumnList._COLUMNS_PROTECTED: return BG_KICAD # Additional user columns else: return BG_USER def link(text): for t in ["http", "https", "ftp", "www"]: if text.startswith(t): return '{t}'.format(t=text) return text def WriteHTML(filename, groups, net, headings, prefs): """ Write BoM out to a HTML file filename = path to output file (must be a .htm or .html file) groups = [list of ComponentGroup groups] net = netlist object headings = [list of headings to display in the BoM file] prefs = BomPref object """ if not filename.endswith(".html") and not filename.endswith(".htm"): print("{fn} is not a valid html file".format(fn=filename)) return False nGroups = len(groups) nTotal = sum([g.getCount() for g in groups]) nFitted = sum([g.getCount() for g in groups if g.isFitted()]) nBuild = nFitted * prefs.boards with open(filename, "w") as html: # HTML Header html.write("\n") html.write("
\n") html.write('\t\n') # UTF-8 encoding for unicode support html.write("\n") html.write("\n") # PCB info if not prefs.hideHeaders: html.write("Source File | {source} |
BoM Date | {date} |
Schematic Version | {version} |
Schematic Date | {date} |
PCB Variant | {variant} |
KiCad Version | {version} |
Component Groups | {n} |
Component Count (per PCB) | {n} |
Fitted Components (per PCB) | {n} |
Number of PCBs | {n} |
Total Component Count (for {n} PCBs) | {t} |
KiCad Fields (default)
\n'.format(bg=BG_KICAD)) html.write('Generated Fields
\n'.format(bg=BG_GEN)) html.write('User Fields
\n'.format(bg=BG_USER)) html.write('Empty Fields
\n'.format(bg=BG_EMPTY)) # Component groups html.write('\n") for i, h in enumerate(headings): # Cell background color bg = bgColor(h) html.write('\t | {h} | \n'.format( h=h, bg=' bgcolor="{c}"'.format(c=bg) if bg else '') ) html.write("
---|---|
{n} | \n'.format(n=rowCount)) for n, r in enumerate(row): if (len(r) == 0) or (r.strip() == "~"): bg = BG_EMPTY else: bg = bgColor(headings[n]) html.write('\t{val} | \n'.format(bg=' bgcolor={c}'.format(c=bg) if bg else '', val=link(r))) html.write("