dsh-cad-studio 0.30.1 → 0.31.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 +4 -2
  2. package/index.js +119 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -14,11 +14,11 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
14
14
  |---|---|
15
15
  | 建模 | `cad_studio_feature` 声明式特征(box/cylinder/sphere/cone/torus/extrude/revolve/fuse/cut/common/fillet/chamfer/shell/pattern/translate/rotate/mirror);`cad_studio_run` 跑任意 FreeCAD Python |
16
16
  | 工程文件 | `cad_studio_doc` 存 `script` 或 `features` 两种模型;`cad_studio_features` 改一条特征→整表重算(0.2–0.6 s),失败整表回滚 |
17
- | 装配 | `cad_studio_assembly` 读 manifest,`App::Link` 语义的实例位姿;零件是 STEP 文件级引用(改零件重开即跟随);`measure:true` 回每实例体积/包围盒,`interference:true` 回真正重叠的实例对 |
17
+ | 装配 | `cad_studio_assembly` 读 manifest,`App::Link` 语义的实例位姿;零件是 STEP 文件级引用(改零件重开即跟随;没有文件监视,重跑才刷新);**同名 `assembly` 原地重建**(保关节/mate/外观),要再开一个副本用 `assembly:'new'`;`measure:true` 回每实例体积/包围盒,`interference:true` 回真正重叠的实例对 |
18
18
  | 运动 | `cad_studio_joint`(revolute/linear)+ `cad_studio_drive` + `cad_studio_explode`,面板拖滑杆看运动 |
19
19
  | 出图 | `cad_studio_drawing` HLR → SVG(主/俯/左/等轴);`cad_studio_sheet` 正式图纸(图框 + 标题栏 + 三视图 + 尺寸,A4/A3/A2,可出 DXF) |
20
20
  | 装配 | `cad_studio_assembly` 多零件实例、`cad_studio_joint` 关节、`cad_studio_mate` 约束(同心/贴合/距离)、`cad_studio_drive` 驱动、`cad_studio_explode` 爆炸图 |
21
- | 依赖 | `cad_studio_deps` 列出模型的来源 STEP(谁在用、还在不在),`relink` 换路径后原地重建、`rebuild` 给 0.24 之前建的老装配补一次源文件清单;面板「信息」里能直接填新路径 |
21
+ | 依赖 | `cad_studio_deps` 列出模型的来源 STEP(谁在用、还在不在、**哪个件比装配新**),`relink` 换路径后原地重建、`rebuild` 给 0.24 之前建的老装配补一次源文件清单、`delete` 忘掉一条记录、`prune` 一次清掉所有场景已删的空壳记录;面板「信息」里能直接填新路径 |
22
22
  | 外观 | 面板里改的颜色/显隐/透明度/线框按模型保存(`POST /cad-studio/appearance`),刷新和重启后仍在 |
23
23
  | 其它 | `cad_studio_open`(STEP/STL/BREP/IGES 进来看)、`cad_studio_export`、`cad_studio_measure`、`cad_studio_env`;`cad_studio_doc {action:"create", from:"…/x.step"}` 把已有 STEP 变成可编辑模型文档 |
24
24
  | 模型清单 | `cad_studio_scene`(`action: list / delete / rename / prune`,`ids:[…]` 一次删多行)——面板里那条列表就是它;0 体积 0 三角的幽灵行会被标 `stale`,`prune` 一次清掉 |
@@ -102,6 +102,8 @@ npm 包:<https://www.npmjs.com/package/dsh-cad-studio>(`dsh plugin` / DSH
102
102
  - `cad_studio_export` 的返回里 `volume` / `solids` 是**真正写出去的那一套**,`sourceVolume` / `sourceSolids` 是输入侧的;`minVolume` 会按实体体积过滤(滤掉圆丝、灰尘),`alsoStl` 顺手写一份 STL。
