Interactive HTML BOM

repository·master·Indexed 26 days ago

https://github.com/openscopeproject/interactivehtmlbom

A KiCad plugin that generates a searchable, self-contained HTML Bill of Materials. It allows users to visually cross-reference components between a BOM table and a PCB drawing, featuring net highlighting and configurable table fields. The tool can be used as a KiCad plugin or via a CLI entrypoint (generate_interactive_bom.py) with support for INI configuration and headless mode.

Tokens
2.6K
Snippets
5
Records
16
Agent score
89%

What's inside InteractiveHtmlBom

  1. Overview of Interactive HTML BOM

    master

    Interactive HTML BOM is a KiCad plugin that generates a self-contained HTML Bill of Materials (BOM). It allows users to visually correlate components in a list with their physical placements on a PCB drawing.

    Key features include:

    • Visual Correlation: Click a component in the BOM to highlight it on the board, or click a footprint on the board to find its component details.
    • Rich Rendering: Uses the Pcbnew Python API to render silkscreen, fab layers, footprint pads, text, and drawings.
    • Configurable BOM: Fully configurable table fields and grouping. Supports adding custom columns (e.g., manufacturer IDs) via netlist files, Eeschema XML files, or the board file itself.
    • Net Highlighting: Can include tracks/zones and netlist data to allow dynamic highlighting of specific nets on the board.
    • Self-Contained Output: The generated HTML page is fully self-contained and does not require an internet connection, making it suitable for local documentation or web hosting.
  2. Use the InteractiveHtmlBom CLI to generate BOMs

    master

    The generate_interactive_bom.py script is the CLI entrypoint for the KiCad InteractiveHtmlBom plugin. It allows you to generate an interactive HTML Bill of Materials from a KiCad PCB file.

    Basic Usage:

    python3 generate_interactive_bom.py <path_to_kicad_pcb_file>

    Key Features:

    • INI Configuration: Use the --use-ini flag to load settings from an .ini file. Command-line arguments will override values found in the INI file.
    • Dialog Mode: Use the --show-dialog flag to trigger a GUI dialog for configuration (requires wxpython and that the INTERACTIVE_HTML_BOM_NO_DISPLAY environment variable is NOT set).
    • Headless Mode: To run without a display (e.g., in CI/CD), set the INTERACTIVE_HTML_BOM_NO_DISPLAY environment variable.
  3. Configure InteractiveHtmlBom via CLI and INI

    master

    The CLI supports configuration through three layers of precedence:

    1. Command-line arguments (highest priority)
    2. INI file settings (loaded via --use-ini)
    3. Default settings (lowest priority)

    When using --use-ini, the script performs a two-pass parse: it first identifies the PCB file and the presence of the --use-ini flag, loads the INI defaults into the parser, and then performs a final parse to allow command-line overrides.

  4. Reference the `footprint` struct

    master

    Footprints are collections of pads, drawings, and metadata.

    Key Fields:

    • ref: Component reference.
    • center: [x, y] coordinates.
    • bbox: Bounding box containing pos, angle, relpos, and size.
    • pads: Array of pad objects.
      • layers: F, B, or both.
      • shape: rect, oval, circle, roundrect, chamfrect, or custom.
      • type: th (through-hole) or smd (surface mount).
      • pin1: Boolean, indicates if this is the first pin (based on name or lexicographical order).
      • drillsize: For th pads, [x, y] (where x is diameter).
      • chamfpos: Bitmask for chamfered corners (left=1, right=2, bottom left=4, bottom right=8).
    • drawings: Array of copper drawings (F or B) associated with the footprint.
    • layer: The primary layer (F or B).
  5. Understand the `pcbdata` JSON structure

    master

    The pcbdata object is the primary data structure extracted from a PCB file and injected into the generated BOM page. It contains the board's geometry, components, and metadata.

    Key Conventions:

    • Coordinate System: Origin is at the top-left corner; Y grows downwards.
    • Angles: Measured in degrees, clockwise from the positive X-axis.
    • Units: Preferred to be mm or mils to ensure browser compatibility.

    Top-level keys:

    • edges_bbox: Bounding box of all edge cut drawings (used for default zoom/pan).
    • edges: Array of edge cut drawings.
    • drawings: Grouped silkscreen and fabrication drawings (keyed by layer F or B).
    • footprints: Array of component footprints (indexed by numeric ID).
    • tracks: Optional track and via data (keyed by layer F or B).
    • zones: Optional copper zone data (keyed by layer F or B).
    • nets: List of net names.
    • metadata: PCB title block information (title, revision, company, date, variant).
    • bom: Bill of Materials data, including both, F, B, skipped (DNP components), and fields (component field data).
    • font_data: Parsed stroke data for PCB characters.
  6. Reference the `bom` row and `config` structs

    master

    BOM Row Structure

    A BOM row is a list of reference sets. A reference set is an array of tuples containing [reference_name, footprint_id] to handle duplicate references.

    Configuration Structure

    The config object controls the plugin's UI and rendering behavior:

    config = {
      "dark_mode": bool,
      "show_pads": bool,
      "show_fabrication": bool,
      "show_silkscreen": bool,
      "highlight_pin1": "none" | "all" | "selected",
      "redraw_on_drag": bool,
      "board_rotation": int,
      "checkboxes": "checkbox1,checkbox2,...",
      "bom_view": "bom-only" | "left-right" | "top-bottom",
      "layer_view": "F" | "FB" | "B",
      "extra_fields": ["field1_name", "field2_name", ...],
    }
  7. Reference the `drawing` struct for graphical items and text

    master

    All drawings in the pcbdata structure are either graphical items or text. Rendering is determined by the type attribute.

    Graphical Items

    • segment: Line segment defined by start, end, and width.
    • rect: Rectangle defined by opposing corners start and end, and width.
    • circle: Circle defined by start (center), radius, and optional filled (boolean) and width.
    • arc: Arc defined by start (center), radius, startangle, and endangle. Can also use an svgpath.
    • curve: Bezier curve defined by start, end, cpa (control point A), cpb (control point B), and width.
    • polygon: Polygon defined by pos, angle, and polygons (set of outlines). Can also use an svgpath or filled flag.

    Text

    Text objects include pos, text, height, width, angle, and justify (array [horizontal, vertical] where -1 is left/top, 0 is center, and 1 is right/bottom).

    • svgpath: If provided, height, width, angle, and text are ignored; thickness is used as stroke width.
    • ref: Present if the text is a reference designator.
    • val: Present if the text is a component value.
  8. Troubleshoot InteractiveHtmlBom Display and Parsing Errors

    master

    Common issues and their causes:

    • Missing wxpython: If wxpython is not installed and INTERACTIVE_HTML_BOM_NO_DISPLAY is not set, the script will exit with an error stating wxpython is required.
    • Dialog Error: If you attempt to use --show-dialog while the INTERACTIVE_HTML_BOM_NO_DISPLAY environment variable is set, the script will exit with ExitCodes.ERROR_NO_DISPLAY.
    • File Not Found: If the provided PCB file does not exist, the script exits with ExitCodes.ERROR_FILE_NOT_FOUND.
    • INI Not Found: If --use-ini is used but no valid .ini file is found, the script exits with ExitCodes.ERROR_FILE_NOT_FOUND.
    • Parsing Failure: If the PCB file cannot be parsed, the script exits with ExitCodes.ERROR_PARSE.
  9. Terminate the process with exit_error()

    master

    The exit_error function is a utility to log an error message and immediately terminate the application with a specific exit code.

    Parameters:

    • logger: A logger instance capable of calling .error(msg).
    • code: An integer from the ExitCodes class.
    • err: The error message or exception object to be logged.
    def exit_error(logger, code, err):
        logger.error(err)
        sys.exit(code)
  10. Register the InteractiveHtmlBomPlugin in KiCad

    master

    To use the Interactive HTML BOM functionality within KiCad, you must register the InteractiveHtmlBomPlugin. This is typically handled automatically by the plugin's entrypoint, but the core logic relies on the register() method of the InteractiveHtmlBomPlugin class.

    from .ecad.kicad import InteractiveHtmlBomPlugin
    
    plugin = InteractiveHtmlBomPlugin()
    plugin.register()
  11. Workaround for missing toolbar buttons on non-Linux platforms

    master

    On platforms other than Linux where standard KiCad plugin button support might be limited, the plugin uses a background thread to manually inject an "iBOM" button into the ID_H_TOOLBAR. This is triggered if plugin.pcbnew_icon_support is false and the platform is not Linux.

    # Internal logic used when pcbnew_icon_support is False and platform is not Linux
    if not plugin.pcbnew_icon_support and not sys.platform.startswith('linux'):
        t = threading.Thread(target=check_for_bom_button)
        t.daemon = True
        t.start()