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.
Files changed (122) hide show
  1. package/dist/duc_locator_wasm.d.ts +48 -0
  2. package/dist/duc_locator_wasm.js +296 -0
  3. package/dist/duc_locator_wasm_bg.wasm +0 -0
  4. package/dist/duc_locator_wasm_bg.wasm.d.ts +13 -0
  5. package/dist/ducjs_wasi.wasm +0 -0
  6. package/dist/ducjs_wasm.d.ts +14 -2
  7. package/dist/ducjs_wasm.js +96 -8
  8. package/dist/ducjs_wasm_bg.wasm +0 -0
  9. package/dist/ducjs_wasm_bg.wasm.d.ts +6 -2
  10. package/dist/index.d.ts +4 -1
  11. package/dist/index.js +4 -1
  12. package/dist/locator-runtime.d.ts +4 -0
  13. package/dist/locator-runtime.js +7 -0
  14. package/dist/restore/index.d.ts +1 -0
  15. package/dist/restore/index.js +1 -0
  16. package/dist/restore/restoreDataState.d.ts +13 -24
  17. package/dist/restore/restoreDataState.js +49 -83
  18. package/dist/restore/restoreElements.d.ts +10 -15
  19. package/dist/restore/restoreElements.js +128 -125
  20. package/dist/restore/updateModelCode.d.ts +11 -0
  21. package/dist/restore/updateModelCode.js +63 -0
  22. package/dist/thumbnails/bounds.d.ts +21 -0
  23. package/dist/thumbnails/bounds.js +84 -0
  24. package/dist/thumbnails/browserRunner.d.ts +60 -0
  25. package/dist/thumbnails/browserRunner.js +446 -0
  26. package/dist/thumbnails/cameraPresets.d.ts +47 -0
  27. package/dist/thumbnails/cameraPresets.js +222 -0
  28. package/dist/thumbnails/cli.d.ts +2 -0
  29. package/dist/thumbnails/cli.js +102 -0
  30. package/dist/thumbnails/index.d.ts +11 -0
  31. package/dist/thumbnails/index.js +11 -0
  32. package/dist/thumbnails/lib/clientBundle.min.js +10427 -0
  33. package/dist/thumbnails/lib/clientEntry.d.ts +3 -0
  34. package/dist/thumbnails/lib/clientEntry.js +8 -0
  35. package/dist/thumbnails/lib/clippingUtils.d.ts +10 -0
  36. package/dist/thumbnails/lib/clippingUtils.js +3 -0
  37. package/dist/thumbnails/lib/defaultModelColor.d.ts +3 -0
  38. package/dist/thumbnails/lib/defaultModelColor.js +22 -0
  39. package/dist/thumbnails/lib/index.d.ts +12 -0
  40. package/dist/thumbnails/lib/index.js +12 -0
  41. package/dist/thumbnails/lib/mapViewerStateToOptions.d.ts +34 -0
  42. package/dist/thumbnails/lib/mapViewerStateToOptions.js +69 -0
  43. package/dist/thumbnails/lib/offscreen-dxf-thumbnail.d.ts +16 -0
  44. package/dist/thumbnails/lib/offscreen-dxf-thumbnail.js +173 -0
  45. package/dist/thumbnails/lib/offscreen-model-thumbnail.d.ts +16 -0
  46. package/dist/thumbnails/lib/offscreen-model-thumbnail.js +209 -0
  47. package/dist/thumbnails/lib/parametricClip.d.ts +18 -0
  48. package/dist/thumbnails/lib/parametricClip.js +177 -0
  49. package/dist/thumbnails/lib/parametricShapes.d.ts +1 -0
  50. package/dist/thumbnails/lib/parametricShapes.js +75 -0
  51. package/dist/thumbnails/lib/parametricViewerState.d.ts +26 -0
  52. package/dist/thumbnails/lib/parametricViewerState.js +198 -0
  53. package/dist/thumbnails/lib/thumbnail-camera-fit.d.ts +6 -0
  54. package/dist/thumbnails/lib/thumbnail-camera-fit.js +292 -0
  55. package/dist/thumbnails/lib/thumbnail-capture.d.ts +23 -0
  56. package/dist/thumbnails/lib/thumbnail-capture.js +161 -0
  57. package/dist/thumbnails/lib/thumbnail-visibility.d.ts +12 -0
  58. package/dist/thumbnails/lib/thumbnail-visibility.js +97 -0
  59. package/dist/thumbnails/lib/viewerGrid.d.ts +13 -0
  60. package/dist/thumbnails/lib/viewerGrid.js +57 -0
  61. package/dist/thumbnails/modelCache.d.ts +56 -0
  62. package/dist/thumbnails/modelCache.js +165 -0
  63. package/dist/thumbnails/modelOutputArtifact.d.ts +19 -0
  64. package/dist/thumbnails/modelOutputArtifact.js +87 -0
  65. package/dist/thumbnails/render.d.ts +45 -0
  66. package/dist/thumbnails/render.js +162 -0
  67. package/dist/thumbnails/templates.d.ts +16 -0
  68. package/dist/thumbnails/templates.js +786 -0
  69. package/dist/thumbnails/tessellator.d.ts +33 -0
  70. package/dist/thumbnails/tessellator.js +349 -0
  71. package/dist/thumbnails/types.d.ts +233 -0
  72. package/dist/thumbnails/types.js +1 -0
  73. package/dist/thumbnails/worker.d.ts +10 -0
  74. package/dist/thumbnails/worker.js +119 -0
  75. package/dist/transform.d.ts +1 -1
  76. package/dist/transform.js +20 -22
  77. package/dist/types/elements/index.d.ts +22 -28
  78. package/dist/types/elements/index.js +0 -1
  79. package/dist/types/geometry.types.d.ts +5 -5
  80. package/dist/types/index.d.ts +13 -59
  81. package/dist/types/index.js +0 -1
  82. package/dist/types/issues.types.d.ts +1 -2
  83. package/dist/utils/bounds.d.ts +10 -11
  84. package/dist/utils/bounds.js +105 -84
  85. package/dist/utils/constants.d.ts +5 -7
  86. package/dist/utils/constants.js +10 -15
  87. package/dist/utils/elements/freedrawElement.js +6 -3
  88. package/dist/utils/elements/index.d.ts +2 -6
  89. package/dist/utils/elements/index.js +4 -9
  90. package/dist/utils/elements/linearElement.d.ts +12 -14
  91. package/dist/utils/elements/linearElement.js +81 -66
  92. package/dist/utils/elements/newElement.d.ts +37 -22
  93. package/dist/utils/elements/newElement.js +70 -64
  94. package/dist/utils/elements/textElement.d.ts +34 -35
  95. package/dist/utils/elements/textElement.js +79 -83
  96. package/dist/utils/index.d.ts +1 -1
  97. package/dist/utils/index.js +1 -1
  98. package/dist/utils/locator.d.ts +43 -0
  99. package/dist/utils/locator.js +112 -0
  100. package/dist/utils/math/bbox.d.ts +1 -2
  101. package/dist/utils/math/geometry.js +5 -4
  102. package/dist/utils/math/index.d.ts +13 -17
  103. package/dist/utils/math/index.js +53 -41
  104. package/dist/utils/math/random.js +7 -2
  105. package/dist/utils/normalize.d.ts +5 -5
  106. package/dist/utils/normalize.js +15 -10
  107. package/dist/utils/shape.d.ts +3 -4
  108. package/dist/utils/shape.js +64 -48
  109. package/dist/utils/state/index.d.ts +3 -4
  110. package/dist/utils/state/index.js +8 -23
  111. package/dist/wasi.js +38 -12
  112. package/package.json +23 -9
  113. package/dist/technical/index.d.ts +0 -2
  114. package/dist/technical/index.js +0 -2
  115. package/dist/technical/measurements.d.ts +0 -31
  116. package/dist/technical/measurements.js +0 -64
  117. package/dist/technical/scopes.d.ts +0 -261
  118. package/dist/technical/scopes.js +0 -676
  119. package/dist/types/typeChecks.d.ts +0 -5
  120. package/dist/types/typeChecks.js +0 -10
  121. package/dist/utils/traverse.d.ts +0 -30
  122. package/dist/utils/traverse.js +0 -124
