"""SmTIASv4 の LED 基板 2 種類(左右用 6 個・下用 6 個)を EAGLE 形式で作る.
Fusion Electronics から EAGLE 形式で書き出した今の基板(cad/eagle/SmTIAS_LEDs.{sch,brd}.KiCad の
cad/kicad/SmTIAS_LEDs/ を Fusion に取り込んだもの)を手本にし,部品の形(LED 2835・定電流 IC
SOD-123・M3 の取付穴・はんだパッド)と回路図の記号はそのライブラリを使う.部品の並びと配線は
新しい基板の形に合わせて組み立てる.配線の引き方(+12V と GND の線を基板の縁に沿わせ,部品を
中心線に並べて小さく折れ曲がった線で直列につなぐ)と線の太さ 0.6 mm も今の基板と同じ.
回路は今と同じ: +12V → 定電流 IC(NSI45030AT1G,30 mA)2 個を並列(60 mA)→ LED 3 個を直列 → GND.
列はどちらも 2 本.電源パッド(+12V / GND)は 2 組ずつ,左右用は片方の端,下用は両方の端に置く.
下用は SmTIASv3 の下の基板と同じく,外から来たケーブルを片方の端で受け,両端から左右の基板へ
1 本ずつ線を出す(線の途中で分けない.基板の +12V と GND の線が分岐の役をする).
電源パッドはケーブルをはんだごてで付けるので,今の基板のはんだジャンパの部品(1.0 × 1.5 mm・間
0.3 mm.削る溝の幅 0.3 mm と同じで +12V と GND がつながったまま残るおそれがある)をやめ,専用の
部品 `SMTIAS_WIRE_PAD_2X3`(ケーブル 1 本の +12V と GND を基板の幅の向きに並べた 2.0 × 3.0 mm の
パッド 2 つ・間 1.5 mm.+12V は角の丸い形で GND と見分ける)にする.+12V は基板の +12V の線の側,
GND は GND の線の側に来るので,線を交差させずにつながる.ケーブル 2 本分を長さの向きに 2 列並べる.
定電流 IC の 2 つの間は今の基板の 3 mm から 4 mm に広げる(はんだ付けしやすくする).下用は研究室の
材料 s10(75 × 100.基準ピンの間 90 mm)に縦に収まる長さにする(2026-11-05).
できた .sch / .brd は scripts/fusion/import_eagle_boards.py(MCP の fusion_mcp_execute で実行)で
Fusion Electronics に取り込み,SmTIAS プロジェクトに保存する.以後は Fusion を正本とする
(基板の検査 DRC・切削のデータは Fusion で扱う).部品の並びは TEST_21 の L5 と SmTIASv4 の LED の
位置に合わせてある(LED は全部 Z −35.TEST_21 結果 12).
実行方法:
uv run python scripts/generate_led_module_boards.py
"""
from __future__ import annotations
import re
from dataclasses import dataclass, field
from pathlib import Path
from xml.sax.saxutils import escape
ROOT = Path(__file__).resolve().parents[1]
TEMPLATE = ROOT / "cad" / "eagle" / "SmTIAS_LEDs" # .sch / .brd
OUT = ROOT / "cad" / "eagle"
CENTER_Y = 55.0 # 基板の中心線(今の基板の KiCad 座標.Y は下向き)
RAIL_12V_Y = 51.75
RAIL_GND_Y = 58.25
TRACK_W = 0.6
Y_FLIP = 208.28 # EAGLE の Y = Y_FLIP − KiCad の Y(今の基板の書き出しで確かめた値)
# 基板の検査(DRC)の決まり.研究室の MITS 製基板加工機の機種・刃の径は未確認なので,今の基板と同じ
# 線の太さ 0.6 mm・間隔 0.5 mm を暫定で使う(PLAN_03「製造の制約」).Fusion の検査は決まりと同じ
# 間隔も違反にする(電源の線と隣のパッドの間がちょうど 0.5 mm で引っかかった)ので,間隔は 0.49 mm
# にする.はんだパッドの部品は 2 つのパッドの間が 0.3 mm なので SMD どうしだけ 0.25 mm にする.
# 手本のファイルの決まりはほぼ 0 だった
DESIGN_RULES = {
"mdWireWire": "0.49mm", "mdWirePad": "0.49mm", "mdWireVia": "0.49mm",
"mdPadPad": "0.49mm", "mdPadVia": "0.49mm", "mdViaVia": "0.49mm",
"mdSmdPad": "0.49mm", "mdSmdVia": "0.49mm", "mdSmdSmd": "0.25mm",
"mdCopperDimension": "1.0mm", "msWidth": "0.6mm",
}
@dataclass
class Part:
"""基板の部品 1 つ(回路図の記号と基板の部品の両方を作る)."""
ref: str
kind: str # "led" / "ccr" / "conn" / "hole"
u: float # 基板の長さ方向の位置 [mm](X = x0 + u)
y: float = CENTER_Y
nets: dict[str, str] = field(default_factory=dict) # パッド番号 → ネット名
@dataclass
class Board:
"""基板 1 種類."""
name: str
title: str
x0: float # u = 0 の X
outline: list[tuple[float, float]] # (u, Y.下向き) の多角形
parts: list[Part]
segments: list[tuple[tuple[float, float], tuple[float, float], str]] # (u, y) と ネット名
# ---------------------------------------------------------------------------
# 部品の並びと配線(今の基板と同じ引き方)
# ---------------------------------------------------------------------------
def _string(parts: list[Part], segs: list, c1: float, c2: float, leds: list[float],
refs: list[str], net_prefix: str) -> None:
"""定電流 IC 2 個(c1 が LED 側)と LED 3 個の列を足す.leds は c1 から遠ざかる順."""
sign = 1.0 if leds[0] > c1 else -1.0 # LED が c1 より大きい側なら +1
n_ccr = f"{net_prefix}_CCR"
parts.append(Part(refs[0], "ccr", c1, nets={"2": "+12V", "1": n_ccr}))
parts.append(Part(refs[1], "ccr", c2, nets={"2": "+12V", "1": n_ccr}))
for c in (c1, c2):
segs.append(((c, 53.35), (c, RAIL_12V_Y), "+12V"))
# 定電流 IC の陰極どうし → 1 個目の LED の陽極
pts = [(c2, 56.65), (c1, 56.65), (c1 + sign * 1.05, 56.65), (c1 + sign * 1.8, 55.9),
(c1 + sign * 1.8, 54.3), (c1 + sign * 2.625, 53.475), (leds[0], 53.475)]
segs.extend((a, b, n_ccr) for a, b in zip(pts, pts[1:], strict=False))
for k, x in enumerate(leds):
ref = refs[2 + k]
anode = n_ccr if k == 0 else f"{net_prefix}_L{k}"
cathode = "GND" if k == len(leds) - 1 else f"{net_prefix}_L{k + 1}"
parts.append(Part(ref, "led", x, nets={"2": anode, "1": cathode}))
if k < len(leds) - 1:
pts = [(x, 56.05), (x + sign * 1.25, 56.05), (x + sign * 1.9, 55.4),
(x + sign * 1.9, 53.9), (x + sign * 2.325, 53.475), (leds[k + 1], 53.475)]
segs.extend((a, b, cathode) for a, b in zip(pts, pts[1:], strict=False))
else:
segs.append(((x, 56.05), (x, RAIL_GND_Y), "GND"))
# 電源パッドの列の中心の,基板の端からの距離(パッドの幅 2.0 mm・外形まで 1.2 mm・列の間 1.5 mm)
PAD_COLUMNS = (2.2, 5.7)
# 取付穴の中心から電源パッドの端までの距離.カバーのパッドの窓(パッド + 0.3 mm)が M3 のねじの頭
# (半径 2.75 mm)にかからないようにする
HOLE_TO_PADS = 3.6
def _power_pads(parts: list[Part], u_edge: float, refs: tuple[str, str],
side: float = 1.0) -> float:
"""基板の端(u_edge)に電源パッド 2 列(ケーブル 2 本分)を置き,外側の列の u を返す.
side は基板が u_edge から見てどちらにあるか(+1: u が増える側,−1: 減る側).
パッドは +12V が +12V の線(RAIL_12V_Y),GND が GND の線(RAIL_GND_Y)に重なるので,
線をパッドの列まで延ばせばつながる.
"""
cols = [u_edge + side * c for c in PAD_COLUMNS]
for ref, u in zip(refs, cols, strict=True):
parts.append(Part(ref, "conn", u, nets={"1": "GND", "2": "+12V"}))
return cols[0]
def side_board() -> Board:
"""左右用(6 個.装置の Y = u): パッド −16.1・−12.6・穴 −8・+40・IC ±2 / 18・22・LED 6〜34."""
parts: list[Part] = []
segs: list = []
u_edge = -8.0 - HOLE_TO_PADS - PAD_COLUMNS[1] - 1.0
uj = _power_pads(parts, u_edge, ("J1", "J2"))
_string(parts, segs, 2.0, -2.0, [6.0, 10.0, 14.0], ["D7", "D8", "D1", "D2", "D3"], "S1")
_string(parts, segs, 22.0, 18.0, [26.0, 30.0, 34.0], ["D9", "D10", "D4", "D5", "D6"], "S2")
segs.append(((uj, RAIL_12V_Y), (22.0, RAIL_12V_Y), "+12V"))
segs.append(((uj, RAIL_GND_Y), (34.0, RAIL_GND_Y), "GND"))
parts += [Part("H1", "hole", -8.0), Part("H2", "hole", 40.0)]
# 上の端の両角を落とす(箱の角の丸みに当てないため)
outline = [(u_edge, 50.0), (38.0, 50.0), (44.0, 53.5), (44.0, 56.5), (38.0, 60.0),
(u_edge, 60.0)]
return Board("SmTIASv4_LED_side_v2", "SmTIASv4 LED 基板(左右用・6 個)", 120.0, outline,
parts, segs)
BOTTOM_LEDS = (17.0, 21.0, 25.0) # 下用の LED の |X|(s10 の基準ピンの間 90 mm に収める長さ)
def bottom_board() -> Board:
"""下用(6 個.装置の X = u): パッド ±35.6・±39.1・穴 ±31・LED ±17〜±25・IC ±9,±13.
左右用の部品の間隔をそのまま左右対称に並べたもの.真ん中(−8〜+8)は部品が無い.
"""
parts: list[Part] = []
segs: list = []
led_in, _, led_out = BOTTOM_LEDS
hole = led_out + 6.0
u_edge = hole + HOLE_TO_PADS + PAD_COLUMNS[1] + 1.0
uj_l = _power_pads(parts, -u_edge, ("J1", "J2"), side=1.0)
uj_r = _power_pads(parts, u_edge, ("J3", "J4"), side=-1.0)
c1, c2 = led_in - 4.0, led_in - 8.0
_string(parts, segs, -c1, -c2, [-v for v in BOTTOM_LEDS], ["D7", "D8", "D1", "D2", "D3"],
"S1")
_string(parts, segs, c1, c2, list(BOTTOM_LEDS), ["D9", "D10", "D4", "D5", "D6"], "S2")
segs.append(((uj_l, RAIL_12V_Y), (uj_r, RAIL_12V_Y), "+12V"))
segs.append(((uj_l, RAIL_GND_Y), (uj_r, RAIL_GND_Y), "GND"))
parts += [Part("H1", "hole", -hole), Part("H2", "hole", hole)]
outline = [(-u_edge, 50.0), (u_edge, 50.0), (u_edge, 60.0), (-u_edge, 60.0)]
return Board("SmTIASv4_LED_bottom_v2", "SmTIASv4 LED 基板(下用・6 個)", 100.0, outline,
parts, segs)
# ---------------------------------------------------------------------------
# EAGLE 形式の書き出し
# ---------------------------------------------------------------------------
LIBRARY = {"led": "Device", "ccr": "Device", "conn": "Connector_Generic", "hole": "MountingHole"}
PACKAGE = {
"led": "LED_SMD_LED_PLCC_2835_HANDSOLDERING",
"ccr": "DIODE_SMD_D_SOD-123",
"conn": "SMTIAS_WIRE_PAD_2X3",
"hole": "MOUNTINGHOLE_3.2MM_M3_ISO7380",
}
DEVICESET = {"led": "LED", "ccr": "D", "conn": "CONN_01X02"}
VALUE = {"led": "LP-2835D90VW", "ccr": "NSI45030AT1G", "conn": "Conn_01x02",
"hole": "MountingHole_3.2mm_M3_ISO7380"}
# パッド番号 → 記号のピン名と,記号の座標でのピンの位置(Y は上向き)
PINS = {
"led": {"1": ("K", (-3.81, 0.0)), "2": ("A", (3.81, 0.0))},
"ccr": {"1": ("K", (-3.81, 0.0)), "2": ("A", (3.81, 0.0))},
"conn": {"1": ("Pin_1", (-5.08, 0.0)), "2": ("Pin_2", (-5.08, -2.54))},
}
def _replace_section(text: str, tag: str, body: str) -> str:
"""`<tag>...</tag>`(最初の 1 つ)を body に置き換える."""
start = text.index(f"<{tag}>")
end = text.index(f"</{tag}>", start) + len(f"</{tag}>")
return text[:start] + body + text[end:]
def _f(v: float) -> str:
return f"{round(v, 4):g}"
# ケーブルをはんだ付けする電源パッドの部品(1 本分).R90 で置くと pad 1 = GND が GND の線の側,
# pad 2 = +12V が +12V の線の側に来る.間 1.5 mm.+12V は角の丸い形にして見分ける
WIRE_PAD_PACKAGE = """<package name="SMTIAS_WIRE_PAD_2X3">
<description>Wire solder pads for one cable (GND / +12V), 2.0 x 3.0 mm, 1.5 mm gap</description>
<smd name="1" x="-2.25" y="0" dx="3" dy="2" layer="1" cream="no"/>
<smd name="2" x="2.25" y="0" dx="3" dy="2" layer="1" roundness="100" cream="no"/>
</package>
"""
WIRE_PAD_DEVICE = """<device name="SMTIAS_WIRE_PAD_2X3" package="SMTIAS_WIRE_PAD_2X3">
<connects>
<connect gate="G$1" pin="Pin_1" pad="1"/>
<connect gate="G$1" pin="Pin_2" pad="2"/>
</connects>
<technologies>
<technology name=""/>
</technologies>
</device>
"""
def add_wire_pad_part(text: str, schematic: bool) -> str:
"""手本のライブラリ Connector_Generic に電源パッドの部品(回路図では device も)を足す."""
start = text.index('<library name="Connector_Generic">')
pos = text.index("<packages>", start) + len("<packages>")
text = text[:pos] + "\n" + WIRE_PAD_PACKAGE.rstrip("\n") + text[pos:]
if schematic:
ds = text.index('<deviceset name="CONN_01X02"', start)
pos = text.index("<devices>", ds) + len("<devices>")
text = text[:pos] + "\n" + WIRE_PAD_DEVICE.rstrip("\n") + text[pos:]
return text
def write_brd(board: Board, template: str, dest: Path) -> None:
"""基板(.brd): 外形(層 20)・部品の配置・配線(層 1)."""
def xy(u: float, y: float) -> tuple[str, str]:
return _f(board.x0 + u), _f(Y_FLIP - y)
plain = ["<plain>"]
pts = board.outline + board.outline[:1]
for (u0, y0), (u1, y1) in zip(pts, pts[1:], strict=False):
(x1, ya), (x2, yb) = xy(u0, y0), xy(u1, y1)
plain.append(f'<wire x1="{x1}" y1="{ya}" x2="{x2}" y2="{yb}" width="0.05" layer="20"/>')
plain.append("</plain>")
elements = ["<elements>"]
for part in board.parts:
x, y = xy(part.u, part.y)
rot = "" if part.kind == "hole" else ' rot="R90"'
elements.append(f'<element name="{part.ref}" library="{LIBRARY[part.kind]}" '
f'package="{PACKAGE[part.kind]}" value="{escape(VALUE[part.kind])}" '
f'x="{x}" y="{y}"{rot}/>')
elements.append("</elements>")
signals = ["<signals>"]
for net in sorted({n for p in board.parts for n in p.nets.values()}):
signals.append(f'<signal name="{escape(net)}">')
for part in board.parts:
for pad, n in sorted(part.nets.items()):
if n == net:
signals.append(f'<contactref element="{part.ref}" pad="{pad}"/>')
for (u0, y0), (u1, y1), n in board.segments:
if n == net and (u0, y0) != (u1, y1):
(x1, ya), (x2, yb) = xy(u0, y0), xy(u1, y1)
signals.append(f'<wire x1="{x1}" y1="{ya}" x2="{x2}" y2="{yb}" '
f'width="{_f(TRACK_W)}" layer="1"/>')
signals.append("</signal>")
signals.append("</signals>")
text = _replace_section(template, "plain", "\n".join(plain))
text = _replace_section(text, "elements", "\n".join(elements))
text = _replace_section(text, "signals", "\n".join(signals))
for name, value in DESIGN_RULES.items():
text, n = re.subn(rf'<param name="{name}" value="[^"]*"/>',
f'<param name="{name}" value="{value}"/>', text)
if n != 1:
raise RuntimeError(f"手本の .brd に設計の決まり {name} が見つからない")
text = re.sub(r'<designrules name="[^"]*"', '<designrules name="SmTIAS MITS 暫定"', text,
count=1)
dest.write_text(text, encoding="utf-8", newline="\n")
def write_sch(board: Board, template: str, dest: Path) -> None:
"""回路図(.sch): 部品の配置と,ピンごとの短い線とネット名のラベル(同じ名前でつながる)."""
parts = ["<parts>"]
instances = ["<instances>"]
segments: dict[str, list[str]] = {}
col = {"conn": 0, "ccr": 0, "led": 0}
row_y = {"conn": 160.02, "ccr": 134.62, "led": 109.22}
for part in board.parts:
if part.kind == "hole":
continue
parts.append(f'<part name="{part.ref}" library="{LIBRARY[part.kind]}" '
f'deviceset="{DEVICESET[part.kind]}" device="{PACKAGE[part.kind]}" '
f'value="{escape(VALUE[part.kind])}"/>')
x = 50.8 + 25.4 * col[part.kind]
y = row_y[part.kind]
col[part.kind] += 1
instances.append(f'<instance part="{part.ref}" gate="G$1" x="{_f(x)}" y="{_f(y)}"/>')
for pad, (pin, (px, py)) in PINS[part.kind].items():
net = part.nets[pad]
x0, y0 = x + px, y + py
x1 = x0 + (-2.54 if px < 0 else 2.54)
rot = ' rot="MR0"' if px < 0 else ""
segments.setdefault(net, []).append(
f'<segment>\n<wire x1="{_f(x0)}" y1="{_f(y0)}" x2="{_f(x1)}" y2="{_f(y0)}" '
f'width="0.1524" layer="91"/>\n'
f'<label x="{_f(x1)}" y="{_f(y0)}" size="1.27" layer="95"{rot}/>\n'
f'<pinref part="{part.ref}" gate="G$1" pin="{pin}"/>\n</segment>')
parts.append("</parts>")
instances.append("</instances>")
nets = ["<nets>"]
for name, segs in segments.items():
nets.append(f'<net name="{escape(name)}" class="0">')
nets.extend(segs)
nets.append("</net>")
nets.append("</nets>")
text = _replace_section(template, "parts", "\n".join(parts))
text = _replace_section(text, "instances", "\n".join(instances))
text = _replace_section(text, "nets", "\n".join(nets))
text = re.sub(r"<plain>.*?</plain>", "<plain>\n</plain>", text, count=1, flags=re.S)
dest.write_text(text, encoding="utf-8", newline="\n")
def main() -> int:
sch = add_wire_pad_part(TEMPLATE.with_suffix(".sch").read_text(encoding="utf-8"), True)
brd = add_wire_pad_part(TEMPLATE.with_suffix(".brd").read_text(encoding="utf-8"), False)
for board in (side_board(), bottom_board()):
write_sch(board, sch, OUT / f"{board.name}.sch")
write_brd(board, brd, OUT / f"{board.name}.brd")
n_led = sum(p.kind == "led" for p in board.parts)
n_ccr = sum(p.kind == "ccr" for p in board.parts)
print(f"[saved] {OUT / board.name}.sch / .brd(LED {n_led}・定電流 IC {n_ccr}・"
f"配線 {len(board.segments)} 本)")
return 0
if __name__ == "__main__":
raise SystemExit(main())