dsh-cad-studio 0.19.0 → 0.20.0

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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/index.js +116 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -24,6 +24,7 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
24
24
  | 打印网格 | `cad_studio_mesh {source, target:"…/x.stl"}` 三角化 + 去重合并点/面、修形变、统一法向,可选 `fillHoles`、`targetSize`(减面);回报修复前后的 facets/points,避免「导出成功但网格是碎的」 |
25
25
  | 旋转体与凹槽 | `revolve` 的 `axis` 取草图平面内的轴(`"Y"` 或 `[0,1,0]`)才是实体;垂直轴(XY 草图的 Z)只会扫出零厚度圆环,插件会直接报错。凹槽=`{kind:"cut", with:{kind:"revolve", …}}`,不需要单独 op |
26
26
  | 面板改草图 | 特征抽屉里点「草图 N」展开:数值约束是可编辑输入框(`distance*`/`radius`/`angle` 按度),改完立刻重解并重建;解不出来就回滚并报错 |
27
+ | 撤销跨重启 | 文档级撤销栈(建/删/改名)写进 `<数据目录>/journal.json`,重启后仍可撤销;装配与视图的撤销只在本次会话内有效 |
27
28
  | 草图约束 | `equal`(等长)、`symmetric`(三点对称,需 `c` 点)、`angle`(**单位是度**,内部转弧度并在求解后重测);`distance` 是两点距离、`distance_x/y` 是该点的绝对坐标 |
28
29
  | 草图 + 放样/扫掠 | 草图存在文档里(`cad_studio_doc {action:"create", kind:"features", sketches:{S1:{geometry:[…],constraints:[…]}}, features:[{kind:"extrude",sketch:"S1",h:10}]}`);`loft` 用 `sections:[{sketch:"S1"},{sketch:"S2",at:[0,0,40]}]`,`sweep` 用 `path:[[x,y,z],…]`。改草图会重建(草图进了缓存哈希,不会命中旧 BREP) |
29
30
  | 特征 → STEP | `cad_studio_export {model:"名字", target:"…/x.step"}` 或 `cad_studio_doc {action:"show", name, export:"…/x.step"}`;特征列表是源,STEP 是产物,不用自己复刻发射器 |
package/index.js CHANGED
@@ -919,6 +919,39 @@ class FreeCadBridge {
919
919
  this.sessionUndo.push({ at: new Date().toISOString(), ...entry })
920
920
  if (this.sessionUndo.length > 50) this.sessionUndo.shift()
921
921
  this.sessionRedo = []
922
+ // Only entries carrying a serialisable `record` survive a restart: a raw
923
+ // closure cannot be written to disk, so those stay session-only.
924
+ if (entry && entry.record) this.persistJournal()
925
+ }
926
+
927
+ /** Write the replayable slice of the undo stack (newest 20) to journal.json. */
928
+ persistJournal() {
929
+ const rows = this.sessionUndo
930
+ .filter((e) => e && e.record)
931
+ .slice(-20)
932
+ .map((e) => ({ at: e.at, kind: e.kind, label: e.label, record: e.record }))
933
+ writeFile(join(FreeCadBridge.dataDir, 'journal.json'), JSON.stringify({ version: 1, entries: rows }, null, 2)).catch(() => {})
934
+ }
935
+
936
+ /**
937
+ * Rebuild undo entries from journal.json. `makeEntry(record)` supplies the
938
+ * closures (it needs the document helpers, which live in the plugin scope),
939
+ * so the bridge itself stays free of FreeCAD knowledge.
940
+ */
941
+ async loadJournal(makeEntry) {
942
+ let raw = null
943
+ try { raw = JSON.parse(await readFile(join(FreeCadBridge.dataDir, 'journal.json'), 'utf8')) } catch { return 0 }
944
+ const rows = Array.isArray(raw && raw.entries) ? raw.entries : []
945
+ let restored = 0
946
+ for (const row of rows) {
947
+ let built = null
948
+ try { built = makeEntry(row.record) } catch { built = null }
949
+ if (!built) continue
950
+ this.sessionUndo.push({ at: row.at, kind: row.kind, label: row.label, ...built, restored: true })
951
+ restored += 1
952
+ }
953
+ if (restored) this.sessionUndo = this.sessionUndo.slice(-50)
954
+ return restored
922
955
  }
923
956
 
