C#ATIA

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

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

こちらの続きです。
ブラウザツリーのフォルダを表示/非表示する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",
            "rWorkGeometries",
            "VisualAnalyses",
        ]

        [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 getVisualAnalysesPaths() -> str:
        app: adsk.core.Application = adsk.core.Application.get()
        rootOccId = app.executeTextCommand(u'PEntity.ID rootInstance')
        rootComp = getPorpsKey(rootOccId, "rTargetComponent")
        analysesId = app.executeTextCommand(u'PEntity.ID VisualAnalyses')

        return f'{rootOccId}:{rootComp["entityId"]}:{analysesId}'

    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
    # ************

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

    ents: list = []
    if key == "VisualAnalyses":
        ents.append(getVisualAnalysesPaths())
    else:
        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 isVisible(paths) -> bool:
    try:
        props = getPorps(paths)
        for visible_Key in ("isVisible", "visible"):
            if not visible_Key in props:
                continue
            return getPorpsKey(paths, visible_Key)

    except:
        return False
    # "isVisible", "visible" 以外はNone = Falseを返すが良いのか?


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 = getPorps(paths)
        return props[key]
    except:
        return '-1'


def getPorps(paths) -> dict:
    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
    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

こんな感じです。

全部出来ているし、後は無いはず。
他は全て表示に関して "isVisible" で判断出来るのに、
"ジョイント原点" だけ "visible" なの勘弁してほしい。
ネーミングからしてメソッドとプロパティの様にも
感じるけど、統一して欲しい。