Skip to content

Commit e185b3d

Browse files
committed
FIX: dxf export results in error with newer cadquery versions #12
1 parent 8197512 commit e185b3d

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

cadscript/export.py

+7-34
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,33 @@
22
# This file is part of Cadscript
33
# SPDX-License-Identifier: Apache-2.0
44

5-
import ezdxf
65
import cadquery as cq
6+
77
from typing import Optional, Literal
88

99

1010
def export_sketch_DXF(
1111
s: cq.Sketch,
1212
fname: str,
1313
approx: Optional[Literal["spline", "arc"]] = None,
14-
tolerance: float = 1e-3,
14+
tolerance: float = 1e-3
1515
):
1616
"""
1717
Export Sketch content to DXF. Works with 2D sections.
1818
1919
:param s: Sketch to be exported.
2020
:param fname: Output filename.
2121
:param approx: Approximation strategy. None means no approximation is applied.
22-
"spline" results in all splines being approximated as cubic splines. "arc" results
23-
in all curves being approximated as arcs and straight segments.
22+
"spline" results in all splines being approximated as cubic splines. "arc" results
23+
in all curves being approximated as arcs and straight segments.
2424
:param tolerance: Approximation tolerance.
2525
26+
Remark: uses mm as unit.
2627
"""
27-
w = cq.Workplane().placeSketch(s)
28-
plane = w.plane
29-
30-
dxf = ezdxf.new()
31-
dxf.units = ezdxf.units.MM
32-
msp = dxf.modelspace()
33-
34-
for f in s.faces():
35-
36-
shape = f.transformShape(plane.fG)
37-
38-
if approx == "spline":
39-
edges = [
40-
e.toSplines() if e.geomType() == "BSPLINE" else e for e in shape.Edges()
41-
]
42-
43-
elif approx == "arc":
44-
edges = []
45-
46-
# this is needed to handle free wires
47-
for el in shape.Wires():
48-
edges.extend(cq.Face.makeFromWires(el).toArcs(tolerance).Edges())
49-
50-
else:
51-
edges = shape.Edges()
52-
53-
for e in edges:
28+
w = cq.Workplane().add(s._faces) # see https://github.com/CadQuery/cadquery/issues/1575
29+
cq.exporters.dxf.exportDXF(w, fname, approx=approx, tolerance=tolerance)
5430

55-
conv = cq.exporters.dxf.DXF_CONVERTERS.get(e.geomType(), cq.exporters.dxf._dxf_spline)
56-
conv(e, msp, plane)
5731

58-
dxf.saveas(fname)
5932

6033

6134
def export_svg(part, filename, width=300, height=300, strokeWidth=0.6, projectionDir=(1, 1, 1)):

tests/test_dxf.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (C) 2023-2024 Andreas Kahler
2+
# This file is part of Cadscript
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from pathlib import Path
6+
import tempfile
7+
import time
8+
import unittest
9+
import cadscript
10+
11+
12+
class DxfTest(unittest.TestCase):
13+
14+
def test_export_dxf_reimport(self):
15+
# Create a simple sketch
16+
s = cadscript.make_sketch()
17+
s.add_rect(10, 20)
18+
# export to temp file, use library to create unique filename
19+
temp_dir = Path(tempfile.gettempdir())
20+
temp_path = str(temp_dir / f"cadscript_{time.time()}.dxf")
21+
22+
s.export_dxf(temp_path)
23+
24+
# test if file exists
25+
self.assertTrue(Path(temp_path).exists())
26+
27+
# reimport
28+
s2 = cadscript.make_sketch()
29+
s2.add_import_dxf(temp_path)
30+
31+
# test if reimported sketch has same extents
32+
self.assertEqual(s.get_extent(), s2.get_extent())
33+
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

0 commit comments

Comments
 (0)