【问题标题】:Find .dxf entity based on handle with EZDXF library使用 EZDXF 库根据句柄查找 .dxf 实体
【发布时间】:2020-03-01 01:29:55
【问题描述】:

我一直在使用 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 中可行吗?我已经列出了我需要更改的所有特定句柄,理想情况下,我宁愿遍历该列表而不是遍历所有实体以检查它们的句柄。

【问题讨论】:

    标签: python autocad dxf ezdxf


    【解决方案1】:

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

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

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

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

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

    p2 = doc.entitydb.get(p.dxf.handle)
    assert p2 is p
    assert doc.entitydb.get('ABBA') is None
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      相关资源
      最近更新 更多