@rulecad/shapes 0.2.1 → 0.4.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.
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Closed loops (solid on the left) as one JSCAD 2D shape.
3
+ * @param {[number, number][][]} loops
4
+ */
5
+ export function fromOutlines(loops: [number, number][][]): jscad.geometries.geom2.Geom2;
6
+ /**
7
+ * A region of a problem (a circle, a centred rectangle or a polygon) as a JSCAD 2D shape.
8
+ * @param {{ at?: [number, number], r?: number, size?: [number, number], points?: [number, number][] }} r
9
+ * @param {number} [segments]
10
+ */
11
+ export function regionShape(r: {
12
+ at?: [number, number];
13
+ r?: number;
14
+ size?: [number, number];
15
+ points?: [number, number][];
16
+ }, segments?: number): jscad.geometries.geom2.Geom2;
17
+ /**
18
+ * An optimised result's outline, its keep regions added and avoid regions cut exactly.
19
+ * @param {{ outlines: [number, number][][], regions: { keep: object[], avoid: object[] } }} result
20
+ * @param {{ segments?: number }} [options]
21
+ */
22
+ export function optimisedOutline(result: {
23
+ outlines: [number, number][][];
24
+ regions: {
25
+ keep: object[];
26
+ avoid: object[];
27
+ };
28
+ }, { segments }?: {
29
+ segments?: number;
30
+ }): jscad.geometries.geom2.Geom2;
31
+ /**
32
+ * A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
33
+ * null.
34
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
35
+ * @param {number} [segments]
36
+ */
37
+ export function solidRegion(r: {
38
+ at?: number[];
39
+ r?: number;
40
+ size?: number[];
41
+ length?: number;
42
+ axis?: string;
43
+ teardrop?: "-z" | "+z";
44
+ part?: string;
45
+ }, segments?: number): jscad.geometries.geom3.Geom3;
46
+ /**
47
+ * A 3D optimised result's surface as a solid, its keep regions added and avoid regions cut exactly.
48
+ * @param {{ mesh: { vertices: number[][], faces: number[][] }, regions: { keep: object[], avoid: object[] } }} result
49
+ * @param {{ ops?: { union: Function, subtract: Function }, segments?: number }} [options] ops: v.ops (Manifold, ideally)
50
+ */
51
+ export function optimisedSolid(result: {
52
+ mesh: {
53
+ vertices: number[][];
54
+ faces: number[][];
55
+ };
56
+ regions: {
57
+ keep: object[];
58
+ avoid: object[];
59
+ };
60
+ }, { ops, segments }?: {
61
+ ops?: {
62
+ union: Function;
63
+ subtract: Function;
64
+ };
65
+ segments?: number;
66
+ }): jscad.geometries.geom3.Geom3;
67
+ import jscad from '@jscad/modeling';
package/optimised.mjs ADDED
@@ -0,0 +1,85 @@
1
+ // Optimised layouts as shapes: the outlines @rulecad/topopt finds (v.problems[name] in a design, from cad optimise),
2
+ // as a JSCAD 2D shape to extrude. Outlines are closed loops, solid on the left (outer edges counter-clockwise, holes
3
+ // clockwise), so they make one shape with its holes as they are. The problem's keep regions are added back exactly
4
+ // and its avoid regions cut exactly, so bolt holes and the bosses round them come out true to size, not stepped.
5
+ // fromOutlines(loops) the loops as one 2D shape
6
+ // optimisedOutline(result, { segments }) a 2D result's outline with its keep regions added and avoid regions cut
7
+ // optimisedSolid(result, { ops }) a 3D result's surface as a solid, its keep regions added and avoid regions
8
+ // cut (boxes, cylinders and balls; a part kept is the design's to add). The
9
+ // mesh is big: pass v.ops, with the object's engine set to manifold
10
+ import jscad from '@jscad/modeling';
11
+
12
+ const { geom2 } = jscad.geometries, { circle, rectangle, polygon, polyhedron, cuboid, cylinder, sphere } = jscad.primitives, { union, subtract } = jscad.booleans;
13
+ const { rotateX, rotateY, rotateZ, translate } = jscad.transforms, { hull } = jscad.hulls;
14
+
15
+ /**
16
+ * Closed loops (solid on the left) as one JSCAD 2D shape.
17
+ * @param {[number, number][][]} loops
18
+ */
19
+ export function fromOutlines(loops) {
20
+ const sides = [];
21
+ for (const l of loops) for (let i = 0; i < l.length; i++) sides.push([l[i], l[(i + 1) % l.length]]);
22
+ return geom2.create(sides);
23
+ }
24
+
25
+ /**
26
+ * A region of a problem (a circle, a centred rectangle or a polygon) as a JSCAD 2D shape.
27
+ * @param {{ at?: [number, number], r?: number, size?: [number, number], points?: [number, number][] }} r
28
+ * @param {number} [segments]
29
+ */
30
+ export function regionShape(r, segments = 48) {
31
+ if (r.points) return polygon({ points: r.points });
32
+ if (r.size) return rectangle({ center: r.at, size: r.size });
33
+ return circle({ center: r.at, radius: r.r, segments });
34
+ }
35
+
36
+ /**
37
+ * An optimised result's outline, its keep regions added and avoid regions cut exactly.
38
+ * @param {{ outlines: [number, number][][], regions: { keep: object[], avoid: object[] } }} result
39
+ * @param {{ segments?: number }} [options]
40
+ */
41
+ export function optimisedOutline(result, { segments = 48 } = {}) {
42
+ let shape = fromOutlines(result.outlines);
43
+ if (result.regions.keep.length) shape = union(shape, ...result.regions.keep.map((r) => regionShape(r, segments)));
44
+ if (result.regions.avoid.length) shape = subtract(shape, ...result.regions.avoid.map((r) => regionShape(r, segments)));
45
+ return shape;
46
+ }
47
+
48
+ /**
49
+ * A 3D region (a box, a cylinder along x, y or z, a teardrop, or a ball) as a JSCAD solid; a part's mesh region gives
50
+ * null.
51
+ * @param {{ at?: number[], r?: number, size?: number[], length?: number, axis?: string, teardrop?: '-z' | '+z', part?: string }} r
52
+ * @param {number} [segments]
53
+ */
54
+ export function solidRegion(r, segments = 48) {
55
+ if (r.part || r.triangles) return null;
56
+ if (r.size) return cuboid({ center: r.at, size: r.size });
57
+ if (r.length != null) {
58
+ let c = cylinder({ radius: r.r, height: r.length, segments });
59
+ // A teardrop (along x or y): the cylinder hulled with a thin ridge r√2 from its axis, below it ('-z', a boss) or
60
+ // above it ('+z', a hole), so that side is 45° (turned below, +x becomes -z)
61
+ // The ridge: a thin slice of a square turned 45° at each end, its far corner a little past r√2: a round of
62
+ // segments is a little inside its circle, so the faces from it to a point at exactly r√2 would be a touch flatter
63
+ // than 45°
64
+ const edge = 0.05, slice = 1e-3, reach = (r.teardrop === '+z' ? -1 : 1) * (r.r * Math.SQRT2 / Math.cos(Math.PI / segments) - edge / Math.SQRT2);
65
+ const end = (z) => translate([reach, 0, z], rotateZ(Math.PI / 4, cuboid({ size: [edge, edge, slice] })));
66
+ if (r.teardrop && r.axis !== 'z') c = hull(c, end((slice - r.length) / 2), end((r.length - slice) / 2));
67
+ const turned = r.axis === 'x' ? rotateY(Math.PI / 2, c) : r.axis === 'y' ? rotateZ(Math.PI / 2, rotateY(Math.PI / 2, c)) : c;
68
+ return translate(r.at, turned);
69
+ }
70
+ return sphere({ center: r.at, radius: r.r, segments });
71
+ }
72
+
73
+ /**
74
+ * A 3D optimised result's surface as a solid, its keep regions added and avoid regions cut exactly.
75
+ * @param {{ mesh: { vertices: number[][], faces: number[][] }, regions: { keep: object[], avoid: object[] } }} result
76
+ * @param {{ ops?: { union: Function, subtract: Function }, segments?: number }} [options] ops: v.ops (Manifold, ideally)
77
+ */
78
+ export function optimisedSolid(result, { ops, segments = 48 } = {}) {
79
+ const o = ops || { union, subtract };
80
+ let solid = polyhedron({ points: result.mesh.vertices, faces: result.mesh.faces, orientation: 'outward' });
81
+ const keep = result.regions.keep.map((r) => solidRegion(r, segments)).filter(Boolean), avoid = result.regions.avoid.map((r) => solidRegion(r, segments)).filter(Boolean);
82
+ if (keep.length) solid = o.union(solid, ...keep);
83
+ if (avoid.length) solid = o.subtract(solid, ...avoid);
84
+ return solid;
85
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/shapes",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Shape helpers for printed JSCAD designs: text that prints cleanly, anchors on a face, clean local cuts, and enclosure pieces (shells, lids, rails, print-safe holes).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,9 @@
17
17
  "features.mjs",
18
18
  "features.d.mts",
19
19
  "ops.mjs",
20
- "ops.d.mts"
20
+ "ops.d.mts",
21
+ "optimised.mjs",
22
+ "optimised.d.mts"
21
23
  ],
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -40,7 +42,7 @@
40
42
  "test": "node --test"
