ducjs 4.0.4 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/duc_locator_wasm.d.ts +48 -0
- package/dist/duc_locator_wasm.js +296 -0
- package/dist/duc_locator_wasm_bg.wasm +0 -0
- package/dist/duc_locator_wasm_bg.wasm.d.ts +13 -0
- package/dist/ducjs_wasi.wasm +0 -0
- package/dist/ducjs_wasm.d.ts +14 -2
- package/dist/ducjs_wasm.js +96 -8
- package/dist/ducjs_wasm_bg.wasm +0 -0
- package/dist/ducjs_wasm_bg.wasm.d.ts +6 -2
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/locator-runtime.d.ts +4 -0
- package/dist/locator-runtime.js +7 -0
- package/dist/restore/index.d.ts +1 -0
- package/dist/restore/index.js +1 -0
- package/dist/restore/restoreDataState.d.ts +13 -24
- package/dist/restore/restoreDataState.js +49 -83
- package/dist/restore/restoreElements.d.ts +10 -15
- package/dist/restore/restoreElements.js +128 -125
- package/dist/restore/updateModelCode.d.ts +11 -0
- package/dist/restore/updateModelCode.js +63 -0
- package/dist/thumbnails/bounds.d.ts +21 -0
- package/dist/thumbnails/bounds.js +84 -0
- package/dist/thumbnails/browserRunner.d.ts +60 -0
- package/dist/thumbnails/browserRunner.js +446 -0
- package/dist/thumbnails/cameraPresets.d.ts +47 -0
- package/dist/thumbnails/cameraPresets.js +222 -0
- package/dist/thumbnails/cli.d.ts +2 -0
- package/dist/thumbnails/cli.js +102 -0
- package/dist/thumbnails/index.d.ts +11 -0
- package/dist/thumbnails/index.js +11 -0
- package/dist/thumbnails/lib/clientBundle.min.js +10427 -0
- package/dist/thumbnails/lib/clientEntry.d.ts +3 -0
- package/dist/thumbnails/lib/clientEntry.js +8 -0
- package/dist/thumbnails/lib/clippingUtils.d.ts +10 -0
- package/dist/thumbnails/lib/clippingUtils.js +3 -0
- package/dist/thumbnails/lib/defaultModelColor.d.ts +3 -0
- package/dist/thumbnails/lib/defaultModelColor.js +22 -0
- package/dist/thumbnails/lib/index.d.ts +12 -0
- package/dist/thumbnails/lib/index.js +12 -0
- package/dist/thumbnails/lib/mapViewerStateToOptions.d.ts +34 -0
- package/dist/thumbnails/lib/mapViewerStateToOptions.js +69 -0
- package/dist/thumbnails/lib/offscreen-dxf-thumbnail.d.ts +16 -0
- package/dist/thumbnails/lib/offscreen-dxf-thumbnail.js +173 -0
- package/dist/thumbnails/lib/offscreen-model-thumbnail.d.ts +16 -0
- package/dist/thumbnails/lib/offscreen-model-thumbnail.js +209 -0
- package/dist/thumbnails/lib/parametricClip.d.ts +18 -0
- package/dist/thumbnails/lib/parametricClip.js +177 -0
- package/dist/thumbnails/lib/parametricShapes.d.ts +1 -0
- package/dist/thumbnails/lib/parametricShapes.js +75 -0
- package/dist/thumbnails/lib/parametricViewerState.d.ts +26 -0
- package/dist/thumbnails/lib/parametricViewerState.js +198 -0
- package/dist/thumbnails/lib/thumbnail-camera-fit.d.ts +6 -0
- package/dist/thumbnails/lib/thumbnail-camera-fit.js +292 -0
- package/dist/thumbnails/lib/thumbnail-capture.d.ts +23 -0
- package/dist/thumbnails/lib/thumbnail-capture.js +161 -0
- package/dist/thumbnails/lib/thumbnail-visibility.d.ts +12 -0
- package/dist/thumbnails/lib/thumbnail-visibility.js +97 -0
- package/dist/thumbnails/lib/viewerGrid.d.ts +13 -0
- package/dist/thumbnails/lib/viewerGrid.js +57 -0
- package/dist/thumbnails/modelCache.d.ts +56 -0
- package/dist/thumbnails/modelCache.js +165 -0
- package/dist/thumbnails/modelOutputArtifact.d.ts +19 -0
- package/dist/thumbnails/modelOutputArtifact.js +87 -0
- package/dist/thumbnails/render.d.ts +45 -0
- package/dist/thumbnails/render.js +162 -0
- package/dist/thumbnails/templates.d.ts +16 -0
- package/dist/thumbnails/templates.js +786 -0
- package/dist/thumbnails/tessellator.d.ts +33 -0
- package/dist/thumbnails/tessellator.js +349 -0
- package/dist/thumbnails/types.d.ts +233 -0
- package/dist/thumbnails/types.js +1 -0
- package/dist/thumbnails/worker.d.ts +10 -0
- package/dist/thumbnails/worker.js +119 -0
- package/dist/transform.d.ts +1 -1
- package/dist/transform.js +20 -22
- package/dist/types/elements/index.d.ts +22 -28
- package/dist/types/elements/index.js +0 -1
- package/dist/types/geometry.types.d.ts +5 -5
- package/dist/types/index.d.ts +13 -59
- package/dist/types/index.js +0 -1
- package/dist/types/issues.types.d.ts +1 -2
- package/dist/utils/bounds.d.ts +10 -11
- package/dist/utils/bounds.js +105 -84
- package/dist/utils/constants.d.ts +5 -7
- package/dist/utils/constants.js +10 -15
- package/dist/utils/elements/freedrawElement.js +6 -3
- package/dist/utils/elements/index.d.ts +2 -6
- package/dist/utils/elements/index.js +4 -9
- package/dist/utils/elements/linearElement.d.ts +12 -14
- package/dist/utils/elements/linearElement.js +81 -66
- package/dist/utils/elements/newElement.d.ts +37 -22
- package/dist/utils/elements/newElement.js +70 -64
- package/dist/utils/elements/textElement.d.ts +34 -35
- package/dist/utils/elements/textElement.js +79 -83
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/locator.d.ts +43 -0
- package/dist/utils/locator.js +112 -0
- package/dist/utils/math/bbox.d.ts +1 -2
- package/dist/utils/math/geometry.js +5 -4
- package/dist/utils/math/index.d.ts +13 -17
- package/dist/utils/math/index.js +53 -41
- package/dist/utils/math/random.js +7 -2
- package/dist/utils/normalize.d.ts +5 -5
- package/dist/utils/normalize.js +15 -10
- package/dist/utils/shape.d.ts +3 -4
- package/dist/utils/shape.js +64 -48
- package/dist/utils/state/index.d.ts +3 -4
- package/dist/utils/state/index.js +8 -23
- package/dist/wasi.js +38 -12
- package/package.json +23 -9
- package/dist/technical/index.d.ts +0 -2
- package/dist/technical/index.js +0 -2
- package/dist/technical/measurements.d.ts +0 -31
- package/dist/technical/measurements.js +0 -64
- package/dist/technical/scopes.d.ts +0 -261
- package/dist/technical/scopes.js +0 -676
- package/dist/types/typeChecks.d.ts +0 -5
- package/dist/types/typeChecks.js +0 -10
- package/dist/utils/traverse.d.ts +0 -30
- package/dist/utils/traverse.js +0 -124
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* Escapes a string to safely embed it inside a script tag.
|
|
6
|
+
*/
|
|
7
|
+
function safeJsonStringify(data) {
|
|
8
|
+
return JSON.stringify(data).replace(/<\/script/gi, "<\\/script");
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Finds a local file URL for an installed module in node_modules,
|
|
12
|
+
* checking the monorepo root, current working directory, and known package paths.
|
|
13
|
+
*/
|
|
14
|
+
function findModulePath(subPath) {
|
|
15
|
+
try {
|
|
16
|
+
if (typeof require !== "undefined" && typeof require.resolve === "function") {
|
|
17
|
+
const resolved = require.resolve(subPath);
|
|
18
|
+
if (fs.existsSync(resolved)) {
|
|
19
|
+
return `file://${resolved}`;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (_a) {
|
|
24
|
+
// Continue searching candidates
|
|
25
|
+
}
|
|
26
|
+
const roots = [CURRENT_DIR, process.cwd()];
|
|
27
|
+
for (const root of roots) {
|
|
28
|
+
let curr = root;
|
|
29
|
+
while (curr && curr !== path.dirname(curr)) {
|
|
30
|
+
const candidate = path.join(curr, "node_modules", subPath);
|
|
31
|
+
if (fs.existsSync(candidate)) {
|
|
32
|
+
return `file://${candidate}`;
|
|
33
|
+
}
|
|
34
|
+
const direct = path.join(curr, subPath);
|
|
35
|
+
if (fs.existsSync(direct)) {
|
|
36
|
+
return `file://${direct}`;
|
|
37
|
+
}
|
|
38
|
+
curr = path.dirname(curr);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Generates the import map JSON for Three.js, three-cad-viewer, and dxf-renderer.
|
|
45
|
+
*/
|
|
46
|
+
function getImportMapJson() {
|
|
47
|
+
var _a, _b, _c;
|
|
48
|
+
const threePath = (_a = findModulePath("three/build/three.module.js")) !== null && _a !== void 0 ? _a : "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js";
|
|
49
|
+
const tcvPath = (_b = findModulePath("three-cad-viewer/dist/three-cad-viewer.esm.js")) !== null && _b !== void 0 ? _b : "https://esm.sh/three-cad-viewer@5.0.0";
|
|
50
|
+
const dxfPath = (_c = findModulePath("dxf-renderer/dist/index.js")) !== null && _c !== void 0 ? _c : "https://esm.sh/dxf-renderer";
|
|
51
|
+
return JSON.stringify({
|
|
52
|
+
imports: {
|
|
53
|
+
three: threePath,
|
|
54
|
+
"three-cad-viewer": tcvPath,
|
|
55
|
+
"dxf-renderer": dxfPath,
|
|
56
|
+
},
|
|
57
|
+
}, null, 2);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Shared in-browser JavaScript function for trimming transparent borders and
|
|
61
|
+
* compositing onto requested background without artificial raster upscaling.
|
|
62
|
+
*/
|
|
63
|
+
const IN_BROWSER_CROP_AND_SCALE_FN = `
|
|
64
|
+
function getOpaqueBounds(pixels, width, height, alphaThreshold) {
|
|
65
|
+
let minX = width;
|
|
66
|
+
let minY = height;
|
|
67
|
+
let maxX = -1;
|
|
68
|
+
let maxY = -1;
|
|
69
|
+
|
|
70
|
+
for (let y = 0; y < height; y++) {
|
|
71
|
+
for (let x = 0; x < width; x++) {
|
|
72
|
+
const alpha = pixels[(y * width + x) * 4 + 3];
|
|
73
|
+
if (alpha <= alphaThreshold) continue;
|
|
74
|
+
if (x < minX) minX = x;
|
|
75
|
+
if (y < minY) minY = y;
|
|
76
|
+
if (x > maxX) maxX = x;
|
|
77
|
+
if (y > maxY) maxY = y;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (maxX < minX || maxY < minY) return null;
|
|
82
|
+
return { minX, minY, maxX, maxY };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function cropAndScaleCanvasToOpaqueContent(sourceCanvas, targetLongEdge, options) {
|
|
86
|
+
options = options || {};
|
|
87
|
+
targetLongEdge = targetLongEdge || 1200;
|
|
88
|
+
const sourceWidth = Math.max(1, sourceCanvas.width);
|
|
89
|
+
const sourceHeight = Math.max(1, sourceCanvas.height);
|
|
90
|
+
const sourceLongEdge = Math.max(sourceWidth, sourceHeight);
|
|
91
|
+
const scanLongEdge = Math.max(1, Math.floor(options.scanLongEdge || sourceLongEdge));
|
|
92
|
+
const scanScale = Math.min(1, scanLongEdge / sourceLongEdge);
|
|
93
|
+
const scanWidth = Math.max(1, Math.round(sourceWidth * scanScale));
|
|
94
|
+
const scanHeight = Math.max(1, Math.round(sourceHeight * scanScale));
|
|
95
|
+
|
|
96
|
+
const scanCanvas = document.createElement("canvas");
|
|
97
|
+
scanCanvas.width = scanWidth;
|
|
98
|
+
scanCanvas.height = scanHeight;
|
|
99
|
+
const scanCtx = scanCanvas.getContext("2d", { willReadFrequently: true });
|
|
100
|
+
if (!scanCtx) return sourceCanvas;
|
|
101
|
+
|
|
102
|
+
scanCtx.clearRect(0, 0, scanWidth, scanHeight);
|
|
103
|
+
scanCtx.imageSmoothingEnabled = false;
|
|
104
|
+
scanCtx.drawImage(sourceCanvas, 0, 0, scanWidth, scanHeight);
|
|
105
|
+
|
|
106
|
+
const scanBounds = getOpaqueBounds(
|
|
107
|
+
scanCtx.getImageData(0, 0, scanCanvas.width, scanCanvas.height).data,
|
|
108
|
+
scanCanvas.width,
|
|
109
|
+
scanCanvas.height,
|
|
110
|
+
4
|
|
111
|
+
);
|
|
112
|
+
if (!scanBounds) return sourceCanvas;
|
|
113
|
+
|
|
114
|
+
const paddingPx = Math.max(0, Math.floor(options.paddingPx !== undefined ? options.paddingPx : 2));
|
|
115
|
+
const inverseScale = 1 / scanScale;
|
|
116
|
+
const bounds = {
|
|
117
|
+
minX: Math.max(0, Math.floor(scanBounds.minX * inverseScale) - paddingPx),
|
|
118
|
+
minY: Math.max(0, Math.floor(scanBounds.minY * inverseScale) - paddingPx),
|
|
119
|
+
maxX: Math.min(sourceWidth - 1, Math.ceil((scanBounds.maxX + 1) * inverseScale) + paddingPx - 1),
|
|
120
|
+
maxY: Math.min(sourceHeight - 1, Math.ceil((scanBounds.maxY + 1) * inverseScale) + paddingPx - 1),
|
|
121
|
+
};
|
|
122
|
+
const trimmedWidth = bounds.maxX - bounds.minX + 1;
|
|
123
|
+
const trimmedHeight = bounds.maxY - bounds.minY + 1;
|
|
124
|
+
|
|
125
|
+
// Never stretch/upscale beyond native renderer resolution.
|
|
126
|
+
// Downsample smoothly only if targetLongEdge is smaller than the native render.
|
|
127
|
+
let scale = 1;
|
|
128
|
+
if (options.allowUpscale) {
|
|
129
|
+
scale = targetLongEdge / Math.max(trimmedWidth, trimmedHeight);
|
|
130
|
+
} else if (targetLongEdge && targetLongEdge < Math.max(trimmedWidth, trimmedHeight)) {
|
|
131
|
+
scale = targetLongEdge / Math.max(trimmedWidth, trimmedHeight);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const outputWidth = Math.max(1, Math.round(trimmedWidth * scale));
|
|
135
|
+
const outputHeight = Math.max(1, Math.round(trimmedHeight * scale));
|
|
136
|
+
|
|
137
|
+
const outputCanvas = document.createElement("canvas");
|
|
138
|
+
outputCanvas.width = outputWidth;
|
|
139
|
+
outputCanvas.height = outputHeight;
|
|
140
|
+
|
|
141
|
+
const outputCtx = outputCanvas.getContext("2d");
|
|
142
|
+
if (!outputCtx) return sourceCanvas;
|
|
143
|
+
|
|
144
|
+
// Composite background if requested (otherwise keep 100% transparent)
|
|
145
|
+
if (options.background && options.background !== "transparent") {
|
|
146
|
+
outputCtx.fillStyle = options.background;
|
|
147
|
+
outputCtx.fillRect(0, 0, outputWidth, outputHeight);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
outputCtx.imageSmoothingEnabled = true;
|
|
151
|
+
outputCtx.imageSmoothingQuality = "high";
|
|
152
|
+
outputCtx.drawImage(
|
|
153
|
+
sourceCanvas,
|
|
154
|
+
bounds.minX, bounds.minY, trimmedWidth, trimmedHeight,
|
|
155
|
+
0, 0, outputWidth, outputHeight
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
return outputCanvas;
|
|
159
|
+
}
|
|
160
|
+
`;
|
|
161
|
+
/**
|
|
162
|
+
* Creates realistic multi-part optical lens barrel assembly CAD geometry
|
|
163
|
+
* when no custom shapes are supplied.
|
|
164
|
+
*/
|
|
165
|
+
const DEFAULT_CAD_SHAPES_GENERATOR = `
|
|
166
|
+
function createDefaultCadShapes() {
|
|
167
|
+
const segments = 36;
|
|
168
|
+
const outerR = 32;
|
|
169
|
+
const innerR = 25;
|
|
170
|
+
const height = 44;
|
|
171
|
+
|
|
172
|
+
const hVertices = [];
|
|
173
|
+
const hTriangles = [];
|
|
174
|
+
const hNormals = [];
|
|
175
|
+
const hEdges = [];
|
|
176
|
+
|
|
177
|
+
for (let i = 0; i < segments; i++) {
|
|
178
|
+
const t1 = (i / segments) * Math.PI * 2;
|
|
179
|
+
const t2 = ((i + 1) / segments) * Math.PI * 2;
|
|
180
|
+
const c1 = Math.cos(t1), s1 = Math.sin(t1);
|
|
181
|
+
const c2 = Math.cos(t2), s2 = Math.sin(t2);
|
|
182
|
+
|
|
183
|
+
const ot1 = [outerR * c1, outerR * s1, height / 2];
|
|
184
|
+
const ot2 = [outerR * c2, outerR * s2, height / 2];
|
|
185
|
+
const ob1 = [outerR * c1, outerR * s1, -height / 2];
|
|
186
|
+
const ob2 = [outerR * c2, outerR * s2, -height / 2];
|
|
187
|
+
|
|
188
|
+
const it1 = [innerR * c1, innerR * s1, height / 2];
|
|
189
|
+
const it2 = [innerR * c2, innerR * s2, height / 2];
|
|
190
|
+
const ib1 = [innerR * c1, innerR * s1, -height / 2];
|
|
191
|
+
const ib2 = [innerR * c2, innerR * s2, -height / 2];
|
|
192
|
+
|
|
193
|
+
const base = hVertices.length / 3;
|
|
194
|
+
hVertices.push(...ot1, ...ot2, ...ob1, ...ob2, ...it1, ...it2, ...ib1, ...ib2);
|
|
195
|
+
|
|
196
|
+
hTriangles.push(base, base + 1, base + 4);
|
|
197
|
+
hTriangles.push(base + 1, base + 5, base + 4);
|
|
198
|
+
hTriangles.push(base + 2, base + 6, base + 3);
|
|
199
|
+
hTriangles.push(base + 3, base + 6, base + 7);
|
|
200
|
+
hTriangles.push(base, base + 2, base + 1);
|
|
201
|
+
hTriangles.push(base + 1, base + 2, base + 3);
|
|
202
|
+
hTriangles.push(base + 4, base + 5, base + 6);
|
|
203
|
+
hTriangles.push(base + 5, base + 7, base + 6);
|
|
204
|
+
|
|
205
|
+
hEdges.push(...ot1, ...ot2, ...ob1, ...ob2, ...it1, ...it2, ...ib1, ...ib2);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for (let i = 0; i < hVertices.length; i += 3) {
|
|
209
|
+
const x = hVertices[i];
|
|
210
|
+
const y = hVertices[i + 1];
|
|
211
|
+
const len = Math.hypot(x, y) || 1;
|
|
212
|
+
hNormals.push(x / len, y / len, 0);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Inner brass retainer core
|
|
216
|
+
const coreOuterR = 24.5;
|
|
217
|
+
const coreInnerR = 15;
|
|
218
|
+
const coreH = 24;
|
|
219
|
+
|
|
220
|
+
const cVertices = [];
|
|
221
|
+
const cTriangles = [];
|
|
222
|
+
const cNormals = [];
|
|
223
|
+
const cEdges = [];
|
|
224
|
+
|
|
225
|
+
for (let i = 0; i < segments; i++) {
|
|
226
|
+
const t1 = (i / segments) * Math.PI * 2;
|
|
227
|
+
const t2 = ((i + 1) / segments) * Math.PI * 2;
|
|
228
|
+
const c1 = Math.cos(t1), s1 = Math.sin(t1);
|
|
229
|
+
const c2 = Math.cos(t2), s2 = Math.sin(t2);
|
|
230
|
+
|
|
231
|
+
const ot1 = [coreOuterR * c1, coreOuterR * s1, coreH / 2];
|
|
232
|
+
const ot2 = [coreOuterR * c2, coreOuterR * s2, coreH / 2];
|
|
233
|
+
const ob1 = [coreOuterR * c1, coreOuterR * s1, -coreH / 2];
|
|
234
|
+
const ob2 = [coreOuterR * c2, coreOuterR * s2, -coreH / 2];
|
|
235
|
+
|
|
236
|
+
const it1 = [coreInnerR * c1, coreInnerR * s1, coreH / 2];
|
|
237
|
+
const it2 = [coreInnerR * c2, coreInnerR * s2, coreH / 2];
|
|
238
|
+
const ib1 = [coreInnerR * c1, coreInnerR * s1, -coreH / 2];
|
|
239
|
+
const ib2 = [coreInnerR * c2, coreInnerR * s2, -coreH / 2];
|
|
240
|
+
|
|
241
|
+
const base = cVertices.length / 3;
|
|
242
|
+
cVertices.push(...ot1, ...ot2, ...ob1, ...ob2, ...it1, ...it2, ...ib1, ...ib2);
|
|
243
|
+
|
|
244
|
+
cTriangles.push(base, base + 1, base + 4);
|
|
245
|
+
cTriangles.push(base + 1, base + 5, base + 4);
|
|
246
|
+
cTriangles.push(base + 2, base + 6, base + 3);
|
|
247
|
+
cTriangles.push(base + 3, base + 6, base + 7);
|
|
248
|
+
cTriangles.push(base, base + 2, base + 1);
|
|
249
|
+
cTriangles.push(base + 1, base + 2, base + 3);
|
|
250
|
+
cTriangles.push(base + 4, base + 5, base + 6);
|
|
251
|
+
cTriangles.push(base + 5, base + 7, base + 6);
|
|
252
|
+
|
|
253
|
+
cEdges.push(...ot1, ...ot2, ...ob1, ...ob2, ...it1, ...it2, ...ib1, ...ib2);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
for (let i = 0; i < cVertices.length; i += 3) {
|
|
257
|
+
cNormals.push(0, 0, cVertices[i + 2] > 0 ? 1 : -1);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
id: "microscope-optics-assembly",
|
|
262
|
+
name: "Microscope Objective Assembly",
|
|
263
|
+
parts: [
|
|
264
|
+
{
|
|
265
|
+
id: "/microscope-optics-assembly/housing",
|
|
266
|
+
name: "Optical Housing",
|
|
267
|
+
color: "#334155",
|
|
268
|
+
state: [1, 1],
|
|
269
|
+
shape: {
|
|
270
|
+
vertices: hVertices,
|
|
271
|
+
triangles: hTriangles,
|
|
272
|
+
normals: hNormals,
|
|
273
|
+
edges: hEdges,
|
|
274
|
+
obj_vertices: hVertices,
|
|
275
|
+
edge_types: new Array(hEdges.length / 6).fill(1),
|
|
276
|
+
face_types: new Array(hTriangles.length / 3).fill(1),
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
id: "/microscope-optics-assembly/core",
|
|
281
|
+
name: "Brass Retainer & Core",
|
|
282
|
+
color: "#d97706",
|
|
283
|
+
state: [1, 1],
|
|
284
|
+
shape: {
|
|
285
|
+
vertices: cVertices,
|
|
286
|
+
triangles: cTriangles,
|
|
287
|
+
normals: cNormals,
|
|
288
|
+
edges: cEdges,
|
|
289
|
+
obj_vertices: cVertices,
|
|
290
|
+
edge_types: new Array(cEdges.length / 6).fill(1),
|
|
291
|
+
face_types: new Array(cTriangles.length / 3).fill(1),
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
]
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
`;
|
|
298
|
+
const CURRENT_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
299
|
+
let cachedClientBundleCode = null;
|
|
300
|
+
function getClientBundleCode() {
|
|
301
|
+
if (cachedClientBundleCode)
|
|
302
|
+
return cachedClientBundleCode;
|
|
303
|
+
const candidates = [
|
|
304
|
+
// Pre-built in dist
|
|
305
|
+
path.resolve(CURRENT_DIR, "lib", "clientBundle.min.js"),
|
|
306
|
+
path.resolve(CURRENT_DIR, "clientBundle.min.js"),
|
|
307
|
+
path.resolve(CURRENT_DIR, "../../dist/thumbnails/lib/clientBundle.min.js"),
|
|
308
|
+
path.resolve(process.cwd(), "dist/thumbnails/lib/clientBundle.min.js"),
|
|
309
|
+
path.resolve(process.cwd(), "packages/ducjs/dist/thumbnails/lib/clientBundle.min.js"),
|
|
310
|
+
];
|
|
311
|
+
for (const c of candidates) {
|
|
312
|
+
if (fs.existsSync(c)) {
|
|
313
|
+
cachedClientBundleCode = fs.readFileSync(c, "utf8");
|
|
314
|
+
return cachedClientBundleCode;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// If in Bun dev/test environment and bundle not on disk, bundle clientEntry on the fly
|
|
318
|
+
if (typeof Bun !== "undefined" && typeof Bun.spawnSync === "function") {
|
|
319
|
+
try {
|
|
320
|
+
const entryCandidates = [
|
|
321
|
+
path.resolve(CURRENT_DIR, "lib", "clientEntry.ts"),
|
|
322
|
+
path.resolve(CURRENT_DIR, "clientEntry.ts"),
|
|
323
|
+
path.resolve(process.cwd(), "packages/ducjs/src/thumbnails/lib/clientEntry.ts"),
|
|
324
|
+
path.resolve(process.cwd(), "src/thumbnails/lib/clientEntry.ts"),
|
|
325
|
+
];
|
|
326
|
+
for (const entryPath of entryCandidates) {
|
|
327
|
+
if (fs.existsSync(entryPath)) {
|
|
328
|
+
const proc = Bun.spawnSync([
|
|
329
|
+
"bun",
|
|
330
|
+
"build",
|
|
331
|
+
entryPath,
|
|
332
|
+
"--target",
|
|
333
|
+
"browser",
|
|
334
|
+
"--minify",
|
|
335
|
+
]);
|
|
336
|
+
if (proc.success) {
|
|
337
|
+
const code = proc.stdout.toString("utf8");
|
|
338
|
+
cachedClientBundleCode = code;
|
|
339
|
+
return code;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
catch (_a) {
|
|
345
|
+
// Fall through
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
throw new Error("Missing thumbnail client bundle. Build ducjs and package dist/thumbnails/lib/clientBundle.min.js alongside the thumbnail handler.");
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Generates an HTML document that renders 3D CAD shapes using three-cad-viewer,
|
|
352
|
+
* strictly matching the app's offscreen-model-thumbnail.ts and thumbnail-capture.ts.
|
|
353
|
+
*/
|
|
354
|
+
export function buildCadViewerHtml(shapes, config) {
|
|
355
|
+
const shapesJson = safeJsonStringify(shapes !== null && shapes !== void 0 ? shapes : null);
|
|
356
|
+
const configJson = safeJsonStringify(config);
|
|
357
|
+
const importMapJson = getImportMapJson();
|
|
358
|
+
const clientBundleCode = getClientBundleCode();
|
|
359
|
+
return `<!DOCTYPE html>
|
|
360
|
+
<html lang="en">
|
|
361
|
+
<head>
|
|
362
|
+
<meta charset="utf-8">
|
|
363
|
+
<style>
|
|
364
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
365
|
+
html, body {
|
|
366
|
+
width: 100%;
|
|
367
|
+
height: 100%;
|
|
368
|
+
overflow: hidden;
|
|
369
|
+
background: ${config.background === "transparent" ? "transparent" : config.background};
|
|
370
|
+
}
|
|
371
|
+
#viewer-container {
|
|
372
|
+
width: ${config.width || 1200}px;
|
|
373
|
+
height: ${config.height || 900}px;
|
|
374
|
+
position: relative;
|
|
375
|
+
}
|
|
376
|
+
canvas {
|
|
377
|
+
display: block;
|
|
378
|
+
}
|
|
379
|
+
</style>
|
|
380
|
+
<script type="importmap">
|
|
381
|
+
${importMapJson}
|
|
382
|
+
</script>
|
|
383
|
+
<script>
|
|
384
|
+
window.__thumbnailReady = false;
|
|
385
|
+
window.__thumbnailError = null;
|
|
386
|
+
window.onerror = function(msg, url, line) {
|
|
387
|
+
window.__thumbnailError = "Global page error: " + msg + " at " + line;
|
|
388
|
+
window.__thumbnailReady = true;
|
|
389
|
+
};
|
|
390
|
+
window.onunhandledrejection = function(e) {
|
|
391
|
+
window.__thumbnailError = "Unhandled rejection: " + (e.reason ? (e.reason.stack || e.reason) : e);
|
|
392
|
+
window.__thumbnailReady = true;
|
|
393
|
+
};
|
|
394
|
+
</script>
|
|
395
|
+
</head>
|
|
396
|
+
<body>
|
|
397
|
+
<div id="viewer-container"></div>
|
|
398
|
+
<script id="shapes-data" type="application/json">
|
|
399
|
+
${shapesJson}
|
|
400
|
+
</script>
|
|
401
|
+
<script id="config-data" type="application/json">
|
|
402
|
+
${configJson}
|
|
403
|
+
</script>
|
|
404
|
+
<script type="module">
|
|
405
|
+
${clientBundleCode}
|
|
406
|
+
|
|
407
|
+
try {
|
|
408
|
+
const rawShapes = JSON.parse(document.getElementById("shapes-data").textContent || "null");
|
|
409
|
+
const cfg = JSON.parse(document.getElementById("config-data").textContent || "{}");
|
|
410
|
+
|
|
411
|
+
// Construct Viewer3DState from resolved view config
|
|
412
|
+
const viewerState = {
|
|
413
|
+
camera: {
|
|
414
|
+
control: "orbit",
|
|
415
|
+
ortho: cfg.camera.ortho !== false,
|
|
416
|
+
up: cfg.camera.up || "Z",
|
|
417
|
+
position: cfg.camera.position,
|
|
418
|
+
quaternion: cfg.camera.quaternion,
|
|
419
|
+
target: cfg.camera.target,
|
|
420
|
+
zoom: cfg.camera.zoom || 1,
|
|
421
|
+
panSpeed: 1,
|
|
422
|
+
rotateSpeed: 1,
|
|
423
|
+
zoomSpeed: 1,
|
|
424
|
+
holroyd: false,
|
|
425
|
+
},
|
|
426
|
+
display: cfg.display,
|
|
427
|
+
material: cfg.material,
|
|
428
|
+
clipping: cfg.clipping,
|
|
429
|
+
explode: cfg.explode,
|
|
430
|
+
zebra: cfg.zebra,
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const requestedLongEdge = Math.max(cfg.targetLongEdge || 0, cfg.width || 0, cfg.height || 0, 1200);
|
|
434
|
+
const aspect = (cfg.width && cfg.height) ? (cfg.width / cfg.height) : (4 / 3);
|
|
435
|
+
let renderWidth = requestedLongEdge;
|
|
436
|
+
let renderHeight = Math.round(requestedLongEdge / aspect);
|
|
437
|
+
if (aspect < 1) {
|
|
438
|
+
renderHeight = requestedLongEdge;
|
|
439
|
+
renderWidth = Math.round(requestedLongEdge * aspect);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
console.log("[Thumbnail] Data parsed. Starting renderOffscreenModelThumbnail...");
|
|
443
|
+
const result = await window.renderOffscreenModelThumbnail({
|
|
444
|
+
shapes: rawShapes,
|
|
445
|
+
viewerState,
|
|
446
|
+
hasExplicitCamera: cfg.hasExplicitCamera,
|
|
447
|
+
width: renderWidth,
|
|
448
|
+
height: renderHeight,
|
|
449
|
+
targetLongEdge: requestedLongEdge,
|
|
450
|
+
trimTransparentBounds: cfg.trimTransparentBounds,
|
|
451
|
+
trimPaddingPx: cfg.trimPaddingPx !== undefined ? cfg.trimPaddingPx : 2,
|
|
452
|
+
background: cfg.background,
|
|
453
|
+
format: cfg.format || "png",
|
|
454
|
+
});
|
|
455
|
+
console.log("[Thumbnail] renderOffscreenModelThumbnail finished. Result:", Boolean(result));
|
|
456
|
+
|
|
457
|
+
if (!result) {
|
|
458
|
+
throw new Error("renderOffscreenModelThumbnail returned null");
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let binary = "";
|
|
462
|
+
const bytes = result.data;
|
|
463
|
+
const len = bytes.byteLength;
|
|
464
|
+
const chunkSize = 0x8000;
|
|
465
|
+
for (let i = 0; i < len; i += chunkSize) {
|
|
466
|
+
binary += String.fromCharCode.apply(null, bytes.subarray(i, Math.min(i + chunkSize, len)));
|
|
467
|
+
}
|
|
468
|
+
const b64 = btoa(binary);
|
|
469
|
+
|
|
470
|
+
const img = new Image();
|
|
471
|
+
img.src = "data:image/" + (cfg.format || "png") + ";base64," + b64;
|
|
472
|
+
document.body.innerHTML = "";
|
|
473
|
+
document.body.style.width = result.width + "px";
|
|
474
|
+
document.body.style.height = result.height + "px";
|
|
475
|
+
if (cfg.background && cfg.background !== "transparent") {
|
|
476
|
+
document.body.style.background = cfg.background;
|
|
477
|
+
} else {
|
|
478
|
+
document.body.style.background = "transparent";
|
|
479
|
+
}
|
|
480
|
+
document.body.appendChild(img);
|
|
481
|
+
|
|
482
|
+
window.__thumbnailBase64 = b64;
|
|
483
|
+
window.__thumbnailWidth = result.width;
|
|
484
|
+
window.__thumbnailHeight = result.height;
|
|
485
|
+
window.__thumbnailRenderer = result.glRenderer || "Unknown";
|
|
486
|
+
window.__thumbnailIsGpu = Boolean(result.isGpu);
|
|
487
|
+
window.__thumbnailReady = true;
|
|
488
|
+
} catch (err) {
|
|
489
|
+
window.__thumbnailError = err instanceof Error ? err.stack || err.message : String(err);
|
|
490
|
+
window.__thumbnailReady = true;
|
|
491
|
+
}
|
|
492
|
+
</script>
|
|
493
|
+
</body>
|
|
494
|
+
</html>`;
|
|
495
|
+
}
|
|
496
|
+
let cachedDxfFontDataUrl = null;
|
|
497
|
+
/**
|
|
498
|
+
* Returns a base64 data URL for the default CAD font (Roboto-Regular.ttf),
|
|
499
|
+
* ensuring vector text and dimension annotations render in offline and headless environments.
|
|
500
|
+
*/
|
|
501
|
+
export function getDxfDefaultFontDataUrl() {
|
|
502
|
+
if (cachedDxfFontDataUrl !== null)
|
|
503
|
+
return cachedDxfFontDataUrl;
|
|
504
|
+
const fontCandidates = [
|
|
505
|
+
process.env.DUC_DXF_FONT_PATH,
|
|
506
|
+
path.resolve(CURRENT_DIR, "../../../../assets/fonts/RobotoMono-Variable.ttf"),
|
|
507
|
+
path.resolve(CURRENT_DIR, "../../../assets/fonts/RobotoMono-Variable.ttf"),
|
|
508
|
+
path.resolve(CURRENT_DIR, "../../assets/fonts/RobotoMono-Variable.ttf"),
|
|
509
|
+
path.resolve(CURRENT_DIR, "../assets/fonts/RobotoMono-Variable.ttf"),
|
|
510
|
+
path.resolve(process.cwd(), "assets/fonts/RobotoMono-Variable.ttf"),
|
|
511
|
+
path.resolve(process.cwd(), "../assets/fonts/RobotoMono-Variable.ttf"),
|
|
512
|
+
path.resolve(process.cwd(), "../../assets/fonts/RobotoMono-Variable.ttf"),
|
|
513
|
+
path.resolve(process.cwd(), "../../../assets/fonts/RobotoMono-Variable.ttf"),
|
|
514
|
+
path.resolve(process.cwd(), "../dxf-renderer/demo/fonts/Roboto-Regular.ttf"),
|
|
515
|
+
path.resolve(process.cwd(), "../../dxf-renderer/demo/fonts/Roboto-Regular.ttf"),
|
|
516
|
+
"/System/Library/Fonts/Supplemental/Arial.ttf",
|
|
517
|
+
"/System/Library/Fonts/Helvetica.ttc",
|
|
518
|
+
"/Library/Fonts/Arial.ttf",
|
|
519
|
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
|
520
|
+
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
|
521
|
+
"C:\\Windows\\Fonts\\arial.ttf",
|
|
522
|
+
].filter(Boolean);
|
|
523
|
+
for (const candidate of fontCandidates) {
|
|
524
|
+
try {
|
|
525
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).size > 0) {
|
|
526
|
+
const buf = fs.readFileSync(candidate);
|
|
527
|
+
cachedDxfFontDataUrl = `data:font/ttf;base64,${buf.toString("base64")}`;
|
|
528
|
+
return cachedDxfFontDataUrl;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
catch (_a) {
|
|
532
|
+
// Continue to next candidate
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Generates an HTML document that renders 2D/3D DXF content using dxf-renderer,
|
|
539
|
+
* strictly matching the app's offscreen-dxf-thumbnail.ts and thumbnail-capture.ts.
|
|
540
|
+
*/
|
|
541
|
+
export function buildDxfViewerHtml(dxfContentBase64, config) {
|
|
542
|
+
const configJson = safeJsonStringify(config);
|
|
543
|
+
const importMapJson = getImportMapJson();
|
|
544
|
+
const defaultFontDataUrl = getDxfDefaultFontDataUrl();
|
|
545
|
+
const fontSources = [];
|
|
546
|
+
if (defaultFontDataUrl) {
|
|
547
|
+
fontSources.push({
|
|
548
|
+
url: defaultFontDataUrl,
|
|
549
|
+
names: [
|
|
550
|
+
"Standard",
|
|
551
|
+
"Arial",
|
|
552
|
+
"Helvetica",
|
|
553
|
+
"Roboto",
|
|
554
|
+
"txt",
|
|
555
|
+
"simplex",
|
|
556
|
+
"romans",
|
|
557
|
+
"isocp",
|
|
558
|
+
"iso",
|
|
559
|
+
"simplex.shx",
|
|
560
|
+
"txt.shx",
|
|
561
|
+
"romans.shx",
|
|
562
|
+
"monotxt",
|
|
563
|
+
"italic",
|
|
564
|
+
"complex",
|
|
565
|
+
],
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
// Online/CDN fallback fonts for symbols, Greek math, and extended unicode if no local font
|
|
570
|
+
fontSources.push("https://cdn.jsdelivr.net/gh/notofonts/noto-fonts@main/hinted/ttf/NotoSans/NotoSans-Regular.ttf", "https://cdn.jsdelivr.net/gh/notofonts/noto-fonts@main/hinted/ttf/NotoSansSymbols/NotoSansSymbols-Regular.ttf", "https://cdn.jsdelivr.net/gh/notofonts/noto-fonts@main/hinted/ttf/NotoSansSymbols2/NotoSansSymbols2-Regular.ttf", "https://cdn.jsdelivr.net/gh/notofonts/noto-fonts@main/hinted/ttf/NotoSansMath/NotoSansMath-Regular.ttf");
|
|
571
|
+
}
|
|
572
|
+
const fontsJson = safeJsonStringify(fontSources);
|
|
573
|
+
return `<!DOCTYPE html>
|
|
574
|
+
<html lang="en">
|
|
575
|
+
<head>
|
|
576
|
+
<meta charset="utf-8">
|
|
577
|
+
<style>
|
|
578
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
579
|
+
html, body {
|
|
580
|
+
width: 100%;
|
|
581
|
+
height: 100%;
|
|
582
|
+
overflow: hidden;
|
|
583
|
+
background: ${config.background === "transparent" ? "transparent" : config.background};
|
|
584
|
+
}
|
|
585
|
+
#viewer-container {
|
|
586
|
+
width: ${config.width || 1200}px;
|
|
587
|
+
height: ${config.height || 900}px;
|
|
588
|
+
position: relative;
|
|
589
|
+
}
|
|
590
|
+
canvas {
|
|
591
|
+
display: block;
|
|
592
|
+
}
|
|
593
|
+
</style>
|
|
594
|
+
<script type="importmap">
|
|
595
|
+
${importMapJson}
|
|
596
|
+
</script>
|
|
597
|
+
<script>
|
|
598
|
+
window.__thumbnailReady = false;
|
|
599
|
+
window.__thumbnailError = null;
|
|
600
|
+
window.onerror = function(msg, url, line) {
|
|
601
|
+
window.__thumbnailError = "Global page error: " + msg + " at " + line;
|
|
602
|
+
window.__thumbnailReady = true;
|
|
603
|
+
};
|
|
604
|
+
window.onunhandledrejection = function(e) {
|
|
605
|
+
window.__thumbnailError = "Unhandled rejection: " + (e.reason ? (e.reason.stack || e.reason) : e);
|
|
606
|
+
window.__thumbnailReady = true;
|
|
607
|
+
};
|
|
608
|
+
</script>
|
|
609
|
+
</head>
|
|
610
|
+
<body>
|
|
611
|
+
<div id="viewer-container"></div>
|
|
612
|
+
<script type="module">
|
|
613
|
+
|
|
614
|
+
${IN_BROWSER_CROP_AND_SCALE_FN}
|
|
615
|
+
|
|
616
|
+
try {
|
|
617
|
+
const THREE = await import("three");
|
|
618
|
+
const { DxfViewer } = await import("dxf-renderer");
|
|
619
|
+
|
|
620
|
+
const cfg = ${configJson};
|
|
621
|
+
const dxfBase64 = "${dxfContentBase64}";
|
|
622
|
+
const binary = atob(dxfBase64);
|
|
623
|
+
const bytes = new Uint8Array(binary.length);
|
|
624
|
+
for (let i = 0; i < binary.length; i++) {
|
|
625
|
+
bytes[i] = binary.charCodeAt(i);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const dxfBlob = new Blob([bytes], { type: "application/dxf" });
|
|
629
|
+
const dxfBlobUrl = URL.createObjectURL(dxfBlob);
|
|
630
|
+
|
|
631
|
+
const container = document.getElementById("viewer-container");
|
|
632
|
+
|
|
633
|
+
// Compute native renderer resolution directly from requested config
|
|
634
|
+
const requestedLongEdge = Math.max(cfg.targetLongEdge || 0, cfg.width || 0, cfg.height || 0, 1200);
|
|
635
|
+
const aspect = (cfg.width && cfg.height) ? (cfg.width / cfg.height) : (4 / 3);
|
|
636
|
+
let renderWidth = requestedLongEdge;
|
|
637
|
+
let renderHeight = Math.round(requestedLongEdge / aspect);
|
|
638
|
+
if (aspect < 1) {
|
|
639
|
+
renderHeight = requestedLongEdge;
|
|
640
|
+
renderWidth = Math.round(requestedLongEdge * aspect);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
container.style.width = renderWidth + "px";
|
|
644
|
+
container.style.height = renderHeight + "px";
|
|
645
|
+
|
|
646
|
+
const viewer = new DxfViewer(container, {
|
|
647
|
+
autoResize: false,
|
|
648
|
+
antialias: true,
|
|
649
|
+
canvasAlpha: true,
|
|
650
|
+
canvasPremultipliedAlpha: false,
|
|
651
|
+
clearAlpha: 0,
|
|
652
|
+
clearColor: new THREE.Color("#ffffff"),
|
|
653
|
+
colorCorrection: true,
|
|
654
|
+
blackWhiteInversion: true,
|
|
655
|
+
preserveDrawingBuffer: true,
|
|
656
|
+
sceneOptions: {
|
|
657
|
+
layout: "Model",
|
|
658
|
+
suppressPaperSpace: true,
|
|
659
|
+
wireframeMesh: true,
|
|
660
|
+
},
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
viewer.GetRenderer()?.setPixelRatio(cfg.pixelRatio || 1);
|
|
664
|
+
viewer.SetSize(renderWidth, renderHeight);
|
|
665
|
+
const fonts = ${fontsJson};
|
|
666
|
+
await viewer.Load({ url: dxfBlobUrl, fonts });
|
|
667
|
+
URL.revokeObjectURL(dxfBlobUrl);
|
|
668
|
+
|
|
669
|
+
const renderer = viewer.GetRenderer();
|
|
670
|
+
if (renderer) {
|
|
671
|
+
renderer.setClearColor(new THREE.Color("#ffffff"), 0);
|
|
672
|
+
renderer.domElement.style.background = "transparent";
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Optional layer visibility filtering
|
|
676
|
+
if (Array.isArray(cfg.hiddenLayers)) {
|
|
677
|
+
for (const layer of cfg.hiddenLayers) {
|
|
678
|
+
viewer.ShowLayer(layer, false);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
if (Array.isArray(cfg.visibleLayers)) {
|
|
682
|
+
const allLayers = viewer.GetLayers();
|
|
683
|
+
if (allLayers) {
|
|
684
|
+
for (const layerName of allLayers.keys()) {
|
|
685
|
+
viewer.ShowLayer(layerName, cfg.visibleLayers.includes(layerName));
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// Fit viewer to CAD drawing extents, custom view area bounding box, or zoomed section
|
|
691
|
+
const bounds = viewer.GetBounds();
|
|
692
|
+
const origin = viewer.GetOrigin() ?? { x: 0, y: 0 };
|
|
693
|
+
|
|
694
|
+
if (cfg.dxfViewArea) {
|
|
695
|
+
viewer.FitView(
|
|
696
|
+
cfg.dxfViewArea.minX - origin.x,
|
|
697
|
+
cfg.dxfViewArea.maxX - origin.x,
|
|
698
|
+
cfg.dxfViewArea.minY - origin.y,
|
|
699
|
+
cfg.dxfViewArea.maxY - origin.y,
|
|
700
|
+
cfg.dxfViewArea.margin ?? 0.04
|
|
701
|
+
);
|
|
702
|
+
} else if (cfg.camera?.zoom && cfg.camera.zoom > 1 && bounds) {
|
|
703
|
+
const totalW = bounds.maxX - bounds.minX;
|
|
704
|
+
const totalH = bounds.maxY - bounds.minY;
|
|
705
|
+
const cx = cfg.camera.target ? cfg.camera.target[0] : (bounds.minX + bounds.maxX) / 2;
|
|
706
|
+
const cy = cfg.camera.target ? cfg.camera.target[1] : (bounds.minY + bounds.maxY) / 2;
|
|
707
|
+
const halfW = totalW / (cfg.camera.zoom * 2);
|
|
708
|
+
const halfH = totalH / (cfg.camera.zoom * 2);
|
|
709
|
+
viewer.FitView(
|
|
710
|
+
cx - halfW - origin.x,
|
|
711
|
+
cx + halfW - origin.x,
|
|
712
|
+
cy - halfH - origin.y,
|
|
713
|
+
cy + halfH - origin.y,
|
|
714
|
+
0.02
|
|
715
|
+
);
|
|
716
|
+
} else if (bounds) {
|
|
717
|
+
viewer.FitView(
|
|
718
|
+
bounds.minX - origin.x,
|
|
719
|
+
bounds.maxX - origin.x,
|
|
720
|
+
bounds.minY - origin.y,
|
|
721
|
+
bounds.maxY - origin.y,
|
|
722
|
+
0.04
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
viewer.Render();
|
|
727
|
+
|
|
728
|
+
// Trim transparent padding natively without upscaling, or preserve custom framed viewport
|
|
729
|
+
const rawCanvas = viewer.GetCanvas();
|
|
730
|
+
const shouldTrim = cfg.trimTransparentBounds !== false && !cfg.dxfViewArea && !(cfg.camera?.zoom && cfg.camera.zoom > 1);
|
|
731
|
+
const cropped = shouldTrim
|
|
732
|
+
? cropAndScaleCanvasToOpaqueContent(rawCanvas, requestedLongEdge, {
|
|
733
|
+
scanLongEdge: 900,
|
|
734
|
+
paddingPx: cfg.trimPaddingPx !== undefined ? cfg.trimPaddingPx : 2,
|
|
735
|
+
background: cfg.background,
|
|
736
|
+
})
|
|
737
|
+
: rawCanvas;
|
|
738
|
+
|
|
739
|
+
const mime = (cfg.format === "webp") ? "image/webp" : "image/png";
|
|
740
|
+
const blob = await new Promise(r => cropped.toBlob(r, mime));
|
|
741
|
+
if (blob) {
|
|
742
|
+
const ab = await blob.arrayBuffer();
|
|
743
|
+
const u8 = new Uint8Array(ab);
|
|
744
|
+
let b = "";
|
|
745
|
+
for (let i = 0; i < u8.length; i++) b += String.fromCharCode(u8[i]);
|
|
746
|
+
window.__thumbnailBase64 = btoa(b);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// Replace container with cropped canvas for CDP snapshot
|
|
750
|
+
document.body.innerHTML = "";
|
|
751
|
+
document.body.style.width = cropped.width + "px";
|
|
752
|
+
document.body.style.height = cropped.height + "px";
|
|
753
|
+
if (cfg.background && cfg.background !== "transparent") {
|
|
754
|
+
document.body.style.background = cfg.background;
|
|
755
|
+
} else {
|
|
756
|
+
document.body.style.background = "transparent";
|
|
757
|
+
}
|
|
758
|
+
document.body.appendChild(cropped);
|
|
759
|
+
|
|
760
|
+
let glRenderer = "Unknown";
|
|
761
|
+
let isGpu = false;
|
|
762
|
+
try {
|
|
763
|
+
const gl = rawCanvas.getContext("webgl2") || rawCanvas.getContext("webgl");
|
|
764
|
+
if (gl) {
|
|
765
|
+
const dbg = gl.getExtension("WEBGL_debug_renderer_info");
|
|
766
|
+
glRenderer = dbg ? gl.getParameter(dbg.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER);
|
|
767
|
+
if (typeof glRenderer === "string") {
|
|
768
|
+
const lower = glRenderer.toLowerCase();
|
|
769
|
+
isGpu = !lower.includes("swiftshader") && !lower.includes("llvmpipe");
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
} catch {}
|
|
773
|
+
|
|
774
|
+
window.__thumbnailWidth = cropped.width;
|
|
775
|
+
window.__thumbnailHeight = cropped.height;
|
|
776
|
+
window.__thumbnailRenderer = glRenderer;
|
|
777
|
+
window.__thumbnailIsGpu = isGpu;
|
|
778
|
+
window.__thumbnailReady = true;
|
|
779
|
+
} catch (err) {
|
|
780
|
+
window.__thumbnailError = err instanceof Error ? err.stack || err.message : String(err);
|
|
781
|
+
window.__thumbnailReady = true;
|
|
782
|
+
}
|
|
783
|
+
</script>
|
|
784
|
+
</body>
|
|
785
|
+
</html>`;
|
|
786
|
+
}
|