Get all entities inside or crossing a given closed polyline using Ezdxf Python

47 Views Asked by At

Is there any solution to do the following using ezdxf library Input:

  • A DXF file.dxf
  • List coordinates of a closed polygone (2D) Output:
  • All entities (circles, lines hatchs ...) inside (or crossing) this polygone, with propreties (Layer, color ) for each entity

I think the query method can not get entities in a given area. So is there any solution please! Thanks!

I've tried query : https://ezdxf.readthedocs.io/en/stable/tutorials/getting_data.html#tut-getting-data

But did not gives the possibility to filter by coordinates ...

1

There are 1 best solutions below

1
Rishava On

There probably isn't any direct function to do this, but what you could do is:

  • Select the polygon by handle using i.dxf.handle, where i is the modelspace iterable. For that you need to know the handle. If you don't know the handle, you could have it as the only polygon and use i.dxftype() == whatever types there are in the documentation.
  • Next, you iterate through the modelspace and check if a line passing through any two vertices of the polygon also pass through any of the other entities.

The solution will probably require you to make a whole if-elif-else tree for every entity type and also study a bit of coordinate geometry. For example, a circle will require you to check if the perpendicular distance of the line from its centre is lesser than or equal to its radius. At the same time, you need to see if the distance of at least one of the ends of the line segment from the centre of the circle is lesser than or equal to its radius.