C#ATIA

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

全ての原点を表示/非表示する3

こちらの続きです。
全ての原点を表示/非表示する2 - C#ATIA

つまりこうやれば良いみたい!!

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import urllib

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

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

        setAllVisible_Key('OriginWorkGeometry', False)
        ui.messageBox('原点全部消した!!')

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


def setAllVisible_Key(
    key: str,
    visible: bool) -> None:

    onks = getAllOnk_Key_NotVisible(key, visible)

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

    for onk in onks:
        app.log(onk)
        app.executeTextCommand(u'Commands.Select {}'.format(onk))

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

    sels.clear()


def getAllOnk_Key_NotVisible(
    key: str,
    visible: bool) -> list:

    OccOnks = getAllOccONK()
    onks = [f'{o}/{key}' for o in OccOnks]

    return [onk for onk in onks if isVisible_Onk(onk) != visible]


def isVisible_Onk(
    onk: str) -> bool:

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

        app.executeTextCommand(u'Commands.Select {}'.format(onk))
        res = app.executeTextCommand(u'VO.CheckPathVisibility')
        sels.clear()
        value = res.split('.')[0]

        return 'TRUE' == f'{value}'.upper()

    except:
        return False
        # これで良いのか?


def getAllOccONK() -> list:

    app: adsk.core.Application = adsk.core.Application.get()
    des: adsk.fusion.Design = app.activeProduct
    root: adsk.fusion.Component = des.rootComponent

    occ_comp_dict: dict = initDict_Occ_Comp()
    onks = [
        getRootONK()
    ]
    onks.extend([getOccONK(occ, occ_comp_dict) for occ in root.allOccurrences])

    return onks


def getOccONK(
    occ: adsk.fusion.Occurrence,
    occ_comp_dict: dict) -> str:

    onks = [
        getRootONK(occ.component.parentDesign)
    ]
    paths = occ.fullPathName.split('+')
    for path in paths:
        onks.append(
            '/CmpInsts/CmpInst={}/Cmp={}'.format(
                urllib.parse.quote(path),
                urllib.parse.quote(occ_comp_dict[path])
            )
        )
    return ''.join(onks)


def initDict_Occ_Comp():
    app: adsk.core.Application = adsk.core.Application.get()
    des: adsk.fusion.Design = app.activeProduct
    root: adsk.fusion.Component = des.rootComponent

    dict = {}
    for occ in root.allOccurrences:
        comp: adsk.fusion.Component = occ.component
        dict[occ.name] = comp.name

    return dict


def getRootONK(
    des: adsk.fusion.Design = None) -> str:

    if not des:
        app: adsk.core.Application = adsk.core.Application.get()
        des = app.activeProduct

    root: adsk.fusion.Component = des.rootComponent
    return f'ONK::CmpInst={urllib.parse.quote(root.name)}/Cmp={urllib.parse.quote(root.name)}'

リンク付きコンポーネントも出来ている。
選択したり、解除したりを大量に行っているので、
レスポンスが若干悪いけど、今まで出来なかった事が
出来るようになったから許してもらおう。

これなら利用する価値十分あるはず。