From 9e8ae7c8919e5a9648a462140f660bc38af9b2ea Mon Sep 17 00:00:00 2001 From: Lorenz Neureuter Date: Sun, 3 Sep 2023 00:29:56 -0400 Subject: [PATCH] DXF export: handle line orientation --- cadquery/occ_impl/exporters/dxf.py | 11 ++++++++++- tests/test_exporters.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cadquery/occ_impl/exporters/dxf.py b/cadquery/occ_impl/exporters/dxf.py index 20803e20e..3018f9ee5 100644 --- a/cadquery/occ_impl/exporters/dxf.py +++ b/cadquery/occ_impl/exporters/dxf.py @@ -8,6 +8,7 @@ from OCP.GeomConvert import GeomConvert from OCP.gp import gp_Dir from OCP.GC import GC_MakeArcOfEllipse +from OCP.TopAbs import TopAbs_Orientation from typing_extensions import Self from ...cq import Face, Plane, Workplane @@ -199,9 +200,17 @@ def _dxf_line(edge: Edge) -> DxfEntityAttributes: :return: dictionary of DXF entity attributes for creating a line """ + + if edge.wrapped.Orientation() == TopAbs_Orientation.TopAbs_FORWARD: + start = edge.startPoint() + end = edge.endPoint() + else: + start = edge.endPoint() + end = edge.startPoint() + return ( "LINE", - {"start": edge.startPoint().toTuple(), "end": edge.endPoint().toTuple(),}, + {"start": start.toTuple(), "end": end.toTuple(),}, ) @staticmethod diff --git a/tests/test_exporters.py b/tests/test_exporters.py index bb752a7ad..dd99c19bc 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -942,3 +942,21 @@ def test_dxf_ellipse_arc(tmpdir): assert w2.val().isValid() assert w2.val().Volume() == approx(math.pi * r ** 2 / 4) + + +def test_dxf_edge_iteration(tmpdir): + + w1 = Workplane().box(50, 50, 1).edges("|Z and >XY").fillet(10) + + dxf = exporters.dxf.DxfDocument() + dxf.add_layer("layer1", color=1) + dxf.add_shape(w1.section(0), "layer1") + + fname = tmpdir.joinpath("edge_iteration1.dxf").resolve() + dxf.document.saveas(fname) + + s1 = Sketch().importDXF(fname) + edges = s1.edges().vals() + + for e1, e2 in zip(edges, edges[1:]): + assert (e2.startPoint() - e1.endPoint()).Length == approx(0.0)