dsh-cad-studio 0.9.1 → 0.11.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 (2) hide show
  1. package/index.js +44 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1346,6 +1346,35 @@ def _prim(op, _cur=None):
1346
1346
  if abs(vec.Length) < 1e-9:
1347
1347
  raise RuntimeError("extrude 需要 h 或 dir(当前的挤出向量长度为 0)")
1348
1348
  return Part.Face(Part.makePolygon(ps + [ps[0]])).extrude(vec)
1349
+ if k == "hole":
1350
+ # Standard hole: simple through/blind, counterbore, or countersink.
1351
+ # Returns the SOLID TO SUBTRACT, so callers write {kind:"cut", with:{kind:"hole",...}}.
1352
+ d = float(op.get("d", op.get("diameter", 0)) or 0)
1353
+ if d <= 0:
1354
+ raise RuntimeError("hole 需要 d(直径,> 0)")
1355
+ dep = float(op.get("depth", 0) or 0)
1356
+ at = _v3(op.get("at"), (0.0, 0.0, 0.0))
1357
+ direc = _dir(op.get("dir"), (0.0, 0.0, -1.0))
1358
+ length = dep if dep > 0 else 200.0
1359
+ start = at - direc * 0.01
1360
+ body = Part.makeCylinder(d / 2.0, length + 0.02, start, direc)
1361
+ t = str(op.get("type", "simple")).lower()
1362
+ if t in ("counterbore", "cbore"):
1363
+ cbd = float(op.get("cb_d", op.get("cb_diameter", 0)) or 0)
1364
+ cbdep = float(op.get("cb_depth", 0) or 0)
1365
+ if cbd <= d or cbdep <= 0:
1366
+ raise RuntimeError("counterbore 需要 cb_d(> d)与 cb_depth(> 0)")
1367
+ body = body.fuse(Part.makeCylinder(cbd / 2.0, cbdep + 0.01, start, direc))
1368
+ elif t in ("countersink", "csink"):
1369
+ csd = float(op.get("cs_d", op.get("cs_diameter", 0)) or 0)
1370
+ ang = float(op.get("angle", 90.0))
1371
+ if csd <= d or not (10.0 <= ang <= 170.0):
1372
+ raise RuntimeError("countersink 需要 cs_d(> d)与 angle(10–170)")
1373
+ depth = (csd - d) / 2.0 / math.tan(math.radians(ang) / 2.0)
1374
+ body = body.fuse(Part.makeCone(d / 2.0, csd / 2.0, depth, start, direc))
1375
+ elif t != "simple":
1376
+ raise RuntimeError("hole 的 type 只能是 simple / counterbore / countersink:" + t)
1377
+ return body
1349
1378
  if k == "revolve":
1350
1379
  if op.get("sketch"):
1351
1380
  face = Part.Face(_sketch(str(op["sketch"])))
@@ -1515,6 +1544,12 @@ for _idx, _op in enumerate(_OPS):
1515
1544
  if not isinstance(_ws, (list, tuple)):
1516
1545
  _ws = [_ws]
1517
1546
  for _w in _ws:
1547
+ # Nested tools inherit the step's identity so an error inside
1548
+ # {kind:"cut", with:{...}} still says which feature failed.
1549
+ if isinstance(_w, dict):
1550
+ _w = dict(_w)
1551
+ _w.setdefault("_id", _op.get("_id"))
1552
+ _w.setdefault("_label", _op.get("_label"))
1518
1553
  _s = _prim(_w, _shape)
1519
1554
  _shape = _shape.fuse(_s) if _k == "fuse" else (_shape.cut(_s) if _k == "cut" else _shape.common(_s))
1520
1555
  elif _k == "fillet":
@@ -2298,6 +2333,7 @@ else:
2298
2333
  code: { type: 'string', description: 'FreeCAD Python that binds `scene` or `export_shapes`.' },
2299
2334
  from: { type: 'array', items: { type: 'number' }, description: 'First point [x,y,z] for a distance measurement.' },
