'Retrieval of DXF entities from a particular view using ezdxf

I am trying to retrieve entities only from particular view of a DXF file.

The VIEWPORT class doesn't help to separate views.

Help me to solve this out.

Thanks in advance



Solution 1:[1]

In general, for arbitrary viewports & target objects, this is a relatively difficult problem.

The solution can be reduced to calculating the boundary of the viewport relative to WCS, and then determining which entities reside either entirely within or partially within the calculated boundary.

The boundary can be calculated by obtaining the boundary vertices of the viewport (with respect to Paperspace), and then transforming such vertices using a transformation matrix constructed using the scale (or view height), rotation (or twist angle), and normal vector, and viewport center.

  • The scale can be calculated by dividing DXF group 45 (height w.r.t. Modelspace) by DXF group 41 (height w.r.t Paperspace).

  • The rotation can be obtained from DXF group 51 (view twist angle).

  • The normal vector can be obtained from DXF group 16 (WCS view direction vector).

  • The center can be obtained from DXF group 10 (WCS center point)

Upon calculating the boundary coordinates, you'll then need to calculate the extents of all Modelspace geometry (or just the rectangular bounding box, depending on the accuracy you require), and determine whether any portion of the geometrical extents falls within the calculated viewport boundary.

Solution 2:[2]

I also needed to find the model space entities for a given paper space viewport, here is what I have found so far:

  • DXF Groups 10,20: Paper space center point in WCS
  • DXF Groups 12,22: View center point in DCS
  • DXF Groups 17,27: View target center point in WCS

The model space viewport center point appears to be just a vector sum of groups (12,22) and groups (17,27).

Once I found the model space view center point, I can find all the model space entities using the viewport width and height groups (40,41) and scale groups (41/45).

Have tried this method on many dxf files and it worked very well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lee Mac
Solution 2 atsu