Replies: 4 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
I don't think there is currently a simple way to do this. How tricky it will be depends on how many different cases you want to handle (as dxf is quite a complex file format). For example determining whether a multi-line text entity inside an insert entity with rotation and scaling applied would be very challenging, since you need to know the font measurements to get the untransformed bounding box, then apply the transformation which the insert entity applies to its children to get the final bounding box of the text to know where it is. Another question is whether you are interested in all possible types of entities or only a subset. For example if you only care about lines then the filtering is very simple: just iterate over all the lines, then calculate whether Any another thing you haven't specified is whether you even know the bounding boxes for each page that you want to extract. Finding those in arbitrary CAD files would be a nightmare but should be possible if you know how the files were constructed (for example maybe the edges of the pages are stored on a particular layer?) I seem to suggest this a lot, but the drawing addon which I wrote for ezdxf can help with the filtering as it deals with all (known) tricky cases like the first example I gave and breaks down the complex dxf structure into simple primitives. These primitives (such as lines, chunks of text with well defined positions and scales etc) would be quite easy to test whether they lie within the box you want to keep and the rendering backend is given a reference to the dxf entity which is currently being drawn which you could use for filtering. So my suggestion would be:
Your backend would not have to actually draw anything, just implement each of the here is an outline for what to do. I'm using shapely to do the intersection testing. Note that this approach will loose any nested structure of the document as it explodes any Insert entities. It should be possible to overcome this if you want to keep the structure but that would be much more complex. from shapely.geometry import LineString, Polygon
class FilteringBackend(Backend):
def __init__(self, keep_box: Polygon, params: Optional[Dict] = None):
super().__init__(params)
self.box = keep_box
self.kept_entities = []
def draw_line(self, start: Vec3, end: Vec3, properties: Properties) -> None:
if self.current_entity not in self.kept_entities and self.box.contains(LineString([start.xy, end.xy])):
self.kept_entities.append(self.current_entity)
... # implement other methods
my_layout = doc.modelspace()
my_box = Polygon([...])
backend = FilteringBackend(my_box)
ctx = RenderContext(my_layout.doc)
ctx.set_current_layout(my_layout)
Frontend(ctx, backend).draw_layout(my_layout)
for entity in backend.kept_entities:
pass # do something with entity I hope that helps |
Beta Was this translation helpful? Give feedback.
-
Indeed helpful with such a great tool. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
If DXF file has many pages drawings for different buildings or floors,how to split them up into different group so that my calculation can be based on the single building instead?
doc = ezdxf.readfile(filename)
iterate over all entities in modelspace
msp = doc.modelspace()
But msp will only return the single modelspace, how to split the modelspace by different pages in DXF file?
Beta Was this translation helpful? Give feedback.
All reactions