如果AutoCAD工程图中的材料明细表(BOM)位于“块参考”中,则要使用pyautocad读取BOM,可以使用以下代码读取它。

from pyautocad import Autocad

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


但这会引发异常
comtypes \ automation.py,第457行,位于_get_value typ = _vartype_to_ctype [self.vt&〜VT_ARRAY] KeyError:9

如何使用pyautocad在AutoCAD中读取BoM。

最佳答案

根据pyautocad存储库中记录的问题,https://github.com/reclosedev/pyautocad/issues/6 comtypes存在与访问数组有关的问题。
因此,要读取块引用,我们必须使用win32com,如下所示:

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


有关更多详细信息,请参见https://gist.github.com/thengineer/7157510

10-07 18:27