I have tried the following code which can be found here, my objective is to take out one layer from a .dxf file, and the copy to a new file only with that information. On the link where I found the code, the made the following code, but I do not understand why I have an error. I tried to change the layer name, but it didn't work.
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()
My case is very simple and similar to that code, but I get the following error:
raise const.DXFAttributeError(
DXFAttributeError: Invalid DXF attribute "layer" for entity MPOLYGON
I didn't found any bibliography tied to that error there is not too much information about this library error on the site.
MPOLYGON is not documented in the DXF reference and is therefore only preserved as DXFTagStorage and has no layer attribute.
Entities without a layer attribute will be deleted:
delete_entities = [
e for e in msp
if not e.dxf.is_supported('layer') or e.dxf.layer.lower() not in KEEP_LAYERS_LOWER
]
Edit: unlink for unsupported entities will work in v0.16.2, until then destroy the entities:
for e in msp:
if e.dxf.is_supported('layer') and e.dxf.layer.lower() in KEEP_LAYERS_LOWER:
continue
e.destroy()
Maybe you can post your DXF file (zipped) to see what a MPOLYGON should be, I guess it is a synonym for an already supported DXF type.
Edit: MPOLYGON seems to be an AutoCAD Map specific entity.