Skip to content

Commit 5074f94

Browse files
committed
Created fix for handling large jumps in plotted data. Reworked patch collection. Added draw quad mesh but with no handling.
1 parent 970c610 commit 5074f94

File tree

1 file changed

+71
-18
lines changed

1 file changed

+71
-18
lines changed

mpldxf/backend_dxf.py

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,25 @@
3535
"""
3636

3737
from __future__ import absolute_import, division, print_function, unicode_literals
38-
from io import BytesIO, StringIO
39-
import os
40-
import sys
38+
4139
import math
40+
import os
4241
import re
42+
import sys
43+
from io import StringIO
4344

44-
import matplotlib
45+
import ezdxf
46+
import numpy as np
47+
from ezdxf.enums import TextEntityAlignment
48+
from ezdxf.math.clipping import ClippingRect2d
4549
from matplotlib.backend_bases import (
46-
RendererBase,
4750
FigureCanvasBase,
48-
GraphicsContextBase,
4951
FigureManagerBase,
52+
GraphicsContextBase,
53+
RendererBase,
5054
)
5155
from matplotlib.transforms import Affine2D
52-
import matplotlib.transforms as transforms
53-
import matplotlib.collections as mplc
54-
import numpy as np
5556
from shapely.geometry import LineString, Polygon
56-
import ezdxf
57-
from ezdxf.enums import TextEntityAlignment
58-
from ezdxf.math.clipping import Clipping, ClippingRect2d, ConvexClippingPolygon2d
5957

6058
from . import dxf_colors
6159

@@ -140,13 +138,24 @@ def _clip_mpl(self, gc, vertices, obj):
140138
line = LineString(vertices)
141139
try:
142140
intersection = line.intersection(cliprect)
143-
except:
144-
intersection = Polygon()
141+
except Exception:
142+
# Fix plotted values with large gaps/jumps crashing the intersection function
143+
gap_threshold = 50
144+
smoothed_vertices = [vertices[0]] # Start with the first point
145+
for i in range(1, len(vertices)):
146+
if (
147+
abs(vertices[i][0] - vertices[i - 1][0]) <= gap_threshold
148+
and abs(vertices[i][1] - vertices[i - 1][1])
149+
<= gap_threshold
150+
):
151+
smoothed_vertices.append(vertices[i])
152+
line = LineString(smoothed_vertices)
153+
intersection = line.intersection(cliprect)
145154

146155
# Check if intersection is a multi-part geometry
147156
if intersection.is_empty:
148157
vertices = [] # No intersection
149-
elif (
158+
if (
150159
"Multi" in intersection.geom_type
151160
or "GeometryCollection" in intersection.geom_type
152161
):
@@ -205,6 +214,8 @@ def _draw_mpl_patch(self, gc, path, transform, rgbFace=None):
205214
"""Draw a matplotlib patch object"""
206215

207216
poly = self._draw_mpl_lwpoly(gc, path, transform, obj="patch")
217+
if math.isclose(path.vertices[0][1], 421.2598):
218+
pass
208219
if not poly:
209220
return
210221
# check to see if the patch is filled
@@ -307,6 +318,7 @@ def _draw_mpl_hatch(self, gc, path, transform, pline):
307318
hatch = self.modelspace.add_hatch(color=dxfcolor)
308319
line = hatch.paths.add_polyline_path(clipped)
309320

321+
"""
310322
def draw_path_collection(
311323
self,
312324
gc,
@@ -324,16 +336,42 @@ def draw_path_collection(
324336
offset_position,
325337
):
326338
if self._groupd[-1] == "PolyCollection":
327-
# Behandle PolyCollection som en samling av 'patch'-objekter
339+
# Handles PolyCollection as a collection of 'patch'-objects
328340
for path in paths:
329-
# Kombiner master_transform med path_transform for hver path
341+
# combines master_transform with path_transform for each path
330342
combined_transform = master_transform
331-
# Her kan du velge å bruke eller tilpasse rgbFace basert på facecolors, hvis det er relevant
332343
if facecolors.size:
333344
rgbFace = facecolors[0] if facecolors is not None else None
334345
else:
335346
rgbFace = None
336347
self._draw_mpl_patch(gc, path, combined_transform, rgbFace)
348+
"""
349+
350+
def draw_path_collection(
351+
self,
352+
gc,
353+
master_transform,
354+
paths,
355+
all_transforms,
356+
offsets,
357+
offsetTrans,
358+
facecolors,
359+
edgecolors,
360+
linewidths,
361+
linestyles,
362+
antialiaseds,
363+
urls,
364+
offset_position,
365+
):
366+
for i, path in enumerate(paths):
367+
if all_transforms:
368+
combined_transform = master_transform + all_transforms[i]
369+
else:
370+
combined_transform = master_transform
371+
facecolor = facecolors[i] if i < len(facecolors) else None
372+
edgecolor = edgecolors[i] if i < len(edgecolors) else None
373+
# Draw each path as a filled patch
374+
self._draw_mpl_patch(gc, path, combined_transform, rgbFace=facecolor)
337375

338376
def draw_path(self, gc, path, transform, rgbFace=None):
339377
# print('\nEntered ###DRAW_PATH###')
@@ -479,6 +517,21 @@ def new_gc(self):
479517
def points_to_pixels(self, points):
480518
return points / 72.0 * self.dpi
481519

520+
def draw_quad_mesh(
521+
self,
522+
gc,
523+
master_transform,
524+
meshWidth,
525+
meshHeight,
526+
coordinates,
527+
offsets,
528+
offsetTrans,
529+
facecolors,
530+
antialiased,
531+
edgecolors,
532+
):
533+
pass
534+
482535

483536
class FigureCanvasDxf(FigureCanvasBase):
484537
"""

0 commit comments

Comments
 (0)