使用 SolidWorks PDM API 获取文件 (Python)

Getting files using SolidWorks PDM API (Python)

提问人:BraedonM 提问时间:8/26/2023 最后编辑:Brian Tompsett - 汤莱恩BraedonM 更新时间:10/13/2023 访问量:186

问:

我正在尝试用 python 编写一个脚本,该脚本会自动获取我的 SolidWorks EPDM 库中“文档”文件夹中所有 Excel 文件的最新版本。这是我的代码:

import pythoncom
import win32com.client


def connect_to_vault(vaultName):
    newVault = None

    interface = pythoncom.LoadTypeLib('C:/Program Files (x86)/SOLIDWORKS PDM/EdmInterface.dll')

    for index in range(0, interface.GetTypeInfoCount()):
        type_name = interface.GetDocumentation(index)[0]

        if 'EdmVault5' == type_name:
            type_iid = interface.GetTypeInfo(index).GetTypeAttr().iid
            newVault = win32com.client.Dispatch(type_iid)
            break

    newVault.LoginAuto(vaultName, 0)
    return newVault


def traverse_folder(folder, parent_level="", directory_array=None):
    if directory_array is None:  # First time through
        directory_array = []

    current_directory = parent_level + folder.Name + "\\"

    # Go through all files in current folder
    pdm_file_pos = folder.GetFirstFilePosition()
    # Loop files
    while not pdm_file_pos.IsNull:
        pdm_file = folder.GetNextFile(pdm_file_pos)
        currNm = pdm_file.Name
        # Test for excel file
        if ('.xlsx' not in currNm) and ('.xlsm' not in currNm):
            file_path = 'VAULT\\' + current_directory + currNm
            directory_array.append(file_path)

    # Go through all sub-folders in current folder
    pdm_sub_folder_pos = folder.GetFirstSubFolderPosition()  # Get first sub-folder
    # Loop sub-folders
    while not pdm_sub_folder_pos.IsNull:
        # Get next sub-folder and traverse
        pdm_sub_folder = folder.GetNextSubFolder(pdm_sub_folder_pos)
        traverse_folder(pdm_sub_folder, current_directory, directory_array)
        if len(directory_array) >= 80:
            return directory_array

    return directory_array


def getLatestVersions():
    vault_name = 'VAULT'
    newPath = "C:\\PDM\\"
    folder_path = "VAULT\\Documents\\"

    # Connect
    vault = connect_to_vault(vault_name)

    folder_path = newPath + folder_path
    temp_ProjID = vault.GetFolderFromPath(folder_path)

    # Get list of all files
    filteredFiles = traverse_folder(temp_ProjID)
    
    changedFiles = []  # List of files that have been updated
    # Loop through files & Get Latest Version of each
    for idx, file in enumerate(filteredFiles):

        temp_ProjID = vault.GetFolderFromPath(folder_path)
        temp_DocID = vault.GetFileFromPath(newPath + file, temp_ProjID)[0]

        # Get versions to compare
        currentVersion = temp_DocID.CurrentVersion
        localVersion = temp_DocID.GetLocalVersionNo(newPath + file)

        if localVersion != currentVersion:  # Version mismatch
            
            # --------- THIS LINE THROWS ERROR -------------------
            temp_DocID.GetFileCopy(0, '', temp_DocID.ID, 16, '')
            # ----------------------------------------------------
            changedFiles.append(newPath + file)

        filteredFiles[idx] = newPath + file

    return filteredFiles, changedFiles


if __name__ == "__main__":
    getLatestVersions()

在到达 GetFileCopy() 命令之前,一切都有效,然后它给了我这个错误:

pywintypes.com_error: (-2147352573, '未找到成员.', None, None)

我尝试了一堆方法和 PDM 命令,这是我最接近的。我在使用 Python 11 和 win3.7 的 Solidworks PDM 2021 上使用 Windows 32com

有什么建议吗?

蟒蛇 com SOLIDWORKS PDM

评论


答:

0赞 bigcrazyal 8/28/2023 #1

我相信这只是归结为一个不正确的参数,根据 API 文档,该函数需要目录路径或应该复制它的文件夹的 ID。您需要传递项目(文件夹)ID,而不是文档 ID。

更正行:

temp_DocID.GetFileCopy(0, '', temp_ProjID.ID, 16, '')