0

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.

Sultry T.
  • 69
  • 10

1 Answers1

0

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.

mozman
  • 2,001
  • 8
  • 23
  • Thank you for the response. Sadly is a confidential project I can't share that file here, and It still has the same error after your correction, but can you explain me two things @mozman: in the `default` is the name of the layer? and second, the KEEP_LAYERS = {' LAYER '} I did put the layer name but I don't know if I have to put it on the format: LAYER(#45t4578XX) etc.. Sorry, but I am totally new on AutoCAD and I do not have a lot of information on it. – Sultry T. Apr 22 '21 at 13:17
  • Layers are referenced by their name. OK unsupported DXF entities can not be handled that way. I will fix this. – mozman Apr 22 '21 at 13:29
  • Thank you, That look it works, but now I have the error in the `for entity in delete_entities:` part: `AttributError: 'DXFTagStorage' object has no attribute 'set_owner'` maybe now I can't unlink... can I ? – Sultry T. Apr 22 '21 at 13:48
  • If I print the list which you suggest I get a long data like this (cool): `[ ... INSERT(#D5F83B), **MPOLYGON(#D5F83F)**, **MPOLYGON(#D5F840)**, LINE(#11FF27C), ...]` Here is the entity which give the error! But I can't do the `for` I mentioned before... – Sultry T. Apr 22 '21 at 14:18
  • ezdxf is not designed to process unsupported entities, it just preserves them. I will add the possibility to unlink and delete them from layouts in the next version. Until the next version is released you can only destroy the entities, which is the brutal way to remove entities. – mozman Apr 22 '21 at 14:41
  • I thought `unlink_entity` was the correct way, but I have no problem to use `delete_entity`, function. However, I still have the same problem with `delete_entity` function when I put in the FOR: `for entity in delete_entities: msp.unlink_entity(entity)` to 'msp.delete_entity(entity)` I get this error: AttributError: 'DXFTagStorage' object has no attribute 'set_owner' – Sultry T. Apr 22 '21 at 14:59