@jwc/jscad-utils 5.1.0 → 5.5.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/README.md +1 -5
- package/dist/compat.js +641 -100
- package/dist/examples/bisect.jscad +471 -93
- package/dist/examples/bisect2.jscad +2614 -0
- package/dist/examples/boxes.jscad +481 -102
- package/dist/examples/chamfer.jscad +471 -93
- package/dist/examples/fillet.jscad +471 -93
- package/dist/examples/fit.jscad +471 -93
- package/dist/examples/groups.jscad +471 -93
- package/dist/examples/midlineTo.jscad +471 -93
- package/dist/examples/parts-hexagon.jscad +471 -93
- package/dist/examples/rabett-tb.jscad +471 -93
- package/dist/examples/rabett.jscad +471 -93
- package/dist/examples/rabett2.jscad +471 -93
- package/dist/examples/rabett3.jscad +2614 -0
- package/dist/examples/retraction-test.jscad +471 -93
- package/dist/examples/size.jscad +471 -93
- package/dist/examples/snap.jscad +471 -93
- package/dist/examples/text.jscad +471 -93
- package/dist/examples/wedge.jscad +471 -93
- package/dist/index.js +638 -100
- package/package.json +8 -7
- package/src/add-prototype.js +5 -1
- package/src/boxes.js +11 -3
- package/src/compat.js +3 -0
- package/src/group.js +13 -11
- package/src/parts.js +25 -23
- package/src/util.js +239 -72
- package/src/validate.js +335 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jwc/jscad-utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Utilities for use in a jscad script.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -32,14 +32,13 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@jscad/csg": "0.7.0",
|
|
34
34
|
"@jscad/scad-api": "0.5.1",
|
|
35
|
-
"
|
|
36
|
-
"@jwc/jscad2-regl-renderer": "3.0.0",
|
|
37
|
-
"gl": "8.1.6",
|
|
38
|
-
"vitepress": "1.6.4",
|
|
39
|
-
"vitest": "4.0.18"
|
|
35
|
+
"vitepress": "1.6.4"
|
|
40
36
|
},
|
|
41
37
|
"devDependencies": {
|
|
42
38
|
"@babel/preset-env": "7.29.0",
|
|
39
|
+
"@jwc/jscad-test-utils": "3.0.1",
|
|
40
|
+
"@jwc/jscad2-regl-renderer": "3.0.0",
|
|
41
|
+
"gl": "9.0.0-rc.9",
|
|
43
42
|
"gulp": "5.0.1",
|
|
44
43
|
"gulp-debug": "5.0.1",
|
|
45
44
|
"gulp-eslint": "6.0.0",
|
|
@@ -49,7 +48,9 @@
|
|
|
49
48
|
"rollup": "2.79.2",
|
|
50
49
|
"rollup-plugin-babel": "4.4.0",
|
|
51
50
|
"rollup-plugin-commonjs": "10.1.0",
|
|
52
|
-
"rollup-plugin-node-resolve": "5.2.0"
|
|
51
|
+
"rollup-plugin-node-resolve": "5.2.0",
|
|
52
|
+
"vitepress-jsdoc": "1.0.4",
|
|
53
|
+
"vitest": "4.1.2"
|
|
53
54
|
},
|
|
54
55
|
"babel": {
|
|
55
56
|
"presets": [
|
package/src/add-prototype.js
CHANGED
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
/** @typedef {import('@jscad/csg').CSG & ExtendedCSG} CSG */
|
|
77
77
|
import { color } from './color';
|
|
78
78
|
import * as util from './util';
|
|
79
|
-
|
|
79
|
+
import { validateCSG } from './validate';
|
|
80
80
|
/**
|
|
81
81
|
* Initialize `jscad-utils` and add utilities to the `proto` object.
|
|
82
82
|
* @param {CSG} proto The global `proto` object
|
|
@@ -204,6 +204,10 @@ export default function init(proto) {
|
|
|
204
204
|
return condition ? this.subtract(util.result(this, object)) : this;
|
|
205
205
|
};
|
|
206
206
|
|
|
207
|
+
proto.prototype.validate = function validate(options) {
|
|
208
|
+
return validateCSG(this, options);
|
|
209
|
+
};
|
|
210
|
+
|
|
207
211
|
proto.prototype._translate = proto.prototype.translate;
|
|
208
212
|
|
|
209
213
|
/**
|
package/src/boxes.js
CHANGED
|
@@ -54,7 +54,7 @@ export function topMiddleBottom(box, thickness) {
|
|
|
54
54
|
* This will bisect an object using a rabett join. Returns a
|
|
55
55
|
* `group` object with `positive` and `negative` parts.
|
|
56
56
|
*
|
|
57
|
-
*
|
|
57
|
+
* 
|
|
58
58
|
* @example
|
|
59
59
|
*include('dist/jscad-utils.jscad');
|
|
60
60
|
*
|
|
@@ -93,6 +93,14 @@ export function Rabett(box, thickness, gap, height, face, options = {}) {
|
|
|
93
93
|
gap = gap || 0.25;
|
|
94
94
|
var inside = thickness - gap;
|
|
95
95
|
var outside = -thickness + gap;
|
|
96
|
+
|
|
97
|
+
var boxHeight = box.size().z;
|
|
98
|
+
if (Math.abs(height) >= boxHeight) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Rabett: height (${height}) must be less than the object height (${boxHeight})`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
// options.color = true;
|
|
97
105
|
debug('inside', inside, 'outside', outside);
|
|
98
106
|
var group = Group();
|
|
@@ -154,7 +162,7 @@ export function Rabett(box, thickness, gap, height, face, options = {}) {
|
|
|
154
162
|
* Used on a hollow object, this will rabett out the top and/or
|
|
155
163
|
* bottom of the object.
|
|
156
164
|
*
|
|
157
|
-
* 
|
|
158
166
|
*
|
|
159
167
|
* @example
|
|
160
168
|
*include('dist/jscad-utils.jscad');
|
|
@@ -312,7 +320,7 @@ export const Rectangle = function (size, thickness, cb) {
|
|
|
312
320
|
* wall thickness. This is done by reducing the object by half the
|
|
313
321
|
* thickness and subtracting the reduced version from the original object.
|
|
314
322
|
*
|
|
315
|
-
* 
|
|
316
324
|
*
|
|
317
325
|
* @param {CSG} object A CSG object
|
|
318
326
|
* @param {Number} [thickness=2] The thickness of the walls.
|
package/src/compat.js
CHANGED
|
@@ -38,6 +38,9 @@ function initJscadutils(_CSG, options = {}) {
|
|
|
38
38
|
{ enabled: [], disabled: [] }
|
|
39
39
|
);
|
|
40
40
|
|
|
41
|
+
var jscadUtilsAssertValidCSGWarnings = options.assertValidCSGWarnings || false;
|
|
42
|
+
var jscadUtilsAssertValidCSG = options.assertValidCSG || false;
|
|
43
|
+
|
|
41
44
|
// include:compat
|
|
42
45
|
// endinject
|
|
43
46
|
|
package/src/group.js
CHANGED
|
@@ -12,7 +12,9 @@ import {
|
|
|
12
12
|
unitCube,
|
|
13
13
|
zipObject
|
|
14
14
|
} from './util';
|
|
15
|
+
import { AssertValidCSG } from './validate';
|
|
15
16
|
const debug = Debug('jscadUtils:group');
|
|
17
|
+
const assertValidCSG = AssertValidCSG('group');
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* @function JsCadUtilsGroup
|
|
@@ -112,10 +114,10 @@ JsCadUtilsGroup.prototype.combine = function (
|
|
|
112
114
|
)
|
|
113
115
|
);
|
|
114
116
|
|
|
115
|
-
return g.subtractIf(
|
|
117
|
+
return assertValidCSG(g.subtractIf(
|
|
116
118
|
self.holes && Array.isArray(self.holes) ? union(self.holes) : self.holes,
|
|
117
119
|
self.holes && !options.noholes
|
|
118
|
-
);
|
|
120
|
+
), 'combine');
|
|
119
121
|
} catch (err) {
|
|
120
122
|
debug('combine error', this, pieces, options, err);
|
|
121
123
|
throw error(
|
|
@@ -188,7 +190,7 @@ JsCadUtilsGroup.prototype.clone = function (name, map) {
|
|
|
188
190
|
|
|
189
191
|
if (self.holes) {
|
|
190
192
|
group.holes = toArray(self.holes).map(function (part) {
|
|
191
|
-
return map(CSG.fromPolygons(part.toPolygons()), 'holes');
|
|
193
|
+
return assertValidCSG(map(CSG.fromPolygons(part.toPolygons()), 'holes'), 'clone');
|
|
192
194
|
});
|
|
193
195
|
}
|
|
194
196
|
return group;
|
|
@@ -217,7 +219,7 @@ JsCadUtilsGroup.prototype.rotate = function (solid, axis, angle) {
|
|
|
217
219
|
var rotationAxis = axes[axis];
|
|
218
220
|
|
|
219
221
|
self.map(function (part) {
|
|
220
|
-
return part.rotate(rotationCenter, rotationAxis, angle);
|
|
222
|
+
return assertValidCSG(part.rotate(rotationCenter, rotationAxis, angle), 'rotate');
|
|
221
223
|
});
|
|
222
224
|
|
|
223
225
|
return self;
|
|
@@ -258,7 +260,7 @@ JsCadUtilsGroup.prototype.snap = function snap(
|
|
|
258
260
|
// debug(', self);
|
|
259
261
|
var t = calcSnap(self.combine(part), to, axis, orientation, delta);
|
|
260
262
|
self.map(function (part) {
|
|
261
|
-
return part.translate(t);
|
|
263
|
+
return assertValidCSG(part.translate(t), 'snap');
|
|
262
264
|
});
|
|
263
265
|
return self;
|
|
264
266
|
} catch (err) {
|
|
@@ -298,7 +300,7 @@ JsCadUtilsGroup.prototype.align = function align(part, to, axis, delta) {
|
|
|
298
300
|
delta
|
|
299
301
|
);
|
|
300
302
|
self.map(function (part /*, name */) {
|
|
301
|
-
return part.translate(t);
|
|
303
|
+
return assertValidCSG(part.translate(t), 'align');
|
|
302
304
|
});
|
|
303
305
|
|
|
304
306
|
// if (self.holes)
|
|
@@ -370,7 +372,7 @@ JsCadUtilsGroup.prototype.connectTo = function connectTo(
|
|
|
370
372
|
debug('connectTo', matrix);
|
|
371
373
|
|
|
372
374
|
self.map(function (part) {
|
|
373
|
-
return part.transform(matrix);
|
|
375
|
+
return assertValidCSG(part.transform(matrix), 'connectTo');
|
|
374
376
|
});
|
|
375
377
|
|
|
376
378
|
return self;
|
|
@@ -392,7 +394,7 @@ JsCadUtilsGroup.prototype.midlineTo = function midlineTo(part, axis, to) {
|
|
|
392
394
|
// debug(' part, t);
|
|
393
395
|
// var t = util.calcCenterWith(self.combine(part), axis, to, delta);
|
|
394
396
|
self.map(function (part) {
|
|
395
|
-
return part.translate(t);
|
|
397
|
+
return assertValidCSG(part.translate(t), 'midlineTo');
|
|
396
398
|
});
|
|
397
399
|
|
|
398
400
|
// if (self.holes)
|
|
@@ -417,7 +419,7 @@ JsCadUtilsGroup.prototype.translate = function translate(x, y, z) {
|
|
|
417
419
|
var t = Array.isArray(x) ? x : [x, y, z];
|
|
418
420
|
debug('translate', t);
|
|
419
421
|
self.map(function (part) {
|
|
420
|
-
return part.translate(t);
|
|
422
|
+
return assertValidCSG(part.translate(t), 'translate');
|
|
421
423
|
});
|
|
422
424
|
|
|
423
425
|
// if (self.holes)
|
|
@@ -442,7 +444,7 @@ JsCadUtilsGroup.prototype.pick = function (parts, map) {
|
|
|
442
444
|
|
|
443
445
|
var g = Group();
|
|
444
446
|
p.forEach(function (name) {
|
|
445
|
-
g.add(map(CSG.fromPolygons(self.parts[name].toPolygons()), name), name);
|
|
447
|
+
g.add(assertValidCSG(map(CSG.fromPolygons(self.parts[name].toPolygons()), name), 'pick'), name);
|
|
446
448
|
});
|
|
447
449
|
return g;
|
|
448
450
|
};
|
|
@@ -473,7 +475,7 @@ parts: "${parts}"
|
|
|
473
475
|
);
|
|
474
476
|
}
|
|
475
477
|
|
|
476
|
-
a.push(map(CSG.fromPolygons(self.parts[name].toPolygons()), name));
|
|
478
|
+
a.push(assertValidCSG(map(CSG.fromPolygons(self.parts[name].toPolygons()), name), 'array'));
|
|
477
479
|
});
|
|
478
480
|
return a;
|
|
479
481
|
// } catch (err) {
|
package/src/parts.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { fromxyz, div } from './array';
|
|
3
|
-
import * as util from './util';
|
|
4
|
-
import Group from './group';
|
|
1
|
+
import { div, fromxyz } from './array';
|
|
5
2
|
import { Debug } from './debug';
|
|
3
|
+
import Group from './group';
|
|
4
|
+
import { CAG, CSG, union } from './jscad';
|
|
5
|
+
import * as util from './util';
|
|
6
|
+
import { AssertValidCSG } from './validate';
|
|
6
7
|
|
|
7
8
|
const debug = Debug('jscadUtils:parts');
|
|
9
|
+
const assertValidCSG = AssertValidCSG('parts');
|
|
8
10
|
|
|
9
11
|
export default { BBox, Cube, RoundedCube, Cylinder, Cone };
|
|
10
12
|
|
|
@@ -22,18 +24,18 @@ export function BBox(...objects) {
|
|
|
22
24
|
radius: object.size().dividedBy(2)
|
|
23
25
|
});
|
|
24
26
|
}
|
|
25
|
-
return objects.reduce(function(bbox, part) {
|
|
27
|
+
return assertValidCSG(objects.reduce(function (bbox, part) {
|
|
26
28
|
var object = bbox ? union([bbox, box(part)]) : part;
|
|
27
29
|
return box(object);
|
|
28
|
-
}, undefined);
|
|
30
|
+
}, undefined), 'BBox');
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export function Cube(width) {
|
|
32
34
|
var r = div(fromxyz(width), 2);
|
|
33
|
-
return CSG.cube({
|
|
35
|
+
return assertValidCSG(CSG.cube({
|
|
34
36
|
center: r,
|
|
35
37
|
radius: r
|
|
36
|
-
});
|
|
38
|
+
}), 'Cube');
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
// export function Sphere(diameter) {
|
|
@@ -73,7 +75,7 @@ export function RoundedCube(x, y, thickness, corner_radius) {
|
|
|
73
75
|
offset: [0, 0, thickness || 1.62]
|
|
74
76
|
});
|
|
75
77
|
|
|
76
|
-
return roundedcube;
|
|
78
|
+
return assertValidCSG(roundedcube, 'RoundedCube');
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
/**
|
|
@@ -96,7 +98,7 @@ export function Cylinder(diameter, height, options = {}) {
|
|
|
96
98
|
options
|
|
97
99
|
);
|
|
98
100
|
|
|
99
|
-
return CSG.cylinder(options);
|
|
101
|
+
return assertValidCSG(CSG.cylinder(options), 'Cylinder');
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
/**
|
|
@@ -110,7 +112,7 @@ export function Cylinder(diameter, height, options = {}) {
|
|
|
110
112
|
*/
|
|
111
113
|
export function Cone(diameter1, diameter2, height, options = {}) {
|
|
112
114
|
debug('parts.Cone', diameter1, diameter2, height, options);
|
|
113
|
-
return CSG.cylinder(
|
|
115
|
+
return assertValidCSG(CSG.cylinder(
|
|
114
116
|
Object.assign(
|
|
115
117
|
{
|
|
116
118
|
start: [0, 0, 0],
|
|
@@ -121,7 +123,7 @@ export function Cone(diameter1, diameter2, height, options = {}) {
|
|
|
121
123
|
},
|
|
122
124
|
options
|
|
123
125
|
)
|
|
124
|
-
);
|
|
126
|
+
), 'Cone');
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
/**
|
|
@@ -142,9 +144,9 @@ export function Hexagon(diameter, height) {
|
|
|
142
144
|
[radius / 2, -radius * sqrt3]
|
|
143
145
|
]);
|
|
144
146
|
|
|
145
|
-
return hex.extrude({
|
|
147
|
+
return assertValidCSG(hex.extrude({
|
|
146
148
|
offset: [0, 0, height]
|
|
147
|
-
});
|
|
149
|
+
}), 'Hexagon');
|
|
148
150
|
}
|
|
149
151
|
|
|
150
152
|
/**
|
|
@@ -162,9 +164,9 @@ export function Triangle(base, height) {
|
|
|
162
164
|
[0, Math.sin(30) * radius]
|
|
163
165
|
]);
|
|
164
166
|
|
|
165
|
-
return tri.extrude({
|
|
167
|
+
return assertValidCSG(tri.extrude({
|
|
166
168
|
offset: [0, 0, height]
|
|
167
|
-
});
|
|
169
|
+
}), 'Triangle');
|
|
168
170
|
}
|
|
169
171
|
|
|
170
172
|
/**
|
|
@@ -185,9 +187,9 @@ export function Tube(
|
|
|
185
187
|
outsideOptions,
|
|
186
188
|
insideOptions
|
|
187
189
|
) {
|
|
188
|
-
return Cylinder(outsideDiameter, height, outsideOptions).subtract(
|
|
190
|
+
return assertValidCSG(Cylinder(outsideDiameter, height, outsideOptions).subtract(
|
|
189
191
|
Cylinder(insideDiameter, height, insideOptions || outsideOptions)
|
|
190
|
-
);
|
|
192
|
+
), 'Tube');
|
|
191
193
|
}
|
|
192
194
|
|
|
193
195
|
/**
|
|
@@ -219,7 +221,7 @@ export function Board(width, height, corner_radius, thickness) {
|
|
|
219
221
|
offset: [0, 0, thickness || 1.62]
|
|
220
222
|
});
|
|
221
223
|
|
|
222
|
-
return board;
|
|
224
|
+
return assertValidCSG(board, 'Board');
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
export const Hardware = {
|
|
@@ -234,7 +236,7 @@ export const Hardware = {
|
|
|
234
236
|
}
|
|
235
237
|
},
|
|
236
238
|
|
|
237
|
-
Screw: function(head, thread, headClearSpace, options) {
|
|
239
|
+
Screw: function (head, thread, headClearSpace, options) {
|
|
238
240
|
util.depreciated(
|
|
239
241
|
'Screw',
|
|
240
242
|
false,
|
|
@@ -274,7 +276,7 @@ export const Hardware = {
|
|
|
274
276
|
* @param {number} clearLength Length of the clearance section of the head.
|
|
275
277
|
* @param {object} options Screw options include orientation and clerance scale.
|
|
276
278
|
*/
|
|
277
|
-
PanHeadScrew: function(
|
|
279
|
+
PanHeadScrew: function (
|
|
278
280
|
headDiameter,
|
|
279
281
|
headLength,
|
|
280
282
|
diameter,
|
|
@@ -307,7 +309,7 @@ export const Hardware = {
|
|
|
307
309
|
* @param {number} clearLength Length of the clearance section of the head.
|
|
308
310
|
* @param {object} options Screw options include orientation and clerance scale.
|
|
309
311
|
*/
|
|
310
|
-
HexHeadScrew: function(
|
|
312
|
+
HexHeadScrew: function (
|
|
311
313
|
headDiameter,
|
|
312
314
|
headLength,
|
|
313
315
|
diameter,
|
|
@@ -339,7 +341,7 @@ export const Hardware = {
|
|
|
339
341
|
* @param {number} clearLength clearance length
|
|
340
342
|
* @param {object} options options
|
|
341
343
|
*/
|
|
342
|
-
FlatHeadScrew: function(
|
|
344
|
+
FlatHeadScrew: function (
|
|
343
345
|
headDiameter,
|
|
344
346
|
headLength,
|
|
345
347
|
diameter,
|