"""2835 LED（LP-2835D90VW）の 3D モデルを Fusion デザインとして作り，保存する．

保存先は Fusion クラウドの SmTIAS プロジェクト．

Fusion MCP の `fusion_mcp_execute`（featureType=script）で実行する Fusion API スクリプト．
2026-11-03 に実行して `LED_2835_LP-2835D90VW` を作成した（SPEC_05「Fusion 上のデータ」）．

形状: 3.5 × 2.8 × 0.7 mm の箱（HONGLITRONIC HL-A-2835D90VW のデータシートの外形．SPEC_03）．
原点はフットプリントの中心，底面が Z = 0，端子の並ぶ長辺が X 方向（KiCad の
LED_PLCC_2835 フットプリントのパッドの向き）．

例外は捕捉しない（呼び出し側でトレースバックを見るため）．
"""

import adsk.core
import adsk.fusion

# Fusion API の長さは cm
L, W, H = 0.35, 0.28, 0.07  # 3.5 x 2.8 x 0.7 mm

PROJECT = "SmTIAS"
NAME = "LED_2835_LP-2835D90VW"
DESCRIPTION = (
    "2835 LED 3D model 3.5x2.8x0.7mm (HONGLITRONIC HL-A-2835D90VW). "
    "origin = footprint center, bottom on Z=0, terminals along X"
)


def run(_context: str):
    app = adsk.core.Application.get()
    doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    design = adsk.fusion.Design.cast(app.activeProduct)
    units = design.fusionUnitsManager
    units.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
    # ルートコンポーネントの名前は変えられない（RuntimeError になる）ので，文書名で区別する
    root = design.rootComponent
    sk = root.sketches.add(root.xYConstructionPlane)
    sk.sketchCurves.sketchLines.addCenterPointRectangle(
        adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(L / 2, W / 2, 0)
    )
    ext = root.features.extrudeFeatures.addSimple(
        sk.profiles.item(0), adsk.core.ValueInput.createByReal(H),
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation,
    )
    body = ext.bodies.item(0)
    body.name = "LED_2835"
    bb = body.boundingBox
    print("bbox mm", [round(v * 10, 3) for v in (
        bb.minPoint.x, bb.minPoint.y, bb.minPoint.z, bb.maxPoint.x, bb.maxPoint.y, bb.maxPoint.z)])
    proj = [p for p in app.data.dataProjects if p.name == PROJECT][0]
    print("saved", doc.saveAs(NAME, proj.rootFolder, DESCRIPTION, ""))