103
103
  - **布尔链会留共面裂面,`unify` 默认开**:逐次 `fuse`/`cut` 之后同一平面会留一圈缝,体积不变但面数与文件变大(实测托架 174→64 面、STEP 607 KB→274 KB)。`cad_studio_export` 写文件前对每个实体 `removeSplitter()`,传 `unify:false` 可保留原始拓扑;返回里的 `faces` / `shells` / `fingerprint: {solids, faces, shells, volume}` 就是用来核几何的——**别用 md5**,STEP 头带时间戳,同一件两次导出 md5 也不同。
104
104
  - `cad_studio_assembly` 传 `measure: true` 会回每实例的 bbox/体积/实体数与总体积,`interference: true` 会回真正重叠的实例对(先用包围盒筛,再做布尔交)。
105
+ - 装配记录只认**它自己的场景**:场景被删掉之后记录不会被别的场景“借壳复活”(旧版会退回到最新场景的 parts,于是 prune 过的空壳又冒出来)。删场景后要清记录用 `cad_studio_deps {action:'prune'}`,单条用 `action:'delete'`。
106
+ - 部件文件改了但装配没重建时,`deps list` 会把它标成 `changed`(比 `spec.builtAt` 新);这是提示,不是自动重建——自动把用户正在看的场景换掉更糟。
105
107
  - **`fuse` / `cut` / `common` 的 `with` 可以是数组**:U 形围壁写成「一条 `fuse` 外块 + 一条 `cut` 带两个内腔」即可,不必拆成中间状态(中间状态是实心块,顺序错了会削到别处)。
106
108
  - **`extrude` 支持 3D 剖面点与 `dir`**:三角棱柱写 `{kind:"extrude", points:[[0,0,0],[40,0,0],[0,30,0]], dir:[0,0,20]}`,不用再拿 `base`/`h` 手算覆盖范围。
107
109
  - 特征参数在 `normalizeFeature` 阶段就校验,报错带行号与名字:`特征 f10(倒角_左后) cut:需要 with 子图元(…)`——不会再从 FreeCAD 里冒出一句 `第 10 步(cut)失败:'with'`。
