C#ATIA

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

あなたのアカウント上の全ファイルリストを取得2

こちらの続きです。
あなたのアカウント上の全ファイルリストを取得 - C#ATIA

まさか続きを作るとは思っていませんでした。

データパネル内にデータファイルにアクセスするのが、非常に遅くて
使い物にならないと思っていたのですが、Fusion360 Ver2.0.10027で
アクセススピードが改善されるメソッドが追加されました。

※追記
チーム名も書き出すように修正しました。

# Fusion360API Python script GetDataList Ver0.0.4
# Author-kantoku
# Description-サインインしているID内の全ファイル名の取得

import adsk.core, adsk.fusion, adsk.cam, traceback
import time, datetime
from itertools import chain

def run(context):
    ui :adsk.core.UserInterface = None
    try:
        app :adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        
        # get export path
        path = get_Filepath(ui)
        if path is None:
            return
        
        # time
        t = time.time()
        

        # get data names
        names = []
        pros = app.data.dataProjects.asArray()

        if len(pros) < 1:
            return

        # init progress dialog
        progress :adsk.core.ProgressDialog = ui.createProgressDialog()
        progress.isCancelButtonShown = True
        progress.show('Export Files Name', '', 0, len(pros))

        # get files name
        for pro in pros:
            progress.progressValue += 1
            progress.message = f'Getting {pro.name} ....'
            adsk.doEvents()
            if progress.wasCancelled:
                return

            res = []
            names.append(getFiles(pro.rootFolder, res, 0))
        
        if len(names) < 1:
            ui.messageBox('Data not found')
            return
        
        # get info
        info = getInfo()

        # export
        info.extend(names)
        execExport(path, info)
        
        # finish
        ui.messageBox(f'Done:{time.time() - t}s')
        
    except:
        if ui:
            print('Failed:\n{}'.format(traceback.format_exc()))
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# write file
def execExport(path, lst):
    file = open(path, 'w') 
    file.write('\n'.join(list(chain.from_iterable(lst))))
    file.close()

# User Name & Team Name & Date Time
def getInfo():
    app = adsk.core.Application.get()
    info = [
        [toDecode(f'User Name:{app.currentUser.displayName}')],
        [toDecode(f'Team Name:{app.data.activeHub.name}')],
        [f'Date Time:{datetime.datetime.now()}'],
        [' -- file name-- ']
        ]

    return info

# get file names
def getFiles(fld, res, depth):
    #folder
    res.append((' ' * (depth * 4)) + f'[{toDecode(fld.name)}]')

    #files
    fs = [(' ' * ((depth + 1) * 4)) + toDecode(f.name) for f in fld.dataFiles.asArray()]
    if len(fs) != 0:
        res.extend(fs)

    #subfolder
    fls = fld.dataFolders
    for subFld in fls:
        res = getFiles(subFld, res, depth + 1)

    return res

# decode
def toDecode(s):
     return s.encode("CP932", "ignore").decode("CP932")

# get file path
def get_Filepath(ui):
    dlg = ui.createFileDialog()
    dlg.title = 'File name export'
    dlg.isMultiSelectEnabled = False
    dlg.filter = 'text(*.txt)'
    if dlg.showSave() != adsk.core.DialogResults.DialogOK :
        return
    
    return dlg.filename

ものすごく早いわけではないのですが、以前があまりに遅かったので
かなり改善しています。(自分の場合、15~20秒ぐらい)

使い物になりそうなので、ご説明を。
f:id:kandennti:20210406153748p:plain
黄:ユーザー名と実行した際の日時
赤:一番左から書かれている"[ ]"内がプロジェクト名
緑:それ以外の"[ ]"内がフォルダ名
その他がドキュメント名です。

階層的にはスペース4文字で表現しています。

・・・ユーザー名部分のUser.displayNameが正しく
取得出来ない理由がわからん。
Fusion 360 Help