0

我一直在使用 Autocad API 根据(预先确定的)句柄 ID 填写块属性。下面是一个实现示例:

    frame1 = acad.ActiveDocument.HandleToObject('18CA1')
    frame2 = acad.ActiveDocument.HandleToObject('77CE9')

    frames = [frame1, frame2]


    for i in range(len(frames)):
        for attrib in frames[i].GetAttributes():
            if attrib.TagString == 'DATE':
                attrib.TextString = datasource.date
            if attrib.TagString == 'CLIENT_NAME':
                attrib.TextString = datasource.client_name
            attrib.Update()

现在我想使用 ezdxf 库实现相同的功能。我只是找不到类似于.HandleToObject("xxx")的方法。根据以下代码,我确定句柄 ID 确实与 AutoCAD 实现中的相同。

modelspace = dxf.modelspace()

for e in modelspace:
    if e.dxftype()== 'TEXT':
        print("text: %s\n" % e.dxf.text)
        print("handle: %s\n" % e.dxf.handle)

这在ezdxf中可行吗?我已经列出了我需要更改的所有特定句柄,理想情况下,我宁愿遍历该列表而不是遍历所有实体以检查它们的句柄。

4

1 回答 1

3

ezdxf通过句柄作为键将 DXF 文档的所有实体存储在实体数据库中:

doc = ezdxf.new()
msp = doc.modelspace()
p = msp.add_point((0, 0))

通过索引运算符检索实体:

p1 = doc.entitydb[p.dxf.handle]
assert p1 is p

如果句柄不存在,此方法会引发 a KeyError,如果句柄不存在,则get()函数返回None

p2 = doc.entitydb.get(p.dxf.handle)
assert p2 is p
assert doc.entitydb.get('ABBA') is None
于 2020-02-28T17:37:00.293 回答