Sketch trim and extend refuse an offset's ends instead of undoing silently (#3918)
An offset's output curves have their ends recomputed from the source curves on every solve. Trimming a piece off one reported success and left the curve whole, because the refresh pulled the new end straight back. Extending one was refused as "connected to other geometry", which does not say why. Both now refuse with the reason and the way out: edit the source curves, or break the offset association first. Trimming away a whole offset curve, which removes it from its association, still works.
This commit is contained in:
@@ -345,6 +345,22 @@ pub fn trim_piece(sk: &Sketch, curve: &str, at: Vec2) -> Option<Vec<Vec2>> {
|
||||
Some((0..=n).map(|i| sh.point(a + (b - a) * f64::from(i) / f64::from(n))).collect())
|
||||
}
|
||||
|
||||
/// Trim and extend refuse an offset's output: its ends are recomputed from its source curves on
|
||||
/// every solve, so a new end there would be pulled straight back (the trim reported success and
|
||||
/// left the curve whole).
|
||||
fn refuse_offset_ends(sk: &Sketch, ci: usize, what: &str, cmd: &str) -> Result<()> {
|
||||
match sk.curves.get(ci).filter(|c| sk.offset_distance(&c.id).is_some()) {
|
||||
Some(c) => Err(bad(
|
||||
cmd,
|
||||
format!(
|
||||
"`{}` is an offset, and its ends follow its source curves: {what} the source curves instead, or break the offset association first",
|
||||
c.id
|
||||
),
|
||||
)),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn trim(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "sketch.trim";
|
||||
let at = req_vec2(cmd, p, "at")?;
|
||||
@@ -383,6 +399,7 @@ fn remove_piece(sk: &mut Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<(Valu
|
||||
sk.remove_curves(&[ci]);
|
||||
return Ok((json!({"deleted": id}), removed));
|
||||
}
|
||||
refuse_offset_ends(sk, ci, "trim", cmd)?;
|
||||
release_extent(sk, ci);
|
||||
let ts: Vec<f64> = cuts.iter().map(|c| c.0).collect();
|
||||
let (pieces, pts) = split_at(sk, ci, &ts)?;
|
||||
@@ -590,6 +607,7 @@ fn extend(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Err(bad(cmd, rep.failure(|_| "solve the existing sketch constraints before extending".into())));
|
||||
}
|
||||
let ci = curve_arg(sk, p, cmd)?;
|
||||
refuse_offset_ends(sk, ci, "extend", cmd)?;
|
||||
let sh = sk.shape(ci).ok_or_else(|| bad(cmd, "curve"))?;
|
||||
let (pa, pb) = match sk.curves.get(ci).map(|c| c.kind.clone()) {
|
||||
Some(CurveKind::Line { a, b }) | Some(CurveKind::Arc { a, b, .. }) => (a, b),
|
||||
|
||||
@@ -1814,3 +1814,30 @@ fn trim_piece_is_what_a_trim_there_takes_away() {
|
||||
let piece = super::trim_piece(&sketch(&s), &lone, Vec2::new(3.0, 0.0)).unwrap();
|
||||
assert!(near(piece[0], [0.0, 0.0]) && near(piece[piece.len() - 1], [8.0, 0.0]), "{piece:?}");
|
||||
}
|
||||
|
||||
/// An offset's ends follow its source curves, so trimming a piece off it or extending it is
|
||||
/// refused (the trim used to report success and leave the curve whole). Trimming away a whole
|
||||
/// offset curve still works, and once the association is broken the trim cuts it.
|
||||
#[test]
|
||||
fn trim_and_extend_refuse_an_offsets_ends_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"]);
|
||||
run(&mut s, "sketch.line", json!({"points": [[20, -10], [20, 10]]}));
|
||||
let before = serde_json::to_value(sketch(&s)).unwrap();
|
||||
let e = s.execute("sketch.trim", &json!({"curve": "l5", "at": [30, -2]})).unwrap_err().to_string();
|
||||
assert!(e.contains("offset") && e.contains("break the offset association"), "{e}");
|
||||
let e = s.execute("sketch.extend", &json!({"curve": "l5", "at": [42, -2]})).unwrap_err().to_string();
|
||||
assert!(e.contains("offset"), "{e}");
|
||||
assert_eq!(serde_json::to_value(sketch(&s)).unwrap(), before, "a refused trim changes nothing");
|
||||
|
||||
run(&mut s, "sketch.offset.break", json!({"curve": "l5"}));
|
||||
run(&mut s, "sketch.trim", json!({"curve": "l5", "at": [30, -2]}));
|
||||
let sk = sketch(&s);
|
||||
let sh = sk.shape(sk.curve_index("l5").unwrap()).unwrap();
|
||||
assert!(
|
||||
matches!(sh, solidcraft_sketch::Shape::Line { a, b } if a.dist(Vec2::new(-2.0, -2.0)) < 1e-9 && b.dist(Vec2::new(20.0, -2.0)) < 1e-9),
|
||||
"{sh:?}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user