C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

選択時のみ原点平面を表示する

久々にFusion360API

こちらに頭に思い付いた手順をスプリクトにしたのですが
Re: スケッチの線分コマンドのようなスケッチ選択を作成するにはどうすればいいですか - Autodesk Community

考えたら、アクティブなコンポーネントの原点の表示状態は選択時のみの話なので
選択時のみの関数に全て突っ込んでしまった方が、コード的にやさしい気がしました。
しかもPythonの例外処理には、必ず実行する "finally" が存在していたのを忘れてました。

#Fusion360 python
#選択時のみ原点平面を表示するサンプル

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        #モロモロ
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        #選択用のフィルタ
        selFilter = 'ConstructionPlanes,PlanarFaces'
        
        #選択
        msg = '平面を選択'
        selection = Sel(app, msg, selFilter)
        if selection is None: return
        
        #選択要素
        selitem = selection.entity
        
        #オブジェクト名
        ui.messageBox('Select Object Type : {}'.format(selitem.objectType))
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def Sel(app, msg, selFilter):
    ui = app.userInterface
    des = adsk.fusion.Design.cast(app.activeProduct)
    
    light = GetActiveCompOriginLight(des)
    SetActiveCompOriginLight(des, (True,True,True,True))
    
    try:
        return ui.selectEntity(msg, selFilter)
    except:
        return None
    finally:
        SetActiveCompOriginLight(des, light)

#現行コンポーネントの原点表示状態取得
def GetActiveCompOriginLight(des):
    comp = adsk.fusion.Component.cast(des.activeComponent)
    xy = comp.xYConstructionPlane
    xz = comp.xZConstructionPlane
    yz = comp.yZConstructionPlane
    
    return (comp.isOriginFolderLightBulbOn,
            xy.isLightBulbOn,
            xz.isLightBulbOn,
            yz.isLightBulbOn)

#現行コンポーネントの原点表示状態設定
def SetActiveCompOriginLight(des,light_set):
    comp = adsk.fusion.Component.cast(des.activeComponent)
    xy = comp.xYConstructionPlane
    xz = comp.xZConstructionPlane
    yz = comp.yZConstructionPlane
    
    comp.isOriginFolderLightBulbOn = light_set[0]
    xy.isLightBulbOn = light_set[1]
    xz.isLightBulbOn = light_set[2]
    yz.isLightBulbOn = light_set[3]