dsh-cad-studio 0.8.2 → 0.8.3

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 +11 -1
  2. package/index.js +121 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
4
4
 
5
5
  ![CAD 工作室面板](https://raw.githubusercontent.com/STofone/dsh-cad-studio/main/docs/preview.png)
6
6
 
7
- - Host 侧(`index.js`):16 个工具 + `/cad-studio/*` HTTP 路由 + `freecadcmd` 子进程桥
7
+ - Host 侧(`index.js`):18 个工具 + `/cad-studio/*` HTTP 路由 + `freecadcmd` 子进程桥
8
8
  - 客户端侧(`client.js`):右栏页签「CAD 工作室」,three.js 视口 + 图标条浮层(零件 / 特征 / 选中项 / 运动 / 视图 / 模型 / 信息)
9
9
  - 数据:`<modelsDir>/<名字>.json` 存模型工程(脚本或声明式特征 + 参数 + 20 条历史);场景网格落 `$TMPDIR/dsh-cad-studio/{state.json,scenes/*.json}`
10
10
 
@@ -138,6 +138,16 @@ npm 包:<https://www.npmjs.com/package/dsh-cad-studio>(`dsh plugin` / DSH
138
138
 
139
139
  `rot` 是行主序 3×3,`translate` 是 mm。几何按**逐面三角化**,平面与孔壁不共享顶点(避免法线平均出暗色扇形)。
140
140
 
141
+ ### 撤销与重做
142
+
143
+ | 层 | 覆盖 | 谁能用 |
144
+ |---|---|---|
145
+ | 文档 | 特征增删改、移动、抑制、回滚点、变量 | 面板 `Ctrl+Z` / 工具 `cad_studio_features {action:"undo"}` |
146
+ | 装配 | 关节值、关节增删、爆炸系数 | 工具 `cad_studio_undo` / `cad_studio_redo`(`scope: auto|assembly|doc`) |
147
+ | 外观 | 透明度、颜色、显隐、线框、隔离 | 面板 `Ctrl+Z` / 右下角 ↶↷(只活在这个浏览器页签里) |
148
+
149
+ `scope:auto` 先看装配栈,空则退回文档栈(文档栈要求给 `model`)。导出写文件、出工程图、删模型属于不可逆动作,不进任何栈。
150
+
141
151
  ### 视图撤销
142
152
 
143
153
  透明度、颜色、显隐、线框这些**外观改动**也进撤销栈(服务端 `doc.undo` 只管文档结构,管不到它们):同一目标 1.5 s 内的连续拖动合并成一条,切场景清空,最多 40 步。面板「活跃」时(焦点在面板内,或 8 s 内点过面板)按 `Ctrl/Cmd+Z` 撤销、`Shift+Ctrl/Cmd+Z` 或 `Ctrl+Y` 重做;视口右下角还有一对 ↶ / ↷ 按钮,没东西可撤销时是灰的。输入框里的 `Ctrl+Z` 不劫持,「特征」页签里仍归文档撤销。
package/index.js CHANGED
@@ -184,6 +184,15 @@ function diagnose(bridge, cfg = {}) {
184
184
  }
185
185
  }
186
186
 
