Files
support-fins/web/stepworker.js
T
matthewtrahan 2aa86a4b70 import: load the STEP kernel while the user picks a file, cache it a year
The pen holder (195 KB SolidWorks STEP) tessellates in 0.3 s; the rest
of a 1.7-3.4 s cold import was the ~2.9 MB kernel download. Start the
worker when Import is clicked or a file is dragged over, so the download
overlaps with the file dialog. The vendor folder now carries the version
(occt-import-js-0.0.23) and web/_headers marks it immutable for a year.
A failed read drops the worker so the next attempt starts clean.
2026-09-24 16:15:26 -05:00

34 lines
1.3 KiB
JavaScript

/**
* STEP tessellation worker (classic, not a module: the OpenCascade build is an
* Emscripten script loaded with importScripts). Receives { bytes, params },
* returns the kernel's result with typed arrays transferred back rather than
* cloning big plain arrays. A { warm: true } message only loads the kernel, so
* the download can start while the user is still picking a file. See step.js.
*
* The vendor folder carries the version so it can be cached for a year
* (web/_headers): bumping the version is what busts the cache.
*/
const OCCT = './vendor/occt-import-js-0.0.23/';
importScripts(`${OCCT}occt-import-js.js`);
const kernel = occtimportjs({ locateFile: (f) => `${OCCT}${f}` });
onmessage = async (e) => {
try {
const occt = await kernel;
if (e.data.warm) return;
const r = occt.ReadStepFile(e.data.bytes, e.data.params);
const transfer = [];
for (const m of r.meshes ?? []) {
m.attributes.position.array = new Float32Array(m.attributes.position.array);
m.index.array = new Uint32Array(m.index.array);
delete m.attributes.normal; // the app computes its own
delete m.brep_faces;
transfer.push(m.attributes.position.array.buffer, m.index.array.buffer);
}
postMessage(r, transfer);
} catch (err) {
if (!e.data.warm) postMessage({ error: String(err?.message ?? err) });
}
};