@bitbybit-dev/jscad 1.6.2 → 1.7.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.
@@ -1,3 +1,4 @@
1
+ import type { BitbybitSettings } from "@bitbybit-dev/base";
1
2
  import { JSCADExpansions } from "./services/jscad-expansions";
2
3
  import { JSCADBooleans } from "./services/jscad-booleans";
3
4
  import { JSCADExtrusions } from "./services/jscad-extrusions";
@@ -31,7 +32,17 @@ export declare class Jscad {
31
32
  text: JSCADText;
32
33
  colors: JSCADColors;
33
34
  private point;
35
+ private currentSettings;
34
36
  constructor(jscad: typeof JSCAD);
37
+ /**
38
+ * The settings this kernel works by, such as the coordinate system its numbers are in. Assign
39
+ * them once, before the calls they apply to: `jscad.settings = { coordinateSystem }`. With a
40
+ * coordinate system, solids are written to STL, 3MF and DXF files right-handed with Z up. A
41
+ * name that is no setting, or a value its setting does not take, is refused as an `InputError`.
42
+ * The settings read back are frozen, so they change only by assigning new ones.
43
+ */
44
+ get settings(): BitbybitSettings;
45
+ set settings(settings: BitbybitSettings);
35
46
  /**
36
47
  * Turns a solid into a list of triangles, each given as three points, with the solid's own
37
48
  * transform already applied.
@@ -168,6 +179,8 @@ export declare class Jscad {
168
179
  downloadGeometry3MF(inputs: Inputs.JSCAD.DownloadGeometryDto): {
169
180
  blob: Blob;
170
181
  };
182
+ private inFileAxes;
183
+ private solidsMoved;
171
184
  private legacyPolygons;
172
185
  private asSolid;
173
186
  private getArrayDepth;
@@ -1,6 +1,7 @@
1
- import { GeometryHelper, Lists, Point, Transforms, Vector, resolveDto } from "@bitbybit-dev/base";
1
+ import { GeometryHelper, Lists, Point, Transforms, Vector, checkedSettings, resolveDto } from "@bitbybit-dev/base";
2
2
  import { MathBitByBit } from "@bitbybit-dev/base";
3
3
  import { computeVertexNormals } from "@bitbybit-dev/base/lib/api/services/helpers/mesh/mesh-normals";
4
+ import { meshMatrixInFile } from "@bitbybit-dev/base/lib/api/services/helpers/files/mesh-file-axes";
4
5
  import { JSCADExpansions } from "./services/jscad-expansions";
5
6
  import { JSCADBooleans } from "./services/jscad-booleans";
6
7
  import { JSCADExtrusions } from "./services/jscad-extrusions";
@@ -23,6 +24,7 @@ import { writeThreeMf } from "./three-mf/three-mf-writer";
23
24
  */
24
25
  export class Jscad {
25
26
  constructor(jscad) {
27
+ this.currentSettings = Object.freeze({});
26
28
  this.getArrayDepth = (value) => {
27
29
  return Array.isArray(value) ?
28
30
  1 + Math.max(...value.map(this.getArrayDepth)) :
@@ -45,6 +47,19 @@ export class Jscad {
45
47
  this.colors = new JSCADColors(jscad);
46
48
  this.jscad = jscad;
47
49
  }
50
+ /**
51
+ * The settings this kernel works by, such as the coordinate system its numbers are in. Assign
52
+ * them once, before the calls they apply to: `jscad.settings = { coordinateSystem }`. With a
53
+ * coordinate system, solids are written to STL, 3MF and DXF files right-handed with Z up. A
54
+ * name that is no setting, or a value its setting does not take, is refused as an `InputError`.
55
+ * The settings read back are frozen, so they change only by assigning new ones.
56
+ */
57
+ get settings() {
58
+ return this.currentSettings;
59
+ }
60
+ set settings(settings) {
61
+ this.currentSettings = Object.freeze(checkedSettings(settings));
62
+ }
48
63
  /**
49
64
  * Turns a solid into a list of triangles, each given as three points, with the solid's own
50
65
  * transform already applied.
@@ -239,7 +254,7 @@ export class Jscad {
239
254
  * ```
240
255
  */
241
256
  downloadSolidSTL(inputs) {
242
- const rawData = this.jscad.STLSERIALIZER.serialize({ binary: true }, inputs.mesh);
257
+ const rawData = this.jscad.STLSERIALIZER.serialize({ binary: true }, this.inFileAxes(inputs.mesh));
243
258
  const madeBlob = new Blob(rawData, { type: "application/sla" });
244
259
  return { blob: madeBlob };
245
260
  }
@@ -256,7 +271,7 @@ export class Jscad {
256
271
  * ```
257
272
  */
258
273
  downloadSolidsSTL(inputs) {
259
- const rawData = this.jscad.STLSERIALIZER.serialize({ binary: true }, ...inputs.meshes);
274
+ const rawData = this.jscad.STLSERIALIZER.serialize({ binary: true }, ...inputs.meshes.map(mesh => this.inFileAxes(mesh)));
260
275
  const madeBlob = new Blob(rawData, { type: "application/sla" });
261
276
  return { blob: madeBlob };
262
277
  }
@@ -278,7 +293,7 @@ export class Jscad {
278
293
  downloadGeometryDxf(inputs) {
279
294
  const resolved = resolveDto(Inputs.JSCAD.DownloadGeometryDto, inputs);
280
295
  const options = resolved.options ? resolved.options : {};
281
- const rawData = this.jscad.DXFSERIALIZER.serialize(options, resolved.geometry);
296
+ const rawData = this.jscad.DXFSERIALIZER.serialize(options, this.inFileAxes(resolved.geometry));
282
297
  const madeBlob = new Blob(rawData);
283
298
  return { blob: madeBlob };
284
299
  }
@@ -299,7 +314,18 @@ export class Jscad {
299
314
  */
300
315
  downloadGeometry3MF(inputs) {
301
316
  const resolved = resolveDto(Inputs.JSCAD.DownloadGeometryDto, inputs);
302
- return { blob: new Blob(writeThreeMf(this.jscad, resolved.options, resolved.geometry)) };
317
+ return { blob: new Blob(writeThreeMf(this.jscad, resolved.options, this.inFileAxes(resolved.geometry))) };
318
+ }
319
+ inFileAxes(geometry) {
320
+ const system = this.currentSettings.coordinateSystem;
321
+ const matrix = system === undefined ? undefined : meshMatrixInFile(system, true);
322
+ return matrix === undefined ? geometry : this.solidsMoved(geometry, matrix);
323
+ }
324
+ solidsMoved(geometry, matrix) {
325
+ if (Array.isArray(geometry)) {
326
+ return geometry.map(entity => this.solidsMoved(entity, matrix));
327
+ }
328
+ return this.jscad.geometries.geom3.isA(geometry) ? this.jscad.transforms.transform(matrix, geometry) : geometry;
303
329
  }
304
330
  legacyPolygons(entity) {
305
331
  const candidate = entity.toPolygons;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitbybit-dev/jscad",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "Bit By Bit Developers JSCAD based CAD Library to Program Geometry",
5
5
  "homepage": "https://docs.bitbybit.dev/classes/Bit.JSCAD",
6
6
  "main": "index.js",
@@ -54,7 +54,7 @@
54
54
  "types": "./index.d.ts",
55
55
  "type": "module",
56
56
  "dependencies": {
57
- "@bitbybit-dev/base": "1.6.2",
57
+ "@bitbybit-dev/base": "1.7.0",
58
58
  "@jscad/io-utils": "2.0.33",
59
59
  "@jscad/modeling": "2.13.0",
60
60
  "@jscad/stl-serializer": "2.1.23",