@cadit-app/script-params 0.5.2 → 0.5.3
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/exporters.d.ts +107 -0
- package/dist/exporters.d.ts.map +1 -0
- package/dist/exporters.js +29 -0
- package/dist/exporters.js.map +1 -0
- package/dist/index.d.ts +9 -555
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -326
- package/dist/index.js.map +1 -1
- package/dist/params/geometry.d.ts +313 -0
- package/dist/params/geometry.d.ts.map +1 -0
- package/dist/params/geometry.js +207 -0
- package/dist/params/geometry.js.map +1 -0
- package/dist/params/index.d.ts +10 -0
- package/dist/params/index.d.ts.map +1 -0
- package/dist/params/index.js +8 -0
- package/dist/params/index.js.map +1 -0
- package/dist/params/schema.d.ts +95 -0
- package/dist/params/schema.d.ts.map +1 -0
- package/dist/params/schema.js +8 -0
- package/dist/params/schema.js.map +1 -0
- package/dist/params/types.d.ts +239 -0
- package/dist/params/types.d.ts.map +1 -0
- package/dist/params/types.js +8 -0
- package/dist/params/types.js.map +1 -0
- package/dist/script.d.ts +132 -0
- package/dist/script.d.ts.map +1 -0
- package/dist/script.js +117 -0
- package/dist/script.js.map +1 -0
- package/dist/utils.d.ts +115 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +224 -0
- package/dist/utils.js.map +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -19,333 +19,12 @@
|
|
|
19
19
|
* });
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*/
|
|
27
|
-
export function getExporterMetadata(key, exporter) {
|
|
28
|
-
return {
|
|
29
|
-
key,
|
|
30
|
-
name: exporter.name,
|
|
31
|
-
label: exporter.label,
|
|
32
|
-
description: exporter.description,
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Extract metadata array from Exporters record.
|
|
37
|
-
* Preserves the key for each exporter so it can be looked up later.
|
|
38
|
-
*/
|
|
39
|
-
export function getExportersMetadata(exporters) {
|
|
40
|
-
return Object.entries(exporters).map(([key, exporter]) => getExporterMetadata(key, exporter));
|
|
41
|
-
}
|
|
42
|
-
// =============================================================================
|
|
43
|
-
// Core Functions
|
|
44
|
-
// =============================================================================
|
|
45
|
-
/**
|
|
46
|
-
* Build default values object from a schema.
|
|
47
|
-
* @internal
|
|
48
|
-
*/
|
|
49
|
-
function buildDefaults(schema) {
|
|
50
|
-
const defaults = {};
|
|
51
|
-
for (const [key, def] of Object.entries(schema)) {
|
|
52
|
-
if (def.type === 'embedded') {
|
|
53
|
-
// For embedded params, build a nested default object
|
|
54
|
-
const embeddedDef = def;
|
|
55
|
-
defaults[key] = {
|
|
56
|
-
enabled: embeddedDef.enabled ?? false,
|
|
57
|
-
showSettings: embeddedDef.showSettings ?? false,
|
|
58
|
-
params: buildDefaults(embeddedDef.params),
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
defaults[key] = def.default;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return defaults;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Define parameters for a parametric script.
|
|
69
|
-
*
|
|
70
|
-
* Pass a config object with `params` (required), and optionally `main` and `exporters`.
|
|
71
|
-
*
|
|
72
|
-
* @example Basic usage
|
|
73
|
-
* ```typescript
|
|
74
|
-
* export default defineParams({
|
|
75
|
-
* params: {
|
|
76
|
-
* size: { type: 'number', default: 10 },
|
|
77
|
-
* },
|
|
78
|
-
* main: (params) => Manifold.cube([params.size, params.size, params.size]),
|
|
79
|
-
* });
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* @example With exporters
|
|
83
|
-
* ```typescript
|
|
84
|
-
* export default defineParams({
|
|
85
|
-
* params: {
|
|
86
|
-
* size: { type: 'number', default: 10 },
|
|
87
|
-
* },
|
|
88
|
-
* exporters: {
|
|
89
|
-
* svg: { name: 'SVG', export: (p) => svgExport(p) },
|
|
90
|
-
* png: { name: 'PNG', export: (p) => pngExport(p) },
|
|
91
|
-
* },
|
|
92
|
-
* main: (params) => Manifold.cube([params.size, params.size, params.size]),
|
|
93
|
-
* });
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* @example Without main (add main separately with createMain)
|
|
97
|
-
* ```typescript
|
|
98
|
-
* const script = defineParams({
|
|
99
|
-
* params: { size: { type: 'number', default: 10 } },
|
|
100
|
-
* exporters: { svg: svgExporter },
|
|
101
|
-
* });
|
|
102
|
-
* export default createMain(script, myMainFunction);
|
|
103
|
-
* ```
|
|
104
|
-
*/
|
|
105
|
-
export function defineParams(config) {
|
|
106
|
-
const { params: schema, main, exporters } = config;
|
|
107
|
-
const defaults = buildDefaults(schema);
|
|
108
|
-
const scriptModule = ((inputParams) => {
|
|
109
|
-
if (!main) {
|
|
110
|
-
throw new Error('This script does not have a main function. Use createMain() to add one.');
|
|
111
|
-
}
|
|
112
|
-
const merged = { ...defaults, ...inputParams };
|
|
113
|
-
return main(merged);
|
|
114
|
-
});
|
|
115
|
-
scriptModule.params = schema;
|
|
116
|
-
if (exporters) {
|
|
117
|
-
scriptModule.exporters = exporters;
|
|
118
|
-
}
|
|
119
|
-
return scriptModule;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Create an executable script module by adding a main function.
|
|
123
|
-
*
|
|
124
|
-
* Use this when you want to define your main function separately from the params,
|
|
125
|
-
* for example when the main is complex or needs to be tested independently.
|
|
126
|
-
*
|
|
127
|
-
* Preserves any exporters defined on the original ScriptModule.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```typescript
|
|
131
|
-
* const script = defineParams({
|
|
132
|
-
* params: { size: { type: 'number', default: 10 } },
|
|
133
|
-
* exporters: { svg: svgExporter },
|
|
134
|
-
* });
|
|
135
|
-
*
|
|
136
|
-
* export default createMain(script, (p) => Manifold.cube([p.size, ...]));
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
export function createMain(scriptModule, main) {
|
|
140
|
-
const defaults = buildDefaults(scriptModule.params);
|
|
141
|
-
const newModule = ((inputParams) => {
|
|
142
|
-
const merged = { ...defaults, ...inputParams };
|
|
143
|
-
return main(merged);
|
|
144
|
-
});
|
|
145
|
-
newModule.params = scriptModule.params;
|
|
146
|
-
if (scriptModule.exporters) {
|
|
147
|
-
newModule.exporters = scriptModule.exporters;
|
|
148
|
-
}
|
|
149
|
-
return newModule;
|
|
150
|
-
}
|
|
22
|
+
// Geometry utility functions
|
|
23
|
+
export { isPathSegment, getPathPointXY, samplePathTo2D, hasShape3dMesh, getShape3dMeshOptions, toManifold, } from './params';
|
|
24
|
+
export { getExporterMetadata, getExportersMetadata, } from './exporters';
|
|
25
|
+
export { defineParams, createMain, } from './script';
|
|
151
26
|
// =============================================================================
|
|
152
27
|
// Utility Functions
|
|
153
28
|
// =============================================================================
|
|
154
|
-
|
|
155
|
-
* Convert a single param def to ObjectMakerParam format.
|
|
156
|
-
* Handles special cases like embedded params that need recursive conversion.
|
|
157
|
-
* @internal
|
|
158
|
-
*/
|
|
159
|
-
function convertParamDef(name, def) {
|
|
160
|
-
const base = {
|
|
161
|
-
name,
|
|
162
|
-
type: def.type,
|
|
163
|
-
caption: def.label ?? name,
|
|
164
|
-
};
|
|
165
|
-
// Copy common properties
|
|
166
|
-
if ('default' in def) {
|
|
167
|
-
base.initial = def.default;
|
|
168
|
-
}
|
|
169
|
-
if ('min' in def)
|
|
170
|
-
base.min = def.min;
|
|
171
|
-
if ('max' in def)
|
|
172
|
-
base.max = def.max;
|
|
173
|
-
if ('step' in def)
|
|
174
|
-
base.step = def.step;
|
|
175
|
-
if ('maxLength' in def)
|
|
176
|
-
base.maxLength = def.maxLength;
|
|
177
|
-
// Handle type-specific conversions
|
|
178
|
-
switch (def.type) {
|
|
179
|
-
case 'choice': {
|
|
180
|
-
// Handle both simple string options and { value, label } objects
|
|
181
|
-
const options = def.options;
|
|
182
|
-
const values = [];
|
|
183
|
-
const captions = [];
|
|
184
|
-
let hasCaptions = false;
|
|
185
|
-
for (const opt of options) {
|
|
186
|
-
if (typeof opt === 'string') {
|
|
187
|
-
values.push(opt);
|
|
188
|
-
captions.push(opt); // Use value as caption if no label
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
values.push(opt.value);
|
|
192
|
-
captions.push(opt.label ?? opt.value);
|
|
193
|
-
if (opt.label)
|
|
194
|
-
hasCaptions = true;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
base.values = values;
|
|
198
|
-
// Only set captions if at least one option has a label
|
|
199
|
-
if (hasCaptions) {
|
|
200
|
-
base.captions = captions;
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
case 'buttonGrid': {
|
|
205
|
-
// Convert buttonGrid options to ObjectMakerButtonGridParam format
|
|
206
|
-
base.options = def.options.map((opt) => ({
|
|
207
|
-
value: opt.value,
|
|
208
|
-
image: opt.image,
|
|
209
|
-
caption: opt.caption,
|
|
210
|
-
}));
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
case 'embedded': {
|
|
214
|
-
// Convert embedded params recursively
|
|
215
|
-
const embeddedDef = def;
|
|
216
|
-
base.parameters = schemaToArray(embeddedDef.params);
|
|
217
|
-
base.enabled = embeddedDef.enabled ?? false;
|
|
218
|
-
base.showSettings = embeddedDef.showSettings ?? false;
|
|
219
|
-
// Propagate npm package for resolving relative asset URLs
|
|
220
|
-
if (embeddedDef.npmPackage) {
|
|
221
|
-
base.npmPackage = embeddedDef.npmPackage;
|
|
222
|
-
}
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return base;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Convert an object-based schema to array format for legacy UI compatibility.
|
|
230
|
-
*
|
|
231
|
-
* This function transforms the script-params schema format into the
|
|
232
|
-
* ObjectMakerParam[] format expected by the Core UI.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```typescript
|
|
236
|
-
* const schema = { size: { type: 'number', default: 10 } };
|
|
237
|
-
* const array = schemaToArray(schema);
|
|
238
|
-
* // [{ name: 'size', type: 'number', initial: 10, caption: 'size' }]
|
|
239
|
-
* ```
|
|
240
|
-
*/
|
|
241
|
-
export function schemaToArray(schema) {
|
|
242
|
-
return Object.entries(schema).map(([name, def]) => convertParamDef(name, def));
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Extract defaults from a schema as a plain object.
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* ```typescript
|
|
249
|
-
* const schema = {
|
|
250
|
-
* size: { type: 'number', default: 10 },
|
|
251
|
-
* label: { type: 'text', default: 'Hi' },
|
|
252
|
-
* };
|
|
253
|
-
* const defaults = getDefaults(schema);
|
|
254
|
-
* // { size: 10, label: 'Hi' }
|
|
255
|
-
* ```
|
|
256
|
-
*/
|
|
257
|
-
export function getDefaults(schema) {
|
|
258
|
-
return buildDefaults(schema);
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Check if a module export uses the defineParams format.
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```typescript
|
|
265
|
-
* const mod = await import('./script.ts');
|
|
266
|
-
* if (isScriptModule(mod.default)) {
|
|
267
|
-
* const result = mod.default({ size: 20 });
|
|
268
|
-
* }
|
|
269
|
-
* ```
|
|
270
|
-
*/
|
|
271
|
-
export function isScriptModule(value) {
|
|
272
|
-
return (typeof value === 'function' &&
|
|
273
|
-
'params' in value &&
|
|
274
|
-
typeof value.params === 'object' &&
|
|
275
|
-
value.params !== null);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Extract the parameter schema from a ScriptModule.
|
|
279
|
-
* Useful for embedding another script's parameters.
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* ```typescript
|
|
283
|
-
* import qrCodeScript from './qr-code';
|
|
284
|
-
*
|
|
285
|
-
* const qrParams = getParams(qrCodeScript);
|
|
286
|
-
*
|
|
287
|
-
* // Use in embedded param definition
|
|
288
|
-
* export default defineParams({
|
|
289
|
-
* params: {
|
|
290
|
-
* qrCode: {
|
|
291
|
-
* type: 'embedded',
|
|
292
|
-
* params: getParams(qrCodeScript),
|
|
293
|
-
* enabled: false,
|
|
294
|
-
* },
|
|
295
|
-
* },
|
|
296
|
-
* });
|
|
297
|
-
* ```
|
|
298
|
-
*/
|
|
299
|
-
export function getParams(source) {
|
|
300
|
-
return source.params;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Extract exporters from a ScriptModule.
|
|
304
|
-
* Returns an empty object if the module has no exporters.
|
|
305
|
-
*
|
|
306
|
-
* @example
|
|
307
|
-
* ```typescript
|
|
308
|
-
* import qrCodeScript from './qr-code';
|
|
309
|
-
*
|
|
310
|
-
* const exporters = getExporters(qrCodeScript);
|
|
311
|
-
* // { svg: { name: 'SVG', ... }, png: { name: 'PNG', ... } }
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
|
-
export function getExporters(source) {
|
|
315
|
-
return source.exporters ?? {};
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Helper to create an embedded param definition with npm package metadata.
|
|
319
|
-
* This enables relative asset paths (like buttonGrid images) to be resolved
|
|
320
|
-
* correctly when embedding another npm package's parameters.
|
|
321
|
-
*
|
|
322
|
-
* @param npmPackage - The npm package name (e.g., '@cadit-app/qr-code')
|
|
323
|
-
* @param embeddedDef - The embedded param definition
|
|
324
|
-
* @returns The embedded definition with npmPackage attached
|
|
325
|
-
*
|
|
326
|
-
* @example
|
|
327
|
-
* ```typescript
|
|
328
|
-
* import qrCodeModule from '@cadit-app/qr-code';
|
|
329
|
-
* import { defineParams, embedParams } from '@cadit-app/script-params';
|
|
330
|
-
*
|
|
331
|
-
* export default defineParams({
|
|
332
|
-
* params: {
|
|
333
|
-
* qrCodeSettings: embedParams('@cadit-app/qr-code', {
|
|
334
|
-
* type: 'embedded',
|
|
335
|
-
* label: 'QR Code (Optional)',
|
|
336
|
-
* params: qrCodeModule.params,
|
|
337
|
-
* enabled: false,
|
|
338
|
-
* showSettings: false,
|
|
339
|
-
* }),
|
|
340
|
-
* },
|
|
341
|
-
* main: (params) => { ... },
|
|
342
|
-
* });
|
|
343
|
-
* ```
|
|
344
|
-
*/
|
|
345
|
-
export function embedParams(npmPackage, embeddedDef) {
|
|
346
|
-
return {
|
|
347
|
-
...embeddedDef,
|
|
348
|
-
npmPackage,
|
|
349
|
-
};
|
|
350
|
-
}
|
|
29
|
+
export { schemaToArray, getDefaults, isScriptModule, getParams, getExporters, embedParams, } from './utils';
|
|
351
30
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA+DH,6BAA6B;AAC7B,OAAO,EACL,aAAa,EACb,cAAc,EACd,cAAc,EACd,cAAc,EACd,qBAAqB,EACrB,UAAU,GACX,MAAM,UAAU,CAAC;AAalB,OAAO,EACL,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,aAAa,CAAC;AAYrB,OAAO,EACL,YAAY,EACZ,UAAU,GACX,MAAM,UAAU,CAAC;AAElB,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF,OAAO,EACL,aAAa,EACb,WAAW,EACX,cAAc,EACd,SAAS,EACT,YAAY,EACZ,WAAW,GACZ,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geometry-related parameter types for CADit parametric scripts.
|
|
3
|
+
*
|
|
4
|
+
* This module defines the shape2d and shape3d parameter types and their
|
|
5
|
+
* associated value types, as well as utility functions for working with
|
|
6
|
+
* path geometry (bezier curves, point extraction, etc.).
|
|
7
|
+
*/
|
|
8
|
+
import type { ParamDefBase } from './types';
|
|
9
|
+
/**
|
|
10
|
+
* A 2D point with x, y coordinates.
|
|
11
|
+
*/
|
|
12
|
+
export interface Point2D {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A 3D point with x, y, z coordinates.
|
|
18
|
+
*/
|
|
19
|
+
export interface Point3D {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
z: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A bezier segment includes the main point and optional control handles.
|
|
26
|
+
* Used for smooth curves in 2D paths.
|
|
27
|
+
*
|
|
28
|
+
* Handles are RELATIVE to the point (like Paper.js):
|
|
29
|
+
* - handleIn: Controls the curve coming INTO this point from the previous point
|
|
30
|
+
* - handleOut: Controls the curve going OUT of this point to the next point
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // A point with symmetric handles for a smooth curve
|
|
35
|
+
* const smoothPoint: PathSegment = {
|
|
36
|
+
* point: { x: 100, y: 100 },
|
|
37
|
+
* handleIn: { x: -20, y: 0 }, // handle comes from left
|
|
38
|
+
* handleOut: { x: 20, y: 0 }, // handle goes to right
|
|
39
|
+
* };
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export interface PathSegment {
|
|
43
|
+
/** The main anchor point position */
|
|
44
|
+
point: Point2D;
|
|
45
|
+
/** Control handle for incoming curve (relative to point) */
|
|
46
|
+
handleIn?: Point2D;
|
|
47
|
+
/** Control handle for outgoing curve (relative to point) */
|
|
48
|
+
handleOut?: Point2D;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A path point can be either a simple point or a bezier segment with handles.
|
|
52
|
+
* For 2D operations, Point2D (x, y only) is also valid.
|
|
53
|
+
*/
|
|
54
|
+
export type PathPoint = Point2D | Point3D | PathSegment;
|
|
55
|
+
/**
|
|
56
|
+
* A path in a doodle shape.
|
|
57
|
+
* Contains the main points of the path and any holes within it.
|
|
58
|
+
*/
|
|
59
|
+
export interface DoodlePath {
|
|
60
|
+
/**
|
|
61
|
+
* Array of points defining the path outline.
|
|
62
|
+
* Points are normalized with counter-clockwise winding order for CrossSection compatibility.
|
|
63
|
+
*/
|
|
64
|
+
points: PathPoint[];
|
|
65
|
+
/**
|
|
66
|
+
* Optional holes within this path, each defined as an array of points.
|
|
67
|
+
* Holes are normalized with clockwise winding order (opposite of outer contour).
|
|
68
|
+
*/
|
|
69
|
+
holes?: PathPoint[][];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Serialized shape data structure.
|
|
73
|
+
* Contains the geometry information for a 2D shape.
|
|
74
|
+
*/
|
|
75
|
+
export interface SerializedShapeData {
|
|
76
|
+
/** Array of paths that make up the shape. */
|
|
77
|
+
doodlePaths: DoodlePath[];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The runtime value of a shape2d parameter.
|
|
81
|
+
* Contains the shape ID and optionally the serialized geometry data.
|
|
82
|
+
*/
|
|
83
|
+
export interface Shape2dValue {
|
|
84
|
+
/** The ID of the selected shape in the CADit scene. */
|
|
85
|
+
shapeId: string;
|
|
86
|
+
/** The serialized shape geometry, if available. */
|
|
87
|
+
shapeData?: SerializedShapeData;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Shape2d parameter - allows selecting a 2D shape from the CADit scene.
|
|
91
|
+
* The selected shape's geometry can be used as input for the script.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* export default defineParams({
|
|
96
|
+
* params: {
|
|
97
|
+
* inputShape: {
|
|
98
|
+
* type: 'shape2d',
|
|
99
|
+
* label: 'Select a 2D shape',
|
|
100
|
+
* },
|
|
101
|
+
* },
|
|
102
|
+
* main: (params) => {
|
|
103
|
+
* if (params.inputShape?.shapeData) {
|
|
104
|
+
* // Use the shape data to create geometry
|
|
105
|
+
* const paths = params.inputShape.shapeData.doodlePaths;
|
|
106
|
+
* // ...
|
|
107
|
+
* }
|
|
108
|
+
* return Manifold.cube([10, 10, 10]); // fallback
|
|
109
|
+
* },
|
|
110
|
+
* });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
export interface Shape2dParamDef extends ParamDefBase {
|
|
114
|
+
type: 'shape2d';
|
|
115
|
+
/** Optional default value (typically null/undefined - user must select). */
|
|
116
|
+
default?: Shape2dValue | null;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A 3D triangle represented as 3 vertex indices.
|
|
120
|
+
*/
|
|
121
|
+
export interface Triangle {
|
|
122
|
+
/** Indices into the vertices array */
|
|
123
|
+
indices: [number, number, number];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Serialized 3D mesh data structure.
|
|
127
|
+
* Contains vertex positions, triangle indices, and optional normal data.
|
|
128
|
+
*/
|
|
129
|
+
export interface SerializedMeshData {
|
|
130
|
+
/**
|
|
131
|
+
* Flat array of vertex positions [x0, y0, z0, x1, y1, z1, ...].
|
|
132
|
+
* Every 3 consecutive values represent one vertex.
|
|
133
|
+
*/
|
|
134
|
+
vertices: number[];
|
|
135
|
+
/**
|
|
136
|
+
* Flat array of triangle indices [v0, v1, v2, v3, v4, v5, ...].
|
|
137
|
+
* Every 3 consecutive values represent one triangle.
|
|
138
|
+
*/
|
|
139
|
+
triangles: number[];
|
|
140
|
+
/**
|
|
141
|
+
* Optional flat array of vertex normals [nx0, ny0, nz0, nx1, ny1, nz1, ...].
|
|
142
|
+
* Same length as vertices array.
|
|
143
|
+
*/
|
|
144
|
+
normals?: number[];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The runtime value of a shape3d parameter.
|
|
148
|
+
* Contains the 3D object ID and optionally the serialized mesh data.
|
|
149
|
+
*/
|
|
150
|
+
export interface Shape3dValue {
|
|
151
|
+
/** The ID of the selected 3D object in the CADit scene. */
|
|
152
|
+
objectId: string;
|
|
153
|
+
/** The serialized mesh geometry, if available. */
|
|
154
|
+
meshData?: SerializedMeshData;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Shape3d parameter - allows selecting a 3D object from the CADit scene.
|
|
158
|
+
* The selected object's mesh geometry can be used as input for the script.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* export default defineParams({
|
|
163
|
+
* params: {
|
|
164
|
+
* inputMesh: {
|
|
165
|
+
* type: 'shape3d',
|
|
166
|
+
* label: 'Select a 3D object',
|
|
167
|
+
* },
|
|
168
|
+
* },
|
|
169
|
+
* main: (params, manifold) => {
|
|
170
|
+
* if (params.inputMesh?.meshData) {
|
|
171
|
+
* // Use the mesh data to create geometry
|
|
172
|
+
* const { vertices, triangles } = params.inputMesh.meshData;
|
|
173
|
+
* const mesh = new manifold.Mesh({ numProp: 3, vertProperties: vertices, triVerts: triangles });
|
|
174
|
+
* return new manifold.Manifold(mesh);
|
|
175
|
+
* }
|
|
176
|
+
* return manifold.cube([10, 10, 10]); // fallback
|
|
177
|
+
* },
|
|
178
|
+
* });
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export interface Shape3dParamDef extends ParamDefBase {
|
|
182
|
+
type: 'shape3d';
|
|
183
|
+
/** Optional default value (typically null/undefined - user must select). */
|
|
184
|
+
default?: Shape3dValue | null;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Type guard to check if a path point is a bezier segment with handles.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* for (const point of path.points) {
|
|
192
|
+
* if (isPathSegment(point)) {
|
|
193
|
+
* // point is PathSegment with handleIn/handleOut
|
|
194
|
+
* console.log('Bezier point:', point.point, point.handleIn, point.handleOut);
|
|
195
|
+
* } else {
|
|
196
|
+
* // point is Point3D (simple point)
|
|
197
|
+
* console.log('Simple point:', point.x, point.y);
|
|
198
|
+
* }
|
|
199
|
+
* }
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export declare function isPathSegment(point: PathPoint): point is PathSegment;
|
|
203
|
+
/**
|
|
204
|
+
* Get the x, y coordinates from a path point.
|
|
205
|
+
* Works for both simple Point3D and PathSegment.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const points2D = path.points.map(p => {
|
|
210
|
+
* const { x, y } = getPathPointXY(p);
|
|
211
|
+
* return [x, y];
|
|
212
|
+
* });
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export declare function getPathPointXY(point: PathPoint): Point2D;
|
|
216
|
+
/**
|
|
217
|
+
* Convert a path with bezier segments to simple 2D points.
|
|
218
|
+
* This samples the bezier curves at regular intervals.
|
|
219
|
+
*
|
|
220
|
+
* @param points - Array of path points (can include bezier segments)
|
|
221
|
+
* @param samplesPerSegment - Number of samples per bezier segment (default: 8)
|
|
222
|
+
* @returns Array of [x, y] tuples suitable for CrossSection
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* const path = inputShape.shapeData.doodlePaths[0];
|
|
227
|
+
* const points2D = samplePathTo2D(path.points);
|
|
228
|
+
* const crossSection = Module.CrossSection(points2D);
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export declare function samplePathTo2D(points: PathPoint[], samplesPerSegment?: number): [number, number][];
|
|
232
|
+
/**
|
|
233
|
+
* Interface for the Mesh constructor options expected by manifold-3d.
|
|
234
|
+
*/
|
|
235
|
+
export interface MeshOptions {
|
|
236
|
+
numProp: number;
|
|
237
|
+
vertProperties: Float32Array;
|
|
238
|
+
triVerts: Uint32Array;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Type guard to check if a Shape3dValue has valid mesh data.
|
|
242
|
+
* Use this to narrow the type before accessing meshData properties.
|
|
243
|
+
*
|
|
244
|
+
* @param shape3d - The Shape3dValue to check (may be null/undefined)
|
|
245
|
+
* @returns True if the shape has valid mesh data with vertices and triangles
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* import { Manifold, Mesh } from '@cadit-app/manifold-3d/manifoldCAD';
|
|
250
|
+
* import { hasShape3dMesh, getShape3dMeshOptions } from '@cadit-app/script-params';
|
|
251
|
+
*
|
|
252
|
+
* if (hasShape3dMesh(params.inputMesh)) {
|
|
253
|
+
* const manifold = new Manifold(new Mesh(getShape3dMeshOptions(params.inputMesh)));
|
|
254
|
+
* // use manifold...
|
|
255
|
+
* }
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
export declare function hasShape3dMesh(shape3d: Shape3dValue | null | undefined): shape3d is Shape3dValue & {
|
|
259
|
+
meshData: SerializedMeshData;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Get mesh options from a Shape3dValue for use with Manifold's Mesh constructor.
|
|
263
|
+
*
|
|
264
|
+
* IMPORTANT: Always call hasShape3dMesh() first to validate the shape,
|
|
265
|
+
* or use toManifold() for a simpler one-liner.
|
|
266
|
+
*
|
|
267
|
+
* @param shape3d - A Shape3dValue that has valid meshData (use hasShape3dMesh to check)
|
|
268
|
+
* @returns MeshOptions ready to pass to `new Mesh(options)`
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* import { Manifold, Mesh } from '@cadit-app/manifold-3d/manifoldCAD';
|
|
273
|
+
* import { hasShape3dMesh, getShape3dMeshOptions } from '@cadit-app/script-params';
|
|
274
|
+
*
|
|
275
|
+
* if (hasShape3dMesh(baseShape)) {
|
|
276
|
+
* const opts = getShape3dMeshOptions(baseShape);
|
|
277
|
+
* const manifold = new Manifold(new Mesh(opts));
|
|
278
|
+
* }
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
export declare function getShape3dMeshOptions(shape3d: Shape3dValue & {
|
|
282
|
+
meshData: SerializedMeshData;
|
|
283
|
+
}): MeshOptions;
|
|
284
|
+
/**
|
|
285
|
+
* Convert a Shape3dValue to a Manifold in a single expression.
|
|
286
|
+
*
|
|
287
|
+
* This is a convenience function that combines hasShape3dMesh check and
|
|
288
|
+
* Manifold construction. Pass your Mesh and Manifold constructors.
|
|
289
|
+
*
|
|
290
|
+
* Note: We can't import Manifold/Mesh directly in script-params because
|
|
291
|
+
* manifold-3d requires async WASM initialization. The worker pre-initializes
|
|
292
|
+
* it and exposes the classes, which you import in your script.
|
|
293
|
+
*
|
|
294
|
+
* @param shape3d - The Shape3dValue from a shape3d parameter (may be null)
|
|
295
|
+
* @param Mesh - The Mesh constructor from manifold-3d
|
|
296
|
+
* @param Manifold - The Manifold constructor from manifold-3d
|
|
297
|
+
* @returns A Manifold object, or null if the shape3d is invalid
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```typescript
|
|
301
|
+
* import { Manifold, Mesh } from '@cadit-app/manifold-3d/manifoldCAD';
|
|
302
|
+
* import { toManifold } from '@cadit-app/script-params';
|
|
303
|
+
*
|
|
304
|
+
* const manifoldA = toManifold(params.baseShape, Mesh, Manifold);
|
|
305
|
+
* const manifoldB = toManifold(params.sweepShape, Mesh, Manifold);
|
|
306
|
+
*
|
|
307
|
+
* if (manifoldA && manifoldB) {
|
|
308
|
+
* return manifoldA.add(manifoldB);
|
|
309
|
+
* }
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
export declare function toManifold<TMesh, TManifold>(shape3d: Shape3dValue | null | undefined, Mesh: new (options: MeshOptions) => TMesh, Manifold: new (mesh: TMesh) => TManifold): TManifold | null;
|
|
313
|
+
//# sourceMappingURL=geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../../src/params/geometry.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