0

I've been learning python (2.7) for a couple of weeks to be able to automatise autocad, so, be patient I'm python noob.

I'm trying to get the attributes of a newely created block to modify them, the doc says that in VBA it would be:

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 & "    "

I've been translating several snippets of VBA code to Python code and it worked every time, but for this one, I'm trying:

acad = comtypes.client.GetActiveObject("AutoCAD.Application")
doc = acad.ActiveDocument
ms = doc.ModelSpace
myBlock = ms.InsertBlock(array.array('d', [0, 0, 0]), 'TESTBLOCK', 1, 1, 1, 0)
varAttributes = myBlock.getAttributes()
for i in varAttributes:
    print i

But I get the following error:

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

From what I read from the VBA snippet, getAttributes method must return an array, so my question is twofold:

  1. Is using .getAttributes and .getAttributes() the same thing in python? noob question
  2. Why the ActiveX method used in Python does not give the same result (an array) as in VBA?

EDIT From here I understand that perhaps Python is:

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

From the doc, the kind of array returned is:

Type: Variant (array of AttributeReference objects)

So, perhaps it's because it's not a simple array, but an array of objects that is returned, does somebody knows how to circumvent this?

Community
  • 1
  • 1
ubugnu
  • 1,004
  • 2
  • 15
  • 30

1 Answers1

1

I found the solution here:

You have to uncomment two line, for me it's 862 and 863 in Python27\Lib\site-packages\comtypes\automation.py then it works with getAttributes()

ubugnu
  • 1,004
  • 2
  • 15
  • 30