[dxf] fix reading Polyline

This commit is contained in:
Jan Petykiewicz 2026-03-30 20:22:40 -07:00
commit 9ede16df5d
2 changed files with 22 additions and 3 deletions

View file

@ -212,8 +212,10 @@ def _read_block(block: ezdxf.layouts.BlockLayout | ezdxf.layouts.Modelspace) ->
if isinstance(element, LWPolyline | Polyline): if isinstance(element, LWPolyline | Polyline):
if isinstance(element, LWPolyline): if isinstance(element, LWPolyline):
points = numpy.asarray(element.get_points()) points = numpy.asarray(element.get_points())
elif isinstance(element, Polyline): is_closed = element.closed
else:
points = numpy.asarray([pp.xyz for pp in element.points()]) points = numpy.asarray([pp.xyz for pp in element.points()])
is_closed = element.is_closed
attr = element.dxfattribs() attr = element.dxfattribs()
layer = attr.get('layer', DEFAULT_LAYER) layer = attr.get('layer', DEFAULT_LAYER)
@ -233,7 +235,6 @@ def _read_block(block: ezdxf.layouts.BlockLayout | ezdxf.layouts.Modelspace) ->
if width == 0: if width == 0:
width = attr.get('const_width', 0) width = attr.get('const_width', 0)
is_closed = element.closed
verts = points[:, :2] verts = points[:, :2]
if is_closed and (len(verts) < 2 or not numpy.allclose(verts[0], verts[-1])): if is_closed and (len(verts) < 2 or not numpy.allclose(verts[0], verts[-1])):
verts = numpy.vstack((verts, verts[0])) verts = numpy.vstack((verts, verts[0]))

View file

@ -1,5 +1,6 @@
import io
import numpy import numpy
import ezdxf
from numpy.testing import assert_allclose from numpy.testing import assert_allclose
from pathlib import Path from pathlib import Path
@ -109,3 +110,20 @@ def test_dxf_manhattan_precision(tmp_path: Path):
target_name = next(k for k in read_top.refs if k.upper() == "SUB") target_name = next(k for k in read_top.refs if k.upper() == "SUB")
ref = read_top.refs[target_name][0] ref = read_top.refs[target_name][0]
assert isinstance(ref.repetition, Grid), "Grid should be preserved for 90-degree rotation" assert isinstance(ref.repetition, Grid), "Grid should be preserved for 90-degree rotation"
def test_dxf_read_legacy_polyline() -> None:
doc = ezdxf.new()
msp = doc.modelspace()
msp.add_polyline2d([(0, 0), (10, 0), (10, 10)], dxfattribs={"layer": "legacy"}).close(True)
stream = io.StringIO()
doc.write(stream)
stream.seek(0)
read_lib, _ = dxf.read(stream)
top_pat = read_lib.get("Model") or list(read_lib.values())[0]
polys = [shape for shape in top_pat.shapes["legacy"] if isinstance(shape, Polygon)]
assert len(polys) == 1
assert_allclose(polys[0].vertices, [[0, 0], [10, 0], [10, 10]])