From 78dfeaf41f0915aab211c06e1e49c22035b5fe6b Mon Sep 17 00:00:00 2001 From: JR Mobley Date: Sun, 30 Jul 2023 09:08:34 -0400 Subject: [PATCH] More Export2D test coverage. --- .gitignore | 1 + tests/test_exporters.py | 76 ++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index a31af4d..5eb8ee1 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ docs/_build/ *.STEP *.stl *.svg +*.dxf #mypy cache .mypy_cache diff --git a/tests/test_exporters.py b/tests/test_exporters.py index b3c7a25..bfa155f 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -1,16 +1,16 @@ import unittest import math -from typing import Any +from typing import Union, Iterable from build123d import ( Mode, Shape, Plane, Locations, BuildLine, Line, Bezier, RadiusArc, - BuildSketch, Sketch, make_face, RegularPolygon, Circle, - BuildPart, Part, Cone, extrude, add, + BuildSketch, Sketch, make_face, RegularPolygon, Circle, PolarLocations, + BuildPart, Part, Cone, extrude, add, mirror, section ) from build123d.exporters import ( - ExportSVG, Drawing, LineType + ExportSVG, ExportDXF, Drawing, LineType ) class ExportersTestCase(unittest.TestCase): @@ -37,23 +37,43 @@ class ExportersTestCase(unittest.TestCase): return party.part @staticmethod - def basic_svg_export(shape: Shape, filename: str, reverse: bool = False): + def basic_svg_export(shape: Union[Shape, Iterable[Shape]], filename: str, reverse: bool = False): svg = ExportSVG() svg.add_shape(shape, reverse_wires=reverse) svg.write(filename) - def test_sketch_svg(self): - sketch = ExportersTestCase.create_test_sketch() - ExportersTestCase.basic_svg_export(sketch, "test-sketch.svg") + @staticmethod + def basic_dxf_export(shape: Union[Shape, Iterable[Shape]], filename: str): + dxf = ExportDXF() + dxf.add_shape(shape) + dxf.write(filename) - def test_drawing_svg(self): + @staticmethod + def basic_combo_export(shape: Shape, filebase: str, reverse: bool = False): + ExportersTestCase.basic_svg_export(shape, filebase + ".svg", reverse) + ExportersTestCase.basic_dxf_export(shape, filebase + ".dxf") + + @staticmethod + def drawing_combo_export(dwg: Drawing, filebase: str): + svg = ExportSVG(line_weight=0.13) + svg.add_layer("hidden", line_weight=0.09, line_type=LineType.HIDDEN) + svg.add_shape(dwg.visible_lines) + svg.add_shape(dwg.hidden_lines, layer='hidden') + svg.write(filebase + ".svg") + dxf = ExportDXF(line_weight=0.13) + dxf.add_layer("hidden", line_weight=0.09, line_type=LineType.HIDDEN) + dxf.add_shape(dwg.visible_lines) + dxf.add_shape(dwg.hidden_lines, layer='hidden') + dxf.write(filebase + ".dxf") + + def test_sketch(self): + sketch = ExportersTestCase.create_test_sketch() + ExportersTestCase.basic_combo_export(sketch, "test-sketch") + + def test_drawing(self): part = ExportersTestCase.create_test_part() drawing = Drawing(part) - svg = ExportSVG(line_weight=0.11) - svg.add_layer("hidden", line_type=LineType.ISO_DOT) - svg.add_shape(drawing.visible_lines) - svg.add_shape(drawing.hidden_lines, layer='hidden') - svg.write("test-drawing.svg") + ExportersTestCase.drawing_combo_export(drawing, "test-drawing") def test_back_section_svg(self): """Export a section through the bottom face. @@ -63,14 +83,14 @@ class ExportersTestCase(unittest.TestCase): test_section = section(part, Plane.XY, height=0) ExportersTestCase.basic_svg_export(test_section, "test-back-section.svg", reverse=True) - def test_angled_section_svg(self): + def test_angled_section(self): """Export an angled section. This tests more slightly more complex geometry.""" part = ExportersTestCase.create_test_part() angle = math.degrees(math.atan2(4, 8)) section_plane = Plane.XY.rotated((angle, 0, 0)) angled_section = section_plane.to_local_coords(section(part, section_plane)) - ExportersTestCase.basic_svg_export(angled_section, "test-angled-section.svg") + ExportersTestCase.basic_combo_export(angled_section, "test-angled-section") def test_cam_section_svg(self): """ Export a section through the top face, with a simple @@ -94,5 +114,27 @@ class ExportersTestCase(unittest.TestCase): cone = Cone(8, 0, 16, align=None) section_plane = Plane.XZ.offset(4) conic_section = section_plane.to_local_coords(section(cone, section_plane)) - ExportersTestCase.basic_svg_export(conic_section, "test-conic-section.svg") + ExportersTestCase.basic_combo_export(conic_section, "test-conic-section") + def test_circle_rotation(self): + """Export faces with circular arcs in various orientations.""" + with BuildSketch() as sketch: + Circle(20) + Circle(8, mode=Mode.SUBTRACT) + with PolarLocations(20, 5, 90): + Circle(4, mode=Mode.SUBTRACT) + mirror(about=Plane.XZ.offset(25)) + ExportersTestCase.basic_combo_export(sketch.faces(), "test-circle-rotation") + + def test_ellipse_rotation(self): + """Export drawing with elliptical arcs in various orientations.""" + with BuildPart() as part: + with BuildSketch(Plane.ZX): + Circle(20) + Circle(8, mode=Mode.SUBTRACT) + with PolarLocations(20, 5, 90): + Circle(4, mode=Mode.SUBTRACT) + extrude(amount=20, both=True) + mirror(about=Plane.YZ.offset(25)) + drawing = Drawing(part.part) + ExportersTestCase.drawing_combo_export(drawing, "test-ellipse-rotation")