test_pcb_parser.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import annotations
  2. import math
  3. import sys
  4. from pathlib import Path
  5. import pytest
  6. ROOT = Path(__file__).resolve().parents[2]
  7. VIEWER_ROOT = ROOT / "tools" / "board-viewer"
  8. FIXTURE = Path(__file__).parent / "fixtures" / "parser_edge_cases.kicad_pcb"
  9. sys.path.insert(0, str(VIEWER_ROOT))
  10. from board_viewer.pcb import parse_kicad_pcb, select_touchpoints # noqa: E402
  11. def test_parser_ignores_hidden_reference_placeholder_and_non_footprint_text() -> None:
  12. board = parse_kicad_pcb(FIXTURE)
  13. references = {footprint.reference for footprint in board.footprints}
  14. touchpoints = {footprint.reference for footprint in select_touchpoints(board)}
  15. assert references == {"CON1", "TP1"}
  16. assert "${REFERENCE}" not in references
  17. assert touchpoints == {"CON1", "TP1"}
  18. assert "CON5" not in touchpoints
  19. def test_board_outline_bounds_are_extracted_from_edge_cuts() -> None:
  20. board = parse_kicad_pcb(FIXTURE)
  21. assert board.bounds == {"x": 0.0, "y": -10.0, "width": 30.0, "height": 10.0}
  22. def test_front_pad_coordinates_include_footprint_rotation_and_negative_y() -> None:
  23. board = parse_kicad_pcb(FIXTURE)
  24. connector = board.footprint_by_reference("CON1")
  25. pin = connector.pad_by_number("1")
  26. assert connector.component_value == "Fixture:Front"
  27. assert connector.x == 10
  28. assert connector.y == -5
  29. assert connector.rotation == 90
  30. assert connector.side == "front"
  31. assert pin.x == pytest.approx(11)
  32. assert pin.y == pytest.approx(-7)
  33. # Pad at-angle is footprint-relative; board-absolute = local + footprint.
  34. assert pin.rotation == pytest.approx(120)
  35. assert pin.net == "RS232_TX2"
  36. def test_back_pad_coordinates_follow_kicad_board_transform_without_extra_mirroring() -> None:
  37. board = parse_kicad_pcb(FIXTURE)
  38. testpoint = board.footprint_by_reference("TP1")
  39. pin = testpoint.pad_by_number("1")
  40. assert testpoint.side == "back"
  41. assert pin.x == pytest.approx(23)
  42. assert pin.y == pytest.approx(8)
  43. assert pin.rotation == pytest.approx(120)
  44. def test_unnumbered_zero_size_np_thru_hole_is_mechanical_and_finite() -> None:
  45. board = parse_kicad_pcb(FIXTURE)
  46. connector = board.footprint_by_reference("CON1")
  47. hole = next(pad for pad in connector.pads if pad.number == "")
  48. assert hole.mechanical is True
  49. assert hole.electrical is False
  50. assert hole.width == 0
  51. assert hole.height == 0
  52. assert hole.drill_width == 1
  53. assert hole.drill_height == 1
  54. assert all(math.isfinite(value) for value in connector.bounds.values())
  55. assert connector.bounds["width"] > 0
  56. assert connector.bounds["height"] > 0