diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index 29c3f97d6..f91795471 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -77,7 +77,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .at("SKETCH", "MODIFY") .icon("sketch_break") .enabled(in_sketch) - .params(concat!("curve: id, at: [x,y] (split at intersections) | points: [[x,y]…] (1…1000 positions projected onto the curve; duplicates are merged)", created_doc!())), + .params(concat!("curve: id, at: [x,y] (split at intersections; at a point where the curve ends and another curve meets, the curve gets its own end there and the two are no longer joined) | points: [[x,y]…] (1…1000 positions projected onto the curve; duplicates are merged)", created_doc!())), CommandSpec::new("sketch.scale", "Sketch Scale", scale) .at("SKETCH", "MODIFY") .icon("sketch_scale") @@ -777,6 +777,12 @@ fn break_cmd(s: &mut Session, p: &Value) -> Result { let points = if p.get("points").is_some() { Some(vec2_list(cmd, p, "points", 1000)?) } else { None }; let (out, info) = edit(s, p, cmd, false, |sk, doc| { let ci = curve_arg(sk, p, cmd)?; + if points.is_none() + && let Some(end) = shared_end(sk, ci, req_vec2(cmd, p, "at")?) + { + separate_end(sk, ci, end)?; + return Ok(ids_of(sk, &[ci])); + } if sk.curves.get(ci).is_some_and(|c| c.kind.is_freeform()) { let r = if let Some(points) = &points { super::sketch_freeform::break_at_points(sk, ci, points, cmd)? @@ -836,6 +842,32 @@ fn break_cmd(s: &mut Session, p: &Value) -> Result { Ok(json!({"curves": out, "sketch": info})) } +/// The end of curve `ci` at `at` that another curve also ends at or passes through: Break there +/// separates the two (#7410). +fn shared_end(sk: &Sketch, ci: usize, at: Vec2) -> Option { + let (a, b) = sk.curves.get(ci)?.kind.ends()?; + let end = [a, b].into_iter().find(|&e| sk.point(e).is_some_and(|q| q.dist(at) <= 1e-6))?; + sk.curves.iter().enumerate().any(|(oi, c)| oi != ci && uses(c, end)).then_some(end) +} + +/// Give curve `ci` its own point in place of `end`, at the same place (`.start` or +/// `.end` when that id is free): the curves that met there stay where they are, no longer joined. +fn separate_end(sk: &mut Sketch, ci: usize, end: usize) -> Result<()> { + let pos = sk.point(end).ok_or_else(|| bad("sketch.break", "point"))?; + let curve = sk.curves.get(ci).ok_or_else(|| bad("sketch.break", "curve"))?; + let role = if curve.kind.ends().is_some_and(|(a, _)| a == end) { "start" } else { "end" }; + let name = format!("{}.{role}", curve.id); + let own = match sk.add_point(pos, Some(&name)) { + Ok(i) => i, + Err(solidcraft_sketch::SketchError::Duplicate(_)) => sk.add_point(pos, None)?, + Err(e) => return Err(e.into()), + }; + if let Some(c) = sk.curves.get_mut(ci) { + c.kind.map_points(&|q| if q == end { own } else { q }); + } + Ok(()) +} + /// How far a line with nothing in its way extends: the reach its preview draws, and the length /// `sketch.extend` adds, so clicking a preview that overhangs its target adds that line instead /// of nothing. diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index 0157bdd4f..d6c66e862 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -363,6 +363,43 @@ fn an_arc_extended_from_an_end_a_tangent_line_ends_on_adds_no_arc_of_no_length() } } +/// #7410: two lines drawn as one chain share their corner point. Break at that point gives the +/// picked line its own end there: both stay in place, and each can move without the other. +#[test] +fn break_at_a_shared_point_separates_the_curves_that_meet_there() { + let mut s = new_sketch(); + let chain = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 0], [20, 10]]}))["curves"]); + let (first, second) = (chain[0].clone(), chain[1].clone()); + let ends = |s: &Session, id: &str| { + let sk = sketch(s); + let (a, b) = sk.curves[sk.curve_index(id).unwrap()].kind.ends().unwrap(); + (a, b, sk.point(a).unwrap(), sk.point(b).unwrap()) + }; + let (_, shared, ..) = ends(&s, &first); + assert_eq!(ends(&s, &second).0, shared, "the chain shares its corner"); + let points = sketch(&s).points.len(); + + let out = run(&mut s, "sketch.break", json!({"curve": second, "at": [20, 0]})); + assert_eq!(ids(&out["curves"]), vec![second.clone()], "{out}"); + let (own, _, at, far) = ends(&s, &second); + let (_, kept, ..) = ends(&s, &first); + assert_eq!(kept, shared, "the first line keeps the point"); + assert_ne!(own, shared, "the second line has its own end"); + assert_eq!(sketch(&s).points.len(), points + 1); + assert!(at.dist(Vec2::new(20.0, 0.0)) < 1e-9 && far.dist(Vec2::new(20.0, 10.0)) < 1e-9, "the second line stays put: {at:?} {far:?}"); + let own_id = sketch(&s).points[own].id.clone(); + assert_eq!(own_id, format!("{second}.start")); + + // Moving the second line's new end leaves the first line where it was. + run(&mut s, "sketch.move_point", json!({"point": own_id, "to": [25, 0]})); + let (_, _, a, b) = ends(&s, &first); + assert!(a.dist(Vec2::new(0.0, 0.0)) < 1e-9 && b.dist(Vec2::new(20.0, 0.0)) < 1e-9, "{a:?} {b:?}"); + + // A point only one curve ends at has nothing to separate: Break there still refuses. + let lone = s.execute("sketch.break", &json!({"curve": second, "at": [20, 10]})); + assert!(lone.is_err(), "{lone:?}"); +} + #[test] fn trim_circle_makes_an_arc_and_a_profile() { let mut s = new_sketch();