# Copyright (c) 2026 JSOL CORPORATION
#
# 本スクリプトはMITライセンスのもとで公開しています。
# ライセンス全文は以下を参照してください。
# https://www.jmag-international.com/jp/scriptlibrary/jmag_script_library_mit/

app = designer.GetApplication()

def createFluxLineFromPlane(study, fluxLineParam, planeParam):
    """面指定でフラックスラインを作成する"""
    fluxLineDef = study.CreateFluxLine(fluxLineParam[u"title"])
    fluxLineDef.SetResultType(fluxLineParam[u"resultType"])
    # 表示タイプ
    # 0 : シェーディング
    # 1 : 隠線なし
    # 2 : ワイヤフレーム
    fluxLineDef.SetDisplayType(fluxLineParam[u"displayType"])
    fluxLineDef.SetThickness(u"1.1")
    fluxLineDef.SetColor(u"red")
    fluxLineDef.SetLines(planeParam[u"lines"])
    # 描画タイプ
    # point : 点指定
    # plane : 面指定
    fluxLineDef.SetFluxLineType(u"plane")

    normalX, normalY, normalZ = planeParam[u"normal"]
    fluxLineDef.SetNormal( normalX, normalY, normalZ )

    originX, originY, originZ = planeParam[u"origin"]
    fluxLineDef.SetOrigin( originX, originY, originZ )

    plane = planeParam[u"plane"]
    useFinitePlane = plane[u"useFinite"]
    fluxLineDef.SetUseFinitePlane(useFinitePlane)
    if useFinitePlane:
        fluxLineDef.SetPlaneWidth(plane[u"width"])
        fluxLineDef.SetPlaneHeight(plane[u"height"])

    seed = planeParam[u"seed"]
    useAllParts = seed[u"allParts"]
    fluxLineDef.SetSeedLineAllParts(useAllParts)
    if not useAllParts:
        # 部品のIDを指定
        partIdList = seed[u"partIdList"]
        for partId in partIdList:
            fluxLineDef.AddPart(partId)

model = app.GetCurrentModel()
study = app.GetCurrentStudy()
partSet = model.GetSetList().GetSet(u"Rotor")
fluxLineParam = {
    u"title" : u"MagFluxDens",
    u"resultType" : u"MagneticFluxDensity",
    u"displayType" : 2
}
planeParam = {
    u"lines" : u"22",
    u"normal" : (1, 0, 0),
    u"origin" : (8, 7, 8),
    u"plane" : {
        u"useFinite" : True,
        u"width"     : 15,
        u"height"    : 15,
    },
    u"seed" : {
        u"allParts"   : False,
        u"partIdList" : partSet.GetIDs(),
    },
}

createFluxLineFromPlane(study, fluxLineParam, planeParam)

# フラックスラインの表示をON
app.View().SetFluxLineView(True)
