dsh-cad-studio 0.2.0 → 0.2.1
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 +7 -1
- package/client.js +17 -15
- package/index.js +54 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,10 +31,16 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
|
|
|
31
31
|
2. **自己装**:`apt install freecad`,然后在插件配置里留空 `freecadPath`(自动探测 `/usr/bin/freecadcmd` 等 5 个位置)或显式指定。
|
|
32
32
|
|
|
33
33
|
```bash
|
|
34
|
+
# 从 npm 装(推荐)
|
|
35
|
+
dsh plugin --profile web add dsh-cad-studio
|
|
36
|
+
|
|
37
|
+
# 或从源码目录装
|
|
34
38
|
dsh plugin --profile web add /path/to/dsh-cad-studio
|
|
35
|
-
#
|
|
39
|
+
# 装完重启 dsh 使 host 侧工具挂载;client 侧刷新页面即可
|
|
36
40
|
```
|
|
37
41
|
|
|
42
|
+
npm 包:<https://www.npmjs.com/package/dsh-cad-studio>(`dsh plugin` / DSH 插件市场按包名解析,发布到 npm 即可被装到)。
|
|
43
|
+
|
|
38
44
|
配置项(cordis 配置):
|
|
39
45
|
|
|
40
46
|
| 键 | 默认 | 说明 |
|
package/client.js
CHANGED
|
@@ -1386,24 +1386,25 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
|
|
|
1386
1386
|
setActivity(act)
|
|
1387
1387
|
}
|
|
1388
1388
|
if (!actId) return
|
|
1389
|
-
|
|
1389
|
+
// A model updates in place: same scene id, new timestamp. Watch the
|
|
1390
|
+
// pair so driving a joint refreshes the viewport instead of being
|
|
1391
|
+
// mistaken for "nothing changed".
|
|
1392
|
+
const stamp = `${actId}@${(act && act.at) || ''}`
|
|
1393
|
+
if (stamp !== seenRef.current) {
|
|
1390
1394
|
const had = Boolean(seenRef.current)
|
|
1391
|
-
seenRef.current =
|
|
1392
|
-
|
|
1395
|
+
seenRef.current = stamp
|
|
1396
|
+
const focused = openTabsRef.current.find((t) => t.key === activeRef.current)
|
|
1397
|
+
const showsIt = activeRef.current === 'activity' || (focused && focused.id === actId)
|
|
1398
|
+
if (showsIt) {
|
|
1399
|
+
const tabKey = activeRef.current
|
|
1393
1400
|
const scene = await loadScene(actId)
|
|
1394
|
-
if (scene && alive && activeRef.current ===
|
|
1395
|
-
rememberView(
|
|
1396
|
-
showScene(scene, { tabKey
|
|
1401
|
+
if (scene && alive && activeRef.current === tabKey) {
|
|
1402
|
+
rememberView(tabKey, payloadRef.current)
|
|
1403
|
+
showScene(scene, { tabKey })
|
|
1397
1404
|
}
|
|
1398
1405
|
} else if (had) {
|
|
1399
1406
|
setFresh(true) // a model tab is focused: flag that activity moved on
|
|
1400
1407
|
}
|
|
1401
|
-
} else if (activeRef.current === 'activity' && visibleIdRef.current !== actId) {
|
|
1402
|
-
const scene = await loadScene(actId)
|
|
1403
|
-
if (scene && alive && activeRef.current === 'activity') {
|
|
1404
|
-
rememberView('activity', payloadRef.current)
|
|
1405
|
-
showScene(scene, { tabKey: 'activity' })
|
|
1406
|
-
}
|
|
1407
1408
|
}
|
|
1408
1409
|
} catch (err) {
|
|
1409
1410
|
const msg = String((err && err.message) || err)
|
|
@@ -1512,9 +1513,10 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
|
|
|
1512
1513
|
// The scene payload carries its own assembly block, so the panel never
|
|
1513
1514
|
// guesses which joints belong to what it is looking at.
|
|
1514
1515
|
const assembly = payload && payload.assembly ? payload.assembly : null
|
|
1515
|
-
const applyScene = async (id) => {
|
|
1516
|
+
const applyScene = async (id, at) => {
|
|
1516
1517
|
const scene = await loadScene(id)
|
|
1517
1518
|
if (!scene) return
|
|
1519
|
+
if (at) seenRef.current = `${id}@${at}`
|
|
1518
1520
|
// Driving a joint or the explode slider is an action the user took on
|
|
1519
1521
|
// the model they are looking at, so that tab follows the republished
|
|
1520
1522
|
// scene; the live tab keeps following the session instead.
|
|
@@ -1538,7 +1540,7 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
|
|
|
1538
1540
|
})
|
|
1539
1541
|
if (!res.ok) return null
|
|
1540
1542
|
const out = await res.json()
|
|
1541
|
-
if (out && out.sceneId) await applyScene(out.sceneId)
|
|
1543
|
+
if (out && out.sceneId) await applyScene(out.sceneId, out.at)
|
|
1542
1544
|
return out
|
|
1543
1545
|
} catch { return null }
|
|
1544
1546
|
}
|
|
@@ -1638,7 +1640,7 @@ const FILE_EXTS = ['step', 'stp', 'iges', 'igs', 'brep', 'stl']
|
|
|
1638
1640
|
return null
|
|
1639
1641
|
}
|
|
1640
1642
|
setFeatError('')
|
|
1641
|
-
if (out.sceneId) await applyScene(out.sceneId)
|
|
1643
|
+
if (out.sceneId) await applyScene(out.sceneId, out.at)
|
|
1642
1644
|
return out
|
|
1643
1645
|
} catch (error) {
|
|
1644
1646
|
setFeatError(String((error && error.message) || error))
|
package/index.js
CHANGED
|
@@ -451,8 +451,17 @@ class FreeCadBridge {
|
|
|
451
451
|
} catch {}
|
|
452
452
|
}
|
|
453
453
|
|
|
454
|
-
|
|
454
|
+
/**
|
|
455
|
+
* Publish a scene for the panel to show.
|
|
456
|
+
*
|
|
457
|
+
* `opts.docKey` names the *model* the scene belongs to (an assembly, a
|
|
458
|
+
* feature model). Re-publishing the same docKey updates that model in place —
|
|
459
|
+
* same scene id, same list row, fresh geometry — instead of stacking a new
|
|
460
|
+
* entry in the models list every time a joint is dragged.
|
|
461
|
+
*/
|
|
462
|
+
async publishScene(scene, title, session = this.currentSession ?? '', opts = {}) {
|
|
455
463
|
if (session) this.lastSession = session
|
|
464
|
+
const docKey = typeof opts.docKey === 'string' ? opts.docKey : ''
|
|
456
465
|
await this.hydrateHistory()
|
|
457
466
|
// Scene ids are the on-disk filenames: after a restart the counter must
|
|
458
467
|
// resume above whatever is already published, or the first new scene would
|
|
@@ -467,7 +476,14 @@ class FreeCadBridge {
|
|
|
467
476
|
}
|
|
468
477
|
} catch {}
|
|
469
478
|
}
|
|
470
|
-
|
|
479
|
+
let id = ''
|
|
480
|
+
if (docKey) {
|
|
481
|
+
const prev = this.history.find((h) => h.docKey === docKey)
|
|
482
|
+
if (prev) id = prev.id
|
|
483
|
+
}
|
|
484
|
+
if (!id && typeof opts.id === 'string' && /^scene-\d+$/.test(opts.id)) id = opts.id
|
|
485
|
+
if (!id) id = `scene-${++this.seq}`
|
|
486
|
+
else this.seq = Math.max(this.seq, Number(id.slice('scene-'.length)))
|
|
471
487
|
this.scenes.set(id, scene)
|
|
472
488
|
// Count what the viewport shows: per-instance totals and only the parts an
|
|
473
489
|
// instance actually references (a scene may carry unreferenced parts).
|
|
@@ -491,6 +507,7 @@ class FreeCadBridge {
|
|
|
491
507
|
parts: usedParts.size,
|
|
492
508
|
triangles,
|
|
493
509
|
volume,
|
|
510
|
+
...(docKey ? { docKey } : {}),
|
|
494
511
|
}
|
|
495
512
|
this.history = [stats, ...this.history.filter((h) => h.id !== id)].slice(0, 30)
|
|
496
513
|
this.latest = { id, title, at: stats.at, scene }
|
|
@@ -500,9 +517,16 @@ class FreeCadBridge {
|
|
|
500
517
|
return { id, stats }
|
|
501
518
|
}
|
|
502
519
|
|
|
520
|
+
/** Reserve the id before publishing, so the scene can carry `asm:<id>`. */
|
|
521
|
+
reserveAssemblyId() {
|
|
522
|
+
return `asm-${++this.assemblySeq}`
|
|
523
|
+
}
|
|
524
|
+
|
|
503
525
|
/** Register the base placements of a published assembly so joints can move it. */
|
|
504
|
-
registerAssembly({ title, sceneId, scene, joints }) {
|
|
505
|
-
const id = `asm-${++this.assemblySeq}`
|
|
526
|
+
registerAssembly({ title, sceneId, scene, joints, id: givenId }) {
|
|
527
|
+
const id = givenId ?? `asm-${++this.assemblySeq}`
|
|
528
|
+
const seq = /^asm-(\d+)$/.exec(id)
|
|
529
|
+
if (seq) this.assemblySeq = Math.max(this.assemblySeq, Number(seq[1]))
|
|
506
530
|
const record = {
|
|
507
531
|
id,
|
|
508
532
|
title,
|
|
@@ -551,7 +575,12 @@ class FreeCadBridge {
|
|
|
551
575
|
/** Recompute instance placements from base + joints + explode, then publish. */
|
|
552
576
|
async republishAssembly(record, titleSuffix) {
|
|
553
577
|
const scene = assembleScene(record)
|
|
554
|
-
const
|
|
578
|
+
const title = titleSuffix ? `${record.title} · ${titleSuffix}` : record.title
|
|
579
|
+
// In place: the assembly owns one scene id and one row in the models list.
|
|
580
|
+
const published = await this.publishScene(scene, title, this.currentSession ?? '', {
|
|
581
|
+
docKey: `asm:${record.id}`,
|
|
582
|
+
id: record.sceneId,
|
|
583
|
+
})
|
|
555
584
|
record.sceneId = published.id
|
|
556
585
|
return published
|
|
557
586
|
}
|
|
@@ -588,7 +617,7 @@ class FreeCadBridge {
|
|
|
588
617
|
return null
|
|
589
618
|
}
|
|
590
619
|
|
|
591
|
-
async run(code, { title, publishHint = true, meta = null } = {}) {
|
|
620
|
+
async run(code, { title, publishHint = true, meta = null, docKey = '', sceneId = '' } = {}) {
|
|
592
621
|
const bin = this.refresh()
|
|
593
622
|
if (!bin) {
|
|
594
623
|
return { ok: false, error: 'freecadcmd not found; set freecadPath in the plugin config' }
|
|
@@ -660,7 +689,7 @@ print("${BEGIN}" + __json.dumps(_safe(_payload)) + "${END}", flush=True)\n`
|
|
|
660
689
|
// `meta` rides inside the scene payload: the panel reads the feature list
|
|
661
690
|
// (and the assembly block) from there instead of calling a tool per frame.
|
|
662
691
|
const scene = meta ? { ...value.scene, ...meta } : value.scene
|
|
663
|
-
const published = await this.publishScene(scene, title ?? value.title ?? 'untitled')
|
|
692
|
+
const published = await this.publishScene(scene, title ?? value.title ?? 'untitled', this.currentSession ?? '', { docKey, id: sceneId })
|
|
664
693
|
value.sceneId = published.id
|
|
665
694
|
delete value.scene
|
|
666
695
|
}
|
|
@@ -1417,12 +1446,15 @@ if _TARGET:
|
|
|
1417
1446
|
__out["exported"] = _TARGET
|
|
1418
1447
|
__out["bytes"] = os.path.getsize(_TARGET)
|
|
1419
1448
|
`
|
|
1420
|
-
|
|
1449
|
+
// Allocate the assembly id before publishing: the scene carries `asm:<id>`
|
|
1450
|
+
// so later joint moves rewrite this same entry instead of adding one.
|
|
1451
|
+
const asmId = bridge.reserveAssemblyId()
|
|
1452
|
+
const out = await bridge.run(py, { title: title ?? 'assembly', docKey: `asm:${asmId}` })
|
|
1421
1453
|
if (!out.ok || !out.sceneId) return out
|
|
1422
1454
|
// Keep the base placements: without them a joint has nothing to move.
|
|
1423
1455
|
const scene = bridge.scenes.get(out.sceneId)
|
|
1424
1456
|
if (!scene) return out
|
|
1425
|
-
const record = bridge.registerAssembly({ title: assembly || title || 'assembly', sceneId: out.sceneId, scene, joints: [] })
|
|
1457
|
+
const record = bridge.registerAssembly({ id: asmId, title: assembly || title || 'assembly', sceneId: out.sceneId, scene, joints: [] })
|
|
1426
1458
|
for (const spec of joints ?? []) {
|
|
1427
1459
|
try {
|
|
1428
1460
|
bridge.setJoint(record, spec)
|
|
@@ -1697,7 +1729,13 @@ if _TARGET:
|
|
|
1697
1729
|
const meta = isFeatures ? { model: doc.name, features: publicFeatures(doc) } : null
|
|
1698
1730
|
doc.run = (doc.run ?? 0) + 1
|
|
1699
1731
|
doc.updatedAt = new Date().toISOString()
|
|
1700
|
-
|
|
1732
|
+
// One row per model: a rebuild rewrites the model's existing scene.
|
|
1733
|
+
const out = await bridge.run(code, {
|
|
1734
|
+
title: doc.name,
|
|
1735
|
+
meta,
|
|
1736
|
+
docKey: `model:${doc.name}`,
|
|
1737
|
+
sceneId: doc.stats?.sceneId || '',
|
|
1738
|
+
})
|
|
1701
1739
|
if (out.ok === false) return { ...out, name: doc.name, params: doc.params }
|
|
1702
1740
|
doc.stats = {
|
|
1703
1741
|
volume: out.volume ?? null,
|
|
@@ -2089,7 +2127,10 @@ if _TARGET:
|
|
|
2089
2127
|
const candidates = resolved.modelsDir ? [resolved.modelsDir, ...bridge.modelsCandidates()] : bridge.modelsCandidates()
|
|
2090
2128
|
const modelsDir = candidates.find((d) => existsSync(join(d, `${body.model}.json`))) || candidates[0] || modelsDirFor(undefined)
|
|
2091
2129
|
const result = await runFeatureAction(body, modelsDir)
|
|
2092
|
-
|
|
2130
|
+
// Tell the panel when the model was republished, so its poller can
|
|
2131
|
+
// recognise the in-place update it triggered itself.
|
|
2132
|
+
const entry = result && result.sceneId ? bridge.history.find((h) => h.id === result.sceneId) : null
|
|
2133
|
+
return send(200, JSON.stringify(entry ? { ...result, at: entry.at } : result))
|
|
2093
2134
|
} catch (error) {
|
|
2094
2135
|
return send(400, JSON.stringify({ ok: false, error: String(error?.message ?? error) }))
|
|
2095
2136
|
}
|
|
@@ -2157,12 +2198,12 @@ if _TARGET:
|
|
|
2157
2198
|
const moved = live.driveJoint(record, jointId, body?.value)
|
|
2158
2199
|
if (!moved) return send(400, JSON.stringify({ error: 'bad joint or value' }))
|
|
2159
2200
|
const published = await live.republishAssembly(record, `${moved.id} ${round6(moved.value)}`)
|
|
2160
|
-
return send(200, JSON.stringify({ ok: true, sceneId: published.id, joint: moved.id, value: round6(moved.value) }))
|
|
2201
|
+
return send(200, JSON.stringify({ ok: true, sceneId: published.id, at: published.stats.at, joint: moved.id, value: round6(moved.value) }))
|
|
2161
2202
|
}
|
|
2162
2203
|
const next = live.setExplode(record, body?.factor ?? 1)
|
|
2163
2204
|
if (next === null) return send(400, JSON.stringify({ error: 'bad factor' }))
|
|
2164
2205
|
const published = await live.republishAssembly(record, next ? `爆炸 ${round6(next)}` : '合拢')
|
|
2165
|
-
return send(200, JSON.stringify({ ok: true, sceneId: published.id, explode: round6(next) }))
|
|
2206
|
+
return send(200, JSON.stringify({ ok: true, sceneId: published.id, at: published.stats.at, explode: round6(next) }))
|
|
2166
2207
|
}
|
|
2167
2208
|
if (url.pathname.startsWith(`${ROUTE_PREFIX}/scene/`)) {
|
|
2168
2209
|
const id = decodeURIComponent(url.pathname.slice(`${ROUTE_PREFIX}/scene/`.length))
|