dsh-cad-studio 0.8.5 → 0.9.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 +91 -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,
|
|
@@ -2250,6 +2338,7 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
|
|
|
2250
2338
|
},
|
|
2251
2339
|
title: { type: 'string', description: 'Panel title for the published scene.' },
|
|
2252
2340
|
export: { type: 'string', description: 'Optional .step/.stp path to also write the result.' },
|
|
2341
|
+
sketches: { type: 'object', description: 'Sketches by name: {S1:{geometry:[{id,type,...}],constraints:[{id,type,...}]}}. A feature extrudes one with {kind:"extrude",sketch:"S1"}.' },
|
|
2253
2342
|
},
|
|
2254
2343
|
required: ['ops'],
|
|
2255
2344
|
additionalProperties: false,
|
|
@@ -2258,11 +2347,11 @@ ${pair ? ` __out["distance"] = round((FreeCAD.Vector(${JSON.stringify(from)})
|
|
|
2258
2347
|
schema: { type: 'object', properties: { ok: { type: 'boolean' }, sceneId: { type: 'string' }, volume: { type: 'number' }, features: { type: 'number' } } },
|
|
2259
2348
|
render: (_a, v) => [{ type: 'text', text: JSON.stringify(v, null, 2) }],
|
|
2260
2349
|
},
|
|
2261
|
-
execute: async ({ ops, title, export: exportPath }) => {
|
|
2350
|
+
execute: async ({ ops, title, sketches, export: exportPath }) => {
|
|
2262
2351
|
if (!Array.isArray(ops) || !ops.length) return { ok: false, error: 'ops 不能为空:至少给一个图元' }
|
|
2263
2352
|
const unknown = ops.find((op) => !op || !FEATURE_KINDS.includes(op.kind))
|
|
2264
2353
|
if (unknown) return { ok: false, error: `不支持的特征:${unknown?.kind ?? '(空)'}(可用 ${FEATURE_KINDS.join(' / ')})` }
|
|
2265
|
-
const py = featuresScript(ops, exportPath)
|
|
2354
|
+
const py = featuresScript(ops, exportPath, { sketches })
|
|
2266
2355
|
return bridge.run(py, { title: title ?? 'feature' })
|
|
2267
2356
|
},
|
|
2268
2357
|
})
|