|
2 | 2 | # This file is part of Cadscript
|
3 | 3 | # SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 |
| -import ezdxf |
6 | 5 | import cadquery as cq
|
| 6 | + |
7 | 7 | from typing import Optional, Literal
|
8 | 8 |
|
9 | 9 |
|
10 | 10 | def export_sketch_DXF(
|
11 | 11 | s: cq.Sketch,
|
12 | 12 | fname: str,
|
13 | 13 | approx: Optional[Literal["spline", "arc"]] = None,
|
14 |
| - tolerance: float = 1e-3, |
| 14 | + tolerance: float = 1e-3 |
15 | 15 | ):
|
16 | 16 | """
|
17 | 17 | Export Sketch content to DXF. Works with 2D sections.
|
18 | 18 |
|
19 | 19 | :param s: Sketch to be exported.
|
20 | 20 | :param fname: Output filename.
|
21 | 21 | :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. |
24 | 24 | :param tolerance: Approximation tolerance.
|
25 | 25 |
|
| 26 | + Remark: uses mm as unit. |
26 | 27 | """
|
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) |
54 | 30 |
|
55 |
| - conv = cq.exporters.dxf.DXF_CONVERTERS.get(e.geomType(), cq.exporters.dxf._dxf_spline) |
56 |
| - conv(e, msp, plane) |
57 | 31 |
|
58 |
| - dxf.saveas(fname) |
59 | 32 |
|
60 | 33 |
|
61 | 34 | def export_svg(part, filename, width=300, height=300, strokeWidth=0.6, projectionDir=(1, 1, 1)):
|
|
0 commit comments