dsh-cad-studio 0.8.3 → 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.
- package/index.js +116 -26
- 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') {
|
|
@@ -783,10 +784,27 @@ class FreeCadBridge {
|
|
|
783
784
|
* otherwise only removable by editing state.json by hand — which is exactly
|
|
784
785
|
* how a 28-row list happened in the first place.
|
|
785
786
|
*/
|
|
786
|
-
async removeScene(id) {
|
|
787
|
+
async removeScene(id, opts = {}) {
|
|
787
788
|
const key = String(id ?? '')
|
|
788
789
|
const row = this.history.find((h) => h.id === key)
|
|
789
790
|
if (!row) throw new Error(`没有这个场景:${key}(可用 ${this.history.map((h) => h.id).join(', ') || '(无)'})`)
|
|
791
|
+
const snapshot = JSON.parse(JSON.stringify(row))
|
|
792
|
+
let bytes = null
|
|
793
|
+
if (opts.journal !== false) {
|
|
794
|
+
try { bytes = await readFile(join(FreeCadBridge.dataDir, 'scenes', `${key}.json`)) } catch {}
|
|
795
|
+
this.journal({
|
|
796
|
+
kind: 'scene',
|
|
797
|
+
label: `delete ${key}`,
|
|
798
|
+
undo: async () => {
|
|
799
|
+
this.history = [snapshot, ...this.history.filter((h) => h.id !== key)]
|
|
800
|
+
if (bytes) await writeFile(join(FreeCadBridge.dataDir, 'scenes', `${key}.json`), bytes)
|
|
801
|
+
this.latest = { id: snapshot.id, title: snapshot.title, at: snapshot.at, scene: this.scenes.get(key) ?? null }
|
|
802
|
+
await this.persist()
|
|
803
|
+
return { scene: key }
|
|
804
|
+
},
|
|
805
|
+
redo: async () => { await this.removeScene(key, { journal: false }); return { scene: key } },
|
|
806
|
+
})
|
|
807
|
+
}
|
|
790
808
|
this.history = this.history.filter((h) => h.id !== key)
|
|
791
809
|
this.scenes.delete(key)
|
|
792
810
|
for (const [asmId, rec] of [...this.assemblies]) if (rec.sceneId === key) this.assemblies.delete(asmId)
|
|
@@ -799,12 +817,21 @@ class FreeCadBridge {
|
|
|
799
817
|
return { ok: true, removed: key, scenes: this.history.length, latest: this.latest ? this.latest.id : '' }
|
|
800
818
|
}
|
|
801
819
|
|
|
802
|
-
async renameScene(id, title) {
|
|
820
|
+
async renameScene(id, title, opts = {}) {
|
|
803
821
|
const key = String(id ?? '')
|
|
804
822
|
const row = this.history.find((h) => h.id === key)
|
|
805
823
|
if (!row) throw new Error(`没有这个场景:${key}`)
|
|
806
824
|
const name = String(title ?? '').trim()
|
|
807
825
|
if (!name) throw new Error('新名字不能为空')
|
|
826
|
+
const old = row.title
|
|
827
|
+
if (opts.journal !== false && old !== name) {
|
|
828
|
+
this.journal({
|
|
829
|
+
kind: 'scene',
|
|
830
|
+
label: `rename ${key}`,
|
|
831
|
+
undo: async () => { await this.renameScene(key, old, { journal: false }); return { scene: key, title: old } },
|
|
832
|
+
redo: async () => { await this.renameScene(key, name, { journal: false }); return { scene: key, title: name } },
|
|
833
|
+
})
|
|
834
|
+
}
|
|
808
835
|
row.title = name
|
|
809
836
|
if (this.latest && this.latest.id === key) this.latest = { ...this.latest, title: name }
|
|
810
837
|
await this.persist()
|
|
@@ -877,7 +904,18 @@ class FreeCadBridge {
|
|
|
877
904
|
|
|
878
905
|
endAssemblyEdit(entry, record) {
|
|
879
906
|
if (!entry) return
|
|
880
|
-
|
|
907
|
+
const before = entry.before
|
|
908
|
+
const after = this.snapshotAssembly(record)
|
|
909
|
+
this.journal({
|
|
910
|
+
kind: 'assembly',
|
|
911
|
+
label: entry.label,
|
|
912
|
+
undo: async () => this.applyAssemblySnapshot(before),
|
|
913
|
+
redo: async () => this.applyAssemblySnapshot(after),
|
|
914
|
+
})
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/** One stack for every undoable mutation, in the order it happened. */
|
|
918
|
+
journal(entry) {
|
|
881
919
|
this.sessionUndo.push({ at: new Date().toISOString(), ...entry })
|
|
882
920
|
if (this.sessionUndo.length > 50) this.sessionUndo.shift()
|
|
883
921
|
this.sessionRedo = []
|
|
@@ -892,20 +930,24 @@ class FreeCadBridge {
|
|
|
892
930
|
return { record, published }
|
|
893
931
|
}
|
|
894
932
|
|
|
895
|
-
async undoSession() {
|
|
896
|
-
const
|
|
933
|
+
async undoSession(kind) {
|
|
934
|
+
const stack = kind ? this.sessionUndo.filter((e) => e.kind === kind) : this.sessionUndo
|
|
935
|
+
const entry = stack[stack.length - 1]
|
|
897
936
|
if (!entry) return null
|
|
898
|
-
|
|
937
|
+
this.sessionUndo = this.sessionUndo.filter((e) => e !== entry)
|
|
938
|
+
const out = await entry.undo()
|
|
899
939
|
this.sessionRedo.push(entry)
|
|
900
|
-
return { label: entry.label,
|
|
940
|
+
return { label: entry.label, kind: entry.kind, ...(out ?? {}) }
|
|
901
941
|
}
|
|
902
942
|
|
|
903
|
-
async redoSession() {
|
|
904
|
-
const
|
|
905
|
-
|
|
906
|
-
|
|
943
|
+
async redoSession(kind) {
|
|
944
|
+
const stack = kind ? this.sessionRedo.filter((e) => e.kind === kind) : this.sessionRedo
|
|
945
|
+
const entry = stack[stack.length - 1]
|
|
946
|
+
if (!entry) return null
|
|
947
|
+
this.sessionRedo = this.sessionRedo.filter((e) => e !== entry)
|
|
948
|
+
const out = await entry.redo()
|
|
907
949
|
this.sessionUndo.push(entry)
|
|
908
|
-
return { label: entry.label,
|
|
950
|
+
return { label: entry.label, kind: entry.kind, ...(out ?? {}) }
|
|
909
951
|
}
|
|
910
952
|
|
|
911
953
|
/** Recompute instance placements from base + joints + explode, then publish. */
|
|
@@ -1714,18 +1756,17 @@ __out = {
|
|
|
1714
1756
|
parameters: {
|
|
1715
1757
|
type: 'object',
|
|
1716
1758
|
properties: {
|
|
1717
|
-
scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
|
|
1759
|
+
scope: { type: 'string', enum: ['auto', 'assembly', 'scene', 'doc'], description: 'default auto' },
|
|
1718
1760
|
model: { type: 'string', description: 'document name, for doc scope' },
|
|
1719
1761
|
},
|
|
1720
1762
|
},
|
|
1721
1763
|
output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
|
|
1722
1764
|
async execute(args, exec) {
|
|
1723
1765
|
const scope = typeof args.scope === 'string' ? args.scope : 'auto'
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
}
|
|
1728
|
-
if (scope === 'assembly') return { ok: false, error: '装配层没有可撤销的操作' }
|
|
1766
|
+
const kind = scope === 'auto' ? undefined : scope
|
|
1767
|
+
const undone = await bridge.undoSession(kind)
|
|
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 })
|
|
1769
|
+
if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可撤销的操作' : scope === 'scene' ? '场景列表没有可撤销的操作' : '没有可撤销的操作' }
|
|
1729
1770
|
const target = typeof args.model === 'string' && args.model ? args.model : ''
|
|
1730
1771
|
if (!target) return { ok: false, error: '文档撤销需要 model 名称(可用 cad_studio_doc list 查看)' }
|
|
1731
1772
|
try {
|
|
@@ -1743,18 +1784,17 @@ __out = {
|
|
|
1743
1784
|
parameters: {
|
|
1744
1785
|
type: 'object',
|
|
1745
1786
|
properties: {
|
|
1746
|
-
scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
|
|
1787
|
+
scope: { type: 'string', enum: ['auto', 'assembly', 'scene', 'doc'], description: 'default auto' },
|
|
1747
1788
|
model: { type: 'string', description: 'document name, for doc scope' },
|
|
1748
1789
|
},
|
|
1749
1790
|
},
|
|
1750
1791
|
output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
|
|
1751
1792
|
async execute(args, exec) {
|
|
1752
1793
|
const scope = typeof args.scope === 'string' ? args.scope : 'auto'
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
}
|
|
1757
|
-
if (scope === 'assembly') return { ok: false, error: '装配层没有可重做的操作' }
|
|
1794
|
+
const kindR = scope === 'auto' ? undefined : scope
|
|
1795
|
+
const redone = await bridge.redoSession(kindR)
|
|
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 })
|
|
1797
|
+
if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可重做的操作' : '没有可重做的操作' }
|
|
1758
1798
|
const target = typeof args.model === 'string' && args.model ? args.model : ''
|
|
1759
1799
|
if (!target) return { ok: false, error: '文档重做需要 model 名称' }
|
|
1760
1800
|
try {
|
|
@@ -2462,9 +2502,11 @@ if _TARGET:
|
|
|
2462
2502
|
}
|
|
2463
2503
|
if (mode === 'remove') {
|
|
2464
2504
|
const before = record.joints.length
|
|
2465
|
-
|
|
2505
|
+
const editRm = bridge.beginAssemblyEdit(record, 'joint remove')
|
|
2506
|
+
record.joints = record.joints.filter((j) => j.id !== joint && j !== joint)
|
|
2466
2507
|
if (record.joints.length === before) return { ok: false, error: `没有这个关节:${joint ?? '(未指定)'}` }
|
|
2467
2508
|
const published = await bridge.republishAssembly(record)
|
|
2509
|
+
bridge.endAssemblyEdit(editRm, record)
|
|
2468
2510
|
return { ok: true, assembly: record.id, removed: joint, sceneId: published.id, joints: record.joints.length }
|
|
2469
2511
|
}
|
|
2470
2512
|
const edit = bridge.beginAssemblyEdit(record, 'joint')
|
|
@@ -2939,6 +2981,33 @@ if _TARGET:
|
|
|
2939
2981
|
* Every mutating action rebuilds the whole list (one FreeCAD run, ~0.2–0.6 s),
|
|
2940
2982
|
* so the model can never be left half-edited — a failed edit is rolled back.
|
|
2941
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
|
+
|
|
2942
3011
|
const runFeatureAction = async (args, dir) => {
|
|
2943
3012
|
bridge.noteModelsDir(dir)
|
|
2944
3013
|
const action = String(args.action ?? 'list')
|
|
@@ -3184,6 +3253,7 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
|
|
|
3184
3253
|
if (result.ok === false) return result
|
|
3185
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 })
|
|
3186
3255
|
await writeModel(doc, dir)
|
|
3256
|
+
await journalDocCreate(key, dir, result)
|
|
3187
3257
|
return { ...result, kind: 'features', features: publicFeatures(doc) }
|
|
3188
3258
|
}
|
|
3189
3259
|
const doc = {
|
|
@@ -3199,6 +3269,7 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
|
|
|
3199
3269
|
if (result.ok === false) return result
|
|
3200
3270
|
doc.history.push({ at: doc.updatedAt, note: args.note ?? 'create', params: JSON.parse(JSON.stringify(doc.params)), run: doc.run })
|
|
3201
3271
|
await writeModel(doc, dir)
|
|
3272
|
+
await journalDocCreate(key, dir, result)
|
|
3202
3273
|
return result
|
|
3203
3274
|
}
|
|
3204
3275
|
|
|
@@ -3256,7 +3327,26 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
|
|
|
3256
3327
|
|
|
3257
3328
|
if (action === 'delete') {
|
|
3258
3329
|
const key = checkName(args.name)
|
|
3259
|
-
|
|
3330
|
+
const file = modelFile(key, dir)
|
|
3331
|
+
let bytes = null
|
|
3332
|
+
try { bytes = await readFile(file) } catch { throw new Error(`没有这个模型:${key}`) }
|
|
3333
|
+
const sceneRow = bridge.history.find((h) => h.title === key || (h.docKey === `model:${key}`))
|
|
3334
|
+
bridge.journal({
|
|
3335
|
+
kind: 'doc',
|
|
3336
|
+
label: `delete ${key}`,
|
|
3337
|
+
undo: async () => {
|
|
3338
|
+
await writeFile(file, bytes)
|
|
3339
|
+
const doc = await readModel(key, dir)
|
|
3340
|
+
try { await rebuild(doc, dir, { note: 'undo delete' }) } catch {}
|
|
3341
|
+
return { model: key }
|
|
3342
|
+
},
|
|
3343
|
+
redo: async () => {
|
|
3344
|
+
await unlink(file).catch(() => {})
|
|
3345
|
+
if (sceneRow) { try { await bridge.removeScene(sceneRow.id, { journal: false }) } catch {} }
|
|
3346
|
+
return { model: key }
|
|
3347
|
+
},
|
|
3348
|
+
})
|
|
3349
|
+
await unlink(file).catch(() => { throw new Error(`没有这个模型:${key}`) })
|
|
3260
3350
|
return { ok: true, name: key, deleted: true, dir }
|
|
3261
3351
|
}
|
|
3262
3352
|
|