2300
2335
  to: { type: 'array', items: { type: 'number' }, description: 'Second point [x,y,z].' },
2336
+ density: { type: 'number', description: 'Material density in g/cm3 (e.g. 2.70 aluminium, 1.24 PLA, 7.85 steel) — adds mass in grams.' },
2301
2337
  },
2302
2338
  additionalProperties: false,
2303
2339
  },
@@ -2305,7 +2341,7 @@ else:
2305
2341
  schema: { type: 'object', properties: { ok: { type: 'boolean' }, volume: { type: 'number' }, area: { type: 'number' } } },
2306
2342
  render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
2307
2343
  },
2308
- execute: async ({ source, code, from, to }) => {
2344
+ execute: async ({ source, code, from, to, density }) => {
2309
2345
  if (!source && !code) return { ok: false, error: 'pass either source (a model file) or code (FreeCAD Python)' }
2310
2346
  const src = shapesFrom({ source, code })
2311
2347
  const pair = Array.isArray(from) && Array.isArray(to) && from.length === 3 && to.length === 3
@@ -2327,6 +2363,11 @@ else:
2327
2363
  "faces": sum(len(x.Faces) for x in _shapes),
2328
2364
  "edges": sum(len(x.Edges) for x in _shapes),
2329
2365
  }
2366
+ _rho = float(${Number.isFinite(density) ? density : 0})
2367
+ if _rho > 0:
2368
+ # volume is mm3; 1000 mm3 = 1 cm3, density is g/cm3
2369
+ __out["density_g_cm3"] = _rho
2370
+ __out["mass_g"] = round(sum(x.Volume for x in _shapes) / 1000.0 * _rho, 3)
2330
2371
  ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)}) - FreeCAD.Vector(${JSON.stringify(to)})).Length, 3)` : ''}
2331
2372
  `
2332
2373
  return bridge.run(py, { title: 'measure', publishHint: false })
@@ -2337,7 +2378,7 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
2337
2378
  // A list of ops applied in order. `run` stays the escape hatch; this is the
2338
2379
  // path for "60×40×20 的盒子挖个 φ16 通孔、倒 2mm 圆角".
2339
2380
 
2340
- const FEATURE_KINDS = ['box', 'cylinder', 'sphere', 'cone', 'torus', 'extrude', 'revolve', 'fuse', 'cut', 'common', 'fillet', 'chamfer', 'shell', 'pattern_linear', 'pattern_circular', 'translate', 'rotate', 'mirror']
2381
+ const FEATURE_KINDS = ['box', 'cylinder', 'sphere', 'cone', 'torus', 'hole', 'extrude', 'revolve', 'fuse', 'cut', 'common', 'fillet', 'chamfer', 'shell', 'pattern_linear', 'pattern_circular', 'translate', 'rotate', 'mirror']
2341
2382
 
2342
2383
  tool({
2343
2384
  name: 'cad_studio_feature',
@@ -2871,7 +2912,7 @@ if _TARGET:
2871
2912
  } else if (kind === 'fuse' || kind === 'cut' || kind === 'common') {
2872
2913
  const list = Array.isArray(withs) ? withs : withs ? [withs] : []
2873
2914
  need(list.length > 0, `需要 with 子图元({kind:"box",size:[…]},或用数组一次减/并多个)`)
2874
- need(list.every(tool), 'with 里的每一项都要是图元(box/cylinder/sphere/cone/torus/extrude/revolve),且带 kind')
2915
+ need(list.every(tool), 'with 里的每一项都要是图元(box/cylinder/sphere/cone/torus/hole/extrude/revolve),且带 kind')
2875
2916
  } else if (kind === 'fillet') need(pos(probe.r), '需要 r>0')
2876
2917
  else if (kind === 'chamfer') need(pos(probe.d), '需要 d>0')
2877
2918
  else if (kind === 'shell') need(pos(probe.t), '需要 t>0(壁厚)')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.9.1",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {