"""Tests for DXF 3D writer.""" import pytest import ezdxf from pathlib import Path from pdf2imos.output.dxf_writer import write_dxf from pdf2imos.models import PartGeometry @pytest.fixture def test_part(): return PartGeometry( width_mm=600.0, height_mm=720.0, depth_mm=18.0, origin=(0.0, 0.0, 0.0), name="test_panel", ) @pytest.fixture def output_dxf(tmp_path): return tmp_path / "test_panel.dxf" class TestWriteDxf: def test_returns_path(self, test_part, output_dxf): result = write_dxf(test_part, output_dxf) assert isinstance(result, Path) def test_file_created(self, test_part, output_dxf): write_dxf(test_part, output_dxf) assert output_dxf.exists() def test_dxf_audit_clean(self, test_part, output_dxf): """Generated DXF must pass audit with no errors.""" write_dxf(test_part, output_dxf) doc = ezdxf.readfile(str(output_dxf)) auditor = doc.audit() assert len(auditor.errors) == 0, f"DXF audit errors: {auditor.errors}" def test_mesh_entity_present(self, test_part, output_dxf): """Modelspace must contain at least one MESH entity.""" write_dxf(test_part, output_dxf) doc = ezdxf.readfile(str(output_dxf)) msp = doc.modelspace() meshes = list(msp.query("MESH")) assert len(meshes) >= 1, "No MESH entity found in modelspace" def test_layers_created(self, test_part, output_dxf): """Required layers must exist.""" write_dxf(test_part, output_dxf) doc = ezdxf.readfile(str(output_dxf)) layer_names = {layer.dxf.name for layer in doc.layers} assert "GEOMETRY" in layer_names, "GEOMETRY layer missing" assert "DIMENSIONS" in layer_names, "DIMENSIONS layer missing" assert "ANNOTATIONS" in layer_names, "ANNOTATIONS layer missing" def test_bounding_box_matches_dimensions(self, test_part, output_dxf): """Mesh bounding box should match part dimensions within tolerance.""" write_dxf(test_part, output_dxf) doc = ezdxf.readfile(str(output_dxf)) msp = doc.modelspace() meshes = list(msp.query("MESH")) assert len(meshes) >= 1 # Get mesh vertices and compute bounding box mesh = meshes[0] vertices = list(mesh.vertices) if not vertices: pytest.skip("No vertices in mesh") xs = [v[0] for v in vertices] ys = [v[1] for v in vertices] zs = [v[2] for v in vertices] width_actual = max(xs) - min(xs) depth_actual = max(ys) - min(ys) height_actual = max(zs) - min(zs) assert abs(width_actual - test_part.width_mm) < 0.01, ( f"Width mismatch: {width_actual} vs {test_part.width_mm}" ) assert abs(height_actual - test_part.height_mm) < 0.01, ( f"Height mismatch: {height_actual} vs {test_part.height_mm}" ) assert abs(depth_actual - test_part.depth_mm) < 0.01, ( f"Depth mismatch: {depth_actual} vs {test_part.depth_mm}" ) def test_different_part_sizes(self, tmp_path): """Test various part sizes.""" for w, h, d in [(300, 200, 15), (1200, 800, 18), (600, 720, 400)]: part = PartGeometry( width_mm=float(w), height_mm=float(h), depth_mm=float(d), origin=(0.0, 0.0, 0.0), name=f"part_{w}x{h}x{d}", ) output = tmp_path / f"part_{w}x{h}x{d}.dxf" write_dxf(part, output) doc = ezdxf.readfile(str(output)) assert len(doc.audit().errors) == 0