@rulecad/model-link 0.3.2 → 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.
- package/README.md +16 -2
- package/model-link.d.mts +50 -20
- package/model-link.mjs +1519 -243
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Share a printable model as a link. The model is compressed into the URL's `#fragment`, which browsers never send to
|
|
4
4
|
the server, so a public web page can open a private design and nothing is uploaded. It also writes STL files, and 3MF
|
|
5
5
|
files in Bambu Studio's project layout with each part on its own filament (AMS) slot. No dependencies: it uses the
|
|
6
|
-
browser's own `CompressionStream`, which Node
|
|
6
|
+
browser's own `CompressionStream`, which Node 22.13 or later has too.
|
|
7
7
|
|
|
8
8
|
```js
|
|
9
9
|
import { fromParts, encode, decode, stl, threemf } from '@rulecad/model-link';
|
|
@@ -32,7 +32,21 @@ const zip = await threemf(back); // Uint8Array, a .3mf (one object, two
|
|
|
32
32
|
the top of the motion section in `model-link.mjs`.
|
|
33
33
|
- **A machine's link:** a model can carry `task` (the tool's anchor and the plane it moves on, with what the margins read), `steps` (a step file cut down by `compactSteps(file)`, small enough for a link), `wires` (routes drawn faint, never parts) and `build` (the build page's text and numbers). All of it is checked as data when a link is decoded, and a page never runs any of it. The formats are in the comment at the top of the "a machine's link" section in `model-link.mjs`.
|
|
34
34
|
- **Safety:** links are data, never code. `decode()` checks the format and sizes (`LIMITS`), refuses damaged links,
|
|
35
|
-
and refuses links from a newer format version.
|
|
35
|
+
and refuses links from a newer format version. A part's filament slot must be a whole number from 1 to 16, and a
|
|
36
|
+
board's fab file must be a zip holding only a board's files (Gerbers, drill files, parts list, placement and notes,
|
|
37
|
+
by their file extensions), so a link can't put text of its own into a 3MF or hand out any other file. The 3MF writers
|
|
38
|
+
refuse a slot that isn't a whole number from 1 to 16 too. The rules card's lines must each be a name, a value and
|
|
39
|
+
perhaps a unit; the build page's parts must be in the form the page reads; and every piece of text, wherever it is
|
|
40
|
+
in the link, is held to `LIMITS.text` (a flat view's drawing and a fab file's bytes have limits of their own).
|
|
41
|
+
`encode()` makes the same checks and throws an error saying what is wrong, so a link the page would turn away is
|
|
42
|
+
refused when it is made, not when someone opens it.
|
|
43
|
+
- **One table of fields:** every field a link may hold is described once, in a table in `model-link.mjs` (`FIELDS`
|
|
44
|
+
lists their names), and every check reads it. A refusal names the field that is wrong by its path and says what it
|
|
45
|
+
should be: "broken motion: motion.joints[3].dofs must be 1 [least, most, value] for a revolute joint, not 2".
|
|
46
|
+
`check(model)` gives the same as findings, `{ level, text, code: 'link.data', values: { path } }`. A field the table
|
|
47
|
+
doesn't have (from a newer cad, say) is not refused: `decode(token, { warnings })` leaves it out of the model and
|
|
48
|
+
adds a warning to `warnings`, and `check()` gives it as a `warn` finding. `encode()` refuses one, so a link made
|
|
49
|
+
today holds only what the page reads.
|
|
36
50
|
- **Short links:** `parseHash(location.hash)` also reads `#s=<id>`, a GitHub Gist holding the data, and
|
|
37
51
|
`fromGist(id)` fetches it without signing in.
|
|
38
52
|
- **The file says who made it:** `stl(m, keep, { generator })` and `threemf(m, keep, { generator })`; the default is
|
package/model-link.d.mts
CHANGED
|
@@ -17,7 +17,11 @@ export function fromParts(list: {
|
|
|
17
17
|
t: number[];
|
|
18
18
|
parts: any[];
|
|
19
19
|
};
|
|
20
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* A model as a link token ('1.' + compressed data), for #m= in a URL. Checked first, as decode() checks it: a model the
|
|
22
|
+
* page would turn away is refused here, when the link is made, not when someone opens it.
|
|
23
|
+
* @param {Model} model @returns {Promise<string>} @throws an Error saying what is wrong with the model
|
|
24
|
+
*/
|
|
21
25
|
export function encode(model: Model): Promise<string>;
|
|
22
26
|
/** @param {string} hash location.hash @returns {{ token: string | null, gist: string | null, get: string | null }} */
|
|
23
27
|
export function parseHash(hash: string): {
|
|
@@ -27,17 +31,29 @@ export function parseHash(hash: string): {
|
|
|
27
31
|
};
|
|
28
32
|
/** A short link's token, from its GitHub Gist. @param {string} id @returns {Promise<string>} */
|
|
29
33
|
export function fromGist(id: string): Promise<string>;
|
|
30
|
-
/** A link token back to a model, checked: damaged, newer or oversized links are refused. @param {string} token @returns {Promise<Model>} */
|
|
31
|
-
export function decode(token: string): Promise<Model>;
|
|
32
34
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
35
|
+
* A link token back to a model, checked: damaged, newer or oversized links are refused, with a message naming the field
|
|
36
|
+
* (check() gives the same as findings). A field a link doesn't have is left out, not refused, so links made by a newer
|
|
37
|
+
* cad still open: give options.warnings, a list, for a warning about each (findings, level 'warn').
|
|
38
|
+
* @param {string} token @param {{ warnings?: any[] }} [options] @returns {Promise<Model>}
|
|
39
|
+
*/
|
|
40
|
+
export function decode(token: string, { warnings }?: {
|
|
41
|
+
warnings?: any[];
|
|
42
|
+
}): Promise<Model>;
|
|
43
|
+
/**
|
|
44
|
+
* What's wrong with a model's data, as findings (the kit's one result shape): the first fault in each field
|
|
45
|
+
* ({ level: 'fail', text, code: 'link.data', values: { path } }, path naming the field, such as
|
|
46
|
+
* 'motion.joints[3].dofs'), then a warning for each field a link doesn't have (level 'warn': decode() leaves those out,
|
|
47
|
+
* encode() refuses them). Empty when it's sound. Takes a model or its placed-shapes form.
|
|
48
|
+
* @param {any} m @returns {{ level: 'fail' | 'warn', text: string, code: string, values: { path: string } }[]}
|
|
36
49
|
*/
|
|
37
50
|
export function check(m: any): {
|
|
38
|
-
level: "fail";
|
|
51
|
+
level: "fail" | "warn";
|
|
39
52
|
text: string;
|
|
40
53
|
code: string;
|
|
54
|
+
values: {
|
|
55
|
+
path: string;
|
|
56
|
+
};
|
|
41
57
|
}[];
|
|
42
58
|
/**
|
|
43
59
|
* A step file (rulecad.steps/1, from cad plot) as a link or a page carries it: what playback needs, in whole numbers.
|
|
@@ -48,13 +64,14 @@ export function compactSteps(steps: object, { maxBlocks, maxPoints }?: {
|
|
|
48
64
|
maxBlocks?: number;
|
|
49
65
|
maxPoints?: number;
|
|
50
66
|
}): object;
|
|
51
|
-
|
|
67
|
+
/** @param {any} m the placed-shapes form, { shapes, place, … } @returns {Model} @throws an Error saying what is wrong */
|
|
68
|
+
export function expand(m: any): Model;
|
|
52
69
|
export function fromShapes(list: any): {
|
|
53
70
|
shapes: {
|
|
54
71
|
q: number;
|
|
55
72
|
p: number[];
|
|
56
73
|
t: number[];
|
|
57
|
-
slot?:
|
|
74
|
+
slot?: number;
|
|
58
75
|
role: any;
|
|
59
76
|
label: any;
|
|
60
77
|
}[];
|
|
@@ -95,9 +112,12 @@ export function stl(m: Model, keep?: Set<number> | ((part: any[], index: number)
|
|
|
95
112
|
export function threemf(m: Model, keep?: Set<number> | ((part: any[], index: number) => boolean), { generator }?: {
|
|
96
113
|
generator?: string;
|
|
97
114
|
}): Promise<Uint8Array>;
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
115
|
+
/** @param {Triangle[]} tris @param {{ budget: number }} [work] @returns {{ verts: number[][], tris: number[][] }} */
|
|
116
|
+
export function mesh(tris: Triangle[], work?: {
|
|
117
|
+
budget: number;
|
|
118
|
+
}): {
|
|
119
|
+
verts: number[][];
|
|
120
|
+
tris: number[][];
|
|
101
121
|
};
|
|
102
122
|
/** @param {{ name: string, parts: { name: string, triangles: Triangle[], slot?: number }[] }[]} objects @param {{ centre?: [number, number], generator?: string, date?: string, title?: string }} [options] @returns {Promise<Uint8Array>} a .3mf (a zip) */
|
|
103
123
|
export function bambu3mf(objects: {
|
|
@@ -120,7 +140,7 @@ export function bambu3mf(objects: {
|
|
|
120
140
|
*/
|
|
121
141
|
export function zip(entries: [string, string][]): Promise<Uint8Array>;
|
|
122
142
|
/** @typedef {[[number, number, number], [number, number, number], [number, number, number]]} Triangle corners in mm, counter-clockwise seen from outside, Z up */
|
|
123
|
-
/** @typedef {{ title?: string, version?: string, src?: string,
|
|
143
|
+
/** @typedef {{ q?: number, p?: number[], t?: number[], parts?: any[], title?: string, version?: string, src?: string, set?: string, material?: string, orientation?: string, sheet?: string[][], views?: [string, string][], fab?: { name: string, zip: string }[], motion?: any, task?: any, steps?: any, wires?: any[], show?: 'built', rules?: [string, any, string?][], build?: any, shapes?: any[], place?: any[] }} Model */
|
|
124
144
|
export const VERSION: 1;
|
|
125
145
|
export namespace LIMITS {
|
|
126
146
|
let vertices: number;
|
|
@@ -136,6 +156,8 @@ export namespace LIMITS {
|
|
|
136
156
|
let text: number;
|
|
137
157
|
let bytes: number;
|
|
138
158
|
}
|
|
159
|
+
/** The names of a link's fields, from the table (the header comment and the Model typedef are held to it). @type {string[]} */
|
|
160
|
+
export const FIELDS: string[];
|
|
139
161
|
export function partsOf(m: any): any;
|
|
140
162
|
export function posesOf(m: Model): {
|
|
141
163
|
k: number;
|
|
@@ -154,28 +176,36 @@ export const turn: (degrees: number[]) => number[];
|
|
|
154
176
|
/** @type {(points: number[][]) => number} */
|
|
155
177
|
export const pathLength: (points: number[][]) => number;
|
|
156
178
|
export function filename(m: any, ext: any, picked?: string): string;
|
|
179
|
+
export function fabName(name: any): string;
|
|
157
180
|
export function thingOf(name: any): string;
|
|
158
181
|
/**
|
|
159
182
|
* corners in mm, counter-clockwise seen from outside, Z up
|
|
160
183
|
*/
|
|
161
184
|
export type Triangle = [[number, number, number], [number, number, number], [number, number, number]];
|
|
162
185
|
export type Model = {
|
|
186
|
+
q?: number;
|
|
187
|
+
p?: number[];
|
|
188
|
+
t?: number[];
|
|
189
|
+
parts?: any[];
|
|
163
190
|
title?: string;
|
|
164
191
|
version?: string;
|
|
165
192
|
src?: string;
|
|
193
|
+
set?: string;
|
|
166
194
|
material?: string;
|
|
167
195
|
orientation?: string;
|
|
196
|
+
sheet?: string[][];
|
|
197
|
+
views?: [string, string][];
|
|
168
198
|
fab?: {
|
|
169
199
|
name: string;
|
|
170
200
|
zip: string;
|
|
171
201
|
}[];
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
202
|
+
motion?: any;
|
|
203
|
+
task?: any;
|
|
204
|
+
steps?: any;
|
|
205
|
+
wires?: any[];
|
|
206
|
+
show?: "built";
|
|
207
|
+
rules?: [string, any, string?][];
|
|
208
|
+
build?: any;
|
|
177
209
|
shapes?: any[];
|
|
178
210
|
place?: any[];
|
|
179
|
-
views?: [string, string][];
|
|
180
|
-
motion?: any;
|
|
181
211
|
};
|