"""SmTIASv4 の筐体・LED の基板とカバー・配線を,シミュのシーンを作るための STL に書き出す.
Fusion の中で実行するスクリプト(MCP の fusion_mcp_execute で `exec(open(パス).read())` の形で
読み込んで `run` を呼ぶ.TECH_12).書き出したフォルダを `scripts/build_fusion_v4_scene.py
--export-dir` に渡す.
- `housing_front.stl`・`housing_back.stl`: コンポーネント「筐体」の「前側」「後側」
- `board_<k>.stl`: コンポーネント「LEDモジュール」の「基板_*」「カバー_*」(LED と定電流 IC は
シミュでは面光源にするので書き出さない)
- `wire_<k>.stl`: コンポーネント「配線」のボディ
STL はボディの属するコンポーネントの座標で出る(TECH_12).どのオカレンスも原点に置いてある
前提なので,原点に無ければ止める.
"""
import os
import adsk.core
import adsk.fusion
OUT_DIR = r"C:/Users/rinto/source/SmTIAS-LightSim/configs/_tmp_candidates/fusion_v4"
def _export(em, body, path):
opt = em.createSTLExportOptions(body, path)
opt.isBinaryFormat = True
opt.meshRefinement = adsk.fusion.MeshRefinementSettings.MeshRefinementHigh
opt.sendToPrintUtility = False
if not em.execute(opt) or not os.path.exists(path):
raise RuntimeError(f"STL の書き出しに失敗: {body.name}")
def run(_context: str):
app = adsk.core.Application.get()
if app.activeDocument.name != "SmTIASv4":
raise RuntimeError("SmTIASv4 をアクティブにしてから実行する")
des = adsk.fusion.Design.cast(app.activeProduct)
occs = {o.component.name: o for o in des.rootComponent.occurrences}
identity = adsk.core.Matrix3D.create()
for name in ("筐体", "LEDモジュール", "配線"):
if not occs[name].transform2.isEqualTo(identity):
raise RuntimeError(f"「{name}」のオカレンスが原点に無い")
out = os.path.normpath(OUT_DIR)
os.makedirs(out, exist_ok=True)
for f in os.listdir(out):
if f.endswith(".stl"):
os.remove(os.path.join(out, f))
em = des.exportManager
files = []
# 名前の付いた STL のパスに日本語を入れない(書き出しが失敗することがある)
for body in occs["筐体"].bRepBodies:
key = {"前側": "front", "後側": "back"}[body.name]
files.append((body, f"housing_{key}.stl"))
boards = [b for b in occs["LEDモジュール"].bRepBodies
if b.name.startswith(("基板_", "カバー_"))]
files += [(b, f"board_{k}.stl") for k, b in enumerate(boards)]
files += [(b, f"wire_{k}.stl") for k, b in enumerate(occs["配線"].bRepBodies)]
for body, name in files:
_export(em, body, os.path.join(out, name))
print(name, body.name)