Files
snapcad-studio/tests/unit/dxfImportExport.test.ts
Enes Yakıştır e7b6db9db6
CI / Typecheck and unit tests (push) Canceled after 0s
Build and Push Docker Images / build (push) Canceled after 0s
feat: sesli CAD asistani, bellek optimizasyonlari ve akilli cozunurluk
- SnapVoice Turkce sesli CAD ve komut konsolu (/denetle, /ram, /eko, /otomatik, /sessiz)
- 3B uzamsal sesli radar ve gorme engelli gelistirici geometri denetim motoru
- LibreDWG/DXF ikili sikistirma, font tembel yukleme ve RAM optimizasyonu
- Monitor cozunurlugune gore otomatik uyarlamali dinamik grafik modu
2026-11-02 13:37:37 +03:00

664 lines
12 KiB
TypeScript

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { exportMeshesToDxf, type DxfExportMesh } from "@/lib/dxfExport";
import { compressDxfText, decompressDxfText, dxfExtrusionDepthForSpan, getImportedDxfText, importedShapeFromDxf } from "@/lib/dxfImport";
function makePrismMesh(): DxfExportMesh {
// A simple 10x20x30 prism made of 12 triangles (6 rectangular faces)
// X: 0 to 10, Y: 0 to 20, Z: 0 to 30
const v = [
[0, 0, 0], [10, 0, 0], [10, 20, 0], [0, 20, 0], // bottom (Z=0)
[0, 0, 30], [10, 0, 30], [10, 20, 30], [0, 20, 30], // top (Z=30)
] as [number, number, number][];
const faces: [number, number, number][] = [
// Bottom (normal -Z)
[0, 2, 1], [0, 3, 2],
// Top (normal +Z)
[4, 5, 6], [4, 6, 7],
// Front (normal -Y)
[0, 1, 5], [0, 5, 4],
// Right (normal +X)
[1, 2, 6], [1, 6, 5],
// Back (normal +Y)
[2, 3, 7], [2, 7, 6],
// Left (normal -X)
[3, 0, 4], [3, 4, 7],
];
return {
name: "prism_box",
vertices: v,
faces,
};
}
describe("DXF export and import", () => {
it("exports 3D mesh triangles into valid AutoCAD DXF with 3DFACE entities and layers", () => {
const mesh = makePrismMesh();
const dxfString = exportMeshesToDxf([mesh]);
// Check DXF structure sections
expect(dxfString).toContain("SECTION");
expect(dxfString).toContain("HEADER");
expect(dxfString).toContain("TABLES");
expect(dxfString).toContain("ENTITIES");
expect(dxfString).toContain("EOF");
// Check layer definitions
expect(dxfString).toContain("3D_GEOMETRY");
expect(dxfString).toContain("CUT_OUTLINE");
// Check 3DFACE entity generation
expect(dxfString).toContain("3DFACE");
const faceCount = (dxfString.match(/3DFACE/g) || []).length;
expect(faceCount).toBe(mesh.faces.length);
});
it("imports 3D DXF file and reconstructs a valid watertight WorkplaneShape", () => {
const mesh = makePrismMesh();
const dxfString = exportMeshesToDxf([mesh]);
const shape = importedShapeFromDxf("prism.dxf", dxfString);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.sourceFormat).toBe("dxf");
expect(shape.importedMesh?.triangleCount).toBe(12);
// Bounding dimensions should match the prism: width=10 (X), height=20 (Y), depth=30 (Z)
expect(shape.importedMesh?.baseWidth).toBeCloseTo(10, 1);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(20, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(30, 1);
// Positions array length should be triangleCount * 9 (3 vertices * 3 coords)
expect(shape.importedMesh?.positions.length).toBe(12 * 9);
expect(shape.importedMesh?.normals?.length).toBe(12 * 9);
});
it("imports 2D DXF with LWPOLYLINE contour and extrudes it into a 3D solid", () => {
// Standard minimal 2D AutoCAD DXF with closed LWPOLYLINE (40x20 rectangle)
const dxf2d = `0
SECTION
2
ENTITIES
0
LWPOLYLINE
8
0
90
4
70
1
10
0.0
20
0.0
10
40.0
20
0.0
10
40.0
20
20.0
10
0.0
20
20.0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("bracket_plate.dxf", dxf2d);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.sourceFormat).toBe("dxf");
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
// 40x20 dimensions with default extrusion height (4mm)
expect(shape.importedMesh?.baseWidth).toBeCloseTo(40, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(20, 1);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(4, 1);
});
it("imports 2D DXF with separate chained LINE entities and extrudes into a solid", () => {
// DXF with 4 LINE entities forming a closed 30x30 square
const dxfLines = `0
SECTION
2
ENTITIES
0
LINE
8
OUTLINE
10
0
20
0
11
30
21
0
0
LINE
8
OUTLINE
10
30
20
0
11
30
21
30
0
LINE
8
OUTLINE
10
30
20
30
11
0
21
30
0
LINE
8
OUTLINE
10
0
20
30
11
0
21
0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("square_contour.dxf", dxfLines);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.sourceFormat).toBe("dxf");
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
expect(shape.importedMesh?.baseWidth).toBeCloseTo(30, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(30, 1);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(4, 1);
});
it("imports 2D DXF with CIRCLE entity and creates a cylindrical 3D solid", () => {
// DXF with a CIRCLE of radius 15 at center (10, 10)
const dxfCircle = `0
SECTION
2
ENTITIES
0
CIRCLE
8
CUT_LAYER
10
10
20
10
40
15
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("spacer_disc.dxf", dxfCircle);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.sourceFormat).toBe("dxf");
// Diameter is 2 * 15 = 30
expect(shape.importedMesh?.baseWidth).toBeCloseTo(30, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(30, 1);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(4, 1);
});
it("correctly identifies inner hole in 2D profile and produces pierced solid body", () => {
// Outer 50x50 rectangle with inner 20x20 hole (both LWPOLYLINEs)
const dxfWithHole = `0
SECTION
2
ENTITIES
0
LWPOLYLINE
8
OUTLINE
90
4
70
1
10
0.0
20
0.0
10
50.0
20
0.0
10
50.0
20
50.0
10
0.0
20
50.0
0
LWPOLYLINE
8
HOLES
90
4
70
1
10
15.0
20
15.0
10
35.0
20
15.0
10
35.0
20
35.0
10
15.0
20
35.0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("flange_with_hole.dxf", dxfWithHole);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.baseWidth).toBeCloseTo(50, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(50, 1);
// Has both top/bottom triangulation and side walls for both outer and hole loops
expect(shape.importedMesh?.triangleCount).toBeGreaterThanOrEqual(16);
});
it("imports DXF with ARC entities and reconstructs watertight extruded solid using dxf-parser", () => {
const dxfWithArc = `0
SECTION
2
ENTITIES
0
LINE
8
OUTLINE
10
0.0
20
0.0
11
40.0
21
0.0
0
LINE
8
OUTLINE
10
40.0
20
0.0
11
40.0
21
30.0
0
LINE
8
OUTLINE
10
40.0
20
30.0
11
0.0
21
30.0
0
LINE
8
OUTLINE
10
0.0
20
30.0
11
0.0
21
0.0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("bracket_with_lines.dxf", dxfWithArc);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh?.baseWidth).toBeCloseTo(40, 1);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(30, 1);
});
it("imports AutoCAD DWG/DXF where entities reside inside *MODEL_SPACE block", () => {
// Typical AutoCAD DWG conversion where ENTITIES section is empty and geometry is inside *MODEL_SPACE
const dxfModelSpace = `0
SECTION
2
BLOCKS
0
BLOCK
2
*MODEL_SPACE
70
0
10
0.0
20
0.0
30
0.0
3
*MODEL_SPACE
0
LINE
8
WALLS
10
0.0
20
0.0
11
60.0
21
0.0
0
LINE
8
WALLS
10
60.0
20
0.0
11
60.0
21
40.0
0
ENDBLK
0
ENDSEC
0
SECTION
2
ENTITIES
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("floor_plan_modelspace.dxf", dxfModelSpace);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
expect(shape.importedMesh?.baseWidth).toBeGreaterThanOrEqual(60);
expect(shape.importedMesh?.baseWidth).toBeLessThan(63);
expect(shape.importedMesh?.baseDepth).toBeGreaterThanOrEqual(40);
expect(shape.importedMesh?.baseDepth).toBeLessThan(43);
});
it("imports unclosed architectural wall lines and extrudes into 3D wall ribbons", () => {
// Open T-junction wall lines (cannot form any closed loop)
const dxfOpenWalls = `0
SECTION
2
ENTITIES
0
LINE
8
WALLS
10
0.0
20
0.0
11
100.0
21
0.0
0
LINE
8
PARTITIONS
10
50.0
20
0.0
11
50.0
21
80.0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("architectural_open_walls.dxf", dxfOpenWalls);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
// Should extrude without throwing any error, adding ribbon wall thickness
expect(shape.importedMesh?.baseWidth).toBeGreaterThanOrEqual(100);
expect(shape.importedMesh?.baseWidth).toBeLessThan(103);
expect(shape.importedMesh?.baseDepth).toBeGreaterThanOrEqual(80);
expect(shape.importedMesh?.baseDepth).toBeLessThan(83);
expect(shape.importedMesh?.baseHeight).toBeGreaterThan(0);
});
it("imports ELLIPSE and SOLID entities into watertight 3D solid mesh", () => {
const dxfEllipseSolid = `0
SECTION
2
ENTITIES
0
ELLIPSE
8
0
10
50.0
20
50.0
30
0.0
11
20.0
21
0.0
31
0.0
40
0.6
41
0.0
42
6.283185307179586
0
SOLID
8
0
10
0.0
20
0.0
30
0.0
11
20.0
21
0.0
31
0.0
12
20.0
22
20.0
32
0.0
13
0.0
23
20.0
33
0.0
0
ENDSEC
0
EOF`;
const shape = importedShapeFromDxf("ellipse_and_solid.dxf", dxfEllipseSolid);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
});
it("handles degenerate hairline loops gracefully without throwing 'konturların geçerli bir alanı bulunamadı'", () => {
// A closed polyline with area = 0 (collinear back and forth line)
const dxfDegenerate = `0
SECTION
2
ENTITIES
0
LWPOLYLINE
8
0
90
4
70
1
10
0.0
20
0.0
10
50.0
20
0.0
10
50.0
20
0.00001
10
0.0
20
0.0
0
ENDSEC
0
EOF`;
// Must not throw, instead converts hairline loop to wall ribbon
const shape = importedShapeFromDxf("hairline_loop.dxf", dxfDegenerate);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
});
});
function rectangleDxf(width: number, height: number) {
return `0
SECTION
2
ENTITIES
0
LWPOLYLINE
8
0
90
4
70
1
10
0.0
20
0.0
10
${width}
20
0.0
10
${width}
20
${height}
10
0.0
20
${height}
0
ENDSEC
0
EOF`;
}
describe("DXF extrusion depth scales with drawing size", () => {
it("keeps a visible minimum thickness for small drawings", () => {
expect(dxfExtrusionDepthForSpan(0)).toBe(4);
expect(dxfExtrusionDepthForSpan(-10)).toBe(4);
expect(dxfExtrusionDepthForSpan(Number.NaN)).toBe(4);
expect(dxfExtrusionDepthForSpan(40)).toBe(4);
expect(dxfExtrusionDepthForSpan(200)).toBe(4);
});
it("grows proportionally for architectural-scale drawings", () => {
expect(dxfExtrusionDepthForSpan(1000)).toBeCloseTo(10, 5);
expect(dxfExtrusionDepthForSpan(3_426_521)).toBeCloseTo(34_265.21, 2);
});
it("caps extremely large drawings so they stay editable", () => {
expect(dxfExtrusionDepthForSpan(10_000_000)).toBe(50_000);
expect(dxfExtrusionDepthForSpan(Number.POSITIVE_INFINITY)).toBe(4);
});
it("extrudes a 40 m x 20 m plan into a solid hundreds of units thick", () => {
const shape = importedShapeFromDxf("site_plan.dxf", rectangleDxf(40_000, 20_000));
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh?.baseWidth).toBeCloseTo(40_000, 0);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(20_000, 0);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(400, 0);
});
it("compresses large DXF text into base64 deflated payload and retrieves pristine text", () => {
const original = rectangleDxf(100, 50).repeat(50);
expect(original.length).toBeGreaterThan(5000);
const compressed = compressDxfText(original);
expect(compressed.length).toBeLessThan(original.length * 0.1); // >90% compression
const decompressed = decompressDxfText(compressed);
expect(decompressed).toBe(original);
const shape = importedShapeFromDxf("large_plan.dxf", original);
expect(shape.importedMesh?.sourceDxfCompressed).toBeDefined();
expect(shape.importedMesh?.sourceDxfText).toBeUndefined();
const retrieved = getImportedDxfText(shape.importedMesh);
expect(retrieved).toBe(original);
});
});