From f77f14a8ece145faef250dce18134fcf3097c8ff Mon Sep 17 00:00:00 2001 From: mozman Date: Thu, 4 Jan 2024 06:12:17 +0100 Subject: [PATCH] fixes #1003 - NumpyPath2d could contain a 3d vertex --- notes/pages/CHANGELOG.md | 2 ++ src/ezdxf/addons/drawing/matplotlib.py | 1 + src/ezdxf/npshapes.py | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/notes/pages/CHANGELOG.md b/notes/pages/CHANGELOG.md index e25dc2426..a12642890 100644 --- a/notes/pages/CHANGELOG.md +++ b/notes/pages/CHANGELOG.md @@ -27,6 +27,8 @@ id:: 6588217b-c1d3-44c1-a0d7-e5ee465cc6de - REMOVE: `ezdxf.math.linspace`, replaced by `numpy.linspace` - BUGFIX: Restore lost links between `LAYOUT` and `BLOCK_RECORD` entities - {{issue 997}} + - BUGFIX: `NumpyPath2d` could contain a 3d vertex + - {{issue 1003}} - - ## Version 1.1.4 - 2023-12-24 id:: 6568dc88-ce84-4f46-b490-43768c491a2b diff --git a/src/ezdxf/addons/drawing/matplotlib.py b/src/ezdxf/addons/drawing/matplotlib.py index e9e767302..d31405803 100644 --- a/src/ezdxf/addons/drawing/matplotlib.py +++ b/src/ezdxf/addons/drawing/matplotlib.py @@ -159,6 +159,7 @@ def draw_path(self, path: BkPath2d, properties: BackendProperties): """Draw a solid line path, line type rendering is done by the frontend since v0.18.1 """ + mpl_path = to_matplotlib_path([path]) try: patch = PathPatch( diff --git a/src/ezdxf/npshapes.py b/src/ezdxf/npshapes.py index 54115501f..3548b19d3 100644 --- a/src/ezdxf/npshapes.py +++ b/src/ezdxf/npshapes.py @@ -152,7 +152,7 @@ def __init__(self, path: Optional[Path]) -> None: vertices = [(v.x, v.y) for v in path.control_vertices()] if len(vertices) == 0: try: # control_vertices() does not return the start point of empty paths - vertices = [path.start] + vertices = [Vec2(path.start)] except IndexError: vertices = [] self._vertices = np.array(vertices, dtype=VertexNumpyType) @@ -503,11 +503,17 @@ def to_matplotlib_path(paths: Iterable[NumpyPath2d], *, detect_holes=False): vertices: list[np.ndarray] = [] codes: list[int] = [] for path in paths: + vertices.append(path.np_vertices()) codes.append(MPL_MOVETO) for cmd in path.command_codes(): codes.extend(MPL_CODES[cmd]) - return Path(np.concatenate(vertices), codes) + points = np.concatenate(vertices) + try: + return Path(points, codes) + except Exception as e: + raise ValueError(f"matplotlib.path.Path({str(points)}, {str(codes)}): {str(e)}") + def single_paths(paths: Iterable[NumpyPath2d]) -> list[NumpyPath2d]: