C#ATIA

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

ブラウザツリーのフォルダを表示/非表示する1

1タイトル違いますがこちらの続きです。
全ての原点を表示/非表示する(失敗) - C#ATIA

この辺りのフォルダー(と呼んで良いのかな?)を表示/非表示
させます。

”解析” だけは類似した処理とはならなそうなので、後回し・・・。

# 内部コンポーネントのみです・・・。
# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import json

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface

        keys = [
            "rOriginWorkGeometry",
            "rJointOrigins",
            "rAssyConstraints",
            "rBodies",
            "rSketches",
            "rCanvases",
            "rDecalPatches",
        ]

        [setTreeFolderVisible(key, False) for key in keys]
        ui.messageBox('全部消した!!')

        [setTreeFolderVisible(key, True) for key in keys]
        ui.messageBox('全部表示した!')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


# Tree Folder Visible
def setTreeFolderVisible(key: str, value: bool):

    def getIdsPaths() -> list:
        occPaths = getOccPaths()
        compIds = getTargetComponentIds(occPaths)
        originIds = getPorpsKeyIds(compIds, key)

        pathsLst = []
        for lst in zip(occPaths, compIds, originIds):
            pathsLst.append(':'.join(lst))

        return pathsLst

    def isVisible(paths) -> bool:
        for visible_Key in ("isVisible", "visible"):
            if hasattr(paths, visible_Key):
                return getPorpsKey(paths, visible_Key)
        # "isVisible", "visible" 以外はNone = Falseを返すが良いのか?

    # ************

    app: adsk.core.Application = adsk.core.Application.get()
    sels: adsk.core.Selections = app.userInterface.activeSelections
    sels.clear()

    ents = getIdsPaths()
    ents = [paths for paths in ents if isVisible(paths) != value]
    if len(ents) < 1:
        return

    for paths in ents:
        try:
            app.executeTextCommand(u'Selections.Add {}'.format(paths))
        except:
            pass

    app.executeTextCommand(u'Commands.Start VisibilityToggleCmd')
    sels.clear()

# ****************

def getTargetComponentIds(lst) -> list:
    return getPorpsKeyIds(lst, "rTargetComponent")


def getPorpsKeyIds(lst, key) -> list:
    idsLst = []
    for paths in lst:
        try:
            res = getPorpsKey(paths, key)
            idsLst.append(str(res['entityId']))
        except:
            idsLst.append('-1')

    return idsLst


def getPorpsKey(paths, key) -> str:
    app: adsk.core.Application = adsk.core.Application.get()

    try:
        id = paths.split(':')[-1]

        props = json.loads(
            app.executeTextCommand(u'PEntity.Properties {}'.format(id))
        )
        
        return props[key]
    except:
        return '-1'


def getOccPaths() -> list:
    app: adsk.core.Application = adsk.core.Application.get()
    root: adsk.fusion.Component = app.activeProduct.rootComponent

    lst = [o for o in root.allOccurrences]
    lst.append(root)

    return getPaths(lst)


def getPaths(lst) -> list:
    app: adsk.core.Application = adsk.core.Application.get()
    sels: adsk.core.Selections = app.userInterface.activeSelections

    resLst = []
    for ent in lst:
        sels.clear()
        sels.add(ent)
        res = app.executeTextCommand(u'Selections.List')
        resLst.extend(res.split())
    sels.clear()

    return resLst

・原点
・ジョイントの原点
・ジョイント
・ボディ
・スケッチ
・キャンパス
デカール
が対象です。が相変わらず、リンク付きのコンポーネントは未対応です。
無理。