我已经尝试了可以在此处找到的以下代码,我的目标是从 .dxf 文件中取出一层,然后仅使用该信息将其复制到新文件中。在我找到代码的链接上,制作了以下代码,但我不明白为什么我有错误。我尝试更改图层名称,但没有成功。
from shutil import copyfile
import ezdxf
ORIGINAL_FILE = 'test.dxf'
FILE_COPY = 'test2.dxf'
KEEP_LAYERS = {'Layer1', 'Layer2', 'AndSoOn...'}
KEEP_LAYERS_LOWER = {layer.lower() for layer in KEEP_LAYERS}
# copy original DXF file
copyfile(ORIGINAL_FILE, FILE_COPY)
dwg = ezdxf.readfile(FILE_COPY)
msp = dwg.modelspace()
# AutoCAD treats layer names case insensitive: 'Test' == 'TEST'
# but this is maybe not true for all CAD applications.
# And NEVER delete entities from a collection while iterating.
delete_entities = [entity for entity in msp if entity.dxf.layer.lower() not in KEEP_LAYERS_LOWER]
for entity in delete_entities:
msp.unlink_entity(entity)
dwg.save()
我的案例非常简单,与该代码类似,但出现以下错误:
raise const.DXFAttributeError(
DXFAttributeError: Invalid DXF attribute "layer" for entity MPOLYGON
我没有找到与该错误相关的任何参考书目,网站上没有太多关于此库错误的信息。