C#ATIA

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

全ての原点を表示/非表示する(失敗)

Fusion360です。

Assy状態で表示されている原点が邪魔な場合ってありますよね?
非表示させれば良いだけなのですが、深いAssyの場合は面倒
ですよね?

この辺全部です。


それをスクリプトで、、、ってAPIが提供されていないんです。

そこでTextCommands利用して頑張りました。

# 内部コンポーネントのみです・・・。
# 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

        setAllOriginVisible(True)
        ui.messageBox('原点全部表示した!')

        setAllOriginVisible(False)
        ui.messageBox('原点全部消した!!')

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



def setAllOriginVisible(value: bool):
    app: adsk.core.Application = adsk.core.Application.get()
    sels: adsk.core.Selections = app.userInterface.activeSelections
    sels.clear()

    originPaths = getOriginPaths()

    ents = [paths for paths in originPaths if getPorpsKey(paths, "isVisible") != 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 getOriginPaths() -> list:
    occPaths = getOccPaths()
    compIds = getTargetComponentIds(occPaths )
    originIds = getOriginWorkGeometrytIds(compIds)

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

    return pathsLst


def getOriginWorkGeometrytIds(lst) -> list:
    return getPorpsKeyIds(lst, "rOriginWorkGeometry")


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('')

    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 ''


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

テストしてみてガッカリ。 内部コンポーネントはOKです。
しかしリンク付きのコンポーネントはダメなんですよ・・・。
残念。