Skip to content

Commit f973c50

Browse files
committed
Fixed nan gap in line segment
1 parent eea2b0d commit f973c50

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

mpldxf/backend_dxf.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ def _clip_mpl(self, gc, vertices, obj):
131131
[bbox.x0, bbox.y1],
132132
]
133133

134-
# Remove any NaN values from the vertices
135-
vertices = vertices[~np.isnan(vertices).any(axis=1)]
136-
137134
if obj == "patch":
138135
vertices = ClippingRect2d(cliprect[0], cliprect[2]).clip_polyline(
139136
vertices
@@ -168,6 +165,25 @@ def _draw_mpl_lwpoly(self, gc, path, transform, obj):
168165
dxfattribs = self._get_polyline_attribs(gc)
169166
vertices = path.transformed(transform).vertices
170167

168+
# Check if vertices hold NaN values
169+
if np.isnan(vertices).any():
170+
nan_rows = np.isnan(vertices).all(axis=1)
171+
split_indices = np.where(nan_rows)[0]
172+
173+
# Split the array at NaN indices
174+
list_of_split_vertices = np.split(vertices, split_indices)
175+
176+
# Remove NaN values from sub-arrays
177+
list_of_split_vertices = [
178+
arr[~np.isnan(arr).any(axis=1)] for arr in list_of_split_vertices
179+
]
180+
181+
for split_vertices in list_of_split_vertices:
182+
self._clip_and_add_mpl_lwpoly(gc, dxfattribs, split_vertices, obj)
183+
else:
184+
self._clip_and_add_mpl_lwpoly(gc, dxfattribs, vertices, obj)
185+
186+
def _clip_and_add_mpl_lwpoly(self, gc, dxfattribs, vertices, obj):
171187
# clip the polygon if clip rectangle present
172188
if len(vertices) > 0:
173189
if isinstance(vertices[0][0], float or np.float64):

tests/test_backend_ezdxf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_plot_line_with_no_axis(self):
5656
modelspace = doc.modelspace()
5757
entities = list(modelspace)
5858
assert len(entities) == 1 # 1 line and the bounding box of the plot
59-
59+
6060
def test_plot_line(self):
6161
"""Test a simple line-plot command."""
6262
plt.gca().patch.set_visible(False)
@@ -163,7 +163,7 @@ def test_contourf(self):
163163
X, Y = np.meshgrid(x, y)
164164
Z = np.sin(np.sqrt(X**2 + Y**2))
165165
plt.contourf(X, Y, Z)
166-
166+
167167
plt.savefig("tests/files/test_contourf.png")
168168

169169
try:
@@ -172,7 +172,7 @@ def test_contourf(self):
172172

173173
finally:
174174
plt.close()
175-
175+
176176
def test_plot_with_nans(self):
177177
"""Test a plot with NaNs."""
178178
plt.gca().patch.set_visible(False)
@@ -194,5 +194,5 @@ def test_plot_with_nans(self):
194194
modelspace = doc.modelspace()
195195
entities = list(modelspace)
196196
assert (
197-
len(entities) == 1
198-
) # ideally we should have two lines (i.e. one broken line), but one interpolated line works as a hotfix
197+
len(entities) == 2
198+
) # ideally we should have two lines (i.e. one broken line), but one interpolated line works as a hotfix

0 commit comments

Comments
 (0)