@@ -0,0 +1,33 @@
1
+ import type { DucModelElement } from "../types/elements";
2
+ import type { DucExternalFiles, ExternalFilesData } from "../types";
3
+ import { type ModelCacheKind } from "./modelCache";
4
+ export interface TessellationResult {
5
+ success: boolean;
6
+ kind: ModelCacheKind;
7
+ modelType: string;
8
+ shapes?: unknown;
9
+ dxf?: string;
10
+ rawPayload: string;
11
+ elementId?: string;
12
+ }
13
+ export interface TessellateOptions {
14
+ ducPath?: string;
15
+ files?: DucExternalFiles;
16
+ filesData?: ExternalFilesData;
17
+ ducpyDir?: string;
18
+ timeoutMs?: number;
19
+ }
20
+ /**
21
+ * Tessellates a model element from a .duc file path.
22
+ */
23
+ export declare function tessellateDucFileElement(ducPath: string, elementId: string, options?: TessellateOptions): Promise<TessellationResult>;
24
+ /**
25
+ * Tessellates a standalone CAD or BIM file (.step, .stl, .ifc, .dxf).
26
+ */
27
+ export declare function tessellateCadFile(filePath: string, modelType?: string, options?: TessellateOptions): Promise<TessellationResult>;
28
+ /**
29
+ * Tessellates a DucModelElement into renderable shapes.
30
+ * If the element is associated with a .duc file on disk, extracts and tessellates it via ducpy.
31
+ * If external files data is provided in-memory, writes them to temporary paths and executes code.
32
+ */
33
+ export declare function tessellateModelElement(element: DucModelElement, options?: TessellateOptions): Promise<TessellationResult>;
@@ -0,0 +1,349 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { spawn } from "child_process";
11
+ import fs from "fs";
12
+ import os from "os";
13
+ import path from "path";
14
+ import { fileURLToPath } from "node:url";
15
+ import { getSharedModelCacheManager, createModelElementCacheInput, } from "./modelCache";
16
+ /**
17
+ * Resolves the directory path of the ducpy Python package.
18
+ */
19
+ function resolveDucpyDir(customDir) {
20
+ if (customDir && fs.existsSync(customDir)) {
21
+ return customDir;
22
+ }
23
+ const envDir = process.env.DUCPY_DIR;
24
+ if (envDir && fs.existsSync(envDir)) {
25
+ return envDir;
26
+ }
27
+ const baseDir = path.dirname(fileURLToPath(import.meta.url));
28
+ const candidates = [
29
+ path.resolve(baseDir, "../../ducpy"),
30
+ path.resolve(baseDir, "../../../ducpy"),
31
+ path.resolve(process.cwd(), "packages/ducpy"),
32
+ path.resolve(process.cwd(), "../ducpy"),
33
+ ];
34
+ for (const candidate of candidates) {
35
+ if (fs.existsSync(path.join(candidate, "pyproject.toml"))) {
36
+ return candidate;
37
+ }
38
+ }
39
+ // Traverse parent directories to locate ducpy or packages/ducpy
40
+ let curr = baseDir;
41
+ while (curr && curr !== path.dirname(curr)) {
42
+ const p1 = path.join(curr, "packages", "ducpy");
43
+ if (fs.existsSync(path.join(p1, "pyproject.toml")))
44
+ return p1;
45
+ const p2 = path.join(curr, "ducpy");
46
+ if (fs.existsSync(path.join(p2, "pyproject.toml")))
47
+ return p2;
48
+ curr = path.dirname(curr);
49
+ }
50
+ return candidates[0];
51
+ }
52
+ /**
53
+ * Invokes the ducpy.tessellate CLI runner via uv in a child process.
54
+ */
55
+ function invokeDucpyTessellate(args_1, ducpyDir_1, stdinData_1) {
56
+ return __awaiter(this, arguments, void 0, function* (args, ducpyDir, stdinData, timeoutMs = 60000) {
57
+ return new Promise((resolve, reject) => {
58
+ const child = spawn("uv", ["run", "python", "-m", "ducpy.tessellate", ...args], {
59
+ cwd: ducpyDir,
60
+ env: Object.assign(Object.assign({}, process.env), { PYTHONUNBUFFERED: "1" }),
61
+ stdio: ["pipe", "pipe", "pipe"],
62
+ });
63
+ let stdout = "";
64
+ let stderr = "";
65
+ let timer = null;
66
+ if (timeoutMs > 0) {
67
+ timer = setTimeout(() => {
68
+ child.kill("SIGKILL");
69
+ reject(new Error(`ducpy.tessellate timed out after ${timeoutMs}ms`));
70
+ }, timeoutMs);
71
+ }
72
+ child.stdout.on("data", (chunk) => {
73
+ stdout += chunk.toString("utf8");
74
+ });
75
+ child.stderr.on("data", (chunk) => {
76
+ stderr += chunk.toString("utf8");
77
+ });
78
+ child.on("error", (err) => {
79
+ if (timer)
80
+ clearTimeout(timer);
81
+ reject(err);
82
+ });
83
+ child.on("close", (code) => {
84
+ if (timer)
85
+ clearTimeout(timer);
86
+ resolve({ stdout, stderr, code: code !== null && code !== void 0 ? code : 0 });
87
+ });
88
+ if (stdinData) {
89
+ child.stdin.write(stdinData);
90
+ child.stdin.end();
91
+ }
92
+ else {
93
+ child.stdin.end();
94
+ }
95
+ });
96
+ });
97
+ }
98
+ /**
99
+ * Tessellates a model element from a .duc file path.
100
+ */
101
+ export function tessellateDucFileElement(ducPath_1, elementId_1) {
102
+ return __awaiter(this, arguments, void 0, function* (ducPath, elementId, options = {}) {
103
+ var _a, _b;
104
+ const ducpyDir = resolveDucpyDir(options.ducpyDir);
105
+ const cache = getSharedModelCacheManager();
106
+ const ducStat = fs.statSync(ducPath);
107
+ const mtimeStr = String(ducStat.mtimeMs);
108
+ // Check cache before running Python sidecar
109
+ for (const candidateKind of ["parametric-json", "ifc-json", "python-dxf", "model-artifact"]) {
110
+ const candidateInput = {
111
+ kind: candidateKind,
112
+ sourceFileId: `duc:${path.basename(ducPath)}:${elementId}`,
113
+ sourceRevisionId: mtimeStr,
114
+ variant: candidateKind,
115
+ };
116
+ const cached = yield cache.get(candidateInput);
117
+ if ((_a = cached === null || cached === void 0 ? void 0 : cached.data) === null || _a === void 0 ? void 0 : _a.byteLength) {
118
+ const payloadRaw = Buffer.from(cached.data).toString("utf8");
119
+ let shapes;
120
+ let dxf;
121
+ if (candidateKind === "parametric-json" || candidateKind === "ifc-json") {
122
+ shapes = JSON.parse(payloadRaw);
123
+ }
124
+ else {
125
+ dxf = payloadRaw;
126
+ }
127
+ return {
128
+ success: true,
129
+ kind: candidateKind,
130
+ modelType: candidateKind === "ifc-json" ? "ifc" : candidateKind === "python-dxf" ? "dxf" : "step",
131
+ shapes,
132
+ dxf,
133
+ rawPayload: payloadRaw,
134
+ elementId,
135
+ };
136
+ }
137
+ }
138
+ // Create temporary output file to stream large payloads directly without CLI stdout limits
139
+ const tempOutputFile = path.join(os.tmpdir(), `duc-tess-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
140
+ try {
141
+ const { stdout, stderr, code } = yield invokeDucpyTessellate(["--duc", path.resolve(ducPath), `--element=${elementId}`, "--output", tempOutputFile], ducpyDir, undefined, (_b = options.timeoutMs) !== null && _b !== void 0 ? _b : 120000);
142
+ if (code !== 0 || !fs.existsSync(tempOutputFile)) {
143
+ throw new Error(`Failed to tessellate element '${elementId}' from '${ducPath}': ${stderr || stdout}`);
144
+ }
145
+ const payloadRaw = fs.readFileSync(tempOutputFile, "utf8");
146
+ const meta = JSON.parse(stdout.trim());
147
+ const kind = meta.kind;
148
+ const modelType = meta.modelType || "python";
149
+ let shapes;
150
+ let dxf;
151
+ if (kind === "parametric-json" || kind === "ifc-json") {
152
+ shapes = JSON.parse(payloadRaw);
153
+ }
154
+ else if (kind === "python-dxf" || kind === "dwg-dxf") {
155
+ dxf = payloadRaw;
156
+ }
157
+ // Cache the result
158
+ const cacheInput = {
159
+ kind,
160
+ sourceFileId: `duc:${path.basename(ducPath)}:${elementId}`,
161
+ sourceRevisionId: mtimeStr,
162
+ variant: kind,
163
+ };
164
+ yield cache.save(cacheInput, Buffer.from(payloadRaw, "utf8"));
165
+ return {
166
+ success: true,
167
+ kind,
168
+ modelType,
169
+ shapes,
170
+ dxf,
171
+ rawPayload: payloadRaw,
172
+ elementId,
173
+ };
174
+ }
175
+ finally {
176
+ try {
177
+ if (fs.existsSync(tempOutputFile)) {
178
+ fs.unlinkSync(tempOutputFile);
179
+ }
180
+ }
181
+ catch (_c) {
182
+ // Best-effort cleanup
183
+ }
184
+ }
185
+ });
186
+ }
187
+ /**
188
+ * Tessellates a standalone CAD or BIM file (.step, .stl, .ifc, .dxf).
189
+ */
190
+ export function tessellateCadFile(filePath_1, modelType_1) {
191
+ return __awaiter(this, arguments, void 0, function* (filePath, modelType, options = {}) {
192
+ var _a;
193
+ const ducpyDir = resolveDucpyDir(options.ducpyDir);
194
+ const cache = getSharedModelCacheManager();
195
+ const tempOutputFile = path.join(os.tmpdir(), `duc-cad-tess-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
196
+ const args = ["--file", path.resolve(filePath), "--output", tempOutputFile];
197
+ if (modelType) {
198
+ args.push("--model-type", modelType);
199
+ }
200
+ try {
201
+ const { stdout, stderr, code } = yield invokeDucpyTessellate(args, ducpyDir, undefined, (_a = options.timeoutMs) !== null && _a !== void 0 ? _a : 120000);
202
+ if (code !== 0 || !fs.existsSync(tempOutputFile)) {
203
+ throw new Error(`Failed to tessellate file '${filePath}': ${stderr || stdout}`);
204
+ }
205
+ const payloadRaw = fs.readFileSync(tempOutputFile, "utf8");
206
+ const meta = JSON.parse(stdout.trim());
207
+ const kind = meta.kind;
208
+ let shapes;
209
+ let dxf;
210
+ if (kind === "parametric-json" || kind === "ifc-json") {
211
+ shapes = JSON.parse(payloadRaw);
212
+ }
213
+ else if (kind === "python-dxf" || kind === "dwg-dxf") {
214
+ dxf = payloadRaw;
215
+ }
216
+ const cacheInput = {
217
+ kind,
218
+ sourceFileId: path.basename(filePath),
219
+ sourceRevisionId: String(fs.statSync(filePath).mtimeMs),
220
+ variant: `${kind}`,
221
+ };
222
+ yield cache.save(cacheInput, Buffer.from(payloadRaw, "utf8"));
223
+ return {
224
+ success: true,
225
+ kind,
226
+ modelType: meta.modelType || modelType || "step",
227
+ shapes,
228
+ dxf,
229
+ rawPayload: payloadRaw,
230
+ };
231
+ }
232
+ finally {
233
+ try {
234
+ if (fs.existsSync(tempOutputFile)) {
235
+ fs.unlinkSync(tempOutputFile);
236
+ }
237
+ }
238
+ catch (_b) {
239
+ // Best-effort cleanup
240
+ }
241
+ }
242
+ });
243
+ }
244
+ /**
245
+ * Tessellates a DucModelElement into renderable shapes.
246
+ * If the element is associated with a .duc file on disk, extracts and tessellates it via ducpy.
247
+ * If external files data is provided in-memory, writes them to temporary paths and executes code.
248
+ */
249
+ export function tessellateModelElement(element_1) {
250
+ return __awaiter(this, arguments, void 0, function* (element, options = {}) {
251
+ var _a, _b, _c;
252
+ const modelType = (element.modelType || "python").toLowerCase();
253
+ const kind = modelType === "ifc"
254
+ ? "ifc-json"
255
+ : modelType === "dxf" || modelType === "dwg"
256
+ ? "python-dxf"
257
+ : "parametric-json";
258
+ const cache = getSharedModelCacheManager();
259
+ const cacheInput = createModelElementCacheInput(element, options.files);
260
+ const cached = yield cache.get(cacheInput);
261
+ if ((_a = cached === null || cached === void 0 ? void 0 : cached.data) === null || _a === void 0 ? void 0 : _a.byteLength) {
262
+ const rawPayload = Buffer.from(cached.data).toString("utf8");
263
+ const cachedKind = cached.outputKind || kind;
264
+ let shapes;
265
+ let dxf;
266
+ if (cachedKind === "parametric-json" || cachedKind === "ifc-json") {
267
+ shapes = JSON.parse(rawPayload);
268
+ }
269
+ else if (cachedKind === "python-dxf" || cachedKind === "dwg-dxf") {
270
+ dxf = rawPayload;
271
+ }
272
+ return {
273
+ success: true,
274
+ kind: cachedKind,
275
+ modelType: cached.modelType || modelType,
276
+ shapes,
277
+ dxf,
278
+ rawPayload,
279
+ elementId: element.id,
280
+ };
281
+ }
282
+ // If a .duc path is available, use fast native extraction
283
+ if (options.ducPath && fs.existsSync(options.ducPath)) {
284
+ return yield tessellateDucFileElement(options.ducPath, element.id, options);
285
+ }
286
+ // Otherwise, handle in-memory external files and code
287
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "duc-elem-tess-"));
288
+ const tempOutputFile = path.join(tempDir, "output.json");
289
+ try {
290
+ const externalFilesList = [];
291
+ if (element.fileIds && options.files && options.filesData) {
292
+ for (const fid of element.fileIds.filter(id => !id.startsWith("model_output_") && !id.startsWith("model_preview_") && !id.startsWith("dwg_dxf_cache_") && !(modelType === "python" && id.startsWith("model_python_")))) {
293
+ const fileEntry = options.files[fid];
294
+ const revId = fileEntry === null || fileEntry === void 0 ? void 0 : fileEntry.activeRevisionId;
295
+ if (revId && options.filesData[revId]) {
296
+ const revMeta = (_b = fileEntry === null || fileEntry === void 0 ? void 0 : fileEntry.revisions) === null || _b === void 0 ? void 0 : _b[revId];
297
+ const fileName = (revMeta === null || revMeta === void 0 ? void 0 : revMeta.sourceName) || `${fid}.bin`;
298
+ const filePath = path.join(tempDir, fileName);
299
+ fs.writeFileSync(filePath, options.filesData[revId]);
300
+ externalFilesList.push({
301
+ id: fid,
302
+ localPath: filePath,
303
+ name: fileName,
304
+ });
305
+ }
306
+ }
307
+ }
308
+ const payloadJson = JSON.stringify({
309
+ code: element.code || "",
310
+ modelType,
311
+ externalFiles: externalFilesList,
312
+ });
313
+ const ducpyDir = resolveDucpyDir(options.ducpyDir);
314
+ const { stdout, stderr, code } = yield invokeDucpyTessellate(["--input-json", "-", "--output", tempOutputFile], ducpyDir, payloadJson, (_c = options.timeoutMs) !== null && _c !== void 0 ? _c : 120000);
315
+ if (code !== 0 || !fs.existsSync(tempOutputFile)) {
316
+ throw new Error(`Failed to tessellate element '${element.id}' via code: ${stderr || stdout}`);
317
+ }
318
+ const rawPayload = fs.readFileSync(tempOutputFile, "utf8");
319
+ const meta = JSON.parse(stdout.trim());
320
+ const outputKind = (meta.kind || kind);
321
+ let shapes;
322
+ let dxf;
323
+ if (outputKind === "parametric-json" || outputKind === "ifc-json") {
324
+ shapes = JSON.parse(rawPayload);
325
+ }
326
+ else if (outputKind === "python-dxf" || outputKind === "dwg-dxf") {
327
+ dxf = rawPayload;
328
+ }
329
+ yield cache.save(Object.assign(Object.assign({}, cacheInput), { outputKind, modelType: meta.modelType || modelType }), Buffer.from(rawPayload, "utf8"));
330
+ return {
331
+ success: true,
332
+ kind: outputKind,
333
+ modelType: meta.modelType || modelType,
334
+ shapes,
335
+ dxf,
336
+ rawPayload,
337
+ elementId: element.id,
338
+ };
339
+ }
340
+ finally {
341
+ try {
342
+ fs.rmSync(tempDir, { recursive: true, force: true });
343
+ }
344
+ catch (_d) {
345
+ // Best-effort cleanup
346
+ }
347
+ }
348
+ });
349
+ }
@@ -0,0 +1,233 @@
1
+ import type { DucModelElement, Viewer3DCamera, Viewer3DClipping, Viewer3DDisplay, Viewer3DExplode, Viewer3DMaterial, Viewer3DState, Viewer3DZebra } from "../types/elements";
2
+ /**
3
+ * Standard engineering camera viewpoint presets.
4
+ */
5
+ export type CameraPreset = "iso" | "top" | "bottom" | "front" | "rear" | "left" | "right";
6
+ /**
7
+ * Specification for rendering a model view thumbnail/frame.
8
+ */
9
+ export interface ModelViewConfig {
10
+ /**
11
+ * Predefined camera perspective. If set, provides standard coordinate transforms.
12
+ */
13
+ cameraPreset?: CameraPreset;
14
+ /**
15
+ * Explicit camera options overriding presets or existing viewerState.
16
+ */
17
+ camera?: Partial<Viewer3DCamera>;
18
+ /**
19
+ * Display options such as wireframe, axes, and grid.
20
+ */
21
+ display?: Partial<Viewer3DDisplay>;
22
+ /**
23
+ * Material and lighting properties for 3D surfaces and edges.
24
+ */
25
+ material?: Partial<Viewer3DMaterial>;
26
+ /**
27
+ * Clipping plane states along X, Y, and Z axes.
28
+ */
29
+ clipping?: Partial<Viewer3DClipping>;
30
+ /**
31
+ * CAD explosion animation state.
32
+ */
33
+ explode?: Partial<Viewer3DExplode>;
34
+ /**
35
+ * Surface curvature zebra stripe inspection configuration.
36
+ */
37
+ zebra?: Partial<Viewer3DZebra>;
38
+ /**
39
+ * Background color or "transparent".
40
+ * @default "transparent"
41
+ */
42
+ background?: "transparent" | "white" | "dark" | string;
43
+ /**
44
+ * Target rendered pixel width.
45
+ * @default 1024
46
+ */
47
+ width?: number;
48
+ /**
49
+ * Target rendered pixel height.
50
+ * @default 768
51
+ */
52
+ height?: number;
53
+ /**
54
+ * Device pixel ratio (1 for standard offscreen rendering).
55
+ * @default 1
56
+ */
57
+ pixelRatio?: number;
58
+ /**
59
+ * Whether to trim excess transparent border pixels from the output thumbnail.
60
+ * Directly mirrors packages/client/ui/blocks/core/three/lib/thumbnail-capture.ts.
61
+ * @default true
62
+ */
63
+ trimTransparentBounds?: boolean;
64
+ /**
65
+ * Padding in pixels around the trimmed content.
66
+ * @default 2
67
+ */
68
+ trimPaddingPx?: number;
69
+ /**
70
+ * Target long edge size in pixels when trimming/scaling.
71
+ * @default 1200
72
+ */
73
+ targetLongEdge?: number;
74
+ /**
75
+ * Output image encoding format.
76
+ * @default "png"
77
+ */
78
+ format?: "png" | "webp" | "jpeg";
79
+ /**
80
+ * DXF specific view bounding box to frame or zoom into a specific sub-region / section.
81
+ */
82
+ dxfViewArea?: {
83
+ minX: number;
84
+ maxX: number;
85
+ minY: number;
86
+ maxY: number;
87
+ margin?: number;
88
+ };
89
+ /**
90
+ * DXF layer names to hide.
91
+ */
92
+ hiddenLayers?: string[];
93
+ /**
94
+ * DXF layer names to display exclusively.
95
+ */
96
+ visibleLayers?: string[];
97
+ }
98
+ /**
99
+ * Input descriptor for grabbing a model thumbnail.
100
+ */
101
+ export interface ModelThumbnailInput {
102
+ /**
103
+ * Model type (e.g. "PYTHON", "CAD", "DXF", "STEP", "STL").
104
+ */
105
+ modelType?: string | null;
106
+ /**
107
+ * 3D CAD parametric shapes tree, geometry payload, or Three.js scene descriptor.
108
+ */
109
+ shapes?: unknown;
110
+ /**
111
+ * Raw DXF content (either binary buffer or string).
112
+ */
113
+ dxf?: Uint8Array | string;
114
+ /**
115
+ * Associated Python code source (e.g. build123d script) if available.
116
+ */
117
+ pythonCode?: string | null;
118
+ /**
119
+ * Desired view configuration overrides (camera, lighting, dimensions).
120
+ */
121
+ viewConfig?: ModelViewConfig;
122
+ /**
123
+ * Existing 3D viewer state from a DucModelElement.
124
+ */
125
+ viewerState?: Viewer3DState | null;
126
+ /**
127
+ * Output image format.
128
+ * @default "png"
129
+ */
130
+ format?: "png" | "jpeg" | "webp";
131
+ /**
132
+ * Compression quality (0.0 to 1.0) for formats like jpeg and webp.
133
+ * @default 0.9
134
+ */
135
+ quality?: number;
136
+ }
137
+ /**
138
+ * Result returned after capturing a model thumbnail/frame.
139
+ */
140
+ export interface ModelThumbnailResult {
141
+ /**
142
+ * Raw image binary bytes (e.g. PNG buffer).
143
+ */
144
+ data: Uint8Array;
145
+ /**
146
+ * Base64-encoded representation of the image data.
147
+ */
148
+ base64: string;
149
+ /**
150
+ * Image format produced.
151
+ */
152
+ format: "png" | "jpeg" | "webp";
153
+ /**
154
+ * Output width in pixels.
155
+ */
156
+ width: number;
157
+ /**
158
+ * Output height in pixels.
159
+ */
160
+ height: number;
161
+ /**
162
+ * Execution time in milliseconds.
163
+ */
164
+ renderTimeMs: number;
165
+ /**
166
+ * Active WebGL graphics renderer string reported by the browser/driver.
167
+ */
168
+ glRenderer?: string;
169
+ /**
170
+ * Graphics acceleration backend used: "gpu" (Metal/Vulkan/D3D) or "cpu" (SwiftShader).
171
+ */
172
+ backend?: "gpu" | "cpu";
173
+ /**
174
+ * Proportional element bounds update maintaining thumbnail aspect ratio.
175
+ * Mirrors packages/client/ui/blocks/core/three/lib/model-thumbnail-bounds.ts.
176
+ */
177
+ boundsUpdate?: {
178
+ width: number;
179
+ height: number;
180
+ } | null;
181
+ }
182
+ /**
183
+ * Persistent worker IPC request packet.
184
+ */
185
+ export interface ThumbnailWorkerRequest {
186
+ /**
187
+ * Correlating request identifier.
188
+ */
189
+ id: string | number;
190
+ /**
191
+ * Action to perform.
192
+ */
193
+ action: "render" | "ping" | "shutdown";
194
+ /**
195
+ * Input payload for render action.
196
+ */
197
+ input?: ModelThumbnailInput;
198
+ /**
199
+ * Or direct DucModelElement payload.
200
+ */
201
+ element?: DucModelElement;
202
+ /**
203
+ * Optional view config override when passing element.
204
+ */
205
+ viewConfig?: ModelViewConfig;
206
+ }
207
+ /**
208
+ * Persistent worker IPC response packet.
209
+ */
210
+ export interface ThumbnailWorkerResponse {
211
+ /**
212
+ * Correlating request identifier.
213
+ */
214
+ id: string | number;
215
+ /**
216
+ * Whether the operation succeeded.
217
+ */
218
+ ok: boolean;
219
+ /**
220
+ * Error message if the operation failed.
221
+ */
222
+ error?: string;
223
+ /**
224
+ * Render result on success.
225
+ */
226
+ result?: {
227
+ base64: string;
228
+ format: "png" | "jpeg" | "webp";
229
+ width: number;
230
+ height: number;
231
+ renderTimeMs: number;
232
+ };
233
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { type RenderThumbnailOptions } from "./render";
2
+ export interface WorkerServerOptions extends RenderThumbnailOptions {
3
+ stdin?: NodeJS.ReadableStream;
4
+ stdout?: NodeJS.WritableStream;
5
+ }
6
+ /**
7
+ * Runs a persistent thumbnail rendering server over stdin/stdout using JSON lines.
8
+ * Designed for seamless integration with Rust subprocesses (e.g. ducpdf, ducrs).
9
+ */
10
+ export declare function runThumbnailWorkerServer(options?: WorkerServerOptions): Promise<void>;