pyautocad icon indicating copy to clipboard operation
pyautocad copied to clipboard

can not read attributes from block reference

Open thengineer opened this issue 12 years ago • 12 comments

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..

thengineer avatar Oct 25 '13 14:10 thengineer

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

thengineer avatar Oct 25 '13 16:10 thengineer

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 avatar Jun 11 '14 02:06 cfarrow

@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)

thengineer avatar Jun 11 '14 17:06 thengineer

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. :)

cfarrow avatar Jun 11 '14 17:06 cfarrow

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 avatar Jun 11 '14 17:06 thengineer

@thengineer : Your workaround using win32com is great.

LiaungYip avatar Aug 09 '15 19:08 LiaungYip

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 HasAttributes = entity.HasAttributes() # => tohle nějak nefunguje nevím proč File "E:\Python\lib\site-packages\win32com\client_init_.py", line 473, in getattr raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr)) AttributeError: '<win32com.gen_py.AutoCAD/ObjectDBX Common 21.0 Type Library.IAcadEntity instance at 0x2236061099512>' object has no attribute 'HasAttributes'

Has anyone the same problem? Thanks for the reply.

OndrejJanota avatar May 01 '18 20:05 OndrejJanota

I've got the same issue, but using getattributes() method.

image Here are my investigations...

image

Pilou59 avatar Jun 24 '18 14:06 Pilou59

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.

thengineer avatar Aug 17 '18 11:08 thengineer

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???

lam2912 avatar Jun 08 '23 05:06 lam2912