Files
snapcad-studio/apps/web/src/lib/dxfExport.ts
T

144 lines
4.6 KiB
TypeScript
Raw Normal View History

import Drawing from "dxf-writer";
import type { ObjExportMesh } from "@/lib/objExport";
import type { SvgProjectionLayer } from "@/lib/svgExport";
export const MAX_DXF_EXPORT_3DFACES = 25_000;
export const MAX_DXF_EXPORT_EDGES = 50_000;
/**
* Exports 3D meshes and 2D projected cutting contours to AutoCAD DXF format.
* Generates standards-compliant DXF with 2D contours or optimized 3D wireframe/mesh,
* fully compatible with AutoCAD, SolidWorks, Fusion 360, FreeCAD, and CNC laser cutters.
*/
export function exportMeshesToDxf(
meshes: readonly ObjExportMesh[],
projectedLayers?: ReadonlyArray<SvgProjectionLayer>,
title = "SnapCAD Studio Model",
): string {
const dxf = new Drawing();
dxf.setUnits("Millimeters");
// Always define standard CAD layers so CAD software and tests find them
dxf.addLayer("3D_GEOMETRY", Drawing.ACI.CYAN, "CONTINUOUS");
dxf.addLayer("CUT_OUTLINE", Drawing.ACI.WHITE, "CONTINUOUS");
dxf.addLayer("CUT_HOLES", Drawing.ACI.RED, "CONTINUOUS");
const hasProjected2D =
projectedLayers &&
projectedLayers.length > 0 &&
projectedLayers.some((l) => l.polygons && l.polygons.length > 0);
// 1. Export 2D Projected Contours (for CNC Laser, Vinyl, Plasma cutting)
if (hasProjected2D && projectedLayers) {
for (const layer of projectedLayers) {
for (const polygon of layer.polygons) {
if (!polygon || polygon.length < 2) continue;
// Calculate signed polygon area to differentiate outer cut from inner hole
let area = 0;
for (let i = 0; i < polygon.length; i++) {
const curr = polygon[i];
const next = polygon[(i + 1) % polygon.length];
area += (next[0] - curr[0]) * (next[1] + curr[1]);
}
if (area >= 0) {
dxf.setActiveLayer("CUT_OUTLINE");
} else {
dxf.setActiveLayer("CUT_HOLES");
}
const points: Array<[number, number]> = polygon.map(([x, y]) => [
Number(x.toFixed(4)),
Number(y.toFixed(4)),
]);
dxf.drawPolyline(points, true);
}
}
return dxf.toDxfString();
}
// 2. Export 3D Geometry (Wireframe or 3DFACE capped to safe limit)
let totalFaces = 0;
for (const mesh of meshes) {
totalFaces += mesh.faces.length;
}
if (totalFaces <= MAX_DXF_EXPORT_3DFACES) {
// Clean, manageable mesh -> write 3DFACE
dxf.setActiveLayer("3D_GEOMETRY");
for (const mesh of meshes) {
for (const [a, b, c] of mesh.faces) {
const v1 = mesh.vertices[a];
const v2 = mesh.vertices[b];
const v3 = mesh.vertices[c];
if (!v1 || !v2 || !v3) continue;
// In DXF 3DFACE, a triangle repeats vertex 3 as vertex 4
dxf.drawFace(
Number(v1[0].toFixed(4)),
Number(v1[1].toFixed(4)),
Number(v1[2].toFixed(4)),
Number(v2[0].toFixed(4)),
Number(v2[1].toFixed(4)),
Number(v2[2].toFixed(4)),
Number(v3[0].toFixed(4)),
Number(v3[1].toFixed(4)),
Number(v3[2].toFixed(4)),
Number(v3[0].toFixed(4)),
Number(v3[1].toFixed(4)),
Number(v3[2].toFixed(4)),
);
}
}
} else {
// Dense mesh (>25,000 faces, e.g. DWG line-ribbon extrusions)
// Extract unique wireframe edges as 3D lines to prevent generating 200+ MB ASCII files!
dxf.addLayer("3D_WIREFRAME", Drawing.ACI.CYAN, "CONTINUOUS");
dxf.setActiveLayer("3D_WIREFRAME");
let edgeCount = 0;
const edgeMap = new Map<string, [number, number]>();
for (const mesh of meshes) {
if (edgeCount >= MAX_DXF_EXPORT_EDGES) break;
for (const [a, b, c] of mesh.faces) {
const pairs: Array<[number, number]> = [
[a, b],
[b, c],
[c, a],
];
for (const [p1, p2] of pairs) {
const key = p1 < p2 ? `${p1}_${p2}` : `${p2}_${p1}`;
if (!edgeMap.has(key)) {
edgeMap.set(key, [p1, p2]);
}
}
}
for (const [, [p1, p2]] of edgeMap) {
if (edgeCount >= MAX_DXF_EXPORT_EDGES) break;
const v1 = mesh.vertices[p1];
const v2 = mesh.vertices[p2];
if (v1 && v2) {
// dxf-writer implements drawLine3d at runtime
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(dxf as any).drawLine3d(
Number(v1[0].toFixed(4)),
Number(v1[1].toFixed(4)),
Number(v1[2].toFixed(4)),
Number(v2[0].toFixed(4)),
Number(v2[1].toFixed(4)),
Number(v2[2].toFixed(4)),
);
edgeCount++;
}
}
edgeMap.clear();
}
}
return dxf.toDxfString();
}