| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #!/usr/bin/env python3
- """Regenerate MR viewer assets from the BRD parser; no KiCad/IPC claim.
- Install beside generate_v2_0_assets.py and pass the independently extracted
- MRDIMM main_pins.csv. Geometry is a simplified viewer representation.
- """
- from __future__ import annotations
- import argparse
- import csv
- from collections import Counter
- from datetime import datetime, timezone
- import json
- from pathlib import Path
- import platform
- import tempfile
- from jsonschema import Draft202012Validator
- import generate_v2_0_assets as native
- from board_viewer.assets import validate_generated_bundle
- from board_viewer.data import build_board_data
- from board_viewer.generator import _publish_directory
- ROOT = Path(__file__).resolve().parents[2]
- VIEWER = ROOT / 'tools/board-viewer'
- ASSETS = ['board-data.json', 'import-report.json', 'board-mask-edge.svg',
- 'board-mask-edge-back.svg', 'board-copper.svg', 'board-copper-back.svg',
- 'board-silkscreen.svg', 'board-silkscreen-back.svg']
- def read(path):
- return json.loads(path.read_text(encoding='utf-8'))
- def write(path, value):
- path.write_text(json.dumps(value, indent=2, ensure_ascii=False) + '\n', encoding='utf-8')
- def source(path):
- return {'path': str(path.relative_to(ROOT)), 'sha256': native.sha256_file(path)}
- def main():
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument('--pins', type=Path, required=True, help='Independent native MR placed-pad CSV')
- args = parser.parse_args()
- brd = ROOT / 'reference/original/board/FCB_FPGA_MCU_V1.2_20260717/FCB_FPGA+MCU_V1.2_20260717.brd'
- annotations_path = ROOT / 'config/board_viewer_annotations.fcb_fpga_mcu_v1_2.json'
- inventory_path = ROOT / 'config/pin_inventory.fcb_fpga_mcu0611.json'
- data_schema = ROOT / 'schemas/board_viewer_data.schema.json'
- annotation_schema = ROOT / 'schemas/board_viewer_annotations.schema.json'
- build_schema = ROOT / 'schemas/board_viewer_build_info.schema.json'
- assert native.sha256_file(brd) == '269b5bab2f2e1215b9392079a003badc34238ae3cbe8e5094c9a11bba8b84ebb'
- annotations = read(annotations_path)
- Draft202012Validator(read(annotation_schema)).validate(annotations)
- board, geometry = native.parse_allegro_brd(brd)
- # This exact MR BRD has no outline recognized by the pattern parser. Its
- # generic 430x60 fallback clips real components (e.g. R568 y=-67.275).
- # Retain the same-BRD historical SVG envelope. These two archived files
- # match each other; the subsequently edited historical board-data does not.
- # This run does not decode a native outline or prove exact pad geometry.
- historical_dir = ROOT / 'evidence/hardware/20261023/mrdimm/historical-viewer-outline'
- historical_info = historical_dir / 'build-info.json'
- historical_outline = historical_dir / 'board-mask-edge.svg'
- assert native.sha256_file(historical_info) == '128bc9cbd79a0f83432f7cee8cb8a9298f5b5e625d8d9cbe2574bed0e50e2c7e'
- assert native.sha256_file(historical_outline) == '72af48fddb6914cee8b592ac17cd8d610fb62988ddc4f4959138e44482b9ac90'
- parsed_bounds = dict(board.bounds)
- assert parsed_bounds == {'x': 0.0, 'y': -60.0, 'width': 430.0, 'height': 60.0}
- board.bounds = {'x': 0.0, 'y': -75.0, 'width': 430.0, 'height': 75.0}
- bounds_provenance = {
- 'source': 'Historical KiCad SVG and its matching build-info, same BRD SHA; not a current native outline extraction',
- 'historical_outline': source(historical_outline),
- 'historical_build_info': source(historical_info),
- 'historical_svg_size_mm': {'width': 429.9966, 'height': 74.9808},
- 'historical_build_info_generated_at': '2026-10-14T06:57:25',
- 'historical_recorded_toolchain': {'python': '3.11.6', 'kicad': '10.0.4'},
- 'native_outline_detected': False,
- 'parser_fallback_bounds': parsed_bounds,
- 'applied_view_envelope': dict(board.bounds),
- 'limits': 'Historical display-envelope dimensions only; this run does not verify the physical board edge or exact pad shape/position.',
- }
- # Bind component values to the independently decoded complete native BOM.
- # A profile with an empty expected value must be corrected from that BOM;
- # annotation value validation remains strict.
- bom_path = args.pins.resolve().with_name('main_bom.csv')
- with bom_path.open(encoding='utf-8', newline='') as stream:
- bom_rows = list(csv.DictReader(stream))
- bom_by_ref = {row['reference']: row for row in bom_rows}
- board_refs = [fp.reference for fp in board.footprints]
- assert len(bom_rows) == len(bom_by_ref) == len(board_refs) == len(set(board_refs)) == 1317
- assert set(bom_by_ref) == set(board_refs), 'Native BOM/reference coverage mismatch'
- assert all('value' in row and row.get('footprint') for row in bom_rows)
- for fp in board.footprints:
- fp.component_value = bom_by_ref[fp.reference]['value']
- actual_rows = [(fp.reference, str(pad.number), pad.net or '') for fp in board.footprints for pad in fp.pads]
- with args.pins.open(encoding='utf-8', newline='') as stream:
- expected_rows = [(r['reference'], r['pin'], r['net']) for r in csv.DictReader(stream)]
- # Keep every numbered pad (even NC) and every named net. Only entries with
- # neither a number nor a net are outside this electrical comparison. The
- # pattern parser includes two extra such Q52 geometry entries; they are not
- # independently verified pads and must not be counted as electrical matches.
- actual = Counter(row for row in actual_rows if row[1] or row[2])
- expected = Counter(row for row in expected_rows if row[1] or row[2])
- assert actual == expected, f'Native pad mismatch: missing={list((expected-actual).items())[:10]}, extra={list((actual-expected).items())[:10]}'
- assert len(board.footprints) == 1317 and len(expected_rows) == 3694
- assert sum(expected.values()) == 3686
- geometry_counts = {
- 'viewer_all_pad_entries': len(actual_rows),
- 'independent_csv_rows': len(expected_rows),
- 'viewer_unnumbered_unconnected_by_ref': dict(Counter(row[0] for row in actual_rows if not row[1] and not row[2])),
- 'independent_unnumbered_unconnected_by_ref': dict(Counter(row[0] for row in expected_rows if not row[1] and not row[2])),
- 'excluded_geometry_is_not_electrically_verified': True,
- }
- payload = build_board_data(board, read(inventory_path), annotations, board_name='FCB FPGA+MCU V1.2 20260717')
- Draft202012Validator(read(data_schema)).validate(payload)
- counts = payload['counts']
- assert {k: counts[k] for k in ('touchpoints', 'pads', 'resistors', 'resistor_pads')} == {'touchpoints': 118, 'pads': 629, 'resistors': 644, 'resistor_pads': 1288}
- # Existing production MR annotation has no photographic overlay. If that
- # changes, use the normal photo-aware generator rather than dropping it.
- assert payload.get('photo_overlay') is None
- stamp = datetime.now(timezone.utc).isoformat()
- with tempfile.TemporaryDirectory(prefix='prod2u-mrdimm-native-') as directory:
- out = Path(directory)
- write(out / 'board-data.json', payload)
- native.generate_svg_layers_from_brd(board, geometry, 'FCB FPGA+MCU V1.2 20260717', payload['touchpoints'], out)
- write(out / 'import-report.json', {
- 'source_file': brd.name, 'source_format': 'allegro_brd', 'output_file': 'board-data.json',
- 'status': 'success', 'errors': [], 'timestamp': stamp,
- 'native_pin_comparison': {'status': 'pass', 'independent_csv_sha256': native.sha256_file(args.pins), 'scope': 'All numbered pads including NC, plus any unnumbered entry with a named net; tuple multiplicity must match', 'electrical_pad_entries': sum(expected.values()), 'unique_ref_pin_net': len(expected), 'missing': 0, 'extra': 0},
- 'geometry_entry_counts': geometry_counts,
- 'view_bounds_provenance': bounds_provenance,
- 'native_component_inventory': {'status': 'pass', 'independent_csv_sha256': native.sha256_file(bom_path), 'references': len(bom_rows), 'reference_coverage': 'exact', 'value_source': 'independent native BOM value column', 'footprint_package_names': 'Preserved in the source BOM; this simplified viewer model has no footprint-package field.'},
- 'limits': 'Independent native CSV comparison covers ref/pin/net only. Geometry is simplified; no KiCad import or IPC-D-356 comparison was run.'})
- info = {
- 'schema': 'board-viewer-build-info-v1', 'generated_at': stamp, 'generator_version': '2.1.0',
- 'toolchain': {'python': platform.python_version(), 'kicad': 'not_used'},
- 'sources': {**native._generator_sources(), 'generator_mrdimm_native': source(Path(__file__).resolve()),
- 'brd': source(brd), 'annotations': source(annotations_path), 'inventory': source(inventory_path),
- 'native_pins': source(args.pins.resolve()), 'native_bom': source(bom_path),
- 'historical_mrdimm_outline': source(historical_outline), 'historical_mrdimm_build_info': source(historical_info),
- 'schema_data': source(data_schema), 'schema_annotations': source(annotation_schema), 'schema_build_info': source(build_schema)},
- 'assets': {name: {'sha256': native.sha256_file(out/name), 'bytes': (out/name).stat().st_size} for name in ASSETS},
- 'verification': {
- 'ipcd356': {'status': 'not_run', 'reason': 'Native BRD parser and independent native CSV comparison used; no IPC-D-356 export or comparison executed.'},
- 'side_views': {'front_layers': ['board-mask-edge.svg', 'board-silkscreen.svg', 'board-copper.svg'],
- 'back_layers': ['board-mask-edge-back.svg', 'board-silkscreen-back.svg', 'board-copper-back.svg'],
- 'back_mirror_axis': 'y', 'back_svg_export_mirrored': True,
- 'convention': 'Native-parser simplified SVGs; back view mirrored about Y. This is not a KiCad export or exact CAD rendering.'}},
- 'counts': {key: counts[key] for key in ('footprints', 'touchpoints', 'pads', 'resistors', 'resistor_pads')}}
- Draft202012Validator(read(build_schema)).validate(info)
- write(out / 'build-info.json', info)
- result = validate_generated_bundle(out, repository_root=ROOT)
- assert result.status == 'pass', {'status': result.status, 'asset_errors': result.asset_errors, 'stale_sources': result.stale_sources}
- _publish_directory(out, VIEWER/'generated')
- print(json.dumps({'status': 'pass', 'output_dir': str(VIEWER/'generated'), 'counts': info['counts'], 'toolchain': info['toolchain'], 'ipcd356': 'not_run'}, ensure_ascii=False))
- if __name__ == '__main__':
- main()
|