@rulecad/model-link 0.1.0 → 0.1.2
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 -0
- package/model-link.d.mts +113 -0
- package/model-link.mjs +20 -2
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -35,3 +35,4 @@ const zip = await threemf(back); // Uint8Array, a .3mf (one object, two
|
|
|
35
35
|
the same model gives the same bytes.
|
|
36
36
|
- `bambu3mf([{ name, parts: [{ name, triangles, slot }] }])` writes a 3MF from triangles directly. It has been
|
|
37
37
|
checked by loading the files back into Bambu Studio; other slicers read the same file as plain objects.
|
|
38
|
+
- `zip([[name, text], …])` writes a ZIP of text files, with the platform's own compression (no library).
|
package/model-link.d.mts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/** @param {Triangle[]} tris @returns {{ q: number, p: number[], t: number[] }} */
|
|
2
|
+
export function fromTriangles(tris: Triangle[]): {
|
|
3
|
+
q: number;
|
|
4
|
+
p: number[];
|
|
5
|
+
t: number[];
|
|
6
|
+
};
|
|
7
|
+
/** @param {{ name: string, role?: 'part' | 'support' | 'reference', label?: string, slot?: number, triangles: Triangle[] }[]} list @returns {{ q: number, p: number[], t: number[], parts: any[] }} */
|
|
8
|
+
export function fromParts(list: {
|
|
9
|
+
name: string;
|
|
10
|
+
role?: "part" | "support" | "reference";
|
|
11
|
+
label?: string;
|
|
12
|
+
slot?: number;
|
|
13
|
+
triangles: Triangle[];
|
|
14
|
+
}[]): {
|
|
15
|
+
q: number;
|
|
16
|
+
p: number[];
|
|
17
|
+
t: number[];
|
|
18
|
+
parts: any[];
|
|
19
|
+
};
|
|
20
|
+
/** A model as a link token ('1.' + compressed data), for #m= in a URL. @param {Model} model @returns {Promise<string>} */
|
|
21
|
+
export function encode(model: Model): Promise<string>;
|
|
22
|
+
/** @param {string} hash location.hash @returns {{ token: string | null, gist: string | null, get: string | null }} */
|
|
23
|
+
export function parseHash(hash: string): {
|
|
24
|
+
token: string | null;
|
|
25
|
+
gist: string | null;
|
|
26
|
+
get: string | null;
|
|
27
|
+
};
|
|
28
|
+
/** A short link's token, from its GitHub Gist. @param {string} id @returns {Promise<string>} */
|
|
29
|
+
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
|
+
export function check(m: any): string[];
|
|
33
|
+
export function expand(m: any): any;
|
|
34
|
+
export function fromShapes(list: any): {
|
|
35
|
+
shapes: {
|
|
36
|
+
q: number;
|
|
37
|
+
p: number[];
|
|
38
|
+
t: number[];
|
|
39
|
+
slot?: any;
|
|
40
|
+
role: any;
|
|
41
|
+
label: any;
|
|
42
|
+
}[];
|
|
43
|
+
place: any[][];
|
|
44
|
+
};
|
|
45
|
+
/** The model's triangles, or one part's. @param {Model} m @param {any[]} [part] @returns {Triangle[]} */
|
|
46
|
+
export function triangles(m: Model, part?: any[]): Triangle[];
|
|
47
|
+
/** @param {ArrayBuffer | Uint8Array} bytes binary or text STL @returns {Triangle[]} */
|
|
48
|
+
export function fromSTL(bytes: ArrayBuffer | Uint8Array): Triangle[];
|
|
49
|
+
/** A binary STL of the printed parts (or those kept). @param {Model} m @param {(part: any[]) => boolean} [keep] @param {{ generator?: string }} [options] @returns {Uint8Array} */
|
|
50
|
+
export function stl(m: Model, keep?: (part: any[]) => boolean, { generator }?: {
|
|
51
|
+
generator?: string;
|
|
52
|
+
}): Uint8Array;
|
|
53
|
+
/** A 3MF in Bambu Studio's layout, each part on its filament slot. @param {Model} m @param {(part: any[]) => boolean} [keep] @param {{ generator?: string }} [options] @returns {Promise<Uint8Array>} */
|
|
54
|
+
export function threemf(m: Model, keep?: (part: any[]) => boolean, { generator }?: {
|
|
55
|
+
generator?: string;
|
|
56
|
+
}): Promise<Uint8Array>;
|
|
57
|
+
export function mesh(tris: any): {
|
|
58
|
+
verts: any[];
|
|
59
|
+
tris: any[][];
|
|
60
|
+
};
|
|
61
|
+
/** @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) */
|
|
62
|
+
export function bambu3mf(objects: {
|
|
63
|
+
name: string;
|
|
64
|
+
parts: {
|
|
65
|
+
name: string;
|
|
66
|
+
triangles: Triangle[];
|
|
67
|
+
slot?: number;
|
|
68
|
+
}[];
|
|
69
|
+
}[], { centre, generator, date }?: {
|
|
70
|
+
centre?: [number, number];
|
|
71
|
+
generator?: string;
|
|
72
|
+
date?: string;
|
|
73
|
+
title?: string;
|
|
74
|
+
}): Promise<Uint8Array>;
|
|
75
|
+
/**
|
|
76
|
+
* A ZIP file, each file deflated with the browser's (or Node's) own CompressionStream: no library needed.
|
|
77
|
+
* @param {[string, string][]} entries [name, text] pairs
|
|
78
|
+
* @returns {Promise<Uint8Array>}
|
|
79
|
+
*/
|
|
80
|
+
export function zip(entries: [string, string][]): Promise<Uint8Array>;
|
|
81
|
+
/** @typedef {[[number, number, number], [number, number, number], [number, number, number]]} Triangle corners in mm, counter-clockwise seen from outside, Z up */
|
|
82
|
+
/** @typedef {{ title?: string, version?: string, src?: string, material?: string, orientation?: string, rules?: [string, any, string][], q?: number, p?: number[], t?: number[], parts?: any[], shapes?: any[], place?: any[], views?: [string, string][] }} Model */
|
|
83
|
+
export const VERSION: 1;
|
|
84
|
+
export namespace LIMITS {
|
|
85
|
+
let vertices: number;
|
|
86
|
+
let triangles: number;
|
|
87
|
+
let shapes: number;
|
|
88
|
+
let places: number;
|
|
89
|
+
let views: number;
|
|
90
|
+
let viewSize: number;
|
|
91
|
+
}
|
|
92
|
+
export function partsOf(m: any): any;
|
|
93
|
+
export function filename(m: any, ext: any, picked?: string): string;
|
|
94
|
+
export function thingOf(name: any): string;
|
|
95
|
+
/**
|
|
96
|
+
* corners in mm, counter-clockwise seen from outside, Z up
|
|
97
|
+
*/
|
|
98
|
+
export type Triangle = [[number, number, number], [number, number, number], [number, number, number]];
|
|
99
|
+
export type Model = {
|
|
100
|
+
title?: string;
|
|
101
|
+
version?: string;
|
|
102
|
+
src?: string;
|
|
103
|
+
material?: string;
|
|
104
|
+
orientation?: string;
|
|
105
|
+
rules?: [string, any, string][];
|
|
106
|
+
q?: number;
|
|
107
|
+
p?: number[];
|
|
108
|
+
t?: number[];
|
|
109
|
+
parts?: any[];
|
|
110
|
+
shapes?: any[];
|
|
111
|
+
place?: any[];
|
|
112
|
+
views?: [string, string][];
|
|
113
|
+
};
|
package/model-link.mjs
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
// (a 3 × 4 transform by rows, in mm). decode() expands them, so the rest of the page sees one model.
|
|
15
15
|
// "1." is the format version: newer pages must keep reading old links.
|
|
16
16
|
|
|
17
|
+
/** @typedef {[[number, number, number], [number, number, number], [number, number, number]]} Triangle corners in mm, counter-clockwise seen from outside, Z up */
|
|
18
|
+
/** @typedef {{ title?: string, version?: string, src?: string, material?: string, orientation?: string, rules?: [string, any, string][], q?: number, p?: number[], t?: number[], parts?: any[], shapes?: any[], place?: any[], views?: [string, string][] }} Model */
|
|
19
|
+
|
|
17
20
|
export const VERSION = 1;
|
|
18
21
|
export const LIMITS = { vertices: 200000, triangles: 400000, shapes: 512, places: 2048, views: 8, viewSize: 4e6 }; // every keycap with its own legend is its own shape
|
|
19
22
|
|
|
@@ -22,6 +25,7 @@ const unb64 = (s) => Uint8Array.from(atob(s.replace(/-/g, '+').replace(/_/g, '/'
|
|
|
22
25
|
async function pipe(bytes, stream) { return new Uint8Array(await new Response(new Blob([bytes]).stream().pipeThrough(stream)).arrayBuffer()); }
|
|
23
26
|
|
|
24
27
|
// fromTriangles([[[x,y,z] ×3], …]) -> { q, p, t }: shared corners merged on a grid ~1/5000 of the model's size.
|
|
28
|
+
/** @param {Triangle[]} tris @returns {{ q: number, p: number[], t: number[] }} */
|
|
25
29
|
export function fromTriangles(tris) {
|
|
26
30
|
const lo = [Infinity, Infinity, Infinity], hi = [-Infinity, -Infinity, -Infinity]; // a loop: spreading big models overflows the stack
|
|
27
31
|
for (const tri of tris) for (const v of tri) for (let k = 0; k < 3; k++) { if (v[k] < lo[k]) lo[k] = v[k]; if (v[k] > hi[k]) hi[k] = v[k]; }
|
|
@@ -36,6 +40,7 @@ export function fromTriangles(tris) {
|
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
// fromParts([{ name, role, label, triangles }]) -> { q, p, t, parts }: shared corners merged across parts.
|
|
43
|
+
/** @param {{ name: string, role?: 'part' | 'support' | 'reference', label?: string, slot?: number, triangles: Triangle[] }[]} list @returns {{ q: number, p: number[], t: number[], parts: any[] }} */
|
|
39
44
|
export function fromParts(list) {
|
|
40
45
|
const m = fromTriangles(list.flatMap((x) => x.triangles)), parts = [];
|
|
41
46
|
// fromTriangles drops triangles that snapping collapses, so count each part's survivors in order
|
|
@@ -49,6 +54,7 @@ export function fromParts(list) {
|
|
|
49
54
|
return { ...m, parts };
|
|
50
55
|
}
|
|
51
56
|
|
|
57
|
+
/** A model as a link token ('1.' + compressed data), for #m= in a URL. @param {Model} model @returns {Promise<string>} */
|
|
52
58
|
export async function encode(model) {
|
|
53
59
|
const bytes = await pipe(new TextEncoder().encode(JSON.stringify(model)), new CompressionStream('deflate-raw'));
|
|
54
60
|
return `${VERSION}.${b64(bytes)}`;
|
|
@@ -56,12 +62,14 @@ export async function encode(model) {
|
|
|
56
62
|
|
|
57
63
|
// The model link's parts from a location hash: { token, gist, get }. A short link (#s=<id>) names a GitHub Gist
|
|
58
64
|
// holding the token.
|
|
65
|
+
/** @param {string} hash location.hash @returns {{ token: string | null, gist: string | null, get: string | null }} */
|
|
59
66
|
export function parseHash(hash) {
|
|
60
67
|
const params = new URLSearchParams(String(hash).replace(/^#/, ''));
|
|
61
68
|
return { token: params.get('m'), gist: params.get('s'), get: params.get('get') };
|
|
62
69
|
}
|
|
63
70
|
|
|
64
71
|
// The token from a short link's Gist (GitHub's API allows this without signing in, about 60 times an hour).
|
|
72
|
+
/** A short link's token, from its GitHub Gist. @param {string} id @returns {Promise<string>} */
|
|
65
73
|
export async function fromGist(id) {
|
|
66
74
|
if (!/^[0-9a-f]{20,40}$/i.test(String(id))) throw new Error("this short link's ID isn't a Gist ID");
|
|
67
75
|
const r = await fetch(`https://api.github.com/gists/${id}`);
|
|
@@ -72,6 +80,7 @@ export async function fromGist(id) {
|
|
|
72
80
|
return (f.truncated ? await (await fetch(f.raw_url)).text() : f.content).trim();
|
|
73
81
|
}
|
|
74
82
|
|
|
83
|
+
/** A link token back to a model, checked: damaged, newer or oversized links are refused. @param {string} token @returns {Promise<Model>} */
|
|
75
84
|
export async function decode(token) {
|
|
76
85
|
const m = String(token || '').match(/^(\d+)\.([\w-]+)$/);
|
|
77
86
|
if (!m) throw new Error("this isn't a model link");
|
|
@@ -122,6 +131,7 @@ export function fromShapes(list) {
|
|
|
122
131
|
}
|
|
123
132
|
|
|
124
133
|
// Triangles in mm: [[[x,y,z] ×3], …], all of them or one part's ([name, role, label, first, count]).
|
|
134
|
+
/** The model's triangles, or one part's. @param {Model} m @param {any[]} [part] @returns {Triangle[]} */
|
|
125
135
|
export function triangles(m, part) {
|
|
126
136
|
const v = (i) => [m.p[3 * i] * m.q, m.p[3 * i + 1] * m.q, m.p[3 * i + 2] * m.q], out = [];
|
|
127
137
|
const [from, to] = part ? [part[3] * 3, (part[3] + part[4]) * 3] : [0, m.t.length];
|
|
@@ -142,6 +152,7 @@ const chosen = (m, keep) => partsOf(m).filter((x, i) => (keep ? keep.has(i) : x[
|
|
|
142
152
|
// Binary STL (mm): the parts picked, as one mesh, each part mended (mesh) as for 3MF.
|
|
143
153
|
// fromSTL(bytes) -> triangles [[[x, y, z] ×3], …] from an STL file, binary or text (ASCII). Units are as the file has
|
|
144
154
|
// them (millimetres, by convention). Throws on anything that isn't an STL.
|
|
155
|
+
/** @param {ArrayBuffer | Uint8Array} bytes binary or text STL @returns {Triangle[]} */
|
|
145
156
|
export function fromSTL(bytes) {
|
|
146
157
|
const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
147
158
|
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
@@ -161,6 +172,7 @@ export function fromSTL(bytes) {
|
|
|
161
172
|
}
|
|
162
173
|
|
|
163
174
|
// generator: the name the file says made it (rulecad by default)
|
|
175
|
+
/** A binary STL of the printed parts (or those kept). @param {Model} m @param {(part: any[]) => boolean} [keep] @param {{ generator?: string }} [options] @returns {Uint8Array} */
|
|
164
176
|
export function stl(m, keep, { generator = 'rulecad' } = {}) {
|
|
165
177
|
const tris = chosen(m, keep).flatMap((x) => { const { verts, tris: t } = mesh(triangles(m, x)); return t.map((ids) => ids.map((i) => verts[i])); }), buf = new ArrayBuffer(84 + 50 * tris.length), dv = new DataView(buf);
|
|
166
178
|
const head = `${generator} ${m.src || m.title || ''}`.slice(0, 80);
|
|
@@ -181,6 +193,7 @@ export const thingOf = (name) => { const bits = String(name).split('.'); return
|
|
|
181
193
|
|
|
182
194
|
// 3MF of the parts picked, in Bambu Studio's layout (bambu3mf): one object per printed thing, its parts on their AMS
|
|
183
195
|
// slots (parts' sixth field; slot 1 without one).
|
|
196
|
+
/** A 3MF in Bambu Studio's layout, each part on its filament slot. @param {Model} m @param {(part: any[]) => boolean} [keep] @param {{ generator?: string }} [options] @returns {Promise<Uint8Array>} */
|
|
184
197
|
export async function threemf(m, keep, { generator = 'rulecad' } = {}) {
|
|
185
198
|
const things = new Map();
|
|
186
199
|
for (const x of chosen(m, keep)) {
|
|
@@ -297,6 +310,7 @@ const uuid = (n, kind) => `${kind.toString(16).padStart(4, '0')}${(n & 0xffff).t
|
|
|
297
310
|
const IDENTITY = '1 0 0 0 1 0 0 0 1 0 0 0', IDENTITY4 = '1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1';
|
|
298
311
|
const NS = 'xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02" xmlns:BambuStudio="http://schemas.bambulab.com/package/2021" xmlns:p="http://schemas.microsoft.com/3dmanufacturing/production/2015/06" requiredextensions="p"';
|
|
299
312
|
|
|
313
|
+
/** @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) */
|
|
300
314
|
export async function bambu3mf(objects, { centre = [128, 128], generator = 'rulecad', date } = {}) {
|
|
301
315
|
const files = {}, rels = [], resources = [], build = [], settings = [], instances = [];
|
|
302
316
|
let id = 0;
|
|
@@ -327,8 +341,12 @@ export async function bambu3mf(objects, { centre = [128, 128], generator = 'rule
|
|
|
327
341
|
const CRC = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
|
|
328
342
|
const crc32 = (b) => { let c = 0xffffffff; for (const x of b) c = CRC[(c ^ x) & 255] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
|
|
329
343
|
|
|
330
|
-
|
|
331
|
-
|
|
344
|
+
/**
|
|
345
|
+
* A ZIP file, each file deflated with the browser's (or Node's) own CompressionStream: no library needed.
|
|
346
|
+
* @param {[string, string][]} entries [name, text] pairs
|
|
347
|
+
* @returns {Promise<Uint8Array>}
|
|
348
|
+
*/
|
|
349
|
+
export async function zip(entries) {
|
|
332
350
|
const enc = new TextEncoder(), parts = [], dir = [];
|
|
333
351
|
let offset = 0;
|
|
334
352
|
for (const [name, text] of entries) {
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/model-link",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Share a printable model as a link (the model lives in the URL), and write STL or multi-colour Bambu Studio 3MF files, in the browser or Node.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"exports":
|
|
6
|
+
"exports": {
|
|
7
|
+
"types": "./model-link.d.mts",
|
|
8
|
+
"default": "./model-link.mjs"
|
|
9
|
+
},
|
|
7
10
|
"files": [
|
|
8
11
|
"model-link.mjs",
|
|
12
|
+
"model-link.d.mts",
|
|
9
13
|
"README.md",
|
|
10
14
|
"LICENSE"
|
|
11
15
|
],
|
|
@@ -29,5 +33,6 @@
|
|
|
29
33
|
},
|
|
30
34
|
"scripts": {
|
|
31
35
|
"test": "node --test"
|
|
32
|
-
}
|
|
36
|
+
},
|
|
37
|
+
"types": "./model-link.d.mts"
|
|
33
38
|
}
|