187
+ const renderUndo = (v) => {
188
+ if (!v || v.ok === false) return `✗ ${v?.error ?? '没有可撤销的操作'}`
189
+ if (v.scope === 'assembly') {
190
+ const head = v.undone !== undefined ? `↶ 撤销了装配改动(${v.undone})` : `↷ 重做了装配改动(${v.redone})`
191
+ return `${head},还可撤销 ${v.remaining ?? 0} 步 / 可重做 ${v.redo ?? v.undo ?? 0} 步`
192
+ }
193
+ return `${v.undone !== undefined ? '↶ 撤销' : '↷ 重做'}:文档 ${v.model}${v.evaluated === false ? '(求值失败,见 graph)' : ''}`
194
+ }
195
+
187
196
  const renderDoctor = (v) => {
188
197
  const lines = [`ready: ${v.ready ? 'yes' : 'no'}`]
189
198
  if (v.kernel) lines.push(`kernel: ${v.kernel}`)
@@ -531,6 +540,8 @@ class FreeCadBridge {
531
540
  this.touched = new Set()
532
541
  /** Assemblies keep their base placements so joints can re-place instances. */
533
542
  this.assemblies = new Map()
543
+ this.sessionUndo = []
544
+ this.sessionRedo = []
534
545
  this.assemblySeq = 0
535
546
  /**
536
547
  * Models live in the session's workspace, but the panel posts without a
@@ -855,6 +866,48 @@ class FreeCadBridge {
855
866
  return record.explode
856
867
  }
857
868
 
869
+ // ---- 装配层撤销:关节值 / 关节增删 / 爆炸系数 ----
870
+ snapshotAssembly(record) {
871
+ return { id: record.id, joints: JSON.parse(JSON.stringify(record.joints ?? [])), explode: record.explode ?? 0 }
872
+ }
873
+
874
+ beginAssemblyEdit(record, label) {
875
+ return { label, assembly: record.id, before: this.snapshotAssembly(record), after: null }
876
+ }
877
+
878
+ endAssemblyEdit(entry, record) {
879
+ if (!entry) return
880
+ entry.after = this.snapshotAssembly(record)
881
+ this.sessionUndo.push({ at: new Date().toISOString(), ...entry })
882
+ if (this.sessionUndo.length > 50) this.sessionUndo.shift()
883
+ this.sessionRedo = []
884
+ }
885
+
886
+ async applyAssemblySnapshot(snapshot) {
887
+ const record = await this.loadAssembly(snapshot.id)
888
+ if (!record) throw new Error(`没有这个装配:${snapshot.id}`)
889
+ record.joints = JSON.parse(JSON.stringify(snapshot.joints))
890
+ record.explode = Math.max(0, Math.min(4, Number(snapshot.explode) || 0))
891
+ const published = await this.republishAssembly(record)
892
+ return { record, published }
893
+ }
894
+
895
+ async undoSession() {
896
+ const entry = this.sessionUndo.pop()
897
+ if (!entry) return null
898
+ const applied = await this.applyAssemblySnapshot(entry.before)
899
+ this.sessionRedo.push(entry)
900
+ return { label: entry.label, assembly: entry.assembly, sceneId: applied.published.id }
901
+ }
902
+
903
+ async redoSession() {
904
+ const entry = this.sessionRedo.pop()
905
+ if (!entry || !entry.after) return null
906
+ const applied = await this.applyAssemblySnapshot(entry.after)
907
+ this.sessionUndo.push(entry)
908
+ return { label: entry.label, assembly: entry.assembly, sceneId: applied.published.id }
909
+ }
910
+
858
911
  /** Recompute instance placements from base + joints + explode, then publish. */
859
912
  async republishAssembly(record) {
860
913
  const scene = assembleScene(record)
@@ -1655,6 +1708,64 @@ __out = {
1655
1708
  },
1656
1709
  })
1657
1710
 
1711
+ tool({
1712
+ name: 'cad_studio_undo',
1713
+ description: 'Undo the last change. scope=auto prefers the assembly stack (joint value, joint add/remove, explode factor), otherwise falls back to the model document stack; pass model for doc scope.',
1714
+ parameters: {
1715
+ type: 'object',
1716
+ properties: {
1717
+ scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
1718
+ model: { type: 'string', description: 'document name, for doc scope' },
1719
+ },
1720
+ },
1721
+ output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
1722
+ async execute(args, exec) {
1723
+ const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1724
+ if (scope !== 'doc' && bridge.sessionUndo.length) {
1725
+ const undone = await bridge.undoSession()
1726
+ return { ok: true, scope: 'assembly', undone: undone.label, assembly: undone.assembly, sceneId: undone.sceneId, remaining: bridge.sessionUndo.length, redo: bridge.sessionRedo.length }
1727
+ }
1728
+ if (scope === 'assembly') return { ok: false, error: '装配层没有可撤销的操作' }
1729
+ const target = typeof args.model === 'string' && args.model ? args.model : ''
1730
+ if (!target) return { ok: false, error: '文档撤销需要 model 名称(可用 cad_studio_doc list 查看)' }
1731
+ try {
1732
+ const out = await runFeatureAction({ model: target, action: 'undo' }, modelsDirFor(exec))
1733
+ return { ok: out?.ok !== false, scope: 'doc', model: target, undone: 'doc', ...out }
1734
+ } catch (error) {
1735
+ return { ok: false, error: `文档撤销失败:${String(error?.message ?? error)}(脚本模型请用 cad_studio_doc revert)` }
1736
+ }
1737
+ },
1738
+ })
1739
+
1740
+ tool({
1741
+ name: 'cad_studio_redo',
1742
+ description: 'Redo the last undone change, same scopes as cad_studio_undo.',
1743
+ parameters: {
1744
+ type: 'object',
1745
+ properties: {
1746
+ scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
1747
+ model: { type: 'string', description: 'document name, for doc scope' },
1748
+ },
1749
+ },
1750
+ output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
1751
+ async execute(args, exec) {
1752
+ const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1753
+ if (scope !== 'doc' && bridge.sessionRedo.length) {
1754
+ const redone = await bridge.redoSession()
1755
+ return { ok: true, scope: 'assembly', redone: redone.label, assembly: redone.assembly, sceneId: redone.sceneId, remaining: bridge.sessionRedo.length, undo: bridge.sessionUndo.length }
1756
+ }
1757
+ if (scope === 'assembly') return { ok: false, error: '装配层没有可重做的操作' }
1758
+ const target = typeof args.model === 'string' && args.model ? args.model : ''
1759
+ if (!target) return { ok: false, error: '文档重做需要 model 名称' }
1760
+ try {
1761
+ const out = await runFeatureAction({ model: target, action: 'redo' }, modelsDirFor(exec))
1762
+ return { ok: out?.ok !== false, scope: 'doc', model: target, redone: 'doc', ...out }
1763
+ } catch (error) {
1764
+ return { ok: false, error: `文档重做失败:${String(error?.message ?? error)}(脚本模型请用 cad_studio_doc revert)` }
1765
+ }
1766
+ },
1767
+ })
1768
+
1658
1769
  tool({
1659
1770
  name: 'cad_studio_scene',
1660
1771
  description: 'Maintain the model list the panel shows (the persisted scene history): action=list / delete / rename. Use delete to clear probe scenes that a measurement run left behind, and rename when a scene title is a file path or a part id.',
@@ -2356,6 +2467,7 @@ if _TARGET:
2356
2467
  const published = await bridge.republishAssembly(record)
2357
2468
  return { ok: true, assembly: record.id, removed: joint, sceneId: published.id, joints: record.joints.length }
2358
2469
  }
2470
+ const edit = bridge.beginAssemblyEdit(record, 'joint')
2359
2471
  let stored
2360
2472
  try {
2361
2473
  stored = bridge.setJoint(record, { ...spec, id: spec.id })
@@ -2363,6 +2475,7 @@ if _TARGET:
2363
2475
  return { ok: false, error: String(error?.message ?? error) }
2364
2476
  }
2365
2477
  const published = await bridge.republishAssembly(record)
2478
+ bridge.endAssemblyEdit(edit, record)
2366
2479
  return {
2367
2480
  ok: true,
2368
2481
  assembly: record.id,
@@ -2399,9 +2512,11 @@ if _TARGET:
2399
2512
  }
2400
2513
  if (!record.joints.length) return { ok: false, error: '这个装配还没有关节:先 cad_studio_joint set 建一个' }
2401
2514
  const id = joint ?? record.joints[0].id
2515
+ const edit = bridge.beginAssemblyEdit(record, 'drive')
2402
2516
  const moved = bridge.driveJoint(record, id, value)
2403
2517
  if (!moved) return { ok: false, error: `没有这个关节:${id}(现有 ${record.joints.map((j) => j.id).join(', ')})` }
2404
2518
  const published = await bridge.republishAssembly(record)
2519
+ bridge.endAssemblyEdit(edit, record)
2405
2520
  return {
2406
2521
  ok: true,
2407
2522
  assembly: record.id,
@@ -2437,9 +2552,11 @@ if _TARGET:
2437
2552
  } catch (error) {
2438
2553
  return { ok: false, error: String(error?.message ?? error) }
2439
2554
  }
2555
+ const edit = bridge.beginAssemblyEdit(record, 'explode')
2440
2556
  const next = bridge.setExplode(record, factor ?? 1)
2441
2557
  if (next === null) return { ok: false, error: 'factor 必须是数字' }
2442
2558
  const published = await bridge.republishAssembly(record)
2559
+ bridge.endAssemblyEdit(edit, record)
2443
2560
  return { ok: true, assembly: record.id, sceneId: published.id, explode: round6(next), instances: published.stats.instances }
2444
2561
  },
2445
2562
  })
@@ -3329,14 +3446,18 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3329
3446
  if (!record) return send(404, JSON.stringify({ error: 'no assembly' }))
3330
3447
  if (url.pathname.endsWith('/drive')) {
3331
3448
  const jointId = typeof body?.joint === 'string' ? body.joint : record.joints[0]?.id
3449
+ const editLive = live.beginAssemblyEdit(record, 'drive')
3332
3450
  const moved = live.driveJoint(record, jointId, body?.value)
3333
3451
  if (!moved) return send(400, JSON.stringify({ error: 'bad joint or value' }))
3334
3452
  const published = await live.republishAssembly(record)
3453
+ live.endAssemblyEdit(editLive, record)
3335
3454
  return send(200, JSON.stringify({ ok: true, sceneId: published.id, at: published.stats.at, joint: moved.id, value: round6(moved.value) }))
3336
3455
  }
3456
+ const editLive = live.beginAssemblyEdit(record, 'explode')
3337
3457
  const next = live.setExplode(record, body?.factor ?? 1)
3338
3458
  if (next === null) return send(400, JSON.stringify({ error: 'bad factor' }))
3339
3459
  const published = await live.republishAssembly(record)
3460
+ live.endAssemblyEdit(editLive, record)
3340
3461
  return send(200, JSON.stringify({ ok: true, sceneId: published.id, at: published.stats.at, explode: round6(next) }))
3341
3462
  }
3342
3463
  if (url.pathname.startsWith(`${ROUTE_PREFIX}/scene/`)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {