dsh-cad-studio 0.11.0 → 0.12.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 +88 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2374,6 +2374,94 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
2374
2374
  },
2375
2375
  })
2376
2376
 
2377
+ tool({
2378
+ name: 'cad_studio_section',
2379
+ description: `Cross-section of a solid: slice it with a plane and report closed loops, area and perimeter; optionally write the outline as SVG (for laser/CNC nesting). Axis is X/Y/Z, at is the plane offset in mm.`,
2380
+ parameters: {
2381
+ type: 'object',
2382
+ properties: {
2383
+ source: { type: 'string', description: 'Existing model file (STEP/IGES/BREP/STL).' },
2384
+ code: { type: 'string', description: 'FreeCAD Python that binds `scene` or `export_shapes`.' },
2385
+ axis: { type: 'string', enum: ['X', 'Y', 'Z'], description: 'Plane normal axis (default Z).' },
2386
+ at: { type: 'number', description: 'Plane offset along that axis, mm (default 0).' },
2387
+ target: { type: 'string', description: 'Write an SVG outline here (absolute path).' },
2388
+ },
2389
+ additionalProperties: false,
2390
+ },
2391
+ output: {
2392
+ schema: { type: 'object', properties: { ok: { type: 'boolean' }, loops: { type: 'number' }, area: { type: 'number' }, perimeter: { type: 'number' } } },
2393
+ render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
2394
+ },
2395
+ execute: async ({ source, code, axis = 'Z', at = 0, target }) => {
2396
+ if (!source && !code) return { ok: false, error: 'pass either source (a model file) or code (FreeCAD Python)' }
2397
+ const absTarget = target ? String(target) : ''
2398
+ if (absTarget && !(absTarget.startsWith('/') || /^[A-Za-z]:[\\/]/.test(absTarget))) return { ok: false, error: 'target must be an absolute path' }
2399
+ const src = shapesFrom({ source, code })
2400
+ const py = `${PRELUDE}
2401
+ ${src}
2402
+ _AX = ${JSON.stringify(String(axis || 'Z').toUpperCase())}
2403
+ _N = {"X": Vector(1.0, 0.0, 0.0), "Y": Vector(0.0, 1.0, 0.0), "Z": Vector(0.0, 0.0, 1.0)}[_AX]
2404
+ _AT = float(${Number.isFinite(at) ? at : 0})
2405
+ if not _shapes:
2406
+ __out = {"ok": False, "error": "nothing to section"}
2407
+ else:
2408
+ _loops = []
2409
+ _area = 0.0
2410
+ _per = 0.0
2411
+ for _s in _shapes:
2412
+ try:
2413
+ _ws = _s.slice(_N, _AT)
2414
+ except Exception as _e:
2415
+ _ws = []
2416
+ for _w in _ws:
2417
+ _pts = _w.discretize(Deflection=0.15)
2418
+ if len(_pts) < 3:
2419
+ continue
2420
+ _loops.append(_pts)
2421
+ try:
2422
+ _per += _w.Length
2423
+ except Exception:
2424
+ pass
2425
+ try:
2426
+ _area += Part.Face(Part.makePolygon(_pts + [_pts[0]])).Area
2427
+ except Exception:
2428
+ pass
2429
+ __out = {"ok": True, "axis": _AX, "at": _AT, "loops": len(_loops),
2430
+ "area": round(_area, 3), "perimeter": round(_per, 3)}
2431
+ _svg = ${target ? JSON.stringify(String(target)) : 'None'}
2432
+ if _svg and _loops:
2433
+ # Project the 3D loop points to the plane's 2D coordinates.
2434
+ def _uv(_p, _ax):
2435
+ return (_p.y, _p.z) if _ax == "X" else ((_p.x, _p.z) if _ax == "Y" else (_p.x, _p.y))
2436
+ _flat = []
2437
+ _minx = _miny = 1e18; _maxx = _maxy = -1e18
2438
+ for _l in _loops:
2439
+ _uvs = [_uv(_p, _AX) for _p in _l]
2440
+ for _u, _v in _uvs:
2441
+ _minx = min(_minx, _u); _maxx = max(_maxx, _u)
2442
+ _miny = min(_miny, _v); _maxy = max(_maxy, _v)
2443
+ _flat.append(_uvs)
2444
+ _pad = 2.0
2445
+ _w = (_maxx - _minx) + 2 * _pad or 1.0
2446
+ _h = (_maxy - _miny) + 2 * _pad or 1.0
2447
+ _parts = ["<svg xmlns='http://www.w3.org/2000/svg' width='%.2fmm' height='%.2fmm' viewBox='0 0 %.2f %.2f'>" % (_w, _h, _w, _h)]
2448
+ _parts.append("<g fill='none' stroke='black' stroke-width='0.2'>")
2449
+ for _uvs in _flat:
2450
+ _d = " ".join("%.3f,%.3f" % (_u - _minx + _pad, (_maxy - _v) + _pad) for _u, _v in _uvs)
2451
+ _parts.append("<polygon points='%s'/>" % _d)
2452
+ _parts.append("</g></svg>")
2453
+ _dir = os.path.dirname(_svg)
2454
+ if _dir:
2455
+ os.makedirs(_dir, exist_ok=True)
2456
+ with open(_svg, "w") as _f:
2457
+ _f.write("\\n".join(_parts))
2458
+ __out["svg"] = _svg
2459
+ __out["svg_size"] = [round(_w, 2), round(_h, 2)]
2460
+ `
2461
+ return bridge.run(py, { title: 'section', publishHint: false })
2462
+ },
2463
+ })
2464
+
2377
2465
  // ---- Declarative features: the common jobs without writing Python ------
2378
2466
  // A list of ops applied in order. `run` stays the escape hatch; this is the
2379
2467
  // path for "60×40×20 的盒子挖个 φ16 通孔、倒 2mm 圆角".
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {