pyautocad
                                
                                
                                
                                    pyautocad copied to clipboard
                            
                            
                            
                        can not read attributes from block reference
I'm trying to read block attributes:
from pyautocad import Autocad
acad =  Autocad()       
Reading other properties works fine:
In [89]: b.InsertionPoint
Out[89]: (3764.4951495785735, 745.3710248379173, 0.0)
However, when I want to query the attributes (which I'm actually interested in), I get an error message:
In [88]: b.GetAttributes()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-88-a01693316a19> in <module>()
----> 1 b.GetAttributes()
C:\ProgramFiles\Python27\lib\site-packages\comtypes\automation.pyc in __ctypes_from_outparam__(self)
    440     def __ctypes_from_outparam__(self):
    441         # XXX Manual resource management, because of the VARIANT bug:
--> 442         result = self.value
    443         self.value = None
    444         return result
C:\ProgramFiles\Python27\lib\site-packages\comtypes\automation.pyc in _get_value(self, dynamic)
    391             return value
    392         elif self.vt & VT_ARRAY:
--> 393             typ = _vartype_to_ctype[self.vt & ~VT_ARRAY]
    394             return cast(self._.pparray, _midlSAFEARRAY(typ)).unpack()
    395         else:
Did some research on Google, but my knowledge of COM is just too low to help myself here..
I solved this by using win32com. In case anybody comes across the same problem (and wants to save a few hours of work): https://gist.github.com/thengineer/7157510
I just opened an issue for this in the comtypes package, on which pyautocad is based. @thengineer, if you can provide more information about the KeyError, I may be able to fix this issue.
@cfarrow: That's good to know someone is working on COM related things :)
However all that I could find out, was that comtypes doesnt handle the required data type within VT_arrays or something like this.
I wish I could help you more on this. (Well, at least I'm running AutoCAD, so I could  test things)
I could test this out as well. I'll need AutoCAD, and so as much information that you can give me up front will help me make the most of a trial license. :)
you will find the VBA Reference under C:\Program Files\Common Files\Autodesk Shared\acadauto.chm, which gives you a good overview of the object model in autocad.
The actual problematic things are the Attributes (= user definable key/value pairs) of the Blocks (or Block references actually). See my gist file posted above for how to access them with win32com.
If you need help creating such a block or need an example file, let me know.
@thengineer : Your workaround using win32com is great.
Hi, I'm totally new in python (3 days) and I would like to create a program which rewrite block attributes. However, the script provided by thengineer (Here: https://gist.github.com/thengineer/7157510) doesn't work. I get this error (trying on Acad2017, drawing with only just one block with manualy defined block attribute):
Traceback (most recent call last):
File "E:\Python\Programs\Autocad\Blockreference\block.py", line 21, in 
Has anyone the same problem? Thanks for the reply.
I've got the same issue, but using getattributes() method.
Here are my investigations...

Interestingly I came across the same issue a few months ago. I ended up checking the existence of attributes using python's hasattr() before accessing attributes.
I solved this by using win32com. In case anybody comes across the same problem (and wants to save a few hours of work): https://gist.github.com/thengineer/7157510
Please tell me: How to Setup Module Win 32???