C#ATIA

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

カスタムグラフィックスを削除する2

Fusion360APIでは、画面上に面等の要素を表示させる方法が
知っている限りでは3つあります。

・BRep
通常の手動(GUI)で作業した際に出来上がるものです。

・TemporaryBRep
結果的にBRepと同じになりますが、履歴が使えません。
又、複雑な形状を作る方法が恐らく無く、プリミティブなもののみです。
但し、カーネルで直接処理されるようで高速です。

・Custom Graphics
あくまで画面上に要素を表示されるのみで、モデリングには利用出来ないものです。
主にコマンドのプレビューに利用する為のものの様です。

Custom GraphicsがAPIで公開されたのは、TemporaryBRepよりも早いです。
恐らく処理は3つの中では最速なのでは無いかと思ってます。
但し、ちょっとフォーマットが独特で扱いにくいです。


試したことが無いため取り組みたいのですが、Custom Graphicsは手動で
削除出来ません。質が悪い・・・。

そこでまずは、ファイル内の全てのCustom Graphicsを削除するだけのスクリプト
作成しました。

#Fusion360API Python script
#Author-kantoku
#Description-カスタムグラフィックス全てを削除する

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app  :adsk.core.Application = adsk.core.Application.get()
        ui   :adsk.core.UserInterface = app.userInterface
        des  :adsk.fusion.Design = app.activeProduct

        # カスタムグラフィック取得
        cgs = [cmp.customGraphicsGroups for cmp in des.allComponents]
        cgs = [cg for cg in cgs if cg.count > 0]

        msg :str = ''
        if len(cgs) < 1:
            msg = '現在のファイルにはカスタムグラフィックがありませんでした'
            ui.messageBox(msg)
            return

        # 問い合わせ
        msg = '現在のファイルには消去されていないカスタムグラフィックが存在します。\n'
        msg += '全て消去しますか?'
        if not userQuery(ui, msg):
            return

        # 削除
        for cg in cgs:
            gps = [c for c in cg]
            gps.reverse()
            for gp in gps:
                gp.deleteMe()

        # 終了
        app.activeViewport.refresh()
        ui.messageBox('done')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def userQuery(
    ui :adsk.core.UserInterface,
    msg :str) -> bool:

    btnTypes = adsk.core.MessageBoxButtonTypes
    icnTypes = adsk.core.MessageBoxIconTypes
    dlgRes = adsk.core.DialogResults
    res = ui.messageBox(msg, '', btnTypes.OKCancelButtonType, icnTypes.QuestionIconType)
    if res == dlgRes.DialogOK:
        return True
    return False

※全てを削除していなかったバグを修正しました・・・。