41
43
  },
42
44
  "dependencies": {
43
- "@rulecad/variables": "^0.1.0"
45
+ "@rulecad/variables": "^0.2.0"
44
46
  },
45
47
  "peerDependencies": {
46
48
  "@jscad/modeling": "^2.12.0",
package/shapes.d.mts CHANGED
@@ -27,3 +27,4 @@ export const scale: typeof jscad.transforms.scale;
27
27
  import jscad from '@jscad/modeling';
28
28
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from "./enclosure.mjs";
29
29
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from "./features.mjs";
30
+ export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from "./optimised.mjs";
package/shapes.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  // Shape helpers for JSCAD designs that are printed: text that prints, placing things on a face, and the pieces of
2
2
  // enclosures (enclosure.mjs: outlines, slabs, shells, lids, dovetail rails, print-safe holes, vents), and print-aware
3
- // features (features.mjs: countersinks, teardrop holes, nut pockets, crush ribs, wall openings).
3
+ // features (features.mjs: countersinks, teardrop holes, nut pockets, crush ribs, wall openings), and optimised layouts
4
+ // (optimised.mjs: what cad optimise finds, as a 2D shape to extrude or a 3D solid).
4
5
  // text({ text, height, stroke, segments }) -> a 2D outline of the text, centred on 0: JSCAD's single-line
5
6
  // font, scaled so a capital is height tall, each line thickened to stroke wide (whole print lines, so it prints).
6
7
  // The arrows the font lacks (up, down, left, right) are drawn in the same stroke.
@@ -85,3 +86,4 @@ export { scale };
85
86
  export { outline, slab, shell, lid, dovetail, gabled, diamond, vents } from './enclosure.mjs';
86
87
  export { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from './features.mjs';
87
88
  export { ops } from './ops.mjs';
89
+ export { fromOutlines, regionShape, optimisedOutline, solidRegion, optimisedSolid } from './optimised.mjs';