@rulecad/shapes 0.2.1 → 0.3.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,31 @@
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
+ import jscad from '@jscad/modeling';
package/optimised.mjs ADDED
@@ -0,0 +1,42 @@
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 result's outline with its keep regions added and avoid regions cut
7
+ import jscad from '@jscad/modeling';
8
+
9
+ const { geom2 } = jscad.geometries, { circle, rectangle, polygon } = jscad.primitives, { union, subtract } = jscad.booleans;
10
+
11
+ /**
12
+ * Closed loops (solid on the left) as one JSCAD 2D shape.
13
+ * @param {[number, number][][]} loops
14
+ */
15
+ export function fromOutlines(loops) {
16
+ const sides = [];
17
+ for (const l of loops) for (let i = 0; i < l.length; i++) sides.push([l[i], l[(i + 1) % l.length]]);
18
+ return geom2.create(sides);
19
+ }
20
+
21
+ /**
22
+ * A region of a problem (a circle, a centred rectangle or a polygon) as a JSCAD 2D shape.
23
+ * @param {{ at?: [number, number], r?: number, size?: [number, number], points?: [number, number][] }} r
24
+ * @param {number} [segments]
25
+ */
26
+ export function regionShape(r, segments = 48) {
27
+ if (r.points) return polygon({ points: r.points });
28
+ if (r.size) return rectangle({ center: r.at, size: r.size });
29
+ return circle({ center: r.at, radius: r.r, segments });
30
+ }
31
+
32
+ /**
33
+ * An optimised result's outline, its keep regions added and avoid regions cut exactly.
34
+ * @param {{ outlines: [number, number][][], regions: { keep: object[], avoid: object[] } }} result
35
+ * @param {{ segments?: number }} [options]
36
+ */
37
+ export function optimisedOutline(result, { segments = 48 } = {}) {
38
+ let shape = fromOutlines(result.outlines);
39
+ if (result.regions.keep.length) shape = union(shape, ...result.regions.keep.map((r) => regionShape(r, segments)));
40
+ if (result.regions.avoid.length) shape = subtract(shape, ...result.regions.avoid.map((r) => regionShape(r, segments)));
41
+ return shape;
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/shapes",
3
- "version": "0.2.1",
3
+ "version": "0.3.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 } 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: the outlines cad optimise finds, as a 2D shape to extrude).
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 } from './optimised.mjs';