|
|
@@ -2376,8 +2376,10 @@ function freshFaces(oc, maker, before, after) {
|
|
|
// Shared by fillet and chamfer: `make(body, edges)` returns a built maker;
|
|
|
// run it on every body that has picked edges and return the new body list.
|
|
|
// With `report` ({ fresh: [] }) it also notes which faces of each new body
|
|
|
-// are new (freshFaces), for the live preview.
|
|
|
-function onEdges(oc, bodies, feature, what, make, report) {
|
|
|
+// are new (freshFaces), for the live preview. `retry(body, edges)`
|
|
|
+// (optional) builds it another way when the maker's result is broken, or
|
|
|
+// returns null.
|
|
|
+function onEdges(oc, bodies, feature, what, make, report, retry) {
|
|
|
const picked = findEdges(oc, bodies, feature.edges);
|
|
|
if (!picked.length) {
|
|
|
throw new Error('No edge is selected');
|
|
|
@@ -2400,13 +2402,25 @@ function onEdges(oc, bodies, feature, what, make, report) {
|
|
|
let maker = null;
|
|
|
try {
|
|
|
maker = make(body, edges, which);
|
|
|
- const made = maker.Shape();
|
|
|
+ let made = maker.Shape();
|
|
|
if (!isSound(oc, made)) {
|
|
|
made.delete();
|
|
|
- throw new Error('The ' + what + ' does not fit on these edges: it is too large for the faces next to them. Try a smaller size');
|
|
|
+ const other = retry && !maker.fresh ? retry(body, edges, which) : null;
|
|
|
+ if (other) {
|
|
|
+ maker.delete();
|
|
|
+ maker = other;
|
|
|
+ made = maker.Shape();
|
|
|
+ }
|
|
|
+ if (!other || !isSound(oc, made)) {
|
|
|
+ if (other) {
|
|
|
+ made.delete();
|
|
|
+ }
|
|
|
+ throw new Error('The ' + what + ' does not fit on these edges: it is too large for the faces next to them. Try a smaller size');
|
|
|
+ }
|
|
|
}
|
|
|
if (report) {
|
|
|
- report.fresh[index] = freshFaces(oc, maker, body, made);
|
|
|
+ // a chamfer that was cut (cutChamfer) knows its new faces itself
|
|
|
+ report.fresh[index] = maker.fresh ? maker.fresh() : freshFaces(oc, maker, body, made);
|
|
|
}
|
|
|
return made;
|
|
|
} catch (err) {
|
|
|
@@ -2529,8 +2543,9 @@ export function chamfer(oc, bodies, feature, report) {
|
|
|
if (mode === 'angle' && !(angle > 0 && angle < 90)) {
|
|
|
throw new Error('The chamfer angle must be between 0 and 90 degrees');
|
|
|
}
|
|
|
- return onEdges(oc, bodies, feature, 'chamfer', function (body, edges) {
|
|
|
- const maker = new oc.BRepFilletAPI_MakeChamfer(body);
|
|
|
+ // What building the chamfer of `edges` on `body` needs (cutChamfer):
|
|
|
+ // { build(scale), sizes(i, faces, between), free() }
|
|
|
+ const plan = function (body, edges) {
|
|
|
// Two distances or an angle are measured from one of the edge's two
|
|
|
// faces. The edges picked together all measure from the same kind
|
|
|
// of face: the one most of them touch (the top of a box whose top
|
|
|
@@ -2552,11 +2567,12 @@ export function chamfer(oc, bodies, feature, report) {
|
|
|
});
|
|
|
});
|
|
|
};
|
|
|
+ // per edge, the face `distance` is measured on (none: both)
|
|
|
+ let refs;
|
|
|
try {
|
|
|
- edges.forEach(function (edge, i) {
|
|
|
+ refs = edges.map(function (edge, i) {
|
|
|
if (mode === 'equal') {
|
|
|
- maker.Add(distance, edge);
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
const faces = adjacent[i];
|
|
|
if (faces.length < 2) {
|
|
|
@@ -2567,26 +2583,345 @@ export function chamfer(oc, bodies, feature, report) {
|
|
|
}).sort(function (a, b) {
|
|
|
return b.n - a.n || a.k - b.k;
|
|
|
});
|
|
|
- const ref = order[feature.flip ? 1 : 0].face;
|
|
|
- if (mode === 'two') {
|
|
|
- maker.Add(distance, distance2, edge, ref);
|
|
|
- } else {
|
|
|
- maker.AddDA(distance, angle * Math.PI / 180, edge, ref);
|
|
|
- }
|
|
|
+ return order[feature.flip ? 1 : 0].face;
|
|
|
});
|
|
|
} catch (err) {
|
|
|
free();
|
|
|
- maker.delete();
|
|
|
throw err;
|
|
|
}
|
|
|
- free();
|
|
|
- maker.Build();
|
|
|
- if (!maker.IsDone()) {
|
|
|
+ return {
|
|
|
+ // the builder with every distance times `scale`, built, or null
|
|
|
+ build: function (scale) {
|
|
|
+ const maker = new oc.BRepFilletAPI_MakeChamfer(body);
|
|
|
+ try {
|
|
|
+ edges.forEach(function (edge, i) {
|
|
|
+ if (mode === 'equal') {
|
|
|
+ maker.Add(distance * scale, edge);
|
|
|
+ } else if (mode === 'two') {
|
|
|
+ maker.Add(distance * scale, distance2 * scale, edge, refs[i]);
|
|
|
+ } else {
|
|
|
+ maker.AddDA(distance * scale, angle * Math.PI / 180, edge, refs[i]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ maker.Build();
|
|
|
+ if (maker.IsDone()) {
|
|
|
+ return maker;
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ // the kernel throws without a message on an edge it cannot take
|
|
|
+ }
|
|
|
+ maker.delete();
|
|
|
+ return null;
|
|
|
+ },
|
|
|
+ // the distances on the edge's two faces (facesOfEdge order)
|
|
|
+ sizes: function (i, faces, between) {
|
|
|
+ if (mode === 'equal') {
|
|
|
+ return [distance, distance];
|
|
|
+ }
|
|
|
+ const near = faces[0].IsSame(refs[i]) ? 0 : 1;
|
|
|
+ // the bevel leaves the reference face at `angle`, so on the
|
|
|
+ // other face it reaches (law of sines in the cross section)
|
|
|
+ const far = mode === 'two' ? distance2 : distance * Math.sin(angle * Math.PI / 180) / Math.sin(between + angle * Math.PI / 180);
|
|
|
+ const out = [];
|
|
|
+ out[near] = distance;
|
|
|
+ out[1 - near] = far;
|
|
|
+ return out;
|
|
|
+ },
|
|
|
+ free: free
|
|
|
+ };
|
|
|
+ };
|
|
|
+ // The builder's chamfer; cut (cutChamfer) when the builder refuses it
|
|
|
+ const make = function (body, edges) {
|
|
|
+ const p = plan(body, edges);
|
|
|
+ try {
|
|
|
+ const maker = p.build(1) || cutChamfer(oc, body, edges, p.sizes, p.build);
|
|
|
+ if (!maker) {
|
|
|
+ throw new Error('The chamfer could not be built on these edges. Try a smaller distance');
|
|
|
+ }
|
|
|
+ return maker;
|
|
|
+ } finally {
|
|
|
+ p.free();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // The builder may also say done and leave the face it should take away
|
|
|
+ // as a broken sliver (the end of a rib standing on a plate): cut it then
|
|
|
+ const retry = function (body, edges) {
|
|
|
+ const p = plan(body, edges);
|
|
|
+ try {
|
|
|
+ return cutChamfer(oc, body, edges, p.sizes, p.build);
|
|
|
+ } finally {
|
|
|
+ p.free();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return onEdges(oc, bodies, feature, 'chamfer', make, report, retry);
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- a chamfer that takes a whole face away ---- */
|
|
|
+// OpenCascade's chamfer builder refuses a bevel that ends exactly on the far
|
|
|
+// edge of a face next to the edge: a 10 mm chamfer on the end of a 10 mm
|
|
|
+// plate, which takes the end face away, fails while 9.99 builds (and so do
|
|
|
+// two chamfers that meet in a ridge), or it says done and leaves the face as
|
|
|
+// a broken sliver. On straight edges between flat faces a
|
|
|
+// chamfer is the same as cutting a wedge off along the edge, so that is what
|
|
|
+// is done then. The cut is only kept when it takes off what the builder's
|
|
|
+// own chamfers a hair smaller say it should: their volumes, carried on to
|
|
|
+// the full size. A wedge that bites into something past the end of an edge
|
|
|
+// (a wall the edge runs into) fails that check, and the chamfer fails as it
|
|
|
+// did before.
|
|
|
+
|
|
|
+// how much smaller (as a fraction) the builder's chamfers for the check are
|
|
|
+const CUT_CHECK_STEP = 1e-3;
|
|
|
+
|
|
|
+function vSub(a, b) {
|
|
|
+ return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
|
+}
|
|
|
+
|
|
|
+function vAdd(a, b, k) {
|
|
|
+ return [a[0] + b[0] * k, a[1] + b[1] * k, a[2] + b[2] * k];
|
|
|
+}
|
|
|
+
|
|
|
+function vDot(a, b) {
|
|
|
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
|
+}
|
|
|
+
|
|
|
+function vCross(a, b) {
|
|
|
+ return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
|
+}
|
|
|
+
|
|
|
+function vUnit(a) {
|
|
|
+ const l = Math.hypot(a[0], a[1], a[2]);
|
|
|
+ return l > 1e-12 ? [a[0] / l, a[1] / l, a[2] / l] : null;
|
|
|
+}
|
|
|
+
|
|
|
+// A straight edge between two flat faces that meet at an outside corner:
|
|
|
+// { start, e: unit direction, length, faces: [face, face] (facesOfEdge
|
|
|
+// order, the caller's to delete), u: per face the direction across it away
|
|
|
+// from the edge, between: the angle between the two in radians }, or null.
|
|
|
+function bevelSite(oc, body, edge) {
|
|
|
+ const curve = new oc.BRepAdaptor_Curve(edge);
|
|
|
+ let ends = null;
|
|
|
+ if (curve.GetType() === oc.GeomAbs_CurveType.GeomAbs_Line) {
|
|
|
+ const p = curve.Value(curve.FirstParameter());
|
|
|
+ const q = curve.Value(curve.LastParameter());
|
|
|
+ ends = [[p.X(), p.Y(), p.Z()], [q.X(), q.Y(), q.Z()]];
|
|
|
+ p.delete();
|
|
|
+ q.delete();
|
|
|
+ }
|
|
|
+ curve.delete();
|
|
|
+ if (!ends) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const length = Math.hypot(ends[1][0] - ends[0][0], ends[1][1] - ends[0][1], ends[1][2] - ends[0][2]);
|
|
|
+ const e = vUnit(vSub(ends[1], ends[0]));
|
|
|
+ const faces = facesOfEdge(oc, body, edge);
|
|
|
+ const drop = function () {
|
|
|
+ faces.forEach(function (f) {
|
|
|
+ f.delete();
|
|
|
+ });
|
|
|
+ return null;
|
|
|
+ };
|
|
|
+ if (!e || faces.length !== 2) {
|
|
|
+ return drop();
|
|
|
+ }
|
|
|
+ const mid = vAdd(ends[0], e, length / 2);
|
|
|
+ const normals = faces.map(function (face) {
|
|
|
+ const at = surfaceAt(oc, face, mid);
|
|
|
+ return at.key.indexOf('plane:') === 0 ? at.normal : null;
|
|
|
+ });
|
|
|
+ if (!normals[0] || !normals[1]) {
|
|
|
+ return drop();
|
|
|
+ }
|
|
|
+ // An outside corner: a step off the edge across one face and under the
|
|
|
+ // other is out of the material (as edgeSignature)
|
|
|
+ const off = vUnit(vSub(normals[0], normals[1]));
|
|
|
+ if (!off || isInside(oc, body, vAdd(mid, off, 0.01))) {
|
|
|
+ return drop();
|
|
|
+ }
|
|
|
+ // Across a face away from the edge is in the face's plane, square to the
|
|
|
+ // edge, and (at an outside corner) against the other face's normal
|
|
|
+ const u = [0, 1].map(function (k) {
|
|
|
+ const across = vUnit(vCross(normals[k], e));
|
|
|
+ return across && vDot(across, normals[1 - k]) > 0 ? across.map(function (c) {
|
|
|
+ return -c;
|
|
|
+ }) : across;
|
|
|
+ });
|
|
|
+ if (!u[0] || !u[1]) {
|
|
|
+ return drop();
|
|
|
+ }
|
|
|
+ // Per end, does the material stop there? Every other face through the
|
|
|
+ // end looks away from the edge (the end of a box): the wedge may run on
|
|
|
+ // past it. A face that looks back at the edge (the plate a rib stands
|
|
|
+ // on) carries the material on: the wedge stops at the end.
|
|
|
+ const open = [false, false];
|
|
|
+ [0, 1].forEach(function (k) {
|
|
|
+ const end = ends[k];
|
|
|
+ const away = k === 0 ? e.map(function (c) {
|
|
|
+ return -c;
|
|
|
+ }) : e;
|
|
|
+ let stops = true;
|
|
|
+ const explorer = new oc.TopExp_Explorer(body, oc.TopAbs_ShapeEnum.TopAbs_FACE);
|
|
|
+ for (; explorer.More() && stops; explorer.Next()) {
|
|
|
+ const face = oc.TopoDS.Face(explorer.Current());
|
|
|
+ if (!face.IsSame(faces[0]) && !face.IsSame(faces[1]) && distanceTo(oc, face, end) < 1e-5) {
|
|
|
+ const normal = surfaceAt(oc, face, end).normal;
|
|
|
+ stops = !!normal && vDot(normal, away) > 1e-6;
|
|
|
+ }
|
|
|
+ face.delete();
|
|
|
+ }
|
|
|
+ explorer.delete();
|
|
|
+ open[k] = stops;
|
|
|
+ });
|
|
|
+ return { start: ends[0], e: e, length: length, faces: faces, u: u, open: open, between: Math.acos(Math.max(-1, Math.min(1, vDot(u[0], u[1])))) };
|
|
|
+}
|
|
|
+
|
|
|
+// A solid: the triangle `corners` swept by `sweep`
|
|
|
+function wedgeSolid(oc, corners, sweep) {
|
|
|
+ const points = corners.map(function (p) {
|
|
|
+ return pnt(oc, p);
|
|
|
+ });
|
|
|
+ const wireMaker = new oc.BRepBuilderAPI_MakeWire();
|
|
|
+ points.forEach(function (point, i) {
|
|
|
+ const edgeMaker = new oc.BRepBuilderAPI_MakeEdge(point, points[(i + 1) % points.length]);
|
|
|
+ wireMaker.Add(edgeMaker.Edge());
|
|
|
+ edgeMaker.delete();
|
|
|
+ });
|
|
|
+ const faceMaker = new oc.BRepBuilderAPI_MakeFace(wireMaker.Wire(), true);
|
|
|
+ const along = vec(oc, sweep);
|
|
|
+ const prism = new oc.BRepPrimAPI_MakePrism(faceMaker.Face(), along, false, true);
|
|
|
+ const solid = prism.Shape();
|
|
|
+ [prism, along, faceMaker, wireMaker].concat(points).forEach(function (o) {
|
|
|
+ o.delete();
|
|
|
+ });
|
|
|
+ return outward(oc, solid);
|
|
|
+}
|
|
|
+
|
|
|
+// The chamfer of `edges` (on `body`) cut as wedges. sizes(i, faces,
|
|
|
+// between): the distances on the two faces of edge i; build(scale): the
|
|
|
+// builder's chamfer with every distance times scale, or null. Returns a
|
|
|
+// stand-in for the builder ({ Shape, fresh, delete }) or null.
|
|
|
+function cutChamfer(oc, body, edges, sizes, build) {
|
|
|
+ const sites = [];
|
|
|
+ const free = function () {
|
|
|
+ sites.forEach(function (site) {
|
|
|
+ site.faces.forEach(function (f) {
|
|
|
+ f.delete();
|
|
|
+ });
|
|
|
+ });
|
|
|
+ };
|
|
|
+ let tool = null;
|
|
|
+ let cut = null;
|
|
|
+ try {
|
|
|
+ const bevels = [];
|
|
|
+ for (let i = 0; i < edges.length; i++) {
|
|
|
+ const site = bevelSite(oc, body, edges[i]);
|
|
|
+ if (!site) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ sites.push(site);
|
|
|
+ const d = sizes(i, site.faces, site.between);
|
|
|
+ if (!(d[0] > 0 && d[1] > 0 && isFinite(d[0]) && isFinite(d[1]))) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // The cross section: the bevel from one face to the other, and a
|
|
|
+ // corner off the material so no side lies on a face of the body.
|
|
|
+ // Run on past an end where the material stops, so it clears the
|
|
|
+ // face there (the end of a box may lean); else stop at the end.
|
|
|
+ const a = site.u[0].map(function (c) {
|
|
|
+ return c * d[0];
|
|
|
+ });
|
|
|
+ const b = site.u[1].map(function (c) {
|
|
|
+ return c * d[1];
|
|
|
+ });
|
|
|
+ const outside = vUnit(vAdd(site.u[0], site.u[1], 1));
|
|
|
+ if (!outside) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const past = 2 * Math.max(d[0], d[1]);
|
|
|
+ const head = site.open[0] ? past : 0;
|
|
|
+ const tail = site.open[1] ? past : 0;
|
|
|
+ const base = vAdd(site.start, site.e, -head);
|
|
|
+ const corners = [vAdd(base, a, 1), vAdd(base, b, 1), vAdd(base, outside, -0.5 * Math.min(d[0], d[1]))];
|
|
|
+ const solid = wedgeSolid(oc, corners, site.e.map(function (c) {
|
|
|
+ return c * (site.length + head + tail);
|
|
|
+ }));
|
|
|
+ const joined = tool ? boolean(oc, 'join', tool, solid) : solid;
|
|
|
+ if (tool) {
|
|
|
+ tool.delete();
|
|
|
+ solid.delete();
|
|
|
+ }
|
|
|
+ tool = joined;
|
|
|
+ if (!tool) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // the bevel's plane, its normal pointing out of the material
|
|
|
+ let normal = vUnit(vCross(vSub(b, a), site.e));
|
|
|
+ if (vDot(normal, outside) > 0) {
|
|
|
+ normal = normal.map(function (c) {
|
|
|
+ return -c;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ bevels.push({ point: vAdd(site.start, a, 1), normal: normal });
|
|
|
+ }
|
|
|
+ cut = boolean(oc, 'cut', body, tool);
|
|
|
+ if (!cut || isEmpty(oc, cut)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // The check: the material the builder takes off at 1 - h and 1 - 2h
|
|
|
+ // of the size, carried on to the full size (a chamfer takes off an
|
|
|
+ // amount that grows smoothly with its size: square, plus a cube
|
|
|
+ // part where bevels meet at a corner, so the error is tiny)
|
|
|
+ const before = volume(oc, body);
|
|
|
+ const taken = [1 - CUT_CHECK_STEP, 1 - 2 * CUT_CHECK_STEP].map(function (scale) {
|
|
|
+ const maker = build(scale);
|
|
|
+ if (!maker) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const shape = maker.Shape();
|
|
|
+ const v = volume(oc, shape);
|
|
|
+ shape.delete();
|
|
|
maker.delete();
|
|
|
- throw new Error('The chamfer could not be built on these edges. Try a smaller distance');
|
|
|
+ return before - v;
|
|
|
+ });
|
|
|
+ if (taken[0] === null || taken[1] === null) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- return maker;
|
|
|
- }, report);
|
|
|
+ const expected = 2 * taken[0] - taken[1];
|
|
|
+ const got = before - volume(oc, cut);
|
|
|
+ if (!(expected > 0) || !(Math.abs(got - expected) <= Math.max(expected * 1e-4, Math.abs(before) * 1e-9))) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const shape = cut;
|
|
|
+ cut = null;
|
|
|
+ return {
|
|
|
+ Shape: function () {
|
|
|
+ return shape;
|
|
|
+ },
|
|
|
+ // the new faces: those on a bevel's plane (see freshFaces)
|
|
|
+ fresh: function () {
|
|
|
+ const flags = [];
|
|
|
+ const explorer = new oc.TopExp_Explorer(shape, oc.TopAbs_ShapeEnum.TopAbs_FACE);
|
|
|
+ for (; explorer.More(); explorer.Next()) {
|
|
|
+ const face = oc.TopoDS.Face(explorer.Current());
|
|
|
+ const info = faceInfo(oc, face, face.Orientation() === oc.TopAbs_Orientation.TopAbs_REVERSED);
|
|
|
+ flags.push(info.type === 'plane' && bevels.some(function (bevel) {
|
|
|
+ return vDot(info.normal, bevel.normal) > 1 - 1e-6 && Math.abs(vDot(vSub(info.origin, bevel.point), bevel.normal)) < 1e-4;
|
|
|
+ }));
|
|
|
+ face.delete();
|
|
|
+ }
|
|
|
+ explorer.delete();
|
|
|
+ return flags;
|
|
|
+ },
|
|
|
+ delete: function () {}
|
|
|
+ };
|
|
|
+ } finally {
|
|
|
+ free();
|
|
|
+ if (tool) {
|
|
|
+ tool.delete();
|
|
|
+ }
|
|
|
+ if (cut) {
|
|
|
+ cut.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// How far a point is from a shape
|