dsh-cad-studio 0.10.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.
- package/index.js +95 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2333,6 +2333,7 @@ else:
|
|
|
2333
2333
|
code: { type: 'string', description: 'FreeCAD Python that binds `scene` or `export_shapes`.' },
|
|
2334
2334
|
from: { type: 'array', items: { type: 'number' }, description: 'First point [x,y,z] for a distance measurement.' },
|
|
2335
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.' },
|
|
2336
2337
|
},
|
|
2337
2338
|
additionalProperties: false,
|
|
2338
2339
|
},
|
|
@@ -2340,7 +2341,7 @@ else:
|
|
|
2340
2341
|
schema: { type: 'object', properties: { ok: { type: 'boolean' }, volume: { type: 'number' }, area: { type: 'number' } } },
|
|
2341
2342
|
render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
|
|
2342
2343
|
},
|
|
2343
|
-
execute: async ({ source, code, from, to }) => {
|
|
2344
|
+
execute: async ({ source, code, from, to, density }) => {
|
|
2344
2345
|
if (!source && !code) return { ok: false, error: 'pass either source (a model file) or code (FreeCAD Python)' }
|
|
2345
2346
|
const src = shapesFrom({ source, code })
|
|
2346
2347
|
const pair = Array.isArray(from) && Array.isArray(to) && from.length === 3 && to.length === 3
|
|
@@ -2362,12 +2363,105 @@ else:
|
|
|
2362
2363
|
"faces": sum(len(x.Faces) for x in _shapes),
|
|
2363
2364
|
"edges": sum(len(x.Edges) for x in _shapes),
|
|
2364
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)
|
|
2365
2371
|
${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)}) - FreeCAD.Vector(${JSON.stringify(to)})).Length, 3)` : ''}
|
|
2366
2372
|
`
|
|
2367
2373
|
return bridge.run(py, { title: 'measure', publishHint: false })
|
|
2368
2374
|
},
|
|
2369
2375
|
})
|
|
2370
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
|
+
|
|
2371
2465
|
// ---- Declarative features: the common jobs without writing Python ------
|
|
2372
2466
|
// A list of ops applied in order. `run` stays the escape hatch; this is the
|
|
2373
2467
|
// path for "60×40×20 的盒子挖个 φ16 通孔、倒 2mm 圆角".
|