Rohan D
Rohan D

Reputation: 514

How to access Block reference attributes in pyautocad

If Bill of Material (BOM) in AutoCAD drawing are in 'Block Reference', to read BOM using pyautocad we can read it using following code.

from pyautocad import Autocad

acad = Autocad()
for obj in acad.iter_objects(['Block']):
        if obj.HasAttributes:
            obj.GetAttributes()

But it throws exception in
comtypes\automation.py, line 457, in _get_value typ = _vartype_to_ctype[self.vt & ~VT_ARRAY] KeyError: 9

How to read BoM in AutoCAD using pyautocad.

Upvotes: 2

Views: 6205

Answers (1)

Rohan D
Rohan D

Reputation: 514

As per issue logged in pyautocad repository, https://github.com/reclosedev/pyautocad/issues/6 comtypes has issue related to accessing array. Hence to read block references we have to use win32com as follows:

import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")

# iterate through all objects (entities) in the currently opened drawing
# and if its a BlockReference, display its attributes.
for entity in acad.ActiveDocument.ModelSpace:
    name = entity.EntityName
    if name == 'AcDbBlockReference':
        HasAttributes = entity.HasAttributes
        if HasAttributes:
            for attrib in entity.GetAttributes():
                print("  {}: {}".format(attrib.TagString, attrib.TextString))

for more details you can see https://gist.github.com/thengineer/7157510.

Upvotes: 2

Related Questions