win32com 变体:SAFEARRAYS 必须是序列

问题描述 投票:0回答:0

我正在尝试通过 win23com 从 Python 使用它的 VBA API 访问 Solidworks CAD 模型的数据。 我成功地打开了应用程序并从文件中访问了一些数据。

现在,我想移植对配置特定参数的访问: VB代码是:

    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swConfigMgr As SldWorks.ConfigurationManager
    Dim vConfName As Variant
    Dim vConfParam As Variant
    Dim vConfValue As Variant
    Dim i As Long
    Dim j As Long
    Dim bRet As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swConfigMgr = swModel.ConfigurationManager
    Debug.Print "File = " + swModel.GetPathName
    vConfName = swModel.GetConfigurationNames
    For i = 0 To UBound(vConfName)
        bRet = swConfigMgr.GetConfigurationParams(vConfName(i), vConfParam, vConfValue)
        Debug.Assert bRet
        Debug.Print "  Configuration = " & vConfName(i)
        If Not IsEmpty(vConfParam) Then
            For j = 0 To UBound(vConfParam)
                Debug.Print "    " & vConfParam(j) & " = " & vConfValue(j)
            Next j
        End If
    Next i    
End Sub

(有关详细信息,请参见此处:https://help.solidworks.com/2021/english/api/sldworksapi/Extract_Configuration-Specific_Parameters_Example_VB.htm

在 Python 中,我的代码如下所示:

...
    configManager = sw.cm
    configurations = doc.GetConfigurationNames

    for config in configurations:
        keys = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_ARRAY, "")
        values = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_ARRAY, "")
        result = configManager.GetConfigurationParams(config, keys, values)
...

我卡在了

GetConfigurationParams
(可以在这里找到VB函数参考)并出现以下错误:

Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.

问题是如何复制变量的定义

Dim vConfName As Variant,Dim vConfParam As Variant
我尝试了
win32com.client.VARIANT
定义的多种组合,但没有任何效果。

对于

keys & values
的更简单定义,例如
None, 0, "", []
我得到
pywintypes.com_error: (-2147352571, 'Typenkonflikt.', None, 2)

我也尝试了

win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_ARRAY, VALUE)
几个值,比如
'', 0, None, {}
没有效果。像
(), [],
这样的价值观给
MemoryError: CreatingSafeArray

python win32com solidworksapi
© www.soinside.com 2019 - 2024. All rights reserved.