Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not read attributes from block reference #6

Open
thengineer opened this issue Oct 25, 2013 · 10 comments
Open

can not read attributes from block reference #6

thengineer opened this issue Oct 25, 2013 · 10 comments

Comments

@thengineer
Copy link

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
Copy link
Author

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

@cfarrow
Copy link

cfarrow commented Jun 11, 2014

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.

@thengineer
Copy link
Author

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

@cfarrow
Copy link

cfarrow commented Jun 11, 2014

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

@thengineer
Copy link
Author

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.

@LiaungYip
Copy link

@thengineer : Your workaround using win32com is great.

@OndrejJanota
Copy link

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.

@Pilou59
Copy link

Pilou59 commented Jun 24, 2018

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

image
Here are my investigations...

image

@thengineer
Copy link
Author

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.

@lam2912
Copy link

lam2912 commented Jun 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants