代码之家  ›  专栏  ›  技术社区  ›  gg sps

win32com变量:SAFEARRAYS必须是序列

  •  0
  • gg sps  · 技术社区  · 2 年前

    我试图通过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函数引用 here )

    GetConfigurationParams的类型库如下所示:

    def GetConfigurationParams(self, ConfigName=defaultNamedNotOptArg, Params=pythoncom.Missing, Values=pythoncom.Missing):
            'Get the parameters for a configuration'
            return self._ApplyTypes_(2, 1, (11, 0), ((8, 1), (16396, 2), (16396, 2)), 'GetConfigurationParams', None,ConfigName
                , Params, Values)
    

    据我所知,我应该打电话 keys,values = configManager.GetConfigurationParams(config) 但我明白 pywintypes.com_error: (-2147352561, 'Parameter nicht optional.', None, None)

    0 回复  |  直到 2 年前
        1
  •  1
  •   gg sps    2 年前

    问题解决了。

    类型库具有所需的信息和 this issue 有助于理解所写内容:

    def GetConfigurationParams(self, ConfigName=defaultNamedNotOptArg, Params=pythoncom.Missing, Values=pythoncom.Missing):
            'Get the parameters for a configuration'
            return self._ApplyTypes_(2, 1, (11, 0), ((8, 1), (16396, 2), (16396, 2)), 'GetConfigurationParams', None,ConfigName
                , Params, Values)
    

    的第三个元素 self._ApplyTypes_ 以下为: ((8, 1), (16396, 2), (16396, 2)) 提供了有关所需数据的提示:

    1. 有3个功能参数。
    1: (8, 1)
    2: (16396, 2)
    3: (16396, 2)
    
    1. 对于每个参数,第一个元素是数据类型:

    (参见 https://en.wikipedia.org/wiki/Variant_type_(COM) 用于VarType、HEX和Propvariant上的参考)。

    特殊情况: 16396 = 0x400c = VT_BYREF 但是VT_BYREF必须出现在VARIANT中,因此它 VT_VARIANT | VT_BYREF (必须阅读 this this 详细到Unerstand)

    1. 对于每个参数,第二个元素指示其用途:
    1: input
    2: output 
    3: both (3 = 1 | 2)
    

    所以有效的解决方案是

    config = win32com.client.VARIANT(pythoncom.VT_BSTR, config)
    keys = win32com.client.VARIANT(pythoncom.VT_VARIANT | pythoncom.VT_BYREF, [])
    values = win32com.client.VARIANT(pythoncom.VT_VARIANT | pythoncom.VT_BYREF, [])
    
    result = configManager.GetConfigurationParams(config, keys, values)
    

    result 返回True和 keys & values 以达到预期的结果。

    推荐文章