Reputation: 11
I hope to develop some Solidworks PDM tools using Python. Currently, I am testing the connection of PDM, checking in and checking out files. Here's my code:
import pythoncom
import win32com.client
from pdm_lib import *
folder_path = 'C:\\Users\\SJT\\Desktop\\DevelopmentBall\\'
# Replace with the folder path
file_path = 'C:\\Users\\SJT\\Desktop\\DevelopmentBall\\Part1002.SLDPRT'
# Replace with the file path that needs to be checked in and out
vault_name = 'DevelopmentBall'
# PDM vault name
vault = EdmVault5()
# Login using the PDM vault name
vault.LoginAuto(vault_name, 0)
folder = vault.GetFolderFromPath(folder_path)
file = vault.GetFileFromPath(file_path, folder)[0]
# Check out the file
file.LockFile(folder.ID, file.CurrentVersion)
if file.IsLocked:
print('File checked out successfully')
# Open the file using SolidWorks, make changes, and save the file
swApp = win32com.client.Dispatch(f'SldWorks.Application')
swApp.CommandInProgress = True
swApp.Visible = True
Errors = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
Warnings = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
sw_app = swApp.OpenDoc6(file_path, 1, 1, "", Errors, Warnings)
sw_app.Save()
swApp.CloseDoc(file_path)
# swApp.ExitApp()
# After updating the file, refresh the folder and file parameters
folder = vault.GetFolderFromPath(folder_path)
file = vault.GetFileFromPath(file_path, folder)[0]
# Check in the file
file.UnlockFile(0, "Check-in file test", 0, None)
if file.IsLocked != '':
print('File checked in successfully')
At present, it is possible to log in to PDM normally, check out files normally, and then open the file through Solidworks. After saving the file, using Unlockfile to check in the file will result in an error.
file.UnlockFile(0, "Check-in file test", 0, None)
# Check in the file
if file.IsLocked != '':
print('File checked in successfully')
The following is an error message:
Traceback (most recent call last):
File "d:\PY-SW\76.py", line 36, in <module>
file.UnlockFile(0, "Check-in file test", 0, None)
File "d:\PY-SW\pdm_lib.py", line 4255, in UnlockFile
return self._oleobj_.InvokeTypes(12, LCID, 1, (24, 0), ((3, 1), (8, 1), (3, 49), (13, 49)),lParentWnd
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: The Python instance can not be converted to a COM object
When I delete the middle section of code about opening and saving files in Solidworks, the overall code runs normally, including checking out and checking in files.
swApp = win32com.client.Dispatch(f'SldWorks.Application')
swApp.CommandInProgress = True
swApp.Visible = True
Errors = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
Warnings = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
sw_app = swApp.OpenDoc6(file_path, 1, 1, "", Errors, Warnings)
sw_app.Save()
swApp.CloseDoc(file_path)
# swApp.ExitApp()
# Open the file using SolidWorks, make changes, and save the file
How should I modify the code to make it run properly?
Upvotes: 1
Views: 187