* claim: #2874 * Sketch fillet and chamfer refuse an offset's corners (#2874) An analytic offset's output ends are recomputed from its source curves on every solve and held fixed there. Filleting an output corner moved those ends to the tangent points, the next refresh pulled them back to the sharp corner, and the new arcs closed into full circles with the sketch left unsolved. Fillet and chamfer now refuse a corner on an offset output before anything changes (a batch fillet applies all of its corners or none) and say how to get the rounded result: round the source corners before offsetting, or break the offset association first, after which the same corners round normally.
This commit is contained in:
@@ -758,6 +758,26 @@ fn refuse_infinite_corner(sk: &Sketch, p: &Value, cmd: &str) -> Result<()> {
|
||||
if at_corner { Err(bad(cmd, "an infinite line has no corner to round or cut: make it a normal line first")) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// Fillets and chamfers refuse a corner of an offset's output (#2874). The output's ends are
|
||||
/// recomputed from its source curves on every solve, so a corner cut there would be pulled back
|
||||
/// to the sharp corner, leaving full circles and an unsolved sketch.
|
||||
fn refuse_offset_corner(sk: &Sketch, p: &Value, cmd: &str) -> Result<()> {
|
||||
let output = |c: usize| sk.curves.get(c).filter(|c| sk.offset_distance(&c.id).is_some()).map(|c| c.id.clone());
|
||||
let found = match str_(p, "point").and_then(|r| sk.resolve_point(r)) {
|
||||
Some(q) => (0..sk.curves.len()).filter(|c| sk.curves.get(*c).is_some_and(|x| x.kind.uses(q))).find_map(output),
|
||||
None => ["a", "b"].iter().find_map(|k| str_(p, k).and_then(|i| sk.curve_index(i)).and_then(output)),
|
||||
};
|
||||
match found {
|
||||
Some(id) => Err(bad(
|
||||
cmd,
|
||||
format!(
|
||||
"`{id}` is an offset, and its corners follow its source curves: round or cut the source corners before offsetting, or break the offset association first"
|
||||
),
|
||||
)),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A corner of two lines: (line a, line b, corner point, far end of a, far end of b).
|
||||
fn corner(sk: &Sketch, p: &Value, cmd: &str) -> Result<(usize, usize, usize, usize, usize)> {
|
||||
let ends = |l: usize| match sk.curves.get(l).map(|c| &c.kind) {
|
||||
@@ -976,6 +996,7 @@ fn kept_json(kept: Vec<CornerKept>) -> (Vec<String>, Vec<String>) {
|
||||
/// the arc, the tangent constraints and what became of the corner point.
|
||||
fn fillet_corner(sk: &mut Sketch, p: &Value, r: f64, cmd: &str) -> Result<(usize, Vec<String>, CornerKept)> {
|
||||
refuse_infinite_corner(sk, p, cmd)?;
|
||||
refuse_offset_corner(sk, p, cmd)?;
|
||||
weld_corner(sk, p);
|
||||
let (la, lb, c, fa, fb) = match corner(sk, p, cmd) {
|
||||
Ok(x) => x,
|
||||
@@ -1086,6 +1107,7 @@ fn round_corner(sk: &mut Sketch, p: &Value, r: f64, cmd: &str) -> Result<Option<
|
||||
fn chamfer(s: &mut Session, p: &Value, cmd: &str, dists: impl FnOnce(f64) -> Result<(f64, f64)>) -> Result<Value> {
|
||||
let (out, info) = edit(s, p, cmd, false, |sk, _| {
|
||||
refuse_infinite_corner(sk, p, cmd)?;
|
||||
refuse_offset_corner(sk, p, cmd)?;
|
||||
weld_corner(sk, p);
|
||||
let (la, lb, c, fa, fb) = corner(sk, p, cmd)?;
|
||||
let (pc, pa, pb) = (sk.point(c).unwrap_or_default(), sk.point(fa).unwrap_or_default(), sk.point(fb).unwrap_or_default());
|
||||
|
||||
@@ -1841,3 +1841,33 @@ fn trim_and_extend_refuse_an_offsets_ends_until_its_association_is_broken() {
|
||||
"{sh:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// #2874: an offset's corners follow its source curves, so filleting them is refused before
|
||||
/// anything changes (the whole batch), instead of leaving full circles and an unsolved sketch.
|
||||
/// Once the offset association is broken the same corners round normally.
|
||||
#[test]
|
||||
fn fillet_refuses_an_offsets_corners_until_its_association_is_broken() {
|
||||
let mut s = new_sketch();
|
||||
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 30]}));
|
||||
let out = ids(&run(&mut s, "sketch.offset", json!({"curves": ["l1", "l2", "l3", "l4"], "distance": "2 mm", "side": [-5, -5]}))["curves"]);
|
||||
assert_eq!(out, ["l5", "l6", "l7", "l8"]);
|
||||
let before = serde_json::to_value(sketch(&s)).unwrap();
|
||||
let corners = json!({"points": ["l5.start", "l5.end", "l6.end", "l7.end"], "radius": "2 mm"});
|
||||
let e = s.execute("sketch.fillet", &corners).unwrap_err().to_string();
|
||||
assert!(e.contains("offset") && e.contains("break the offset association"), "{e}");
|
||||
assert_eq!(serde_json::to_value(sketch(&s)).unwrap(), before, "a refused fillet changes nothing");
|
||||
let e = s.execute("sketch.chamfer.equal_distance", &json!({"point": "l6.end", "distance": 1})).unwrap_err().to_string();
|
||||
assert!(e.contains("offset"), "{e}");
|
||||
|
||||
run(&mut s, "sketch.offset.break", json!({"curve": "l5"}));
|
||||
let f = run(&mut s, "sketch.fillet", corners);
|
||||
assert_eq!(f["sketch"]["solved"], json!(true), "{f}");
|
||||
let sk = sketch(&s);
|
||||
let arcs = ids(&f["curves"]);
|
||||
assert_eq!(arcs.len(), 4);
|
||||
for arc in &arcs {
|
||||
let ai = sk.curve_index(arc).unwrap();
|
||||
assert!(matches!(sk.curves[ai].kind, CurveKind::Arc { .. }), "{arc} is a trimmed arc");
|
||||
assert!((sk.radius(ai).unwrap() - 2.0).abs() < 1e-7);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user