dsh-cad-studio 0.8.5 → 0.9.1
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 +105 -2
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1202,6 +1202,7 @@ const featuresScript = (ops, exportPath, opts = {}) => `${PRELUDE}
|
|
|
1202
1202
|
import os
|
|
1203
1203
|
_OPS = json.loads(${JSON.stringify(JSON.stringify(Array.isArray(ops) ? ops : []))})
|
|
1204
1204
|
_EXPORT = ${exportPath ? JSON.stringify(exportPath) : 'None'}
|
|
1205
|
+
_SKETCHES = json.loads(${JSON.stringify(JSON.stringify(opts.sketches && typeof opts.sketches === 'object' ? opts.sketches : {}))})
|
|
1205
1206
|
_CACHE = json.loads(${JSON.stringify(JSON.stringify(opts.cache && opts.cache.hashes ? { dir: opts.cache.dir, hashes: opts.cache.hashes } : null))})
|
|
1206
1207
|
_RESUME = json.loads(${JSON.stringify(JSON.stringify(opts.resume && opts.resume.brep ? { index: opts.resume.index, brep: opts.resume.brep } : null))})
|
|
1207
1208
|
|
|
@@ -1231,6 +1232,90 @@ def _anchor(_spec, _cur):
|
|
|
1231
1232
|
_off = _spec.get("offset")
|
|
1232
1233
|
return _center_of(_hits[0]) + (_v3(_off) if _off else Vector(0.0, 0.0, 0.0))
|
|
1233
1234
|
|
|
1235
|
+
|
|
1236
|
+
_SK_CONSTRAINT = {
|
|
1237
|
+
"coincident": ("Coincident", "ab2"), "concentric": ("Coincident", "circles"),
|
|
1238
|
+
"horizontal": ("Horizontal", "one"), "vertical": ("Vertical", "one"),
|
|
1239
|
+
"parallel": ("Parallel", "pair"), "perpendicular": ("Perpendicular", "pair"),
|
|
1240
|
+
"tangent": ("Tangent", "pair"), "distance_x": ("DistanceX", "size"),
|
|
1241
|
+
"distance_y": ("DistanceY", "size"), "radius": ("Radius", "size_one"),
|
|
1242
|
+
"angle": ("Angle", "size_pair"), "distance": ("Distance", "ab2v"),
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
def _sketch(name):
|
|
1246
|
+
import Sketcher
|
|
1247
|
+
spec = _SKETCHES.get(name)
|
|
1248
|
+
if not spec:
|
|
1249
|
+
raise RuntimeError("no such sketch: %s" % name)
|
|
1250
|
+
doc = FreeCAD.newDocument("sk_" + name)
|
|
1251
|
+
sk = doc.addObject("Sketcher::SketchObject", "K")
|
|
1252
|
+
ids = []
|
|
1253
|
+
for g in spec.get("geometry") or []:
|
|
1254
|
+
t = g.get("type")
|
|
1255
|
+
if t == "line":
|
|
1256
|
+
sk.addGeometry(Part.LineSegment(_v3(g["p1"]), _v3(g["p2"])), False)
|
|
1257
|
+
elif t == "circle":
|
|
1258
|
+
sk.addGeometry(Part.Circle(_v3(g["c"]), Vector(0, 0, 1), float(g["r"])), False)
|
|
1259
|
+
else:
|
|
1260
|
+
raise RuntimeError("sketch %s: unknown geometry %s" % (name, t))
|
|
1261
|
+
ids.append(g.get("id"))
|
|
1262
|
+
ix = dict((gid, i) for i, gid in enumerate(ids))
|
|
1263
|
+
def gi(ref):
|
|
1264
|
+
if ref not in ix:
|
|
1265
|
+
raise RuntimeError("sketch %s: constraint refers to missing geometry %s" % (name, ref))
|
|
1266
|
+
return ix[ref]
|
|
1267
|
+
dims = []
|
|
1268
|
+
for c in spec.get("constraints") or []:
|
|
1269
|
+
t = c.get("type")
|
|
1270
|
+
row = _SK_CONSTRAINT.get(t)
|
|
1271
|
+
if not row:
|
|
1272
|
+
raise RuntimeError("sketch %s: unknown constraint %s" % (name, t))
|
|
1273
|
+
fc, form = row
|
|
1274
|
+
v = c.get("value")
|
|
1275
|
+
if form == "one":
|
|
1276
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"])))
|
|
1277
|
+
elif form == "ab2":
|
|
1278
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), int(c.get("pa", 1)), gi(c["b"]), int(c.get("pb", 1))))
|
|
1279
|
+
elif form == "circles":
|
|
1280
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), 3, gi(c["b"]), 3))
|
|
1281
|
+
elif form == "pair":
|
|
1282
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), gi(c["b"])))
|
|
1283
|
+
elif form == "size":
|
|
1284
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), int(c.get("pa", 2)), float(v)))
|
|
1285
|
+
dims.append((c, fc, (gi(c["a"]), int(c.get("pa", 2))), None, float(v)))
|
|
1286
|
+
elif form == "size_one":
|
|
1287
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), float(v)))
|
|
1288
|
+
dims.append((c, fc, (gi(c["a"]), None), None, float(v)))
|
|
1289
|
+
elif form == "ab2v":
|
|
1290
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), int(c.get("pa", 1)), gi(c["b"]), int(c.get("pb", 2)), float(v)))
|
|
1291
|
+
dims.append((c, fc, (gi(c["a"]), int(c.get("pa", 1))), (gi(c["b"]), int(c.get("pb", 2))), float(v)))
|
|
1292
|
+
else:
|
|
1293
|
+
sk.addConstraint(Sketcher.Constraint(fc, gi(c["a"]), gi(c["b"]), float(v)))
|
|
1294
|
+
dims.append((c, fc, (gi(c["a"]), None), (gi(c["b"]), None), float(v)))
|
|
1295
|
+
rc = sk.solve()
|
|
1296
|
+
if rc < 0:
|
|
1297
|
+
raise RuntimeError("sketch %s: solver failed (rc=%s), constraints conflict" % (name, rc))
|
|
1298
|
+
doc.recompute()
|
|
1299
|
+
for c, fc, ref1, ref2, want in dims:
|
|
1300
|
+
def _pt(ref):
|
|
1301
|
+
seg = sk.Geometry[ref[0]]
|
|
1302
|
+
return seg.StartPoint if ref[1] == 1 else seg.EndPoint
|
|
1303
|
+
if fc == "Radius":
|
|
1304
|
+
got = sk.Geometry[ref1[0]].Radius
|
|
1305
|
+
elif fc == "Distance":
|
|
1306
|
+
got = (_pt(ref2) - _pt(ref1)).Length
|
|
1307
|
+
elif fc in ("DistanceX", "DistanceY"):
|
|
1308
|
+
p = _pt(ref1)
|
|
1309
|
+
got = p.x if fc == "DistanceX" else p.y
|
|
1310
|
+
else:
|
|
1311
|
+
continue
|
|
1312
|
+
if abs(got - want) > 1e-6:
|
|
1313
|
+
raise RuntimeError("sketch %s: constraint %s not satisfied: want %s got %.4f" % (name, c.get("id"), want, got))
|
|
1314
|
+
wires = sk.Shape.Wires
|
|
1315
|
+
if not wires:
|
|
1316
|
+
raise RuntimeError("sketch %s: not a closed contour (%d curves)" % (name, len(ids)))
|
|
1317
|
+
return wires[0]
|
|
1318
|
+
|
|
1234
1319
|
def _prim(op, _cur=None):
|
|
1235
1320
|
k = op.get("kind")
|
|
1236
1321
|
_at_spec = op.get("at")
|
|
@@ -1248,6 +1333,9 @@ def _prim(op, _cur=None):
|
|
|
1248
1333
|
if k == "torus":
|
|
1249
1334
|
return Part.makeTorus(float(op["r1"]), float(op["r2"]), at, _dir(op.get("axis")))
|
|
1250
1335
|
if k == "extrude":
|
|
1336
|
+
if op.get("sketch"):
|
|
1337
|
+
face = Part.Face(_sketch(str(op["sketch"])))
|
|
1338
|
+
return face.extrude(_dir(op.get("dir"), (0.0, 0.0, float(op.get("h") or 10.0))))
|
|
1251
1339
|
base = float(op.get("base", 0.0))
|
|
1252
1340
|
h = float(op.get("h", 0.0))
|
|
1253
1341
|
# Points may be 2D (z = base) or 3D; dir replaces the +Z extrusion,
|
|
@@ -1259,6 +1347,20 @@ def _prim(op, _cur=None):
|
|
|
1259
1347
|
raise RuntimeError("extrude 需要 h 或 dir(当前的挤出向量长度为 0)")
|
|
1260
1348
|
return Part.Face(Part.makePolygon(ps + [ps[0]])).extrude(vec)
|
|
1261
1349
|
if k == "revolve":
|
|
1350
|
+
if op.get("sketch"):
|
|
1351
|
+
face = Part.Face(_sketch(str(op["sketch"])))
|
|
1352
|
+
rev = face.revolve(_v3(op.get("origin")), _dir(op.get("axis"), (0.0, 0.0, 1.0)), float(op.get("angle", 360.0)))
|
|
1353
|
+
# A revolve of a face yields a shell; a full turn closes it, but only
|
|
1354
|
+
# becomes a solid after we say so. Volume 0 means "still a shell".
|
|
1355
|
+
if abs(rev.Volume) < 1e-9 and rev.Shells:
|
|
1356
|
+
try:
|
|
1357
|
+
rev = Part.makeSolid(rev.Shells[0])
|
|
1358
|
+
except Exception:
|
|
1359
|
+
pass
|
|
1360
|
+
if abs(rev.Volume) < 1e-9:
|
|
1361
|
+
# Do not hand a zero-volume shell downstream: say it out loud.
|
|
1362
|
+
raise RuntimeError("草图旋转得到的是壳体、没有闭合(需要 sewShape 缝合);先用挤出,或把草图轮廓贴住旋转轴")
|
|
1363
|
+
return rev
|
|
1262
1364
|
ps = [Vector(float(p[0]), 0.0, float(p[1])) for p in op["points"]]
|
|
1263
1365
|
ang = float(op.get("angle", 360.0))
|
|
1264
1366
|
return Part.Face(Part.makePolygon(ps + [ps[0]])).revolve(Vector(0.0, 0.0, 0.0), Vector(0.0, 0.0, 1.0), ang)
|
|
@@ -2250,6 +2352,7 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
|
|
|
2250
2352
|
},
|
|
2251
2353
|
title: { type: 'string', description: 'Panel title for the published scene.' },
|
|
2252
2354
|
export: { type: 'string', description: 'Optional .step/.stp path to also write the result.' },
|
|
2355
|
+
sketches: { type: 'object', description: 'Sketches by name: {S1:{geometry:[{id,type,...}],constraints:[{id,type,...}]}}. A feature extrudes one with {kind:"extrude",sketch:"S1"}.' },
|
|
2253
2356
|
},
|
|
2254
2357
|
required: ['ops'],
|
|
2255
2358
|
additionalProperties: false,
|
|
@@ -2258,11 +2361,11 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
|
|
|
2258
2361
|
schema: { type: 'object', properties: { ok: { type: 'boolean' }, sceneId: { type: 'string' }, volume: { type: 'number' }, features: { type: 'number' } } },
|
|
2259
2362
|
render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
|
|
2260
2363
|
},
|
|
2261
|
-
execute: async ({ ops, title, export: exportPath }) => {
|
|
2364
|
+
execute: async ({ ops, title, sketches, export: exportPath }) => {
|
|
2262
2365
|
if (!Array.isArray(ops) || !ops.length) return { ok: false, error: 'ops 不能为空:至少给一个图元' }
|
|
2263
2366
|
const unknown = ops.find((op) => !op || !FEATURE_KINDS.includes(op.kind))
|
|
2264
2367
|
if (unknown) return { ok: false, error: `不支持的特征:${unknown?.kind ?? '(空)'}(可用 ${FEATURE_KINDS.join(' / ')})` }
|
|
2265
|
-
const py = featuresScript(ops, exportPath)
|
|
2368
|
+
const py = featuresScript(ops, exportPath, { sketches })
|
|
2266
2369
|
return bridge.run(py, { title: title ?? 'feature' })
|
|
2267
2370
|
},
|
|
2268
2371
|
})
|