dsh-cad-studio 0.24.0 → 0.25.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/README.md +1 -0
- package/index.js +236 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ DeepSeek Harness 的 CAD 插件:**把 FreeCAD 桥接成给 AI 用的工具**
|
|
|
24
24
|
| 模型清单 | `cad_studio_scene`(`action: list / delete / rename`)——面板里那条列表就是它;探查脚本留下的卡片可以直接删 |
|
|
25
25
|
| 模型改名 / 复制 | `cad_studio_doc {action:"rename", name, to}` 与 `{action:"duplicate", name, to}`:改名会同步列表里的标题与 `docKey`,复制会得到一条独立的新场景(副本不继承运行历史与撤销栈);两者都可 `cad_studio_undo` 回退 |
|
|
26
26
|
| 几何自检 | `cad_studio_check {source}` 在导出/打印前问一次:是否有效实体、外壳是否闭合、每个实体体积多少,返回中文问题清单(零体积 / 外壳未闭合 / 几何无效 / 没有实体) |
|
|
27
|
+
| 展开图 | `cad_studio_unfold {source, target:"…/net.dxf"}` 把板类零件按共享边摊平成下料图(面片等距铺开,面积与模型一致;孔各自成环;圆柱/圆角等曲面报 `curvedSkipped` 而不是瞎编),可写 DXF 或 SVG |
|
|
27
28
|
| 打印网格 | `cad_studio_mesh {source, target:"…/x.stl"}` 三角化 + 去重合并点/面、修形变、统一法向,可选 `fillHoles`、`targetSize`(减面);回报修复前后的 facets/points,避免「导出成功但网格是碎的」 |
|
|
28
29
|
| 旋转体与凹槽 | `revolve` 的 `axis` 取草图平面内的轴(`"Y"` 或 `[0,1,0]`)才是实体;垂直轴(XY 草图的 Z)只会扫出零厚度圆环,插件会直接报错。凹槽=`{kind:"cut", with:{kind:"revolve", …}}`,不需要单独 op |
|
|
29
30
|
| 面板改草图 | 特征抽屉里点「草图 N」展开:数值约束是可编辑输入框(`distance*`/`radius`/`angle` 按度),改完立刻重解并重建;解不出来就回滚并报错 |
|
package/index.js
CHANGED
|
@@ -3153,6 +3153,242 @@ __out = {
|
|
|
3153
3153
|
},
|
|
3154
3154
|
})
|
|
3155
3155
|
|
|
3156
|
+
tool({
|
|
3157
|
+
name: 'cad_studio_unfold',
|
|
3158
|
+
description: 'Unfold a solid made of flat faces into a 2D net (板金展开): every planar face is laid flat next to the one it shares an edge with, without stretching — a net you can cut and fold. Curved faces (cylinders, fillets, bends) are reported as skipped, not invented. Writes DXF (polylines, mm) or SVG; areas are reported so a net that came out wrong is visible.',
|
|
3159
|
+
parameters: {
|
|
3160
|
+
type: 'object',
|
|
3161
|
+
properties: {
|
|
3162
|
+
source: { type: 'string', description: 'Existing model file (STEP/IGES/BREP).' },
|
|
3163
|
+
code: { type: 'string', description: 'FreeCAD Python that binds `scene` or `export_shapes`.' },
|
|
3164
|
+
target: { type: 'string', description: 'Absolute path: .dxf (polylines) or .svg (outline).' },
|
|
3165
|
+
deflection: { type: 'number', description: 'Curve sampling for the outline, mm (default 0.25).' },
|
|
3166
|
+
holes: { type: 'boolean', description: 'Include inner wires (holes) as separate closed outlines (default true).' },
|
|
3167
|
+
},
|
|
3168
|
+
additionalProperties: false,
|
|
3169
|
+
},
|
|
3170
|
+
output: {
|
|
3171
|
+
schema: { type: 'object', properties: { ok: { type: 'boolean' }, unfolded: { type: 'number' }, areaModel: { type: 'number' }, areaFlat: { type: 'number' }, curvedSkipped: { type: 'number' }, target: { type: 'string' } } },
|
|
3172
|
+
render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
|
|
3173
|
+
},
|
|
3174
|
+
execute: async ({ source, code, target, deflection = 0.25, holes = true }) => {
|
|
3175
|
+
if (!source && !code) return { ok: false, error: 'pass either source (a model file) or code (FreeCAD Python)' }
|
|
3176
|
+
const absTarget = target ? String(target) : ''
|
|
3177
|
+
if (absTarget && !(absTarget.startsWith('/') || /^[A-Za-z]:[\\/]/.test(absTarget))) return { ok: false, error: 'target must be an absolute path' }
|
|
3178
|
+
const src = shapesFrom({ source, code })
|
|
3179
|
+
const py = `${PRELUDE}
|
|
3180
|
+
${src}
|
|
3181
|
+
_DEFL = float(${Number.isFinite(deflection) ? deflection : 0.25})
|
|
3182
|
+
_HOLES = ${holes === false ? 'False' : 'True'}
|
|
3183
|
+
_TARGET = ${absTarget ? JSON.stringify(absTarget) : 'None'}
|
|
3184
|
+
|
|
3185
|
+
import math
|
|
3186
|
+
from FreeCAD import Vector as _V
|
|
3187
|
+
|
|
3188
|
+
def _k(_p):
|
|
3189
|
+
return (round(_p.x, 4), round(_p.y, 4), round(_p.z, 4))
|
|
3190
|
+
|
|
3191
|
+
def _ordered(_wire):
|
|
3192
|
+
"""Vertices of a wire in traversal order (OuterWire.Vertexes is unordered)."""
|
|
3193
|
+
_edges = _wire.Edges
|
|
3194
|
+
if not _edges:
|
|
3195
|
+
return []
|
|
3196
|
+
_pts = [_edges[0].Vertexes[0].Point, _edges[0].Vertexes[-1].Point]
|
|
3197
|
+
_used = {0}
|
|
3198
|
+
while len(_used) < len(_edges):
|
|
3199
|
+
_tail = _pts[-1]; _found = False
|
|
3200
|
+
for _i, _e in enumerate(_edges):
|
|
3201
|
+
if _i in _used: continue
|
|
3202
|
+
_a, _b = _e.Vertexes[0].Point, _e.Vertexes[-1].Point
|
|
3203
|
+
if _k(_a) == _k(_tail): _pts.append(_b); _used.add(_i); _found = True; break
|
|
3204
|
+
if _k(_b) == _k(_tail): _pts.append(_a); _used.add(_i); _found = True; break
|
|
3205
|
+
if not _found: break
|
|
3206
|
+
if len(_pts) > 1 and _k(_pts[0]) == _k(_pts[-1]): _pts.pop()
|
|
3207
|
+
return _pts
|
|
3208
|
+
|
|
3209
|
+
def _ordered_edges(_wire):
|
|
3210
|
+
"""Edges of a wire in traversal order, each flagged when it runs backwards."""
|
|
3211
|
+
_edges = list(_wire.Edges)
|
|
3212
|
+
if not _edges:
|
|
3213
|
+
return []
|
|
3214
|
+
_out = [(_edges[0], False)]
|
|
3215
|
+
_used = {0}
|
|
3216
|
+
_tail = _edges[0].Vertexes[-1].Point
|
|
3217
|
+
while len(_used) < len(_edges):
|
|
3218
|
+
_found = False
|
|
3219
|
+
for _i, _e in enumerate(_edges):
|
|
3220
|
+
if _i in _used:
|
|
3221
|
+
continue
|
|
3222
|
+
_a, _b = _e.Vertexes[0].Point, _e.Vertexes[-1].Point
|
|
3223
|
+
if _k(_a) == _k(_tail):
|
|
3224
|
+
_out.append((_e, False)); _used.add(_i); _tail = _b; _found = True; break
|
|
3225
|
+
if _k(_b) == _k(_tail):
|
|
3226
|
+
_out.append((_e, True)); _used.add(_i); _tail = _a; _found = True; break
|
|
3227
|
+
if not _found:
|
|
3228
|
+
break
|
|
3229
|
+
return _out
|
|
3230
|
+
|
|
3231
|
+
def _sample(_wire):
|
|
3232
|
+
"""Outline points in traversal order: endpoints for lines, sampled arcs.
|
|
3233
|
+
|
|
3234
|
+
Wire.Edges comes back unordered, so walking them as given would splice the
|
|
3235
|
+
outline out of order and the shoelace area would come out wrong.
|
|
3236
|
+
"""
|
|
3237
|
+
_out = []
|
|
3238
|
+
for _e, _rev in _ordered_edges(_wire):
|
|
3239
|
+
_c = _e.Curve
|
|
3240
|
+
_straight = isinstance(_c, Part.Line) or isinstance(_c, Part.LineSegment)
|
|
3241
|
+
_pts = list(_e.discretize(Deflection=_DEFL)) if not _straight else [_e.Vertexes[0].Point, _e.Vertexes[-1].Point]
|
|
3242
|
+
if _rev:
|
|
3243
|
+
_pts.reverse()
|
|
3244
|
+
for _p in _pts:
|
|
3245
|
+
if not _out or _k(_out[-1]) != _k(_p):
|
|
3246
|
+
_out.append(_p)
|
|
3247
|
+
if len(_out) > 1 and _k(_out[0]) == _k(_out[-1]):
|
|
3248
|
+
_out.pop()
|
|
3249
|
+
return _out
|
|
3250
|
+
|
|
3251
|
+
def _frame(_f):
|
|
3252
|
+
_n = _f.Surface.Axis.normalize()
|
|
3253
|
+
_c = _f.CenterOfMass
|
|
3254
|
+
_pts = _ordered(_f.OuterWire)
|
|
3255
|
+
if not _pts:
|
|
3256
|
+
return None
|
|
3257
|
+
_u = _pts[0] - _c
|
|
3258
|
+
_u = _u - _n * _u.dot(_n)
|
|
3259
|
+
if _u.Length < 1e-9:
|
|
3260
|
+
return None
|
|
3261
|
+
_u.normalize()
|
|
3262
|
+
return _c, _u, _n.cross(_u)
|
|
3263
|
+
|
|
3264
|
+
def _unfold(_shape):
|
|
3265
|
+
_faces = [_f for _f in _shape.Faces if isinstance(_f.Surface, Part.Plane)]
|
|
3266
|
+
_curved = len(_shape.Faces) - len(_faces)
|
|
3267
|
+
if not _faces:
|
|
3268
|
+
return {"ok": False, "error": "这个形状没有平面:展开只适用于板类零件"}
|
|
3269
|
+
_owner = {}
|
|
3270
|
+
for _fi, _f in enumerate(_faces):
|
|
3271
|
+
for _e in _f.Edges:
|
|
3272
|
+
_ka, _kb = _k(_e.Vertexes[0].Point), _k(_e.Vertexes[-1].Point)
|
|
3273
|
+
_owner.setdefault((_ka, _kb) if _ka <= _kb else (_kb, _ka), []).append(_fi)
|
|
3274
|
+
_root = max(range(len(_faces)), key=lambda i: _faces[i].Area)
|
|
3275
|
+
_fr = _frame(_faces[_root])
|
|
3276
|
+
if _fr is None:
|
|
3277
|
+
return {"ok": False, "error": "最大平面没有可用的局部坐标系"}
|
|
3278
|
+
_c, _u, _v = _fr
|
|
3279
|
+
def _to2(_p):
|
|
3280
|
+
_d = _p - _c
|
|
3281
|
+
return (_d.dot(_u), _d.dot(_v))
|
|
3282
|
+
_pos = {}
|
|
3283
|
+
for _p in _ordered(_faces[_root].OuterWire):
|
|
3284
|
+
_pos[_k(_p)] = _to2(_p)
|
|
3285
|
+
_polys = []
|
|
3286
|
+
def _rings(_f, _map3):
|
|
3287
|
+
# Takes a 3D point to the flat layout: the root face projects with its
|
|
3288
|
+
# own frame, every other face composes that with its hinge placement.
|
|
3289
|
+
_rs = []
|
|
3290
|
+
_wires = _f.Wires if _HOLES else [_f.OuterWire]
|
|
3291
|
+
for _w in _wires:
|
|
3292
|
+
_pts = _sample(_w)
|
|
3293
|
+
if len(_pts) >= 3:
|
|
3294
|
+
_rs.append([_map3(_p) for _p in _pts])
|
|
3295
|
+
return _rs
|
|
3296
|
+
_polys.extend(_rings(_faces[_root], _to2))
|
|
3297
|
+
_seen, _queue, _worst = {_root}, [_root], 0.0
|
|
3298
|
+
while _queue:
|
|
3299
|
+
_cur = _queue.pop(0)
|
|
3300
|
+
_cur2 = [_pos[_k(_p)] for _p in _ordered(_faces[_cur].OuterWire) if _k(_p) in _pos]
|
|
3301
|
+
if not _cur2: continue
|
|
3302
|
+
_cxc = sum(q[0] for q in _cur2) / len(_cur2); _cyc = sum(q[1] for q in _cur2) / len(_cur2)
|
|
3303
|
+
for _e in _faces[_cur].Edges:
|
|
3304
|
+
_a, _b = _e.Vertexes[0].Point, _e.Vertexes[-1].Point
|
|
3305
|
+
_ka, _kb = _k(_a), _k(_b)
|
|
3306
|
+
for _nb in _owner.get((_ka, _kb) if _ka <= _kb else (_kb, _ka), []):
|
|
3307
|
+
if _nb in _seen: continue
|
|
3308
|
+
_ag, _bg = _pos.get(_ka), _pos.get(_kb)
|
|
3309
|
+
if _ag is None or _bg is None: continue
|
|
3310
|
+
_fnb = _frame(_faces[_nb])
|
|
3311
|
+
if _fnb is None: continue
|
|
3312
|
+
_cn, _un, _vn = _fnb
|
|
3313
|
+
def _local(_p, _cn=_cn, _un=_un, _vn=_vn):
|
|
3314
|
+
_d = _p - _cn
|
|
3315
|
+
return (_d.dot(_un), _d.dot(_vn))
|
|
3316
|
+
_la, _lb = _local(_a), _local(_b)
|
|
3317
|
+
_th = math.atan2(_bg[1] - _ag[1], _bg[0] - _ag[0]) - math.atan2(_lb[1] - _la[1], _lb[0] - _la[0])
|
|
3318
|
+
_ct, _st = math.cos(_th), math.sin(_th)
|
|
3319
|
+
def _place(q, _ag=_ag, _la=_la, _ct=_ct, _st=_st):
|
|
3320
|
+
_dx, _dy = q[0] - _la[0], q[1] - _la[1]
|
|
3321
|
+
return (_ag[0] + _ct * _dx - _st * _dy, _ag[1] + _st * _dx + _ct * _dy)
|
|
3322
|
+
# A hinge has exactly two ways to fold flat: pick the one that
|
|
3323
|
+
# puts the new face on the other side of the edge from the face
|
|
3324
|
+
# we are folding from (same side = the two would overlap).
|
|
3325
|
+
_out = _ordered(_faces[_nb].OuterWire)
|
|
3326
|
+
_m = [_place(_local(_p)) for _p in _out]
|
|
3327
|
+
_cx = sum(q[0] for q in _m) / len(_m); _cy = sum(q[1] for q in _m) / len(_m)
|
|
3328
|
+
_sd = (_bg[0] - _ag[0]) * (_cy - _ag[1]) - (_bg[1] - _ag[1]) * (_cx - _ag[0])
|
|
3329
|
+
_sc = (_bg[0] - _ag[0]) * (_cyc - _ag[1]) - (_bg[1] - _ag[1]) * (_cxc - _ag[0])
|
|
3330
|
+
if (_sd > 0) == (_sc > 0):
|
|
3331
|
+
_dhx, _dhy = _bg[0] - _ag[0], _bg[1] - _ag[1]
|
|
3332
|
+
_L = math.hypot(_dhx, _dhy)
|
|
3333
|
+
if _L < 1e-9: continue
|
|
3334
|
+
_dhx, _dhy = _dhx / _L, _dhy / _L
|
|
3335
|
+
def _mirror(q, _ag=_ag, _dhx=_dhx, _dhy=_dhy):
|
|
3336
|
+
_vx, _vy = q[0] - _ag[0], q[1] - _ag[1]
|
|
3337
|
+
_dot = _vx * _dhx + _vy * _dhy
|
|
3338
|
+
return (_ag[0] + 2 * _dot * _dhx - _vx, _ag[1] + 2 * _dot * _dhy - _vy)
|
|
3339
|
+
_place_inner = lambda q: _mirror(_place(q))
|
|
3340
|
+
else:
|
|
3341
|
+
_place_inner = _place
|
|
3342
|
+
_polys.extend(_rings(_faces[_nb], lambda _p, _loc=_local, _pl=_place_inner: _pl(_loc(_p))))
|
|
3343
|
+
_seen.add(_nb); _queue.append(_nb)
|
|
3344
|
+
for _p, _q in zip(_out, [_place_inner(_local(_p)) for _p in _out]):
|
|
3345
|
+
if _k(_p) in _pos:
|
|
3346
|
+
_worst = max(_worst, math.hypot(_pos[_k(_p)][0] - _q[0], _pos[_k(_p)][1] - _q[1]))
|
|
3347
|
+
_pos[_k(_p)] = _q
|
|
3348
|
+
def _area2(_m):
|
|
3349
|
+
return abs(sum((_m[_i][0] * _m[(_i + 1) % len(_m)][1] - _m[(_i + 1) % len(_m)][0] * _m[_i][1]) for _i in range(len(_m)))) / 2.0
|
|
3350
|
+
_flat = sum(_area2(_m) for _m in _polys)
|
|
3351
|
+
_xs = [q[0] for _m in _polys for q in _m]
|
|
3352
|
+
_ys = [q[1] for _m in _polys for q in _m]
|
|
3353
|
+
_out = {"ok": True, "faces": len(_faces), "unfolded": len(_seen), "curvedSkipped": _curved,
|
|
3354
|
+
"areaModel": round(_shape.Area, 3), "areaFlat": round(_flat, 3),
|
|
3355
|
+
"bbox": [round(min(_xs), 3), round(min(_ys), 3), round(max(_xs), 3), round(max(_ys), 3)]}
|
|
3356
|
+
if _TARGET:
|
|
3357
|
+
os.makedirs(os.path.dirname(_TARGET) or ".", exist_ok=True)
|
|
3358
|
+
if _TARGET.lower().endswith(".svg"):
|
|
3359
|
+
_minx, _miny, _maxx, _maxy = _out["bbox"]
|
|
3360
|
+
_w = _maxx - _minx + 10; _h = _maxy - _miny + 10
|
|
3361
|
+
_parts = []
|
|
3362
|
+
for _m in _polys:
|
|
3363
|
+
_d = " ".join("%.3f,%.3f" % (q[0] - _minx + 5, _maxy - q[1] + 5) for q in _m)
|
|
3364
|
+
_parts.append('<polygon points="%s" fill="none" stroke="#111" stroke-width="0.3"/>' % _d)
|
|
3365
|
+
open(_TARGET, "w").write('<svg xmlns="http://www.w3.org/2000/svg" width="%.2fmm" height="%.2fmm" viewBox="0 0 %.2f %.2f">%s</svg>' % (_w, _h, _w, _h, "".join(_parts)))
|
|
3366
|
+
else:
|
|
3367
|
+
_wires = []
|
|
3368
|
+
for _m in _polys:
|
|
3369
|
+
_pts = [Vector(q[0], q[1], 0.0) for q in _m]
|
|
3370
|
+
if len(_pts) >= 3:
|
|
3371
|
+
_pts.append(_pts[0])
|
|
3372
|
+
_wires.append(Part.makePolygon(_pts))
|
|
3373
|
+
import importDXF
|
|
3374
|
+
importDXF.export([Part.makeCompound(_wires)], _TARGET)
|
|
3375
|
+
_out["target"] = _TARGET
|
|
3376
|
+
_out["bytes"] = os.path.getsize(_TARGET)
|
|
3377
|
+
return _out
|
|
3378
|
+
|
|
3379
|
+
_out = None
|
|
3380
|
+
for _s in _shapes:
|
|
3381
|
+
_r = _unfold(_s)
|
|
3382
|
+
if _r.get("ok"):
|
|
3383
|
+
_out = _r
|
|
3384
|
+
break
|
|
3385
|
+
_out = _r
|
|
3386
|
+
__out = _out
|
|
3387
|
+
`
|
|
3388
|
+
return bridge.run(py, { title: 'unfold', publishHint: false })
|
|
3389
|
+
},
|
|
3390
|
+
})
|
|
3391
|
+
|
|
3156
3392
|
tool({
|
|
3157
3393
|
name: 'cad_studio_mesh',
|
|
3158
3394
|
description: 'Turn a solid into a printable mesh and clean it up: dedupe coincident points/facets, fix deformations, harmonize normals, optionally fill small holes or decimate to a facet budget, then write an STL. Reports facets/points before and after so you can see what the repair actually changed.',
|