924
957
  async applyAssemblySnapshot(snapshot) {
@@ -931,6 +964,7 @@ class FreeCadBridge {
931
964
  }
932
965
 
933
966
  async undoSession(kind) {
967
+ if (this.journalReady) { try { await this.journalReady } catch {} }
934
968
  const stack = kind ? this.sessionUndo.filter((e) => e.kind === kind) : this.sessionUndo
935
969
  const entry = stack[stack.length - 1]
936
970
  if (!entry) return null
@@ -3483,6 +3517,7 @@ if _TARGET:
3483
3517
  bridge.journal({
3484
3518
  kind: 'doc',
3485
3519
  label: `create ${key}`,
3520
+ record: { op: 'doc-create', model: key, dir, sceneId: sceneRow ? sceneRow.id : '' },
3486
3521
  undo: async () => {
3487
3522
  await unlink(file).catch(() => {})
3488
3523
  if (sceneRow) { try { await bridge.removeScene(sceneRow.id, { journal: false }) } catch {} }
@@ -3497,6 +3532,83 @@ if _TARGET:
3497
3532
  })
3498
3533
  }
3499
3534
 
3535
+ /**
3536
+ * Rebuild one undo entry from a persisted journal record. Only document-level
3537
+ * operations are replayable: they are data (a name, a file, bytes). Assembly
3538
+ * and view journal entries are closures over live objects and stay
3539
+ * session-only — after a restart they are not in the stack at all, which is
3540
+ * honest: the panel then says there is nothing to undo instead of undoing
3541
+ * something else.
3542
+ */
3543
+ const restoredEntry = (record) => {
3544
+ const r = record || {}
3545
+ const retitle = async (target, other) => {
3546
+ const row = bridge.history.find((h) => h.docKey === `model:${other}` || h.title === other)
3547
+ if (row) {
3548
+ row.title = target
3549
+ if (row.docKey) row.docKey = `model:${target}`
3550
+ await bridge.persist()
3551
+ }
3552
+ }
3553
+ if (r.op === 'doc-create') {
3554
+ const file = modelFile(r.model, r.dir)
3555
+ return {
3556
+ undo: async () => {
3557
+ await unlink(file).catch(() => {})
3558
+ if (r.sceneId) { try { await bridge.removeScene(r.sceneId, { journal: false }) } catch {} }
3559
+ return { model: r.model }
3560
+ },
3561
+ redo: async () => {
3562
+ // Re-creating needs the emitter to publish again; let the caller do it.
3563
+ throw new Error('重启后不能「重做创建」:请用 cad_studio_doc create 重新发布')
3564
+ },
3565
+ }
3566
+ }
3567
+ if (r.op === 'doc-delete') {
3568
+ const file = modelFile(r.model, r.dir)
3569
+ const bytes = Buffer.from(String(r.b64 || ''), 'base64')
3570
+ return {
3571
+ undo: async () => {
3572
+ await writeFile(file, bytes)
3573
+ try {
3574
+ const doc = await readModel(r.model, r.dir)
3575
+ await rebuild(doc, r.dir, { note: 'undo delete (restored journal)' })
3576
+ } catch {}
3577
+ return { model: r.model }
3578
+ },
3579
+ redo: async () => {
3580
+ await unlink(file).catch(() => {})
3581
+ return { model: r.model }
3582
+ },
3583
+ }
3584
+ }
3585
+ if (r.op === 'doc-rename') {
3586
+ const from = modelFile(r.from, r.dir)
3587
+ const to = modelFile(r.to, r.dir)
3588
+ const move = async (src, dst, name) => {
3589
+ const doc = JSON.parse(await readFile(src, 'utf8'))
3590
+ doc.name = name
3591
+ await writeModel(doc, r.dir)
3592
+ await unlink(dst).catch(() => {})
3593
+ return { model: name }
3594
+ }
3595
+ return {
3596
+ undo: async () => { const out = await move(to, from, r.from); await retitle(r.from, r.to); return out },
3597
+ redo: async () => { const out = await move(from, to, r.to); await retitle(r.to, r.from); return out },
3598
+ }
3599
+ }
3600
+ return null
3601
+ }
3602
+
3603
+ // Restore the replayable journal once per plugin start (a hot reload keeps the
3604
+ // same bridge, so guard against stacking duplicates).
3605
+ if (bridge && !bridge.journalLoaded) {
3606
+ bridge.journalLoaded = true
3607
+ // Keep the promise: an undo issued before the file has been read would
3608
+ // otherwise see an empty stack and report "nothing to undo".
3609
+ bridge.journalReady = bridge.loadJournal(restoredEntry).catch(() => 0)
3610
+ }
3611
+
3500
3612
  const runFeatureAction = async (args, dir) => {
3501
3613
  bridge.noteModelsDir(dir)
3502
3614
  const action = String(args.action ?? 'list')
@@ -3879,6 +3991,9 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3879
3991
  bridge.journal({
3880
3992
  kind: 'doc',
3881
3993
  label: `delete ${key}`,
3994
+ // The bytes live in journal.json so "undo delete" still works after a
3995
+ // restart; models are small JSON documents.
3996
+ record: { op: 'doc-delete', model: key, dir, b64: bytes.toString('base64') },
3882
3997
  undo: async () => {
3883
3998
  await writeFile(file, bytes)
3884
3999
  const doc = await readModel(key, dir)
@@ -3924,6 +4039,7 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3924
4039
  bridge.journal({
3925
4040
  kind: 'doc',
3926
4041
  label: `rename ${key} → ${to}`,
4042
+ record: { op: 'doc-rename', dir, from: key, to },
3927
4043
  undo: async () => {
3928
4044
  const back = await readModel(to, dir)
3929
4045
  back.name = key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {