dsh-cad-studio 0.8.4 → 0.8.5

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 (2) hide show
  1. package/index.js +35 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -184,6 +184,7 @@ function diagnose(bridge, cfg = {}) {
184
184
  }
185
185
  }
186
186
 
187
+ const defined = (obj) => Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined))
187
188
  const renderUndo = (v) => {
188
189
  if (!v || v.ok === false) return `✗ ${v?.error ?? '没有可撤销的操作'}`
189
190
  if (v.scope === 'assembly') {
@@ -1764,7 +1765,7 @@ __out = {
1764
1765
  const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1765
1766
  const kind = scope === 'auto' ? undefined : scope
1766
1767
  const undone = await bridge.undoSession(kind)
1767
- if (undone) return { ok: true, scope: undone.kind, undone: undone.label, assembly: undone.assembly, scene: undone.scene, model: undone.model, remaining: bridge.sessionUndo.length, redo: bridge.sessionRedo.length }
1768
+ if (undone) return defined({ ok: true, scope: undone.kind, undone: undone.label, assembly: undone.assembly, scene: undone.scene, model: undone.model, remaining: bridge.sessionUndo.length, redo: bridge.sessionRedo.length })
1768
1769
  if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可撤销的操作' : scope === 'scene' ? '场景列表没有可撤销的操作' : '没有可撤销的操作' }
1769
1770
  const target = typeof args.model === 'string' && args.model ? args.model : ''
1770
1771
  if (!target) return { ok: false, error: '文档撤销需要 model 名称(可用 cad_studio_doc list 查看)' }
@@ -1792,7 +1793,7 @@ __out = {
1792
1793
  const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1793
1794
  const kindR = scope === 'auto' ? undefined : scope
1794
1795
  const redone = await bridge.redoSession(kindR)
1795
- if (redone) return { ok: true, scope: redone.kind, redone: redone.label, assembly: redone.assembly, scene: redone.scene, model: redone.model, remaining: bridge.sessionRedo.length, undo: bridge.sessionUndo.length }
1796
+ if (redone) return defined({ ok: true, scope: redone.kind, redone: redone.label, assembly: redone.assembly, scene: redone.scene, model: redone.model, remaining: bridge.sessionRedo.length, undo: bridge.sessionUndo.length })
1796
1797
  if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可重做的操作' : '没有可重做的操作' }
1797
1798
  const target = typeof args.model === 'string' && args.model ? args.model : ''
1798
1799
  if (!target) return { ok: false, error: '文档重做需要 model 名称' }
@@ -2501,9 +2502,11 @@ if _TARGET:
2501
2502
  }
2502
2503
  if (mode === 'remove') {
2503
2504
  const before = record.joints.length
2504
- record.joints = record.joints.filter((j) => j.id !== joint)
2505
+ const editRm = bridge.beginAssemblyEdit(record, 'joint remove')
2506
+ record.joints = record.joints.filter((j) => j.id !== joint && j !== joint)
2505
2507
  if (record.joints.length === before) return { ok: false, error: `没有这个关节:${joint ?? '(未指定)'}` }
2506
2508
  const published = await bridge.republishAssembly(record)
2509
+ bridge.endAssemblyEdit(editRm, record)
2507
2510
  return { ok: true, assembly: record.id, removed: joint, sceneId: published.id, joints: record.joints.length }
2508
2511
  }
2509
2512
  const edit = bridge.beginAssemblyEdit(record, 'joint')
@@ -2978,6 +2981,33 @@ if _TARGET:
2978
2981
  * Every mutating action rebuilds the whole list (one FreeCAD run, ~0.2–0.6 s),
2979
2982
  * so the model can never be left half-edited — a failed edit is rolled back.
2980
2983
  */
2984
+ /**
2985
+ * Journal a document creation so "undo" removes it again (and the scene row it
2986
+ * published). File bytes are captured after the write, so redo is a plain
2987
+ * restore-and-rebuild — no need to replay the tool arguments.
2988
+ */
2989
+ const journalDocCreate = async (key, dir, result) => {
2990
+ const file = modelFile(key, dir)
2991
+ let bytes = null
2992
+ try { bytes = await readFile(file) } catch { return }
2993
+ const sceneRow = result?.sceneId ? bridge.history.find((h) => h.id === result.sceneId) : null
2994
+ bridge.journal({
2995
+ kind: 'doc',
2996
+ label: `create ${key}`,
2997
+ undo: async () => {
2998
+ await unlink(file).catch(() => {})
2999
+ if (sceneRow) { try { await bridge.removeScene(sceneRow.id, { journal: false }) } catch {} }
3000
+ return { model: key }
3001
+ },
3002
+ redo: async () => {
3003
+ await writeFile(file, bytes)
3004
+ const doc = await readModel(key, dir)
3005
+ try { await rebuild(doc, dir, { note: 'redo create' }) } catch {}
3006
+ return { model: key }
3007
+ },
3008
+ })
3009
+ }
3010
+
2981
3011
  const runFeatureAction = async (args, dir) => {
2982
3012
  bridge.noteModelsDir(dir)
2983
3013
  const action = String(args.action ?? 'list')
@@ -3223,6 +3253,7 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3223
3253
  if (result.ok === false) return result
3224
3254
  doc.history.push({ at: doc.updatedAt, note: args.note ?? 'create', params: JSON.parse(JSON.stringify(doc.params)), features: JSON.parse(JSON.stringify(doc.features)), run: doc.run })
3225
3255
  await writeModel(doc, dir)
3256
+ await journalDocCreate(key, dir, result)
3226
3257
  return { ...result, kind: 'features', features: publicFeatures(doc) }
3227
3258
  }
3228
3259
  const doc = {
@@ -3238,6 +3269,7 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3238
3269
  if (result.ok === false) return result
3239
3270
  doc.history.push({ at: doc.updatedAt, note: args.note ?? 'create', params: JSON.parse(JSON.stringify(doc.params)), run: doc.run })
3240
3271
  await writeModel(doc, dir)
3272
+ await journalDocCreate(key, dir, result)
3241
3273
  return result
3242
3274
  }
3243
3275
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {