@huaqiu/dsh-hardware-ui 0.7.23 → 0.7.25
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,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The internal adapter boundary between the 3D document body and a format.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately tiny and internal: it exists so the MDI body, the viewer and the
|
|
5
|
+
* lifecycle have exactly one thing to know about a file — "give me an
|
|
6
|
+
* `Object3D`, tell me what units it is in, and tell me how to release it" — and
|
|
7
|
+
* so a format can be replaced (3MF with `lib3mf`, say) without touching the
|
|
8
|
+
* document integration (§5.3, §5.6 of the 3D/SolidCraft task).
|
|
9
|
+
*
|
|
10
|
+
* It is not a public package: nothing is exported from the plugin's entry point,
|
|
11
|
+
* because a second consumer would immediately want a second renderer.
|
|
12
|
+
*
|
|
13
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/types
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { Object3D } from 'three'
|
|
17
|
+
|
|
18
|
+
/** Linear unit, spelled the way the format that declared it spells it. */
|
|
19
|
+
export type ModelUnit = 'mm' | 'cm' | 'm' | 'in' | 'ft' | 'um' | 'unknown'
|
|
20
|
+
|
|
21
|
+
/** One loaded model, ready to be mounted. */
|
|
22
|
+
export interface ModelLoadResult {
|
|
23
|
+
/** The model, in the viewer's millimetre convention when the unit was known. */
|
|
24
|
+
readonly object: Object3D
|
|
25
|
+
/** Extension-shaped format name (`stl`, `3mf`, …) for the status line. */
|
|
26
|
+
readonly format: string
|
|
27
|
+
readonly metadata: ModelMetadata
|
|
28
|
+
/**
|
|
29
|
+
* Release the geometries, materials, textures and object URLs this load
|
|
30
|
+
* created. Called once by the body; must be idempotent.
|
|
31
|
+
*/
|
|
32
|
+
dispose(): void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ModelMetadata {
|
|
36
|
+
/** Unit the file declares or its specification fixes; `unknown` when neither. */
|
|
37
|
+
readonly units: ModelUnit
|
|
38
|
+
/**
|
|
39
|
+
* Where {@link units} came from. `declared` is the file's own statement,
|
|
40
|
+
* `format` is its specification (glTF is metres), `unknown` means the format
|
|
41
|
+
* carries none — STL and OBJ above all.
|
|
42
|
+
*/
|
|
43
|
+
readonly unitSource: 'declared' | 'format' | 'unknown'
|
|
44
|
+
/** Model title from the file's metadata, when the format has one. */
|
|
45
|
+
readonly title?: string
|
|
46
|
+
/** Top-level objects the importer produced. */
|
|
47
|
+
readonly objectCount: number
|
|
48
|
+
/** Distinct materials the importer produced. */
|
|
49
|
+
readonly materials: number
|
|
50
|
+
/**
|
|
51
|
+
* Non-blocking problems: a missing MTL, an unreadable texture, a 3MF extension
|
|
52
|
+
* the loader skips. The model still renders; the body states these instead of
|
|
53
|
+
* failing a document that is mostly viewable (§5.4).
|
|
54
|
+
*/
|
|
55
|
+
readonly warnings: readonly string[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Read one file that sits beside the document.
|
|
60
|
+
*
|
|
61
|
+
* Backed by DSH's own authorized workspace read, so a companion file outside the
|
|
62
|
+
* workspace is refused by the Host rather than silently read by this plugin.
|
|
63
|
+
* `undefined` means "could not be read" — missing, refused, too large — which is
|
|
64
|
+
* a warning for the adapter, never an error for the document.
|
|
65
|
+
*/
|
|
66
|
+
export type CompanionReader = (relative: string, signal: AbortSignal) => Promise<Uint8Array | undefined>
|
|
67
|
+
|
|
68
|
+
/** Everything an adapter may use. */
|
|
69
|
+
export interface ModelSource {
|
|
70
|
+
/** Exact document bytes; the file itself is never written back. */
|
|
71
|
+
readonly bytes: Uint8Array
|
|
72
|
+
/** Absolute path the Host reported, when it reported one. */
|
|
73
|
+
readonly path?: string
|
|
74
|
+
readonly signal: AbortSignal
|
|
75
|
+
/** Companion reader, absent when the Host path is unknown. */
|
|
76
|
+
readonly readCompanion?: CompanionReader
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** One format implementation. */
|
|
80
|
+
export interface ModelFormatAdapter {
|
|
81
|
+
/** Stable id, also the registry key. */
|
|
82
|
+
readonly id: string
|
|
83
|
+
/** Suffixes this adapter claims, lower case, no dot. */
|
|
84
|
+
readonly extensions: readonly string[]
|
|
85
|
+
/** Human-facing format name for the status line. */
|
|
86
|
+
readonly label: string
|
|
87
|
+
/** Parse the bytes into a model. Rejects with `DocumentRenderError`. */
|
|
88
|
+
load(source: ModelSource): Promise<ModelLoadResult>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A global byte ceiling shared by every format, matching the STEP reader's gate. */
|
|
92
|
+
export const MAX_MODEL_BYTES = 64 * 1024 * 1024
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Units and the viewer's millimetre convention.
|
|
3
|
+
*
|
|
4
|
+
* §5.7 of the 3D/SolidCraft task is the requirement this module exists for, and
|
|
5
|
+
* it is a correctness requirement rather than a cosmetic one:
|
|
6
|
+
*
|
|
7
|
+
* - STEP and 3MF may declare their unit; glTF's specification fixes it at
|
|
8
|
+
* metres; STL and OBJ carry none at all.
|
|
9
|
+
* - The viewer draws in millimetres so one model can be compared with the next,
|
|
10
|
+
* but "unknown" must stay unknown. Scaling an unreferenced STL by 1 is honest;
|
|
11
|
+
* calling it millimetres is not, and the status line says so.
|
|
12
|
+
* - Nothing here rewrites geometry. A known unit becomes one uniform scale on
|
|
13
|
+
* the container the adapter returns, which is the cheapest transform that
|
|
14
|
+
* cannot introduce the precision errors a per-vertex rewrite would.
|
|
15
|
+
*
|
|
16
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/units
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { ModelUnit } from './types.js'
|
|
20
|
+
|
|
21
|
+
/** Millimetres per unit, for the units CAD and 3D-printing files actually use. */
|
|
22
|
+
const MM_PER_UNIT: Readonly<Record<Exclude<ModelUnit, 'unknown'>, number>> = Object.freeze({
|
|
23
|
+
mm: 1,
|
|
24
|
+
cm: 10,
|
|
25
|
+
m: 1000,
|
|
26
|
+
in: 25.4,
|
|
27
|
+
ft: 304.8,
|
|
28
|
+
um: 0.001,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Normalise a unit spelling to the viewer's vocabulary.
|
|
33
|
+
*
|
|
34
|
+
* Formats spell the same unit differently — 3MF uses `millimeter`, glTF's spec
|
|
35
|
+
* says `metre`, AMF says `micron` — so the mapping is explicit and anything not
|
|
36
|
+
* listed is unknown rather than guessed.
|
|
37
|
+
* @param raw - the unit token as the file wrote it.
|
|
38
|
+
* @returns the recognised unit, or `unknown` for anything else.
|
|
39
|
+
*/
|
|
40
|
+
export function normalizeUnit(raw: string | undefined): ModelUnit {
|
|
41
|
+
if (raw === undefined) return 'unknown'
|
|
42
|
+
switch (raw.trim().toLowerCase()) {
|
|
43
|
+
case 'mm':
|
|
44
|
+
case 'millimeter':
|
|
45
|
+
case 'millimetre':
|
|
46
|
+
case 'millimeters':
|
|
47
|
+
case 'millimetres':
|
|
48
|
+
return 'mm'
|
|
49
|
+
case 'cm':
|
|
50
|
+
case 'centimeter':
|
|
51
|
+
case 'centimetre':
|
|
52
|
+
return 'cm'
|
|
53
|
+
case 'm':
|
|
54
|
+
case 'meter':
|
|
55
|
+
case 'metre':
|
|
56
|
+
case 'meters':
|
|
57
|
+
case 'metres':
|
|
58
|
+
return 'm'
|
|
59
|
+
case 'in':
|
|
60
|
+
case 'inch':
|
|
61
|
+
case 'inches':
|
|
62
|
+
return 'in'
|
|
63
|
+
case 'ft':
|
|
64
|
+
case 'foot':
|
|
65
|
+
case 'feet':
|
|
66
|
+
return 'ft'
|
|
67
|
+
case 'um':
|
|
68
|
+
case 'micron':
|
|
69
|
+
case 'micrometer':
|
|
70
|
+
case 'micrometre':
|
|
71
|
+
return 'um'
|
|
72
|
+
default:
|
|
73
|
+
return 'unknown'
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Scale factor that brings one unit to millimetres.
|
|
79
|
+
* @param unit - a recognised unit, or `unknown`.
|
|
80
|
+
* @returns millimetres per unit, or 1 when the unit is unknown (no scaling, no claim).
|
|
81
|
+
*/
|
|
82
|
+
export function millimetresPerUnit(unit: ModelUnit): number {
|
|
83
|
+
return unit === 'unknown' ? 1 : MM_PER_UNIT[unit]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Should the viewer scale this model, and by how much?
|
|
88
|
+
* @param unit - the unit the adapter resolved.
|
|
89
|
+
* @returns the factor to apply to the model container.
|
|
90
|
+
*/
|
|
91
|
+
export function viewerScale(unit: ModelUnit): number {
|
|
92
|
+
return millimetresPerUnit(unit)
|
|
93
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one Three.js scene every 3D document is drawn in.
|
|
3
|
+
*
|
|
4
|
+
* This module is `step.ts`'s scene, camera rig and disposal logic lifted out
|
|
5
|
+
* verbatim so a second format can reuse them instead of shipping a second
|
|
6
|
+
* renderer (§3.3 of the 3D/SolidCraft task). The behaviour is deliberately
|
|
7
|
+
* unchanged: same light rig, same fitted perspective camera, same orbit
|
|
8
|
+
* controls, same single deferred draw, same teardown order — a STEP document
|
|
9
|
+
* must look and cost exactly what it did before.
|
|
10
|
+
*
|
|
11
|
+
* What the caller supplies is only the model: an `Object3D` already built by a
|
|
12
|
+
* format adapter, plus the function that releases whatever GPU resources that
|
|
13
|
+
* model owns. Nothing here knows what a STEP or a 3MF is.
|
|
14
|
+
*
|
|
15
|
+
* Ownership is explicit because a document tab is opened and closed constantly
|
|
16
|
+
* and browsers cap live WebGL contexts (Chrome drops the oldest at ~16): the
|
|
17
|
+
* view owns the renderer, the canvas, the controls and the observers, and calls
|
|
18
|
+
* back into the caller for the geometries, materials and textures it did not
|
|
19
|
+
* create.
|
|
20
|
+
*
|
|
21
|
+
* @module @huaqiu/dsh-hardware-ui/client/document/model/viewer
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
AmbientLight, Box3, DirectionalLight, HemisphereLight, PerspectiveCamera, Scene, Vector3, WebGLRenderer,
|
|
26
|
+
type Object3D,
|
|
27
|
+
} from 'three'
|
|
28
|
+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
|
|
29
|
+
import { DocumentRenderError } from '../mount.js'
|
|
30
|
+
|
|
31
|
+
/** A live 3D view. */
|
|
32
|
+
export interface MountedModelView {
|
|
33
|
+
/** Release every GPU resource, listener, observer and element this view created. Idempotent. */
|
|
34
|
+
dispose(): void
|
|
35
|
+
/** Re-fit the camera to the model bounds. */
|
|
36
|
+
fit(): void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ModelViewRequest {
|
|
40
|
+
/** The model, already in the viewer's millimetre convention. */
|
|
41
|
+
readonly object: Object3D
|
|
42
|
+
/** Element the canvas is appended to; must have a measurable box. */
|
|
43
|
+
readonly container: HTMLElement
|
|
44
|
+
/**
|
|
45
|
+
* Release the model's own geometries, materials and textures. Called exactly
|
|
46
|
+
* once, on dispose or when the view cannot be created.
|
|
47
|
+
*/
|
|
48
|
+
readonly disposeModel: () => void
|
|
49
|
+
/** `dataset` key that marks this canvas (`stepCanvas`, `modelCanvas`). */
|
|
50
|
+
readonly canvasAttribute: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function messageOf(error: unknown): string {
|
|
54
|
+
return error instanceof Error ? error.message : typeof error === 'string' ? error : 'unknown error'
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Create the renderer, camera, controls and light rig, and draw one frame.
|
|
59
|
+
* @param request - model, container, model disposer, canvas marker.
|
|
60
|
+
* @returns the live view; call `dispose()` when the document closes or reloads.
|
|
61
|
+
*/
|
|
62
|
+
export function mountModelView(request: ModelViewRequest): MountedModelView {
|
|
63
|
+
const { object, container, disposeModel, canvasAttribute } = request
|
|
64
|
+
const scene = new Scene()
|
|
65
|
+
scene.add(object)
|
|
66
|
+
|
|
67
|
+
scene.add(new AmbientLight(0xffffff, 0.55))
|
|
68
|
+
scene.add(new HemisphereLight(0xffffff, 0x30343c, 1.1))
|
|
69
|
+
const key = new DirectionalLight(0xffffff, 2.1)
|
|
70
|
+
key.position.set(1, 1.6, 1.1)
|
|
71
|
+
scene.add(key)
|
|
72
|
+
const fill = new DirectionalLight(0xffffff, 0.9)
|
|
73
|
+
fill.position.set(-1.2, -0.8, -0.7)
|
|
74
|
+
scene.add(fill)
|
|
75
|
+
|
|
76
|
+
let renderer: WebGLRenderer
|
|
77
|
+
try {
|
|
78
|
+
renderer = new WebGLRenderer({ antialias: true, alpha: true })
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// Nothing else holds the model by this point; leaving it to the caller's
|
|
81
|
+
// `catch` would leak every geometry the adapter just built.
|
|
82
|
+
disposeModel()
|
|
83
|
+
throw new DocumentRenderError('render', `webgl unavailable: ${messageOf(error)}`)
|
|
84
|
+
}
|
|
85
|
+
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2))
|
|
86
|
+
|
|
87
|
+
const bounds = new Box3().setFromObject(object)
|
|
88
|
+
const center = bounds.getCenter(new Vector3())
|
|
89
|
+
const size = bounds.getSize(new Vector3())
|
|
90
|
+
const radius = Math.max(size.x, size.y, size.z, 0.001) / 2
|
|
91
|
+
const camera = new PerspectiveCamera(45, 1, Math.max(radius / 1000, 0.01), radius * 1000)
|
|
92
|
+
|
|
93
|
+
let frame = 0
|
|
94
|
+
const draw = (): void => {
|
|
95
|
+
if (frame !== 0) return
|
|
96
|
+
frame = requestAnimationFrame(() => {
|
|
97
|
+
frame = 0
|
|
98
|
+
renderer.render(scene, camera)
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const controls = new OrbitControls(camera, renderer.domElement)
|
|
103
|
+
controls.enableDamping = false
|
|
104
|
+
controls.screenSpacePanning = true
|
|
105
|
+
|
|
106
|
+
const fit = (): void => {
|
|
107
|
+
const distance = (radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.15
|
|
108
|
+
const direction = new Vector3(0.9, 0.65, 1).normalize()
|
|
109
|
+
camera.position.copy(center).addScaledVector(direction, distance)
|
|
110
|
+
camera.near = Math.max(radius / 1000, 0.01)
|
|
111
|
+
camera.far = distance * 20
|
|
112
|
+
camera.updateProjectionMatrix()
|
|
113
|
+
controls.target.copy(center)
|
|
114
|
+
controls.update()
|
|
115
|
+
draw()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const canvas = renderer.domElement
|
|
119
|
+
canvas.style.cssText = 'position:absolute;inset:0;display:block;width:100%;height:100%;outline:none;touch-action:none'
|
|
120
|
+
canvas.dataset[canvasAttribute] = ''
|
|
121
|
+
container.append(canvas)
|
|
122
|
+
|
|
123
|
+
const resize = (): void => {
|
|
124
|
+
const width = Math.max(container.clientWidth, 1)
|
|
125
|
+
const height = Math.max(container.clientHeight, 1)
|
|
126
|
+
renderer.setSize(width, height, false)
|
|
127
|
+
camera.aspect = width / height
|
|
128
|
+
camera.updateProjectionMatrix()
|
|
129
|
+
draw()
|
|
130
|
+
}
|
|
131
|
+
const onControls = (): void => { draw() }
|
|
132
|
+
const onDoubleClick = (): void => { fit() }
|
|
133
|
+
controls.addEventListener('change', onControls)
|
|
134
|
+
renderer.domElement.addEventListener('dblclick', onDoubleClick)
|
|
135
|
+
const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(resize)
|
|
136
|
+
observer?.observe(container)
|
|
137
|
+
resize()
|
|
138
|
+
fit()
|
|
139
|
+
|
|
140
|
+
let disposed = false
|
|
141
|
+
return {
|
|
142
|
+
fit,
|
|
143
|
+
dispose: () => {
|
|
144
|
+
if (disposed) return
|
|
145
|
+
disposed = true
|
|
146
|
+
if (frame !== 0) cancelAnimationFrame(frame)
|
|
147
|
+
observer?.disconnect()
|
|
148
|
+
controls.removeEventListener('change', onControls)
|
|
149
|
+
controls.dispose()
|
|
150
|
+
renderer.domElement.removeEventListener('dblclick', onDoubleClick)
|
|
151
|
+
disposeModel()
|
|
152
|
+
scene.clear()
|
|
153
|
+
// A Group detaches its children here; a Mesh has nothing to detach, so the
|
|
154
|
+
// call is optional rather than required by the type.
|
|
155
|
+
;(object as { clear?: () => void }).clear?.()
|
|
156
|
+
renderer.dispose()
|
|
157
|
+
canvas.remove()
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -30,20 +30,53 @@
|
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
32
|
import { BoardParser, SchematicParser, type boardProto, type schematicProto } from '@huaqiu/kicad-sexpr-parser'
|
|
33
|
-
import { renderPcb, renderSchematic } from '@huaqiu/ecad-renderer'
|
|
33
|
+
import { renderFootprint, renderPcb, renderSchematic, renderSymbol } from '@huaqiu/ecad-renderer'
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Kinds of ECAD document this adapter renders.
|
|
37
|
+
*
|
|
38
|
+
* Four, not two: a `.kicad_sym` library and a `.kicad_mod` footprint are as
|
|
39
|
+
* much a previewable document as a sheet or a board, and the two renderer
|
|
40
|
+
* entry points for them are thin adapters over the same schematic/board
|
|
41
|
+
* viewers — same canvas, same fit, same disposal.
|
|
42
|
+
*/
|
|
43
|
+
export type EcadDocumentKind = 'schematic' | 'pcb' | 'symbol' | 'footprint'
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Kinds whose whole file is one model the parser reads directly.
|
|
47
|
+
*
|
|
48
|
+
* The other two are *library* files: a `.kicad_sym` holds many symbols under a
|
|
49
|
+
* `kicad_symbol_lib` root, and a `.kicad_mod` is a bare footprint that only
|
|
50
|
+
* parses inside a board. Both need a selection step before there is a model to
|
|
51
|
+
* draw, so they are not accepted by `parseEcadSource`.
|
|
52
|
+
*/
|
|
53
|
+
export type EcadWholeDocumentKind = 'schematic' | 'pcb'
|
|
37
54
|
|
|
38
55
|
/** A renderer attached to a live document body. */
|
|
39
56
|
export interface MountedEcadDocument {
|
|
40
57
|
/** Release the renderer and the canvas it was mounted into. Idempotent. */
|
|
41
58
|
dispose(): void
|
|
59
|
+
/**
|
|
60
|
+
* Symbol names in file order for a `.kicad_sym`, empty for every other kind.
|
|
61
|
+
*
|
|
62
|
+
* Returned by the mount rather than parsed again by the body so the picker
|
|
63
|
+
* and the picture cannot disagree about which library was read.
|
|
64
|
+
*/
|
|
65
|
+
readonly symbols: readonly string[]
|
|
42
66
|
}
|
|
43
67
|
|
|
44
68
|
/** Why a mount failed, without losing the renderer's own message. */
|
|
45
69
|
export class DocumentRenderError extends Error {
|
|
46
|
-
|
|
70
|
+
/**
|
|
71
|
+
* `missing-companion` means the document is intact but a file it depends on
|
|
72
|
+
* cannot be read — a glTF's `.bin`, say — and for those the `message` is the
|
|
73
|
+
* reference the document declared rather than prose, so a body can name it.
|
|
74
|
+
*
|
|
75
|
+
* `empty` is not a parse failure: the file is a well-formed library that
|
|
76
|
+
* declares nothing to draw, which is a different sentence to the user and a
|
|
77
|
+
* different next step (pick another file, not fix this one).
|
|
78
|
+
*/
|
|
79
|
+
readonly reason: 'parse' | 'render' | 'timeout' | 'aborted' | 'toolarge' | 'missing-companion' | 'empty'
|
|
47
80
|
|
|
48
81
|
constructor(reason: DocumentRenderError['reason'], message: string) {
|
|
49
82
|
super(message)
|
|
@@ -174,7 +207,7 @@ function createCanvas(container: HTMLElement): HTMLCanvasElement {
|
|
|
174
207
|
}
|
|
175
208
|
|
|
176
209
|
/** Parse ECAD source text with the shared parser, reporting the parser's own message. */
|
|
177
|
-
export function parseEcadSource(kind:
|
|
210
|
+
export function parseEcadSource(kind: EcadWholeDocumentKind, text: string): schematicProto.I_KicadSch | boardProto.I_KicadPCB {
|
|
178
211
|
try {
|
|
179
212
|
return kind === 'schematic' ? new SchematicParser().parse(text) : new BoardParser().parse(text)
|
|
180
213
|
} catch (error) {
|
|
@@ -182,6 +215,51 @@ export function parseEcadSource(kind: EcadDocumentKind, text: string): schematic
|
|
|
182
215
|
}
|
|
183
216
|
}
|
|
184
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Read every top-level symbol a `.kicad_sym` library declares, in file order.
|
|
220
|
+
*
|
|
221
|
+
* A library is the file, a symbol is the document: the picker in the body is
|
|
222
|
+
* what turns "this library" into "the symbol you asked for". Declaring none is
|
|
223
|
+
* `empty`, not a parse failure.
|
|
224
|
+
*/
|
|
225
|
+
export function parseSymbolLibrary(text: string): readonly schematicProto.I_LibSymbol[] {
|
|
226
|
+
let symbols: schematicProto.I_LibSymbol[]
|
|
227
|
+
try {
|
|
228
|
+
symbols = new SchematicParser().parseLibSymbols(text)
|
|
229
|
+
} catch (error) {
|
|
230
|
+
throw new DocumentRenderError('parse', messageOf(error))
|
|
231
|
+
}
|
|
232
|
+
if (symbols.length === 0) {
|
|
233
|
+
throw new DocumentRenderError('empty', 'the library declares no symbols')
|
|
234
|
+
}
|
|
235
|
+
return symbols
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* A `.kicad_mod` root is a bare `(footprint …)`; the board parser only reads
|
|
240
|
+
* footprints as board children, so the file is given the smallest board that
|
|
241
|
+
* can hold it. The wrapper carries no layers, so the renderer falls back to its
|
|
242
|
+
* documented default stack — the same fallback a real board without `layers`
|
|
243
|
+
* gets, not a second opinion about what layers a footprint sits on.
|
|
244
|
+
*/
|
|
245
|
+
const FOOTPRINT_BOARD_PREFIX = '(kicad_pcb (version 20240108) (generator "huaqiu-dsh") '
|
|
246
|
+
|
|
247
|
+
/** Read the single footprint a `.kicad_mod` declares. */
|
|
248
|
+
export function parseFootprintLibrary(text: string): boardProto.I_Footprint {
|
|
249
|
+
// A board renamed to `.kicad_mod` is parsed as itself rather than wrapped a
|
|
250
|
+
// second time; the first footprint is the same answer either way.
|
|
251
|
+
const source = /^\s*\(\s*kicad_pcb\b/u.test(text) ? text : `${FOOTPRINT_BOARD_PREFIX}${text})`
|
|
252
|
+
let board: boardProto.I_KicadPCB
|
|
253
|
+
try {
|
|
254
|
+
board = new BoardParser().parse(source)
|
|
255
|
+
} catch (error) {
|
|
256
|
+
throw new DocumentRenderError('parse', messageOf(error))
|
|
257
|
+
}
|
|
258
|
+
const footprint = board.footprints?.[0]
|
|
259
|
+
if (footprint === undefined) throw new DocumentRenderError('empty', 'the file declares no footprint')
|
|
260
|
+
return footprint
|
|
261
|
+
}
|
|
262
|
+
|
|
185
263
|
export interface EcadDocumentRequest {
|
|
186
264
|
readonly kind: EcadDocumentKind
|
|
187
265
|
/** Decoded document text; the file itself stays authoritative and is never written back. */
|
|
@@ -192,8 +270,32 @@ export interface EcadDocumentRequest {
|
|
|
192
270
|
readonly timeoutMs?: number
|
|
193
271
|
/** Parsed model, when the caller already parsed it to read the sheet hierarchy. */
|
|
194
272
|
readonly parsed?: schematicProto.I_KicadSch | boardProto.I_KicadPCB
|
|
273
|
+
/**
|
|
274
|
+
* Zero-based symbol to draw from a library, clamped into range.
|
|
275
|
+
*
|
|
276
|
+
* Clamping rather than rejecting: the selection lives in the body and can
|
|
277
|
+
* outlive a reload that shrank the library, and drawing the first symbol is a
|
|
278
|
+
* better answer than an error the user did not cause.
|
|
279
|
+
*/
|
|
280
|
+
readonly symbolIndex?: number
|
|
195
281
|
}
|
|
196
282
|
|
|
283
|
+
/** Where one renderer draws: our sized canvas inside the document's box. */
|
|
284
|
+
interface RenderTarget {
|
|
285
|
+
readonly canvas: HTMLCanvasElement
|
|
286
|
+
readonly container: HTMLElement
|
|
287
|
+
readonly interactive: true
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* What every renderer entry point resolves to.
|
|
292
|
+
*
|
|
293
|
+
* Derived from the schematic entry point because all four share its
|
|
294
|
+
* `RenderResult` shape; deriving it rather than importing the interface keeps
|
|
295
|
+
* this module's dependency on the renderer's runtime exports alone.
|
|
296
|
+
*/
|
|
297
|
+
type PendingRender = Awaited<ReturnType<typeof renderSchematic>>
|
|
298
|
+
|
|
197
299
|
/**
|
|
198
300
|
* Parse, render and attach one ECAD document.
|
|
199
301
|
* @param request - content, target container, lifetime and deadline.
|
|
@@ -211,16 +313,32 @@ export async function mountEcadDocument(request: EcadDocumentRequest): Promise<M
|
|
|
211
313
|
await new Promise<void>((resolve) => { requestAnimationFrame(() => { resolve() }) })
|
|
212
314
|
if (signal.aborted) throw abortError(signal)
|
|
213
315
|
|
|
214
|
-
|
|
316
|
+
// One model per kind, resolved before the canvas exists so a library with
|
|
317
|
+
// nothing in it fails as `empty` instead of mounting an empty viewer.
|
|
318
|
+
const symbols = kind === 'symbol' ? parseSymbolLibrary(text) : []
|
|
319
|
+
let start: (target: RenderTarget) => Promise<PendingRender>
|
|
320
|
+
if (kind === 'schematic') {
|
|
321
|
+
const schematic = (request.parsed ?? parseEcadSource('schematic', text)) as schematicProto.I_KicadSch
|
|
322
|
+
start = target => renderSchematic(schematic, target)
|
|
323
|
+
} else if (kind === 'pcb') {
|
|
324
|
+
const board = (request.parsed ?? parseEcadSource('pcb', text)) as boardProto.I_KicadPCB
|
|
325
|
+
start = target => renderPcb(board, target)
|
|
326
|
+
} else if (kind === 'footprint') {
|
|
327
|
+
const footprint = parseFootprintLibrary(text)
|
|
328
|
+
start = target => renderFootprint(footprint, target)
|
|
329
|
+
} else {
|
|
330
|
+
const index = Math.min(Math.max(request.symbolIndex ?? 0, 0), symbols.length - 1)
|
|
331
|
+
const symbol = symbols[index]
|
|
332
|
+
if (symbol === undefined) throw new DocumentRenderError('empty', 'the library declares no symbols')
|
|
333
|
+
start = target => renderSymbol(symbol, target)
|
|
334
|
+
}
|
|
215
335
|
if (signal.aborted) throw abortError(signal)
|
|
216
336
|
|
|
217
337
|
const canvas = createCanvas(container)
|
|
218
338
|
let rendered
|
|
219
339
|
try {
|
|
220
340
|
rendered = await withinDeadline(
|
|
221
|
-
|
|
222
|
-
? renderSchematic(parsed as schematicProto.I_KicadSch, { canvas, container, interactive: true })
|
|
223
|
-
: renderPcb(parsed as boardProto.I_KicadPCB, { canvas, container, interactive: true }),
|
|
341
|
+
start({ canvas, container, interactive: true }),
|
|
224
342
|
signal,
|
|
225
343
|
timeoutMs,
|
|
226
344
|
)
|
|
@@ -257,5 +375,6 @@ export async function mountEcadDocument(request: EcadDocumentRequest): Promise<M
|
|
|
257
375
|
rendered.dispose()
|
|
258
376
|
canvas.remove()
|
|
259
377
|
},
|
|
378
|
+
symbols: symbols.map(symbol => symbol.name),
|
|
260
379
|
}
|
|
261
380
|
}
|
|
@@ -24,12 +24,11 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
import {
|
|
27
|
-
|
|
28
|
-
HemisphereLight, Mesh, MeshStandardMaterial, PerspectiveCamera, Scene, Vector3, WebGLRenderer,
|
|
27
|
+
BufferGeometry, Color, DoubleSide, Float32BufferAttribute, Group, Mesh, MeshStandardMaterial,
|
|
29
28
|
} from 'three'
|
|
30
|
-
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
|
|
31
29
|
import occtimportjs, { type OcctModule } from 'occt-import-js'
|
|
32
30
|
import { DocumentRenderError, waitForLayout, DEFAULT_RENDER_TIMEOUT_MS } from './mount.js'
|
|
31
|
+
import { mountModelView, type MountedModelView } from './model/viewer.js'
|
|
33
32
|
|
|
34
33
|
/** A live 3D document body. */
|
|
35
34
|
export interface MountedStepDocument {
|
|
@@ -131,7 +130,6 @@ export async function mountStepDocument(request: StepDocumentRequest): Promise<M
|
|
|
131
130
|
throw new DocumentRenderError('parse', 'no solid geometry')
|
|
132
131
|
}
|
|
133
132
|
|
|
134
|
-
const scene = new Scene()
|
|
135
133
|
const group = new Group()
|
|
136
134
|
const geometries: BufferGeometry[] = []
|
|
137
135
|
const materials: MeshStandardMaterial[] = []
|
|
@@ -160,96 +158,18 @@ export async function mountStepDocument(request: StepDocumentRequest): Promise<M
|
|
|
160
158
|
object.name = mesh.name ?? ''
|
|
161
159
|
group.add(object)
|
|
162
160
|
}
|
|
163
|
-
scene.add(group)
|
|
164
161
|
|
|
165
|
-
scene
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
let renderer: WebGLRenderer
|
|
175
|
-
try {
|
|
176
|
-
renderer = new WebGLRenderer({ antialias: true, alpha: true })
|
|
177
|
-
} catch (error) {
|
|
178
|
-
for (const geometry of geometries) geometry.dispose()
|
|
179
|
-
for (const material of materials) material.dispose()
|
|
180
|
-
throw new DocumentRenderError('render', `webgl unavailable: ${messageOf(error)}`)
|
|
181
|
-
}
|
|
182
|
-
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2))
|
|
183
|
-
|
|
184
|
-
const bounds = new Box3().setFromObject(group)
|
|
185
|
-
const center = bounds.getCenter(new Vector3())
|
|
186
|
-
const size = bounds.getSize(new Vector3())
|
|
187
|
-
const radius = Math.max(size.x, size.y, size.z, 0.001) / 2
|
|
188
|
-
const camera = new PerspectiveCamera(45, 1, Math.max(radius / 1000, 0.01), radius * 1000)
|
|
189
|
-
|
|
190
|
-
const fit = (): void => {
|
|
191
|
-
const distance = (radius / Math.sin((camera.fov * Math.PI) / 360)) * 1.15
|
|
192
|
-
const direction = new Vector3(0.9, 0.65, 1).normalize()
|
|
193
|
-
camera.position.copy(center).addScaledVector(direction, distance)
|
|
194
|
-
camera.near = Math.max(radius / 1000, 0.01)
|
|
195
|
-
camera.far = distance * 20
|
|
196
|
-
camera.updateProjectionMatrix()
|
|
197
|
-
controls.target.copy(center)
|
|
198
|
-
controls.update()
|
|
199
|
-
draw()
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const controls = new OrbitControls(camera, renderer.domElement)
|
|
203
|
-
controls.enableDamping = false
|
|
204
|
-
controls.screenSpacePanning = true
|
|
205
|
-
|
|
206
|
-
const canvas = renderer.domElement
|
|
207
|
-
canvas.style.cssText = 'position:absolute;inset:0;display:block;width:100%;height:100%;outline:none;touch-action:none'
|
|
208
|
-
canvas.dataset.stepCanvas = ''
|
|
209
|
-
container.append(canvas)
|
|
210
|
-
|
|
211
|
-
let frame = 0
|
|
212
|
-
const draw = (): void => {
|
|
213
|
-
if (frame !== 0) return
|
|
214
|
-
frame = requestAnimationFrame(() => {
|
|
215
|
-
frame = 0
|
|
216
|
-
renderer.render(scene, camera)
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
const resize = (): void => {
|
|
220
|
-
const width = Math.max(container.clientWidth, 1)
|
|
221
|
-
const height = Math.max(container.clientHeight, 1)
|
|
222
|
-
renderer.setSize(width, height, false)
|
|
223
|
-
camera.aspect = width / height
|
|
224
|
-
camera.updateProjectionMatrix()
|
|
225
|
-
draw()
|
|
226
|
-
}
|
|
227
|
-
const onControls = (): void => { draw() }
|
|
228
|
-
const onDoubleClick = (): void => { fit() }
|
|
229
|
-
controls.addEventListener('change', onControls)
|
|
230
|
-
renderer.domElement.addEventListener('dblclick', onDoubleClick)
|
|
231
|
-
const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(resize)
|
|
232
|
-
observer?.observe(container)
|
|
233
|
-
resize()
|
|
234
|
-
fit()
|
|
235
|
-
|
|
236
|
-
let disposed = false
|
|
237
|
-
return {
|
|
238
|
-
fit,
|
|
239
|
-
dispose: () => {
|
|
240
|
-
if (disposed) return
|
|
241
|
-
disposed = true
|
|
242
|
-
if (frame !== 0) cancelAnimationFrame(frame)
|
|
243
|
-
observer?.disconnect()
|
|
244
|
-
controls.removeEventListener('change', onControls)
|
|
245
|
-
controls.dispose()
|
|
246
|
-
renderer.domElement.removeEventListener('dblclick', onDoubleClick)
|
|
162
|
+
// The scene, camera, controls and teardown live in the shared viewer so every
|
|
163
|
+
// 3D format is drawn by one renderer; only this module's own resources are
|
|
164
|
+
// this module's to release.
|
|
165
|
+
const view: MountedModelView = mountModelView({
|
|
166
|
+
object: group,
|
|
167
|
+
container,
|
|
168
|
+
canvasAttribute: 'stepCanvas',
|
|
169
|
+
disposeModel: () => {
|
|
247
170
|
for (const geometry of geometries) geometry.dispose()
|
|
248
171
|
for (const material of materials) material.dispose()
|
|
249
|
-
scene.clear()
|
|
250
|
-
group.clear()
|
|
251
|
-
renderer.dispose()
|
|
252
|
-
canvas.remove()
|
|
253
172
|
},
|
|
254
|
-
}
|
|
173
|
+
})
|
|
174
|
+
return { fit: view.fit, dispose: view.dispose }
|
|
255
175
|
}
|