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


app = designer.GetApplication()

def createCylinderAreaToSelect():
    """Cylinderオブジェクトで円筒領域を定義する """
    pointOnCenterAxis = app.CreatePoint(0, 0, 0)
    centerAxisDirection = app.CreatePoint(0, 0, 1)
    InnerR = 20
    OuterR = 50
    HTop = 70
    HBottom = 0
    XAxisDirection = app.CreatePoint(1, 0, 0)
    startAngle = 0
    endAngle = 90
    
    cyl = app.CreateCylinder()
    cyl.SetCenterPointByPoint(pointOnCenterAxis)
    cyl.SetCenterAxisByPoint(centerAxisDirection)
    cyl.SetOuterRadius(OuterR)
    cyl.SetInnerRadius(InnerR)
    cyl.SetUseHeight(True)
    cyl.SetHeightTop(HTop)
    cyl.SetHeightBottom(HBottom)
    
    cyl.SetUseAngle(True)
    cyl.SetXAxisByPoint(XAxisDirection)
    cyl.SetStartAngle(startAngle)
    cyl.SetEndAngle(endAngle)
    
    return cyl

def getFacesInCylinderArea(model, cylinder):
    """円筒領域の範囲内にある面を選択する """
    sel = model.CreateSelection()
    sel.Clear()
    sel.Attach()

    # The 2rd argumet of SelectFaceByCylinderObject
    # False: Select all parts in the range
    # True: Select only visible parts in the range
    sel.SelectFaceByCylinderObject(cylinder, False)
    
    return sel

def filterFacesByNormalToZAxis(model, faceSelection):
    """選択された面のうち法線がZ軸と平行な面だけを選択しなおす """
    roundDigits = 7
    faceSelection.Detach()
    newSel = model.CreateSelection()
    newSel.Clear()
    for i in range(faceSelection.NumFaces()):
        fid = faceSelection.FaceID(i)
        nv = model.GetFaceNormalVector(fid)
        if round(nv.GetX(), roundDigits) == 0.0 and round(nv.GetY(), roundDigits) == 0.0:
            newSel.SelectFace(fid)
    
    return newSel


cylinderArea = createCylinderAreaToSelect()
model = app.GetCurrentModel()
selFaces = getFacesInCylinderArea(model, cylinderArea)
filteredFaces = filterFacesByNormalToZAxis(model, selFaces)

print(filteredFaces.NumFaces())


