dsh-cad-studio 0.21.0 → 0.22.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 +185 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -17,6 +17,7 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
17
17
  | 装配 | `cad_studio_assembly` 读 manifest,`App::Link` 语义的实例位姿;零件是 STEP 文件级引用(改零件重开即跟随);`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
+ | 装配 | `cad_studio_assembly` 多零件实例、`cad_studio_joint` 关节、`cad_studio_mate` 约束(同心/贴合/距离)、`cad_studio_drive` 驱动、`cad_studio_explode` 爆炸图 |
20
21
  | 其它 | `cad_studio_open`(STEP/STL/BREP/IGES 进来看)、`cad_studio_export`、`cad_studio_measure`、`cad_studio_env` |
21
22
  | 模型清单 | `cad_studio_scene`(`action: list / delete / rename`)——面板里那条列表就是它;探查脚本留下的卡片可以直接删 |
22
23
  | 模型改名 / 复制 | `cad_studio_doc {action:"rename", name, to}` 与 `{action:"duplicate", name, to}`:改名会同步列表里的标题与 `docKey`,复制会得到一条独立的新场景(副本不继承运行历史与撤销栈);两者都可 `cad_studio_undo` 回退 |
package/index.js CHANGED
@@ -353,6 +353,21 @@ function instanceCenter(parts, inst) {
353
353
  return scaleVec(addVec(mn, mx), 0.5)
354
354
  }
355
355
 
356
+ /** World AABB of one placement ({rot, translate}) of an instance. */
357
+ function worldBox(parts, placement) {
358
+ if (!placement) return null
359
+ const bx = partLocalBox(parts, placement.part)
360
+ if (!bx) return null
361
+ const mn = [Infinity, Infinity, Infinity]
362
+ const mx = [-Infinity, -Infinity, -Infinity]
363
+ for (let i = 0; i < 8; i += 1) {
364
+ const p = [i & 1 ? bx.mx[0] : bx.mn[0], i & 2 ? bx.mx[1] : bx.mn[1], i & 4 ? bx.mx[2] : bx.mn[2]]
365
+ const w = addVec(mulVec(placement.rot, p), placement.translate)
366
+ for (let k = 0; k < 3; k += 1) { if (w[k] < mn[k]) mn[k] = w[k]; if (w[k] > mx[k]) mx[k] = w[k] }
367
+ }
368
+ return { mn, mx }
369
+ }
370
+
356
371
  function bboxCenterOfInstances(parts, instances) {
357
372
  const centers = instances.map((inst) => instanceCenter(parts, inst))
358
373
  if (!centers.length) return [0, 0, 0]
@@ -361,6 +376,33 @@ function bboxCenterOfInstances(parts, instances) {
361
376
  }
362
377
 
363
378
  /** Normalise one joint spec against the assembly it belongs to. */
379
+ /**
380
+ * One mate, normalised. A mate PLACES the moving instance against a fixed one
381
+ * and then stops being dynamic — it is solved once per republish, not stepped
382
+ * like a joint. Three kinds cover the assembly idioms we can verify:
383
+ * concentric two axes become collinear, axial distance = offset
384
+ * flush two faces touch, gap = 0 (offset must be 0)
385
+ * distance two faces stay `offset` apart along the axis
386
+ */
387
+ function normalizeMate(record, spec) {
388
+ const names = new Set(record.base.map((b) => b.name))
389
+ const a = String(spec.a ?? '')
390
+ const b = String(spec.b ?? '')
391
+ if (!names.has(a)) throw new Error(`装配里没有这个实例(移动件 a):${a}`)
392
+ if (!names.has(b)) throw new Error(`装配里没有这个实例(固定件 b):${b}`)
393
+ if (a === b) throw new Error('a 与 b 不能是同一个实例')
394
+ const TYPES = ['concentric', 'flush', 'distance']
395
+ const type = TYPES.includes(spec.type) ? spec.type : 'concentric'
396
+ const axis = normVec(Array.isArray(spec.axis) && spec.axis.length === 3 ? spec.axis.map(Number) : [0, 0, 1])
397
+ const offset = Number.isFinite(Number(spec.offset)) ? Number(spec.offset) : 0
398
+ if (type === 'flush' && Math.abs(offset) > 1e-9) {
399
+ throw new Error('flush 是「贴住」,offset 必须为 0(要留缝用 distance)')
400
+ }
401
+ const side = Number(spec.side) === -1 ? -1 : 1
402
+ const id = typeof spec.id === 'string' && spec.id.trim() ? spec.id.trim() : `mate-${(record.mates?.length ?? 0) + 1}`
403
+ return { id, type, a, b, axis, offset, side }
404
+ }
405
+
364
406
  function normalizeJoint(record, spec) {
365
407
  const inst = record.base.find((b) => b.name === spec.instance)
366
408
  if (!inst) throw new Error(`装配里没有这个实例:${spec.instance}`)
@@ -488,12 +530,59 @@ function resolveParams(value, vars, where) {
488
530
  return value
489
531
  }
490
532
 
533
+ /** Projection range of an AABB along a unit axis (handles negative components). */
534
+ function projRange(box, axis) {
535
+ let lo = 0
536
+ let hi = 0
537
+ for (let k = 0; k < 3; k += 1) {
538
+ const a = axis[k]
539
+ lo += a > 0 ? box.mn[k] * a : box.mx[k] * a
540
+ hi += a > 0 ? box.mx[k] * a : box.mn[k] * a
541
+ }
542
+ return [lo, hi]
543
+ }
544
+
545
+ /** Per-instance placements after the mates, before joints and explode. */
546
+ function solveMates(record) {
547
+ const out = new Map()
548
+ for (const base of record.base) out.set(base.name, { name: base.name, part: base.part, rot: base.rot.slice(), translate: base.translate.slice() })
549
+ for (const mate of record.mates ?? []) {
550
+ const moving = out.get(mate.a)
551
+ const fixed = out.get(mate.b)
552
+ if (!moving || !fixed) continue
553
+ const axis = normVec(mate.axis ?? [0, 0, 1])
554
+ const offset = Number.isFinite(Number(mate.offset)) ? Number(mate.offset) : 0
555
+ const boxB = worldBox(record.parts, out.get(mate.b))
556
+ const boxA = worldBox(record.parts, moving)
557
+ if (!boxA || !boxB) continue
558
+ if (mate.type === 'concentric') {
559
+ // Perpendicular drift to zero, axial distance to `offset`: the two axes
560
+ // end up collinear. Keep the moving part's own orientation — a mate moves
561
+ // it, it does not re-aim it.
562
+ const ca = scaleVec(addVec(boxA.mn, boxA.mx), 0.5)
563
+ const cb = scaleVec(addVec(boxB.mn, boxB.mx), 0.5)
564
+ const want = addVec(cb, scaleVec(axis, offset))
565
+ moving.translate = addVec(moving.translate, subVec(want, ca))
566
+ } else {
567
+ // flush / distance: face-to-face gap along the axis equals `offset`.
568
+ const ra = projRange(boxA, axis)
569
+ const rb = projRange(boxB, axis)
570
+ const side = mate.side === -1 ? -1 : 1
571
+ const shift = side > 0 ? (rb[1] + offset) - ra[0] : (rb[0] - offset) - ra[1]
572
+ moving.translate = addVec(moving.translate, scaleVec(axis, shift))
573
+ }
574
+ }
575
+ return out
576
+ }
577
+
491
578
  /** Base placements + joints + explode → a scene payload ready to publish. */
492
579
  function assembleScene(record) {
493
580
  const explode = record.explode ?? 0
581
+ const placed = solveMates(record)
494
582
  const instances = record.base.map((base) => {
495
- let rot = base.rot.slice()
496
- let translate = base.translate.slice()
583
+ const start = placed.get(base.name) ?? base
584
+ let rot = start.rot.slice()
585
+ let translate = start.translate.slice()
497
586
  for (const joint of record.joints) {
498
587
  if (joint.instance !== base.name) continue
499
588
  const axis = normVec(joint.axis)
@@ -657,6 +746,7 @@ class FreeCadBridge {
657
746
  base: a.base,
658
747
  center: a.center,
659
748
  joints: a.joints.map((j) => ({ id: j.id, instance: j.instance, type: j.type, value: j.value, min: j.min, max: j.max, axis: j.axis, origin: j.origin })),
749
+ mates: a.mates ?? [],
660
750
  explode: a.explode,
661
751
  })),
662
752
  }
@@ -862,6 +952,7 @@ class FreeCadBridge {
862
952
  })),
863
953
  center: bboxCenterOfInstances(scene.parts, scene.instances),
864
954
  joints: joints ?? [],
955
+ mates: [],
865
956
  explode: 0,
866
957
  }
867
958
  this.assemblies.set(id, record)
@@ -876,6 +967,24 @@ class FreeCadBridge {
876
967
  return value
877
968
  }
878
969
 
970
+ /** Add or replace one mate; returns the stored spec. */
971
+ setMate(record, spec) {
972
+ const value = normalizeMate(record, spec)
973
+ record.mates = Array.isArray(record.mates) ? record.mates : []
974
+ const existing = record.mates.find((m) => m.id === value.id)
975
+ if (existing) Object.assign(existing, value)
976
+ else record.mates.push(value)
977
+ return value
978
+ }
979
+
980
+ removeMate(record, id) {
981
+ const list = record.mates ?? []
982
+ const at = list.findIndex((m) => m.id === id)
983
+ if (at < 0) return null
984
+ const [gone] = list.splice(at, 1)
985
+ return gone
986
+ }
987
+
879
988
  driveJoint(record, jointId, rawValue) {
880
989
  const joint = record.joints.find((j) => j.id === jointId)
881
990
  if (!joint) return null
@@ -895,7 +1004,12 @@ class FreeCadBridge {
895
1004
 
896
1005
  // ---- 装配层撤销:关节值 / 关节增删 / 爆炸系数 ----
897
1006
  snapshotAssembly(record) {
898
- return { id: record.id, joints: JSON.parse(JSON.stringify(record.joints ?? [])), explode: record.explode ?? 0 }
1007
+ return {
1008
+ id: record.id,
1009
+ joints: JSON.parse(JSON.stringify(record.joints ?? [])),
1010
+ mates: JSON.parse(JSON.stringify(record.mates ?? [])),
1011
+ explode: record.explode ?? 0,
1012
+ }
899
1013
  }
900
1014
 
901
1015
  beginAssemblyEdit(record, label) {
@@ -3335,6 +3449,74 @@ if _TARGET:
3335
3449
  },
3336
3450
  })
3337
3451
 
3452
+ tool({
3453
+ name: 'cad_studio_mate',
3454
+ description: 'Place an assembly instance against another with a mate: concentric (axes collinear, axial distance = offset), flush (faces touch), or distance (faces `offset` apart along the axis). A mate is solved once per republish and then holds — it is not a driven joint. Use cad_studio_drive for the moving degrees of freedom.',
3455
+ parameters: {
3456
+ type: 'object',
3457
+ properties: {
3458
+ assembly: { type: 'string' },
3459
+ action: { type: 'string', enum: ['set', 'list', 'remove'], description: 'Default: set.' },
3460
+ id: { type: 'string', description: 'Mate id (remove / replace). Default: mate-<n>.' },
3461
+ type: { type: 'string', enum: ['concentric', 'flush', 'distance'] },
3462
+ a: { type: 'string', description: 'Instance name that moves.' },
3463
+ b: { type: 'string', description: 'Instance name that stays put.' },
3464
+ axis: { type: 'array', items: { type: 'number' }, description: 'Mate axis. Default [0,0,1].' },
3465
+ offset: { type: 'number', description: 'mm: axial distance (concentric) or face gap (distance). flush requires 0.' },
3466
+ side: { type: 'number', description: '+1 puts `a` on the positive side of `b` (default), -1 the negative side.' },
3467
+ },
3468
+ additionalProperties: false,
3469
+ },
3470
+ output: {
3471
+ schema: { type: 'object', properties: { ok: { type: 'boolean' }, sceneId: { type: 'string' } } },
3472
+ render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
3473
+ },
3474
+ execute: async ({ assembly, action, id, type, a, b, axis, offset, side }) => {
3475
+ let record
3476
+ try {
3477
+ record = findAssembly(assembly)
3478
+ } catch (error) {
3479
+ return { ok: false, error: String(error?.message ?? error) }
3480
+ }
3481
+ const list = () => (record.mates ?? []).map((m) => ({ ...m, axis: m.axis.map(round6) }))
3482
+ const want = action ?? 'set'
3483
+ if (want === 'list') {
3484
+ return { ok: true, assembly: record.id, sceneId: record.sceneId, mates: list(), instances: record.base.map((x) => x.name) }
3485
+ }
3486
+ if (want === 'remove') {
3487
+ const target = String(id ?? '')
3488
+ const edit = bridge.beginAssemblyEdit(record, `mate remove ${target}`)
3489
+ const gone = bridge.removeMate(record, target)
3490
+ if (!gone) {
3491
+ const have = (record.mates ?? []).map((m) => m.id).join(', ') || '(没有)'
3492
+ return { ok: false, error: `没有这个约束:${target}(现有 ${have})` }
3493
+ }
3494
+ const published = await bridge.republishAssembly(record)
3495
+ bridge.endAssemblyEdit(edit, record)
3496
+ return { ok: true, assembly: record.id, sceneId: published.id, removed: gone.id, mates: list(), instances: published.stats.instances }
3497
+ }
3498
+ let value
3499
+ try {
3500
+ value = normalizeMate(record, { id, type, a, b, axis, offset, side })
3501
+ } catch (error) {
3502
+ return { ok: false, error: String(error?.message ?? error) }
3503
+ }
3504
+ const edit = bridge.beginAssemblyEdit(record, `mate ${value.type} ${value.a}→${value.b}`)
3505
+ bridge.setMate(record, value)
3506
+ const published = await bridge.republishAssembly(record)
3507
+ bridge.endAssemblyEdit(edit, record)
3508
+ return {
3509
+ ok: true,
3510
+ assembly: record.id,
3511
+ sceneId: published.id,
3512
+ mate: { ...value, axis: value.axis.map(round6) },
3513
+ mates: list(),
3514
+ instances: published.stats.instances,
3515
+ volume: round6(published.stats.volume),
3516
+ }
3517
+ },
3518
+ })
3519
+
3338
3520
  tool({
3339
3521
  name: 'cad_studio_explode',
3340
3522
  description: 'Exploded view: push every instance radially away from the assembly centre by `factor` (0 = assembled, 0.5 = half the distance outward). Republished scene only — geometry and exports are untouched.',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {