// Read assembly names/rigid transforms only; never convert or alter render meshes. import { readFileSync } from 'node:fs'; import assert from 'node:assert/strict'; import { Matrix4, Vector3 } from 'three'; export function stepPlacements(path) { const bytes = readFileSync(path); const entities = new Map([...bytes.toString('latin1').matchAll(/#(\d+)\s*=\s*([\s\S]*?);/g)].map(m => [m[1], m[2]])); const refs = id => [...entities.get(id).matchAll(/#(\d+)/g)].map(m => m[1]); const decode = s => s.replace(/\\X2\\(.*?)\\X0\\/g, (_, hex) => String.fromCharCode(...hex.match(/.{4}/g).map(x => parseInt(x, 16)))); const names = new Map(); for (const [id, entity] of entities) if (/^PRODUCT_DEFINITION\s*\(/.test(entity)) { names.set(id, decode(entities.get(refs(refs(id)[0])[0]).match(/'([^']*)'/)[1])); } const vector = id => new Vector3(...[...entities.get(id).matchAll(/\(([^()]*)\)/g)].at(-1)[1].split(',').map(Number)); const axis = id => { const [p, z, x] = refs(id).map(vector); return new Matrix4().makeBasis(x, new Vector3().crossVectors(z, x), z).setPosition(p); }; const occurrences = []; for (const [id, entity] of entities) if (/^CONTEXT_DEPENDENT_SHAPE_REPRESENTATION\s*\(/.test(entity)) { const [relation, definition] = refs(id), occurrence = refs(definition)[0]; const [parent, child] = refs(occurrence), [a, b] = refs(refs(relation).at(-1)); const matrix = axis(a).multiply(axis(b).invert()); for (const n of [12, 13, 14]) matrix.elements[n] /= 1000; occurrences.push({ parent, child, name: names.get(child), matrix, occurrence }); } const roots = [...names.keys()].filter(id => !occurrences.some(o => o.child === id)); assert.equal(roots.length, 1); const top = occurrences.filter(o => o.parent === roots[0]); return { rootName: names.get(roots[0]), top, occurrences, bytes }; }