# -*- 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("

KiBoM PCB Bill of Materials

\n") if not prefs.hidePcbInfo: html.write('\n') html.write("\n".format(source=net.getSource())) html.write("\n".format(date=net.getDate())) html.write("\n".format(version=net.getVersion())) html.write("\n".format(date=net.getSheetDate())) html.write("\n".format(variant=', '.join(prefs.pcbConfig))) html.write("\n".format(version=net.getTool())) html.write("\n".format(n=nGroups)) html.write("\n".format(n=nTotal)) html.write("\n".format(n=nFitted)) html.write("\n".format(n=prefs.boards)) html.write("\n".format(n=prefs.boards, t=nBuild)) 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}
\n") html.write("
\n") if not prefs.hideHeaders: html.write("

Component Groups

\n") html.write('

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') # Row titles: html.write("\n") if prefs.numberRows: html.write("\t\n") for i, h in enumerate(headings): # Cell background color bg = bgColor(h) html.write('\t\n'.format( h=h, bg=' bgcolor="{c}"'.format(c=bg) if bg else '') ) html.write("\n") rowCount = 0 for i, group in enumerate(groups): if prefs.ignoreDNF and not group.isFitted(): continue row = group.getRow(headings) rowCount += 1 html.write("\n") if prefs.numberRows: html.write('\t\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\n'.format(bg=' bgcolor={c}'.format(c=bg) if bg else '', val=link(r))) html.write("\n") html.write("
{h}
{n}{val}
\n") html.write("

\n") html.write("") return True