简体   繁体   中英

Remove Circles in DXF using ezdxf

I would like to delete every Circle entities form a DXF file. I'm using ezdxf, it seems to be a good tool for that kind of work.

I used ezdxf doc to write my code but I get an error from Python :

AttributeError: 'str' objet has no attribute 'destroy'

I don't understand why. I used this doc : http://pythonhosted.org/ezdxf/layouts.html#delete-entities

Here's my code :

import dxfgrabber
import ezdxf

dwg = dxfgrabber.readfile("test.dxf")
print(dwg)
c = []
center_points = [entity.center for entity in dwg.entities if entity.dxftype == 'CIRCLE']
dxf = ezdxf.readfile("test.dxf")
modelspace = dxf.modelspace()
for point in center_points:
    modelspace.add_point(point)
    c.append(point)
modelspace.delete_entity('CIRCLE')
dxf.save()
print(c)

Thanks.

I have succeeded. I post my code :

entities = dxf.entities
for e in entities:
        if e.dxftype() == 'CIRCLE':
            modelspace.delete_entity(e)

Instead of :

modelspace.delete_entity('CIRCLE')

I think it may have a better way to do it but it's working.

  1. To delete a DXF entity, you have to pass a DXF entity object not a string.
  2. If you delete objects from model space it is better to just iterate over the entities of the model space, dxf.entities contains the entities of the model space and the active paper space: for e in dxf.modelspace() ...

or use the query() function:

msp = dxf.modelspace()
for circle in msp.query('CIRCLE'):
    msp.delete_entity(circle)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM