"""pcb.gerber(面付けとガーバー・ドリルの書き出し)のテスト.
仕様出典: src/sm_tias_lightsim/pcb/gerber.py の docstring と SPEC_05「MITS 基板加工機での作り方」
- RS-274X・mm・4.6 形式(研究室の KiCad の書き出しと同じ)
- 外形のカッターで切り抜く穴は外形のガーバーに全周の円(G75 + G03)で入れ,ドリルには入れない
- 基板は左をそろえて縦に gap の間隔で並べる
"""
from __future__ import annotations
import re
import pytest
from sm_tias_lightsim.pcb.eagle import Board, Hole, Pad, Track
from sm_tias_lightsim.pcb.gerber import (
copper_gerber,
drill_excellon,
edge_gerber,
min_copper_gap,
panelize,
)
def _rect_board(name: str, x0: float, y0: float, w: float, h: float) -> Board:
pts = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)]
outline = [(*pts[i], *pts[(i + 1) % 4]) for i in range(4)]
return Board(name, outline=outline)
def test_panelize_stacks_boards_left_aligned_with_gap():
a = _rect_board("a", 100, 50, 60, 10)
a.pads.append(Pad(101, 51, 1, 1, "rect"))
b = _rect_board("b", -5, 7, 90, 10)
panel, places = panelize([a, a, b], gap=6.0)
assert [(p.name, p.x, p.y) for p in places] == [("a", 0, 0), ("a", 0, 16), ("b", 0, 32)]
assert panel.bounds() == pytest.approx((0, 0, 90, 42))
assert [(p.x, p.y) for p in panel.pads] == [(1, 1), (1, 17)]
def test_panelize_side_by_side_aligns_bottoms():
a = _rect_board("a", 5, 5, 10, 60)
b = _rect_board("b", -3, 2, 10, 80)
panel, places = panelize([a, a, b], gap=6.0, direction="x")
assert [(p.x, p.y) for p in places] == [(0, 0), (16, 0), (32, 0)]
assert panel.bounds() == pytest.approx((0, 0, 42, 80))
with pytest.raises(ValueError):
panelize([a], gap=6.0, direction="z")
def test_panelize_center_aligns_middles():
a = _rect_board("a", 0, 0, 10, 60)
b = _rect_board("b", 0, 0, 10, 80)
_, places = panelize([a, b], gap=6.0, direction="x", center=True)
assert [(p.x, p.y) for p in places] == [(0, 10), (16, 0)]
_, places = panelize([_rect_board("c", 0, 0, 60, 10), _rect_board("d", 0, 0, 80, 10)],
gap=6.0, direction="y", center=True)
assert [(p.x, p.y) for p in places] == [(10, 0), (0, 16)]
def test_panelize_rejects_non_positive_gap():
with pytest.raises(ValueError):
panelize([_rect_board("a", 0, 0, 1, 1)], gap=0.0)
def test_copper_gerber_format_and_apertures():
b = _rect_board("a", 0, 0, 10, 10)
b.pads += [Pad(1.5, 2.25, 1.0, 1.5, "rect"), Pad(3, 3, 1.6, 1.6, "circle")]
b.tracks.append(Track(0, -1.2, 5, -1.2, 0.6))
text = copper_gerber(b)
assert "%FSLAX46Y46*%" in text and "%MOMM*%" in text and text.rstrip().endswith("M02*")
assert "%ADD10R,1.000000X1.500000*%" in text
assert "%ADD11C,1.600000*%" in text
assert "%ADD12C,0.600000*%" in text
assert "X1500000Y2250000D03*" in text
assert "X0Y-1200000D02*" in text and "X5000000Y-1200000D01*" in text
def test_obround_pad_uses_o_aperture():
b = _rect_board("a", 0, 0, 10, 10)
b.pads.append(Pad(2, 2, 3.0, 2.0, "obround"))
assert "%ADD10O,3.000000X2.000000*%" in copper_gerber(b)
def test_edge_gerber_has_outline_and_routed_hole_circles():
b = _rect_board("a", 0, 0, 10, 5)
b.holes += [Hole(3, 2, 3.2, routed=True), Hole(7, 2, 0.8, routed=False)]
text = edge_gerber(b)
assert len(re.findall(r"D01\*", text)) == 4 + 1 # 外形 4 本 + 穴 1 つ
assert "X4600000Y2000000D02*" in text # 円の始点(中心 + 半径)
assert "G03X4600000Y2000000I-1600000J0D01*" in text
def test_drill_only_for_drilled_holes():
b = _rect_board("a", 0, 0, 10, 5)
b.holes.append(Hole(3, 2, 3.2, routed=True))
assert drill_excellon(b) is None
b.holes.append(Hole(7.5, 2.25, 0.8, routed=False))
text = drill_excellon(b)
assert text is not None
assert "METRIC,TZ" in text and "T1C0.800" in text and "X7500Y2250" in text
assert "X3000Y2000" not in text
def test_min_copper_gap_measures_rectangle_separation():
b = _rect_board("a", 0, 0, 10, 5)
b.pads += [Pad(0, 0, 1.0, 1.5, "rect"), Pad(1.3, 0, 1.0, 1.5, "rect")]
assert min_copper_gap(b) == pytest.approx(0.3)
b.pads.append(Pad(5, 5, 1, 1, "rect"))
assert min_copper_gap(b) == pytest.approx(0.3)