通过win32com.client在我的python项目中使用tlb库。我很容易地处理了许多内置函数,但是其中一个主要函数获得了其中两个被标记为ref int的参数列表。当我试图将python整数传递给函数时,我会得到pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 5)错误,这显然是因为传递给对象的一些参数错误。
这是我的python代码:
import sldworksPython as solidWorks
import sldconstPython as solidConst
import win32com.client
swApp: solidWorks.ISldWorks = win32com.client.Dispatch(solidWorks.SldWorks.CLSID)
swConst = solidConst.constants
fileName = "Assem Of Hinge.SLDASM"
docType = int(swConst.swDocASSEMBLY)
config = int(swConst.swOpenDocOptions_AutoMissingConfig)
error = int(swConst.swFileNotFoundError)
warning = int(swConst.swFileLoadWarning_AlreadyOpen)
print(type(error))
swApp.OpenDoc6(
fileName,
docType,
config,
error,
warning
)下面是openDoc6函数:
ModelDoc2 OpenDoc6(string FileName, int Type, int Options, string Configuration, ref int Errors, ref int Warnings);这个错误把我吓坏了,我真的不想在这个项目中使用C#。谢谢你的帮忙
发布于 2021-10-30 06:57:25
感谢@BoarGules,我刚刚下载了一个名为pySW的SW库。我想知道函数在那里是如何工作的,所以我刚刚检查了它们,并且理解了如何传递一个整数作为C#函数的引用。
我在下面补充我的解决办法:
import sldconstPython as solidConst
import win32com.client
import pythoncom
import pySW
swApp: solidWorks.ISldWorks = win32com.client.Dispatch(solidWorks.SldWorks.CLSID)
swConst = solidConst.constants
fileName = "Assem Of Hinge.SLDASM"
docType = swConst.swDocASSEMBLY
docOpts = swConst.swOpenDocOptions_AutoMissingConfig
error = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, swConst.swFileNotFoundError)
warning = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, swConst.swFileLoadWarning_AlreadyOpen)
swApp.OpenDoc6(fileName, docType, docOpts, "", error, warning)这很奇怪,因为VScode没有建议我使用任何pythoncom常量。不管怎么说,希望这会对面对这样问题的人有所帮助。
https://stackoverflow.com/questions/69772534
复制相似问题