dsh-cad-studio 0.11.0 → 0.13.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 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2374,6 +2374,101 @@ ${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
|
+
_wl = []
|
|
2410
|
+
_area = 0.0
|
|
2411
|
+
_per = 0.0
|
|
2412
|
+
for _s in _shapes:
|
|
2413
|
+
try:
|
|
2414
|
+
_ws = _s.slice(_N, _AT)
|
|
2415
|
+
except Exception as _e:
|
|
2416
|
+
_ws = []
|
|
2417
|
+
for _w in _ws:
|
|
2418
|
+
_wl.append(_w)
|
|
2419
|
+
_pts = _w.discretize(Deflection=0.15)
|
|
2420
|
+
if len(_pts) < 3:
|
|
2421
|
+
continue
|
|
2422
|
+
_loops.append(_pts)
|
|
2423
|
+
try:
|
|
2424
|
+
_per += _w.Length
|
|
2425
|
+
except Exception:
|
|
2426
|
+
pass
|
|
2427
|
+
try:
|
|
2428
|
+
_area += Part.Face(Part.makePolygon(_pts + [_pts[0]])).Area
|
|
2429
|
+
except Exception:
|
|
2430
|
+
pass
|
|
2431
|
+
__out = {"ok": True, "axis": _AX, "at": _AT, "loops": len(_loops),
|
|
2432
|
+
"area": round(_area, 3), "perimeter": round(_per, 3)}
|
|
2433
|
+
_svg = ${target && !/\.dxf$/i.test(String(target)) ? JSON.stringify(String(target)) : 'None'}
|
|
2434
|
+
_dxf = ${target && /\.dxf$/i.test(String(target)) ? JSON.stringify(String(target)) : 'None'}
|
|
2435
|
+
if _svg and _loops:
|
|
2436
|
+
# Project the 3D loop points to the plane's 2D coordinates.
|
|
2437
|
+
def _uv(_p, _ax):
|
|
2438
|
+
return (_p.y, _p.z) if _ax == "X" else ((_p.x, _p.z) if _ax == "Y" else (_p.x, _p.y))
|
|
2439
|
+
_flat = []
|
|
2440
|
+
_minx = _miny = 1e18; _maxx = _maxy = -1e18
|
|
2441
|
+
for _l in _loops:
|
|
2442
|
+
_uvs = [_uv(_p, _AX) for _p in _l]
|
|
2443
|
+
for _u, _v in _uvs:
|
|
2444
|
+
_minx = min(_minx, _u); _maxx = max(_maxx, _u)
|
|
2445
|
+
_miny = min(_miny, _v); _maxy = max(_maxy, _v)
|
|
2446
|
+
_flat.append(_uvs)
|
|
2447
|
+
_pad = 2.0
|
|
2448
|
+
_w = (_maxx - _minx) + 2 * _pad or 1.0
|
|
2449
|
+
_h = (_maxy - _miny) + 2 * _pad or 1.0
|
|
2450
|
+
_parts = ["<svg xmlns='http://www.w3.org/2000/svg' width='%.2fmm' height='%.2fmm' viewBox='0 0 %.2f %.2f'>" % (_w, _h, _w, _h)]
|
|
2451
|
+
_parts.append("<g fill='none' stroke='black' stroke-width='0.2'>")
|
|
2452
|
+
for _uvs in _flat:
|
|
2453
|
+
_d = " ".join("%.3f,%.3f" % (_u - _minx + _pad, (_maxy - _v) + _pad) for _u, _v in _uvs)
|
|
2454
|
+
_parts.append("<polygon points='%s'/>" % _d)
|
|
2455
|
+
_parts.append("</g></svg>")
|
|
2456
|
+
_dir = os.path.dirname(_svg)
|
|
2457
|
+
if _dir:
|
|
2458
|
+
os.makedirs(_dir, exist_ok=True)
|
|
2459
|
+
with open(_svg, "w") as _f:
|
|
2460
|
+
_f.write("\\n".join(_parts))
|
|
2461
|
+
__out["svg"] = _svg
|
|
2462
|
+
__out["svg_size"] = [round(_w, 2), round(_h, 2)]
|
|
2463
|
+
if _wl and _svg is None and _dxf:
|
|
2464
|
+
import importDXF
|
|
2465
|
+
importDXF.export([Part.makeCompound(_wl)], _dxf)
|
|
2466
|
+
__out["dxf"] = _dxf
|
|
2467
|
+
`
|
|
2468
|
+
return bridge.run(py, { title: 'section', publishHint: false })
|
|
2469
|
+
},
|
|
2470
|
+
})
|
|
2471
|
+
|
|
2377
2472
|
// ---- Declarative features: the common jobs without writing Python ------
|
|
2378
2473
|
// A list of ops applied in order. `run` stays the escape hatch; this is the
|
|
2379
2474
|
// path for "60×40×20 的盒子挖个 φ16 通孔、倒 2mm 圆角".
|