Python(ctypes)中的“异常:访问冲突写入”错误

2024-10-03 19:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经学了几个星期的python(2.7)来实现autocad的自动化,所以,耐心点,我是python noob。在

我试图获取新创建的块的属性来修改它们,doc表示在VBA中它将是:

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "TESTBLOCK", 1#, 1#, 1#, 0)
varAttributes = blockRefObj.GetAttributes
For I = LBound(varAttributes) To UBound(varAttributes)
    strAttributes = strAttributes & vbLf & "  Tag: " & varAttributes(I).TagString & _
        vbLf & "  Value: " & varAttributes(I).TextString & vbLf & "    "

我一直在将一些VBA代码片段转换为Python代码,每次都能奏效,但对于这一段,我尝试:

^{pr2}$

但我得到了以下错误:

Traceback (most recent call last):
  File "C:/Ab******/test3/abtro2.py", line 84, in <module>
    add_PB(pt[0], pt[1], PB, int(ELR), PB_type, type_PEO, int(sorties), PB_addr[float(re.sub('PB', '', PB))])
  File "C:/Ab******/test3/abtro2.py", line 33, in add_PB
    varAttributes = myPB_Block.getAttributes()
  File "C:\Python27\lib\site-packages\comtypes\automation.py", line 506, in __ctypes_from_outparam__
    result = self.value
  File "C:\Python27\lib\site-packages\comtypes\automation.py", line 457, in _get_value
    typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
KeyError: 9
Exception WindowsError: 'exception: access violation writing 0x005608A4' in  ignored

根据我从VBA片段中读到的内容,getAttributes方法必须返回一个数组,因此我的问题有两个:

  1. 在python中使用.getAttributes和{}是相同的吗?noob问题
  2. 为什么Python中使用的ActiveX方法不能给出与VBA中相同的结果(数组)?在

编辑here我了解到Python可能是:

performing illegal operations (the kind of operations that if went unchecked would probably crash your system).

从doc返回的数组类型为:

Type: Variant (array of AttributeReference objects)

所以,也许是因为它不是一个简单的数组,而是一个返回的对象数组,有人知道如何绕过它吗?在


Tags: 代码inpydocline数组vbafile