@huaqiu/dsh-hardware-ui 0.7.22 → 0.7.24
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/lib/client.js +5927 -355
- package/lib/client.js.map +1 -1
- package/package.json +1 -1
- package/src/client/copy.ts +4 -0
- package/src/client/document/EcadBody.tsx +64 -14
- package/src/client/document/ModelBody.tsx +202 -0
- package/src/client/document/OpenInApplicationAction.tsx +313 -0
- package/src/client/document/contract.ts +49 -2
- package/src/client/document/copy.ts +56 -0
- package/src/client/document/index.ts +89 -4
- package/src/client/document/model/adapters/amf.ts +93 -0
- package/src/client/document/model/adapters/gltf.ts +120 -0
- package/src/client/document/model/adapters/obj.ts +103 -0
- package/src/client/document/model/adapters/ply.ts +49 -0
- package/src/client/document/model/adapters/stl.ts +49 -0
- package/src/client/document/model/adapters/threemf.ts +103 -0
- package/src/client/document/model/companions.ts +355 -0
- package/src/client/document/model/index.ts +130 -0
- package/src/client/document/model/mesh.ts +163 -0
- package/src/client/document/model/types.ts +92 -0
- package/src/client/document/model/units.ts +93 -0
- package/src/client/document/model/viewer.ts +160 -0
- package/src/client/document/mount.ts +128 -9
- package/src/client/document/step.ts +12 -92
- package/src/client/index.ts +86 -2
- package/src/client/locales.ts +64 -4
- package/src/client/model.ts +37 -5
- package/src/client/theme.ts +26 -1
- package/src/client/toolchain-view.tsx +19 -19
- package/src/client/toolchain.ts +43 -0
- package/src/client/wizard.tsx +14 -8
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AMF adapter — additive manufacturing's XML predecessor to 3MF.
|
|
3
|
+
*
|
|
4
|
+
* AMF is P2 ("evaluate and implement if a suitable maintained loader is
|
|
5
|
+
* practical"). It is practical here: `AMFLoader` ships with the installed
|
|
6
|
+
* Three.js and parses from an `ArrayBuffer`, handling both plain XML and the
|
|
7
|
+
* zipped container some writers produce.
|
|
8
|
+
*
|
|
9
|
+
* Units are the one thing this adapter has to be careful about. The loader reads
|
|
10
|
+
* the document's `<amf unit="…">` and scales the geometry to millimetres — but
|
|
11
|
+
* only for the five spellings in its own table; any other value leaves the
|
|
12
|
+
* geometry untouched. So this adapter reads the same attribute itself and says
|
|
13
|
+
* what the result actually is: `mm` when the loader's conversion applied, and
|
|
14
|
+
* "unknown" when it did not, instead of inheriting a claim the loader never
|
|
15
|
+
* made (§5.7).
|
|
16
|
+
*
|
|
17
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/amf
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { Group } from 'three'
|
|
21
|
+
import { AMFLoader } from 'three/addons/loaders/AMFLoader.js'
|
|
22
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
23
|
+
import { decodeText } from '../companions.js'
|
|
24
|
+
import { disposerFor, hasGeometry, summarize, toArrayBuffer } from '../mesh.js'
|
|
25
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The unit the AMF document declares, or undefined when it declares none.
|
|
29
|
+
*
|
|
30
|
+
* `unit` is an attribute of the root `<amf>` element, so the first match is the
|
|
31
|
+
* document's own statement. Returned exactly as written, because the loader's
|
|
32
|
+
* own lookup is case-sensitive.
|
|
33
|
+
* @param text - the document as decoded text.
|
|
34
|
+
* @returns the raw unit token, or undefined when the root declares none.
|
|
35
|
+
*/
|
|
36
|
+
export function amfUnitOf(text: string): string | undefined {
|
|
37
|
+
const match = /<amf\b[^>]*\bunit\s*=\s*["']([^"']*)["']/iu.exec(text)
|
|
38
|
+
return match?.[1]?.trim() || undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Unit spellings `AMFLoader` actually converts, copied from its own
|
|
43
|
+
* `loadDocumentScale` table (`AMFLoader.js:162-167`).
|
|
44
|
+
*
|
|
45
|
+
* The comparison is case-sensitive on purpose. The loader does a plain object
|
|
46
|
+
* lookup, so `unit="Millimeter"` is *not* converted, and the AMF specification's
|
|
47
|
+
* legal `centimeter` is not in the table either: reporting millimetres for
|
|
48
|
+
* either file would be exactly the false claim §5.7 forbids.
|
|
49
|
+
*/
|
|
50
|
+
const CONVERTED_UNITS: ReadonlySet<string> = new Set(['millimeter', 'inch', 'feet', 'meter', 'micron'])
|
|
51
|
+
|
|
52
|
+
export const amfAdapter: ModelFormatAdapter = {
|
|
53
|
+
id: 'amf',
|
|
54
|
+
extensions: ['amf'],
|
|
55
|
+
label: 'AMF',
|
|
56
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
57
|
+
// A zipped AMF hides its declaration inside the archive; the loader still
|
|
58
|
+
// parses it, and this adapter then reports the unit as unknown rather than
|
|
59
|
+
// claiming a conversion it could not verify.
|
|
60
|
+
const zipped = source.bytes[0] === 0x50 && source.bytes[1] === 0x4b
|
|
61
|
+
const declared = zipped ? undefined : amfUnitOf(decodeText(source.bytes))
|
|
62
|
+
// The attribute's absence means millimetres both in the spec and in the
|
|
63
|
+
// loader; a value the loader does not convert means the geometry is of no
|
|
64
|
+
// known scale, whatever the file intended.
|
|
65
|
+
const conversion = zipped ? false : CONVERTED_UNITS.has(declared ?? 'millimeter')
|
|
66
|
+
|
|
67
|
+
let object
|
|
68
|
+
try {
|
|
69
|
+
object = new AMFLoader().parse(toArrayBuffer(source.bytes))
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
72
|
+
}
|
|
73
|
+
if (object === null || object === undefined) {
|
|
74
|
+
throw new DocumentRenderError('parse', 'no AMF document in this file')
|
|
75
|
+
}
|
|
76
|
+
const group = object instanceof Group ? object : new Group().add(object)
|
|
77
|
+
if (!hasGeometry(group)) throw new DocumentRenderError('parse', 'no mesh geometry in this AMF')
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
object: group,
|
|
81
|
+
format: 'amf',
|
|
82
|
+
metadata: {
|
|
83
|
+
// The loader scaled a recognised unit to millimetres; anything else it
|
|
84
|
+
// left alone, and the geometry is then of no known scale.
|
|
85
|
+
units: conversion ? 'mm' : 'unknown',
|
|
86
|
+
unitSource: conversion ? (declared === undefined ? 'format' : 'declared') : 'unknown',
|
|
87
|
+
...summarize(group),
|
|
88
|
+
warnings: [],
|
|
89
|
+
},
|
|
90
|
+
dispose: disposerFor(group),
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* glTF 2.0 adapter — `.glb` (binary, resources embedded) and `.gltf` (JSON with
|
|
3
|
+
* buffers and images beside it).
|
|
4
|
+
*
|
|
5
|
+
* glTF is the one format here whose linear unit is fixed by its specification:
|
|
6
|
+
* metres. The model is therefore scaled once on its container so the viewer's
|
|
7
|
+
* millimetre convention holds, and the status line reports `m` as a *format*
|
|
8
|
+
* fact rather than a guess (§5.7).
|
|
9
|
+
*
|
|
10
|
+
* External resources are resolved through DSH's authorized read and published as
|
|
11
|
+
* object URLs (§5.5). A `data:` URI is already embedded and is left to the
|
|
12
|
+
* loader; an absolute or remote URL is not this document's to fetch and is left
|
|
13
|
+
* alone too, so a model that references `https://…` shows whatever loaded
|
|
14
|
+
* locally and reports the rest instead of reaching the network.
|
|
15
|
+
*
|
|
16
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/gltf
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { Group } from 'three'
|
|
20
|
+
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'
|
|
21
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
22
|
+
import { createCompanions, decodeText, directoryOf, gltfResourceReferences } from '../companions.js'
|
|
23
|
+
import { disposerFor, hasGeometry, summarize, toArrayBuffer } from '../mesh.js'
|
|
24
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
25
|
+
|
|
26
|
+
/** glTF's magic number: `glTF` little-endian. */
|
|
27
|
+
const GLB_MAGIC = 0x46546c67
|
|
28
|
+
|
|
29
|
+
/** True when the bytes are a binary glTF container rather than JSON text. */
|
|
30
|
+
export function isGlb(bytes: Uint8Array): boolean {
|
|
31
|
+
if (bytes.byteLength < 4) return false
|
|
32
|
+
return (bytes[0] ?? 0) + ((bytes[1] ?? 0) << 8) + ((bytes[2] ?? 0) << 16) + ((bytes[3] ?? 0) << 24) === GLB_MAGIC
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Trailing-slash base the loader concatenates a relative URI onto. */
|
|
36
|
+
function baseOf(path: string | undefined): string {
|
|
37
|
+
if (path === undefined) return ''
|
|
38
|
+
const directory = directoryOf(path)
|
|
39
|
+
return directory === '' ? '' : `${directory}/`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Wrap `GLTFLoader.parse`'s callback API in a promise. */
|
|
43
|
+
function parseGltf(
|
|
44
|
+
loader: GLTFLoader,
|
|
45
|
+
data: ArrayBuffer | string,
|
|
46
|
+
base: string,
|
|
47
|
+
): Promise<{ scene: Group }> {
|
|
48
|
+
return new Promise((resolvePromise, reject) => {
|
|
49
|
+
loader.parse(
|
|
50
|
+
data,
|
|
51
|
+
base,
|
|
52
|
+
result => { resolvePromise({ scene: result.scene as Group }) },
|
|
53
|
+
error => { reject(error instanceof Error ? error : new Error(String(error))) },
|
|
54
|
+
)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const gltfAdapter: ModelFormatAdapter = {
|
|
59
|
+
id: 'gltf',
|
|
60
|
+
extensions: ['glb', 'gltf'],
|
|
61
|
+
label: 'glTF',
|
|
62
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
63
|
+
const binary = isGlb(source.bytes)
|
|
64
|
+
const companions = createCompanions({ path: source.path, read: source.readCompanion, signal: source.signal })
|
|
65
|
+
const warnings: string[] = []
|
|
66
|
+
let scene: Group
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
let payload: ArrayBuffer | string
|
|
70
|
+
if (binary) {
|
|
71
|
+
payload = toArrayBuffer(source.bytes)
|
|
72
|
+
} else {
|
|
73
|
+
const text = decodeText(source.bytes)
|
|
74
|
+
let json: unknown
|
|
75
|
+
try {
|
|
76
|
+
json = JSON.parse(text)
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw new DocumentRenderError('parse', `not valid glTF JSON: ${error instanceof Error ? error.message : String(error)}`)
|
|
79
|
+
}
|
|
80
|
+
for (const resource of gltfResourceReferences(json)) {
|
|
81
|
+
const asset = await companions.readBytes(resource.reference)
|
|
82
|
+
if (asset === undefined) {
|
|
83
|
+
// A buffer is the geometry itself: without it there is no document to
|
|
84
|
+
// show, and the loader's own failure for a truncated buffer is an
|
|
85
|
+
// unreadable `RangeError`. Naming the file is the useful answer.
|
|
86
|
+
if (resource.geometry) throw new DocumentRenderError('missing-companion', resource.reference)
|
|
87
|
+
warnings.push(resource.reference)
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
companions.publish(asset)
|
|
91
|
+
}
|
|
92
|
+
payload = text
|
|
93
|
+
}
|
|
94
|
+
if (source.signal.aborted) throw new DocumentRenderError('aborted', 'render abandoned')
|
|
95
|
+
const loaded = await parseGltf(new GLTFLoader(companions.manager), payload, baseOf(source.path))
|
|
96
|
+
scene = loaded.scene
|
|
97
|
+
} catch (error) {
|
|
98
|
+
companions.dispose()
|
|
99
|
+
if (error instanceof DocumentRenderError) throw error
|
|
100
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!hasGeometry(scene)) {
|
|
104
|
+
companions.dispose()
|
|
105
|
+
throw new DocumentRenderError('parse', 'no mesh geometry in this glTF')
|
|
106
|
+
}
|
|
107
|
+
// Metres to millimetres is one exact factor of 1000 on the container.
|
|
108
|
+
scene.scale.setScalar(1000)
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
object: scene,
|
|
112
|
+
format: binary ? 'glb' : 'gltf',
|
|
113
|
+
metadata: { units: 'm', unitSource: 'format', ...summarize(scene), warnings },
|
|
114
|
+
dispose: () => {
|
|
115
|
+
disposerFor(scene)()
|
|
116
|
+
companions.dispose()
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OBJ adapter, with its MTL and texture companions.
|
|
3
|
+
*
|
|
4
|
+
* OBJ itself is geometry-only text; everything that makes it presentable lives
|
|
5
|
+
* beside it in a `.mtl` library and in the textures that library names. The task
|
|
6
|
+
* (§5.4) is specific about how those are reached: resolve them relative to the
|
|
7
|
+
* document, read them through DSH's authorized file access, and degrade with a
|
|
8
|
+
* warning — never fail the document and never reach for a browser-side fetch of
|
|
9
|
+
* a `file://` path or a duplicate file server.
|
|
10
|
+
*
|
|
11
|
+
* So the flow is: read the OBJ text, find its `mtllib` references, read each
|
|
12
|
+
* library, publish every texture the library names as an object URL, and hand
|
|
13
|
+
* `OBJLoader` a `LoadingManager` that answers from those URLs. A missing library
|
|
14
|
+
* or texture leaves the geometry intact and adds one line to the status, which is
|
|
15
|
+
* the "retain the available geometry" half of the same requirement.
|
|
16
|
+
*
|
|
17
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/obj
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { Group } from 'three'
|
|
21
|
+
import { MTLLoader } from 'three/addons/loaders/MTLLoader.js'
|
|
22
|
+
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'
|
|
23
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
24
|
+
import {
|
|
25
|
+
createCompanions, decodeText, directoryOf, mtlTextureReferences, objLibraryReferences, whenIdle,
|
|
26
|
+
} from '../companions.js'
|
|
27
|
+
import { disposerFor, hasGeometry, summarize } from '../mesh.js'
|
|
28
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
29
|
+
|
|
30
|
+
/** Trailing-slash directory spelling the loaders concatenate onto a name. */
|
|
31
|
+
function baseOf(path: string | undefined): string {
|
|
32
|
+
if (path === undefined) return ''
|
|
33
|
+
const directory = directoryOf(path)
|
|
34
|
+
return directory === '' ? '' : `${directory}/`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const objAdapter: ModelFormatAdapter = {
|
|
38
|
+
id: 'obj',
|
|
39
|
+
extensions: ['obj'],
|
|
40
|
+
label: 'OBJ',
|
|
41
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
42
|
+
const text = decodeText(source.bytes)
|
|
43
|
+
const companions = createCompanions({ path: source.path, read: source.readCompanion, signal: source.signal })
|
|
44
|
+
const warnings: string[] = []
|
|
45
|
+
let object: Group
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
let materials: ReturnType<MTLLoader['parse']> | undefined
|
|
49
|
+
let textures = 0
|
|
50
|
+
for (const reference of objLibraryReferences(text)) {
|
|
51
|
+
const file = await companions.readBytes(reference)
|
|
52
|
+
if (file === undefined) {
|
|
53
|
+
// Named in the file, unreadable on disk: geometry survives, colour does not.
|
|
54
|
+
warnings.push(reference)
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
const libraryText = decodeText(file.bytes)
|
|
58
|
+
// Textures are resolved against the library, not against the OBJ — the
|
|
59
|
+
// two are usually in the same folder, but that is not a rule OBJ states.
|
|
60
|
+
for (const texture of mtlTextureReferences(libraryText)) {
|
|
61
|
+
const asset = await companions.readBytes(texture, file.path)
|
|
62
|
+
if (asset === undefined) {
|
|
63
|
+
warnings.push(texture)
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
companions.publish(asset)
|
|
67
|
+
textures += 1
|
|
68
|
+
}
|
|
69
|
+
const creator = new MTLLoader(companions.manager).parse(libraryText, baseOf(file.path))
|
|
70
|
+
// Loads the maps through the manager now, so every texture is requested
|
|
71
|
+
// against a URL this module has already published.
|
|
72
|
+
creator.preload()
|
|
73
|
+
materials = creator
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The view draws once, immediately after this returns, so the images have
|
|
77
|
+
// to be decoded before that frame or the model appears untextured.
|
|
78
|
+
if (textures > 0) await whenIdle(companions.manager)
|
|
79
|
+
|
|
80
|
+
const loader = new OBJLoader(companions.manager)
|
|
81
|
+
if (materials !== undefined) loader.setMaterials(materials)
|
|
82
|
+
object = loader.parse(text)
|
|
83
|
+
} catch (error) {
|
|
84
|
+
companions.dispose()
|
|
85
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!hasGeometry(object)) {
|
|
89
|
+
companions.dispose()
|
|
90
|
+
throw new DocumentRenderError('parse', 'no faces in this OBJ')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
object,
|
|
95
|
+
format: 'obj',
|
|
96
|
+
metadata: { units: 'unknown', unitSource: 'unknown', ...summarize(object), warnings },
|
|
97
|
+
dispose: () => {
|
|
98
|
+
disposerFor(object)()
|
|
99
|
+
companions.dispose()
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PLY adapter — polygon meshes, ASCII and binary, with vertex colours when the
|
|
3
|
+
* file carries them.
|
|
4
|
+
*
|
|
5
|
+
* PLY is the P2 "if practical" format. `PLYLoader` ships with the installed
|
|
6
|
+
* Three.js and returns a `BufferGeometry` whose `color` attribute holds the
|
|
7
|
+
* per-vertex colours a scanned or printed mesh usually carries, so supporting it
|
|
8
|
+
* costs one wrapper and delivers the requirement with no new dependency.
|
|
9
|
+
*
|
|
10
|
+
* PLY declares no unit, so the unit stays unknown (§5.7).
|
|
11
|
+
*
|
|
12
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/ply
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Group, Mesh } from 'three'
|
|
16
|
+
import { PLYLoader } from 'three/addons/loaders/PLYLoader.js'
|
|
17
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
18
|
+
import { disposerFor, importedMaterial, summarize, toArrayBuffer } from '../mesh.js'
|
|
19
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
20
|
+
|
|
21
|
+
export const plyAdapter: ModelFormatAdapter = {
|
|
22
|
+
id: 'ply',
|
|
23
|
+
extensions: ['ply'],
|
|
24
|
+
label: 'PLY',
|
|
25
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
26
|
+
let geometry
|
|
27
|
+
try {
|
|
28
|
+
geometry = new PLYLoader().parse(toArrayBuffer(source.bytes))
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
31
|
+
}
|
|
32
|
+
const position = geometry.getAttribute('position')
|
|
33
|
+
if (position === undefined || (position.count ?? 0) === 0) {
|
|
34
|
+
throw new DocumentRenderError('parse', 'no vertices in this PLY')
|
|
35
|
+
}
|
|
36
|
+
// A point cloud alone cannot be shaded; the loader leaves normals out when the
|
|
37
|
+
// file has faces only as indices, so compute them when they are missing.
|
|
38
|
+
if (geometry.getAttribute('normal') === undefined) geometry.computeVertexNormals()
|
|
39
|
+
const vertexColors = geometry.getAttribute('color') !== undefined
|
|
40
|
+
const group = new Group()
|
|
41
|
+
group.add(new Mesh(geometry, importedMaterial(vertexColors)))
|
|
42
|
+
return {
|
|
43
|
+
object: group,
|
|
44
|
+
format: 'ply',
|
|
45
|
+
metadata: { units: 'unknown', unitSource: 'unknown', ...summarize(group), warnings: [] },
|
|
46
|
+
dispose: disposerFor(group),
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* STL adapter — the 3D-printing interchange format, binary and ASCII.
|
|
3
|
+
*
|
|
4
|
+
* `STLLoader.parse` accepts an `ArrayBuffer` and decides binary versus ASCII
|
|
5
|
+
* itself: it reads the triangle count at offset 80 and compares it with the
|
|
6
|
+
* expected length, then falls back to looking for the `solid` keyword. Both
|
|
7
|
+
* spellings of the task's P0 requirement (§2.1) therefore take the same path.
|
|
8
|
+
*
|
|
9
|
+
* Units are the honest gap: STL has no unit field and no convention. The model
|
|
10
|
+
* is shown at its own scale and the status line says the unit is unknown, which
|
|
11
|
+
* is what §5.7 asks for instead of quietly assuming millimetres.
|
|
12
|
+
*
|
|
13
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/stl
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { STLLoader } from 'three/addons/loaders/STLLoader.js'
|
|
17
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
18
|
+
import { disposerFor, importedMaterial, summarize, toArrayBuffer } from '../mesh.js'
|
|
19
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
20
|
+
import { Group, Mesh } from 'three'
|
|
21
|
+
|
|
22
|
+
export const stlAdapter: ModelFormatAdapter = {
|
|
23
|
+
id: 'stl',
|
|
24
|
+
extensions: ['stl'],
|
|
25
|
+
label: 'STL',
|
|
26
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
27
|
+
let geometry
|
|
28
|
+
try {
|
|
29
|
+
geometry = new STLLoader().parse(toArrayBuffer(source.bytes))
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
32
|
+
}
|
|
33
|
+
const position = geometry.getAttribute('position')
|
|
34
|
+
if (position === undefined || (position.count ?? 0) === 0) {
|
|
35
|
+
throw new DocumentRenderError('parse', 'no triangles')
|
|
36
|
+
}
|
|
37
|
+
if (geometry.getAttribute('normal') === undefined) geometry.computeVertexNormals()
|
|
38
|
+
// Binary STL may carry per-face colours; honour them when present.
|
|
39
|
+
const vertexColors = geometry.getAttribute('color') !== undefined
|
|
40
|
+
const group = new Group()
|
|
41
|
+
group.add(new Mesh(geometry, importedMaterial(vertexColors)))
|
|
42
|
+
return {
|
|
43
|
+
object: group,
|
|
44
|
+
format: 'stl',
|
|
45
|
+
metadata: { units: 'unknown', unitSource: 'unknown', ...summarize(group), warnings: [] },
|
|
46
|
+
dispose: disposerFor(group),
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 3MF adapter — the P0 additive-manufacturing format.
|
|
3
|
+
*
|
|
4
|
+
* **Why the Three.js loader.** `ThreeMFLoader` ships with the installed
|
|
5
|
+
* `three@0.185.1`, parses from an `ArrayBuffer`, and covers the parts of the core
|
|
6
|
+
* specification this product needs: the build items, their components and
|
|
7
|
+
* component transforms, multiple objects, base materials with their colours, and
|
|
8
|
+
* texture groups. That satisfies §5.3 without adding `lib3mf`: the task is
|
|
9
|
+
* explicit that a maintained loader in the dependency we already ship wins over
|
|
10
|
+
* a new wasm implementation, and that lib3mf is only justified by customer
|
|
11
|
+
* models the loader cannot read.
|
|
12
|
+
*
|
|
13
|
+
* **What it does not do, stated rather than implied:** the loader does *not*
|
|
14
|
+
* scale by the model's `unit` (it parses the attribute and never applies it), it
|
|
15
|
+
* does not support implicit-function geometry beyond data-only parsing, and it
|
|
16
|
+
* ignores production extensions that require a slicer. This adapter therefore
|
|
17
|
+
* reads the unit from the package's own model XML and applies it as one scale on
|
|
18
|
+
* the returned container — a display normalisation, not a geometry rewrite —
|
|
19
|
+
* and reports the rest as a documented limitation (§5.3, format matrix).
|
|
20
|
+
*
|
|
21
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/adapters/threemf
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Group } from 'three'
|
|
25
|
+
import { ThreeMFLoader } from 'three/addons/loaders/3MFLoader.js'
|
|
26
|
+
import { unzipSync } from 'three/addons/libs/fflate.module.js'
|
|
27
|
+
import { DocumentRenderError } from '../../mount.js'
|
|
28
|
+
import { decodeText } from '../companions.js'
|
|
29
|
+
import { disposerFor, hasGeometry, summarize, toArrayBuffer } from '../mesh.js'
|
|
30
|
+
import type { ModelFormatAdapter, ModelLoadResult, ModelSource } from '../types.js'
|
|
31
|
+
import { normalizeUnit, viewerScale } from '../units.js'
|
|
32
|
+
|
|
33
|
+
/** What the package's model part says about itself. */
|
|
34
|
+
interface ModelDeclaration {
|
|
35
|
+
readonly unit: string
|
|
36
|
+
readonly title?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Read `unit` and the title from the package's model XML.
|
|
41
|
+
*
|
|
42
|
+
* A 3MF package is a zip whose model part is named by `_rels/.rels`; in practice
|
|
43
|
+
* every producer writes exactly one `*.model` part, so this reads that one and
|
|
44
|
+
* gives up gracefully (unknown unit, no title) when the package is unusual —
|
|
45
|
+
* the geometry still comes from the loader, which does its own rels lookup.
|
|
46
|
+
* @param bytes - the whole package.
|
|
47
|
+
* @returns the declared unit (3MF defaults to millimetre) and the title.
|
|
48
|
+
*/
|
|
49
|
+
export function readModelDeclaration(bytes: Uint8Array): ModelDeclaration {
|
|
50
|
+
const files = unzipSync(bytes)
|
|
51
|
+
const name = Object.keys(files).find(entry => entry.toLowerCase().endsWith('.model'))
|
|
52
|
+
if (name === undefined) return { unit: 'millimeter' }
|
|
53
|
+
const xml = decodeText(files[name] as Uint8Array)
|
|
54
|
+
const unit = /<model\b[^>]*\bunit\s*=\s*"([^"]*)"/iu.exec(xml)?.[1] ?? 'millimeter'
|
|
55
|
+
const title = /<metadata\b[^>]*\bname\s*=\s*"Title"[^>]*>([^<]*)</iu.exec(xml)?.[1]
|
|
56
|
+
return { unit, ...(title === undefined || title.trim() === '' ? {} : { title: title.trim() }) }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const threeMfAdapter: ModelFormatAdapter = {
|
|
60
|
+
id: '3mf',
|
|
61
|
+
extensions: ['3mf'],
|
|
62
|
+
label: '3MF',
|
|
63
|
+
async load(source: ModelSource): Promise<ModelLoadResult> {
|
|
64
|
+
let declaration: ModelDeclaration
|
|
65
|
+
try {
|
|
66
|
+
declaration = readModelDeclaration(source.bytes)
|
|
67
|
+
} catch (error) {
|
|
68
|
+
throw new DocumentRenderError('parse', `not a 3MF package: ${error instanceof Error ? error.message : String(error)}`)
|
|
69
|
+
}
|
|
70
|
+
let object
|
|
71
|
+
try {
|
|
72
|
+
object = new ThreeMFLoader().parse(toArrayBuffer(source.bytes))
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new DocumentRenderError('parse', error instanceof Error ? error.message : String(error))
|
|
75
|
+
}
|
|
76
|
+
if (!(object instanceof Group)) {
|
|
77
|
+
// A single-part model comes back as one mesh; the adapter's contract is a
|
|
78
|
+
// container either way, and a mesh has no `children` to report.
|
|
79
|
+
const single = new Group()
|
|
80
|
+
single.add(object)
|
|
81
|
+
object = single
|
|
82
|
+
}
|
|
83
|
+
if (!hasGeometry(object)) throw new DocumentRenderError('parse', 'no mesh geometry in this 3MF package')
|
|
84
|
+
|
|
85
|
+
const units = normalizeUnit(declaration.unit)
|
|
86
|
+
// One transform on the container: geometry keeps the coordinates the file
|
|
87
|
+
// wrote, and the viewer's millimetre convention is met by scale alone.
|
|
88
|
+
object.scale.setScalar(viewerScale(units))
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
object,
|
|
92
|
+
format: '3mf',
|
|
93
|
+
metadata: {
|
|
94
|
+
units,
|
|
95
|
+
unitSource: 'declared',
|
|
96
|
+
...(declaration.title === undefined ? {} : { title: declaration.title }),
|
|
97
|
+
...summarize(object),
|
|
98
|
+
warnings: [],
|
|
99
|
+
},
|
|
100
|
+
dispose: disposerFor(object),
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
}
|