package/index.js CHANGED
@@ -761,6 +761,9 @@ class FreeCadBridge {
761
761
  this.touched = new Set()
762
762
  /** Assemblies keep their base placements so joints can re-place instances. */
763
763
  this.assemblies = new Map()
764
+ // Ids the user dropped: persist() merges what an earlier process wrote, so
765
+ // a deletion has to be remembered or the next publish writes the row back.
766
+ this.forgottenAssemblies = new Set()
764
767
  this.sessionUndo = []
765
768
  this.sessionRedo = []
766
769
  this.assemblySeq = 0
@@ -879,7 +882,9 @@ class FreeCadBridge {
879
882
  // Merge with what an earlier process wrote: this process may only know
880
883
  // about the assemblies it touched, and an unrelated publish must not wipe
881
884
  // the others (the panel drives them by hydrating on demand).
882
- assemblies: [...this.assembleRecords(prevAssemblies).values()].map((a) => ({
885
+ assemblies: [...this.assembleRecords(prevAssemblies).values()]
886
+ .filter((a) => !this.forgottenAssemblies.has(a.id))
887
+ .map((a) => ({
883
888
  id: a.id,
884
889
  title: a.title,
885
890
  sceneId: a.sceneId,
@@ -1130,8 +1135,9 @@ class FreeCadBridge {
1130
1135
  joints: joints ?? [],
1131
1136
  mates: [],
1132
1137
  // What was asked for (part files + placements), so a relink can re-run the
1133
- // same build against a corrected path.
1134
- spec: spec ?? null,
1138
+ // same build against a corrected path. `builtAt` makes drift visible: a
1139
+ // part file newer than this was changed after the assembly was built.
1140
+ spec: spec ? { ...spec, builtAt: Date.now() } : null,
1135
1141
  explode: 0,
1136
1142
  }
1137
1143
  this.assemblies.set(id, record)
@@ -1158,6 +1164,8 @@ class FreeCadBridge {
1158
1164
  const names = new Set(record.base.map((b) => b.name))
1159
1165
  record.joints = (record.joints ?? []).filter((j) => names.has(j.instance))
1160
1166
  record.mates = (record.mates ?? []).filter((m) => names.has(m.a) && names.has(m.b))
1167
+ // A rebuild re-reads every part file, so "built" time is now.
1168
+ if (record.spec) record.spec.builtAt = Date.now()
1161
1169
  return record
1162
1170
  }
1163
1171
 
@@ -1350,7 +1358,11 @@ class FreeCadBridge {
1350
1358
  const metas = Array.isArray(pointer.assemblies) ? pointer.assemblies : []
1351
1359
  const meta = id ? metas.find((a) => a.id === id) : metas[metas.length - 1]
1352
1360
  if (!meta) return null
1353
- const candidates = [meta.sceneId, pointer.latest?.id].filter(Boolean)
1361
+ // Only this record's own scene: falling back to the newest scene revived
1362
+ // shell records (their scene was deleted, so they borrowed another
1363
+ // assembly's parts and popped back into the deps list after a prune).
1364
+ const candidates = [meta.sceneId].filter(Boolean)
1365
+ if (!candidates.length && pointer.latest?.id) candidates.push(pointer.latest.id)
1354
1366
  for (const sceneId of candidates) {
1355
1367
  try {
1356
1368
  const scene = JSON.parse(await readFile(join(FreeCadBridge.dataDir, 'scenes', `${sceneId}.json`), 'utf8'))
@@ -1365,6 +1377,57 @@ class FreeCadBridge {
1365
1377
  return null
1366
1378
  }
1367
1379
 
1380
+ /** Every assembly whose title matches, live scenes first. Records are lazy,
1381
+ * so hydrate before looking: after a restart the map is empty until a tool
1382
+ * names one, and a title-only rebuild would otherwise mint a duplicate. */
1383
+ async findAssembliesByTitle(title) {
1384
+ const want = String(title ?? '').trim()
1385
+ if (!want) return []
1386
+ await this.hydrateAssemblies()
1387
+ const live = (record) => this.scenes.has(record.sceneId)
1388
+ return [...this.assemblies.values()]
1389
+ .filter((record) => String(record.title ?? '').trim() === want)
1390
+ .sort((a, b) => (live(b) ? 1 : 0) - (live(a) ? 1 : 0))
1391
+ }
1392
+
1393
+ /** Drop one assembly record. Its scene is a separate object (cad_studio_scene
1394
+ * deletes that), so this only forgets the registry row. */
1395
+ async forgetAssembly(id) {
1396
+ const key = String(id ?? '').trim()
1397
+ if (!key) return false
1398
+ this.forgottenAssemblies.add(key)
1399
+ const had = this.assemblies.delete(key)
1400
+ await this.persist()
1401
+ return had || true
1402
+ }
1403
+
1404
+ /** Forget every record whose scene is gone: the shells left behind when
1405
+ * scenes were deleted one by one. Returns the ids that were dropped. */
1406
+ async pruneAssemblies() {
1407
+ const gone = new Set()
1408
+ for (const [id, record] of [...this.assemblies]) {
1409
+ if (!this.scenes.has(record.sceneId)) {
1410
+ this.assemblies.delete(id)
1411
+ gone.add(id)
1412
+ }
1413
+ }
1414
+ // Shells whose scene file is gone never load, so they are not in the map:
1415
+ // the pointer still lists them and that is what has to be cleaned.
1416
+ let metas = []
1417
+ try {
1418
+ const pointer = JSON.parse(await readFile(join(FreeCadBridge.dataDir, 'state.json'), 'utf8'))
1419
+ metas = Array.isArray(pointer.assemblies) ? pointer.assemblies : []
1420
+ } catch {}
1421
+ for (const meta of metas) {
1422
+ if (!meta?.id) continue
1423
+ const file = meta.sceneId ? join(FreeCadBridge.dataDir, 'scenes', `${meta.sceneId}.json`) : ''
1424
+ if (!this.scenes.has(meta.sceneId) && (!file || !existsSync(file))) gone.add(meta.id)
1425
+ }
1426
+ for (const id of gone) this.forgottenAssemblies.add(id)
1427
+ if (gone.size) await this.persist()
1428
+ return [...gone]
1429
+ }
1430
+
1368
1431
  async run(code, { title, publishHint = true, publish = true, meta = null, docKey = '', sceneId = '' } = {}) {
1369
1432
  const bin = this.refresh()
1370
1433
  if (!bin) {
@@ -3774,9 +3837,21 @@ else:
3774
3837
  // `assembly` is an id when it looks like one (`asm-3`) and a display name
3775
3838
  // otherwise — older callers passed a title here.
3776
3839
  let reuse = null
3840
+ let reusedBy = ''
3841
+ let sameTitle = []
3842
+ const wantsNew = String(assembly ?? '').trim().toLowerCase() === 'new'
3777
3843
  if (assembly && /^asm-\d+$/.test(String(assembly))) {
3778
3844
  try { reuse = await bridge.loadAssembly(String(assembly)) } catch {}
3779
3845
  if (!reuse) return { ok: false, error: `没有这个装配:${assembly}` }
3846
+ } else if (!wantsNew && typeof assembly === 'string' && assembly.trim()) {
3847
+ // A title here used to always mint a new record: re-running a build to
3848
+ // refresh the geometry piled up asm-8/9/11 sharing one title, and the
3849
+ // caller believed it had refreshed the one on screen. Rebuild in place
3850
+ // instead; `assembly:'new'` asks for a second copy on purpose.
3851
+ sameTitle = await bridge.findAssembliesByTitle(assembly)
3852
+ const live = sameTitle.find((record) => bridge.scenes.has(record.sceneId))
3853
+ reuse = live ?? sameTitle[0] ?? null
3854
+ if (reuse) reusedBy = 'title'
3780
3855
  }
3781
3856
  const asmId = reuse ? reuse.id : bridge.reserveAssemblyId()
3782
3857
  const out = await bridge.run(py, { title: title ?? 'assembly', docKey: `asm:${asmId}` })
@@ -3810,6 +3885,16 @@ else:
3810
3885
  await bridge.persist()
3811
3886
  out.assembly = record.id
3812
3887
  out.joints = record.joints.length
3888
+ if (reusedBy) {
3889
+ out.reused = true
3890
+ out.note = `按标题原地重建 ${record.id}(关节、mate、外观都保留)`
3891
+ } else if (wantsNew) {
3892
+ out.reused = false
3893
+ }
3894
+ if (sameTitle.length > 1) {
3895
+ const others = sameTitle.filter((r) => r.id !== record.id).map((r) => r.id)
3896
+ out.warn = `同名装配共有 ${sameTitle.length} 条(${sameTitle.map((r) => r.id).join(', ')}),本次重建 ${record.id};${others.join(', ')} 是历史壳记录,用 cad_studio_deps {action:'prune'} 一次清掉。`
3897
+ }
3813
3898
  return out
3814
3899
  },
3815
3900
  })
@@ -3842,18 +3927,23 @@ else:
3842
3927
  let exists = false
3843
3928
  let bytes = 0
3844
3929
  let modified = ''
3930
+ let mtimeMs = 0
3845
3931
  try {
3846
3932
  const st = statSync(p.path)
3847
3933
  exists = st.isFile()
3848
3934
  bytes = st.size
3935
+ mtimeMs = st.mtimeMs
3849
3936
  modified = new Date(st.mtimeMs).toISOString()
3850
3937
  } catch {}
3938
+ // Newer than the build that read it: the part moved on, this assembly did not.
3939
+ const builtAt = Number(record.spec?.builtAt ?? 0)
3851
3940
  return {
3852
3941
  part: p.name,
3853
3942
  path: p.path,
3854
3943
  exists,
3855
3944
  bytes,
3856
3945
  modified,
3946
+ changed: Boolean(exists && builtAt && mtimeMs > builtAt + 1000),
3857
3947
  color: Array.isArray(p.color) ? p.color : null,
3858
3948
  instances: (record.spec?.instances ?? []).filter((i) => i.part === p.name).map((i) => i.name),
3859
3949
  }
@@ -3908,7 +3998,7 @@ else:
3908
3998
  parameters: {
3909
3999
  type: 'object',
3910
4000
  properties: {
3911
- action: { type: 'string', enum: ['list', 'relink', 'rebuild'], description: 'Default: list.' },
4001
+ action: { type: 'string', enum: ['list', 'relink', 'rebuild', 'delete', 'prune'], description: 'Default: list. delete forgets one assembly record; prune forgets every record whose scene is gone (the shells left by deleted scenes).' },
3912
4002
  assembly: { type: 'string', description: 'Assembly id (asm-1) or unique title. Omitted: every assembly for `list`, newest for `relink`.' },
3913
4003
  part: { type: 'string', description: 'Part name to repoint (relink).' },
3914
4004
  path: { type: 'string', description: 'New file path for that part (relink).' },
@@ -3938,13 +4028,24 @@ else:
3938
4028
  tracked: Boolean(r.spec),
3939
4029
  files,
3940
4030
  missingParts: files.filter((f) => !f.exists).map((f) => f.part),
4031
+ // Files edited after the build: the geometry on screen is older.
4032
+ changedParts: files.filter((f) => f.changed).map((f) => f.part),
4033
+ sceneAlive: bridge.scenes.has(r.sceneId),
3941
4034
  // An assembly built before sources were recorded cannot relink: the
3942
4035
  // part files are unknown, not missing. Say so instead of an empty list.
3943
4036
  ...(r.spec ? {} : { hint: '这个装配没有记录来源文件(0.24 之前建的)。用 action=rebuild 并给 parts:[{name,path}] 记一次,之后 relink 就能用。' }),
3944
4037
  }
3945
4038
  })
3946
4039
  const missing = models.reduce((n, m) => n + m.missingParts.length, 0)
3947
- return { ok: true, models, missing }
4040
+ const changed = models.reduce((n, m) => n + m.changedParts.length, 0)
4041
+ const shells = models.filter((m) => !m.sceneAlive).map((m) => m.assembly)
4042
+ return {
4043
+ ok: true,
4044
+ models,
4045
+ missing,
4046
+ changed,
4047
+ ...(shells.length ? { shells, hint: `有 ${shells.length} 条记录的场景已删除(${shells.join(', ')}),用 action:'prune' 一次清掉。` } : {}),
4048
+ }
3948
4049
  }
3949
4050
  if (mode === 'rebuild') {
3950
4051
  let record
@@ -3968,6 +4069,18 @@ else:
3968
4069
  const published = await bridge.republishAssembly(record)
3969
4070
  return { ok: true, assembly: record.id, sceneId: published.id, tracked: true, files: specRows(record).length, instances: placed.length }
3970
4071
  }
4072
+ if (mode === 'delete') {
4073
+ if (!assembly) return { ok: false, error: 'delete 需要 assembly(asm-N 或标题)' }
4074
+ let record
4075
+ try { record = findAssembly(assembly) } catch (error) { return { ok: false, error: String(error?.message ?? error) } }
4076
+ const had = await bridge.forgetAssembly(record.id)
4077
+ return { ok: had, removed: record.id, title: record.title, note: '场景还在,需要的话用 cad_studio_scene {action:"delete"} 一起删。' }
4078
+ }
4079
+ if (mode === 'prune') {
4080
+ await bridge.hydrateAssemblies()
4081
+ const gone = await bridge.pruneAssemblies()
4082
+ return { ok: true, removed: gone, count: gone.length }
4083
+ }
3971
4084
  return relinkPart({ assembly, part, path })
3972
4085
  },
3973
4086
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.30.1",
3
+ "version": "0.31.0",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {