Loïc Kreseski
Loïc Kreseski

Reputation: 53

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.

Upvotes: 1

Views: 1660

Answers (2)

mozman
mozman

Reputation: 2239

  1. To delete a DXF entity, you have to pass a DXF entity object not a string.
  2. To delete objects from modelspace it's better to query the entities of the modelspace, dxf.entities contains the entities of the modelspace and the active paperspace:

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

Warning:

Do not delete entities from a container while iterating over it - the query() function creates a temporary container, so deleting the entities from the modelspace is safe.

Upvotes: 3

Loïc Kreseski
Loïc Kreseski

Reputation: 53

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.

Upvotes: 2

Related Questions