Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sketch: a Line chain survives Undo (#10016) (#10019)

HiroKenEz64a55392d8d7
diff --git a/crates/ui-egui/src/sketch_line_arc.rs b/crates/ui-egui/src/sketch_line_arc.rs
index 4ee31e69be..3c45ff68f8 100644
--- a/crates/ui-egui/src/sketch_line_arc.rs
+++ b/crates/ui-egui/src/sketch_line_arc.rs
@@ -35,15 +35,60 @@ pub struct LineChain {
rest: Option<(Pos2, f64)>,
/// Just switched back to lines: says so until the pointer leaves.
back_to_lines: bool,
+ /// Where each segment started and the curve the chain was on there, oldest first: where it
+ /// goes back to when Undo takes that segment away (#10016).
+ back: Vec<((Vec2, Option<String>), Option<String>)>,
}
impl LineChain {
- /// Curve `id` now ends the chain, with the pointer still on its end.
- pub fn placed(&mut self, id: &str) {
- *self = LineChain { curve: Some(id.to_string()), ..LineChain::default() };
+ /// Curve `id`, drawn from `start`, now ends the chain, with the pointer still on its end.
+ pub fn placed(&mut self, id: &str, start: (Vec2, Option<String>)) {
+ let mut back = std::mem::take(&mut self.back);
+ back.push((start, self.curve.take()));
+ *self = LineChain { curve: Some(id.to_string()), back, ..LineChain::default() };
}
}
+/// Undo can take away the segment a chain is drawing on from (#10016). The chain then goes back to
+/// the end of the last segment still there, or to where it began (as lines: there is nothing to
+/// be tangent to), and ends if that point has gone too. Clicking on from a point that no longer
+/// exists would only be refused.
+pub fn rejoin(app: &SolveApp, t: &mut Tool) {
+ if t.cmd != LINE && !in_arc_mode(t) {
+ return;
+ }
+ let Some(Some(current)) = t.pts.last().map(|p| p.1.clone()) else { return };
+ let state = app.session.world_state();
+ let Some(ss) = app.session.active_sketch.and_then(|id| state.sketch(id)) else { return };
+ let at = |id: &str| ss.sketch.resolve_point(id).and_then(|p| ss.sketch.point(p));
+ if at(&current).is_some() {
+ return;
+ }
+ let mut back = std::mem::take(&mut t.chain.back);
+ let mut to = None;
+ while let Some(((point, id), curve)) = back.pop() {
+ match id {
+ None => to = Some(((point, None), curve)),
+ Some(id) => match at(&id) {
+ Some(point) => to = Some(((point, Some(id)), curve)),
+ None => continue,
+ },
+ }
+ break;
+ }
+ let (pts, curve) = to.map_or((Vec::new(), None), |(point, curve)| (vec![point], curve));
+ if curve.is_none()
+ && t.cmd != LINE
+ && let Some(line) = Tool::for_command(LINE)
+ {
+ *t = line;
+ }
+ t.pts = pts;
+ t.lined.clear();
+ crate::sketch_dims::reset(t);
+ t.chain = LineChain { curve, back, ..LineChain::default() };
+}
+
/// Whether the tool is a Line tool's chain drawing tangent arcs, not the Tangent Arc tool itself.
pub fn in_arc_mode(t: &Tool) -> bool {
t.cmd == TANGENT_ARC && t.chain.curve.is_some()
@@ -108,7 +153,7 @@ fn switch(app: &mut SolveApp) {
let Some(mut next) = next else { return };
next.pts = old.pts.clone();
next.hover = old.hover.clone();
- next.chain = LineChain { curve: old.chain.curve.clone(), back_to_lines: !to_arc, ..LineChain::default() };
+ next.chain = LineChain { curve: old.chain.curve.clone(), back_to_lines: !to_arc, back: old.chain.back.clone(), ..LineChain::default() };
app.tool = Some(next);
let hint = if to_arc { app.tr("ui.line_arc_hint").to_string() } else { crate::tools::hint(LINE) };
app.set_status(hint, false);
@@ -139,8 +184,11 @@ pub fn place_arc(app: &mut SolveApp, tool: &mut Tool, end: &(Vec2, Option<String
// A failure (the end straight ahead) has said why in the status line; the chain stays as it is.
let made = crate::tools::draw(app, tool, json!({"start": start, "curve": curve, "end": to}));
if let Some(id) = made.as_ref().ok().and_then(|v| v["curves"].get(0)).and_then(Value::as_str) {
+ let drawn_from = tool.pts.first().cloned();
tool.pts = vec![(end.0, Some(format!("{id}.end")))];
- tool.chain.placed(id);
+ if let Some(start) = drawn_from {
+ tool.chain.placed(id, start);
+ }
}
}
diff --git a/crates/ui-egui/src/sketch_moving_click_tests.rs b/crates/ui-egui/src/sketch_moving_click_tests.rs
index c36b72a388..f0bef1582b 100644
--- a/crates/ui-egui/src/sketch_moving_click_tests.rs
+++ b/crates/ui-egui/src/sketch_moving_click_tests.rs
@@ -431,9 +431,18 @@ fn undo_takes_back_one_arc_as_it_takes_back_one_segment() {
h.frames(2);
assert_eq!(curves(&h), 1, "only the arc goes");
assert_eq!(sketch(&h).curves.first().map(|c| matches!(c.kind, CurveKind::Line { .. })), Some(true));
- // As with a line undone mid-chain, the chain's point went with it: the next click is refused
- // and changes nothing, and Esc ends the chain, leaving the Line tool ready for a new one.
- click_at(&mut h, [20.0, 20.0]);
+ // The chain goes back to the line's end, still drawing tangent arcs (#10016): the next click
+ // is an arc from there. Esc ends the chain, leaving the Line tool ready for a new one.
+ click_at(&mut h, [10.0, 10.0]);
+ assert_eq!(curves(&h), 2, "{:?}", h.app.status);
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.frames(2);
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.frames(2);
+ assert_eq!(curves(&h), 0);
+ // Past the line too: lines again, from where the chain began.
+ click_at(&mut h, [25.0, 5.0]);
+ assert_eq!(h.app.tool.as_ref().map(|t| t.cmd), Some("sketch.line"), "nothing is left to be tangent to");
assert_eq!(curves(&h), 1, "{:?}", h.app.status);
h.step(&json!({"key":"Escape"})).unwrap();
h.frames(2);
diff --git a/crates/ui-egui/src/sketch_shortcut_tests.rs b/crates/ui-egui/src/sketch_shortcut_tests.rs
index 2776a6120d..41fcfa8ad7 100644
--- a/crates/ui-egui/src/sketch_shortcut_tests.rs
+++ b/crates/ui-egui/src/sketch_shortcut_tests.rs
@@ -404,3 +404,113 @@ fn the_kept_sketch_tab_sits_after_model_and_its_tools_start_a_sketch() {
h.app.start("sketch.line");
assert!(h.app.dialog.is_none() && tool(&h).is_none());
}
+
+fn line_chain(h: &mut Harness, points: &[[i32; 3]]) {
+ h.step(&json!({"start":"sketch.line"})).unwrap();
+ for point in points {
+ h.step(&json!({"click":{"world":point}})).unwrap();
+ }
+}
+
+/// Each line of the active sketch, as (start, end).
+fn lines(h: &Harness) -> Vec<([f64; 2], [f64; 2])> {
+ use solidcraft_engine::sketch::Shape;
+ let sid = h.app.session.active_sketch.unwrap();
+ let state = h.app.session.model.state();
+ let sketch = &state.sketch(sid).unwrap().sketch;
+ (0..sketch.curves.len())
+ .filter_map(|c| match sketch.shape(c)? {
+ Shape::Line { a, b } => Some(([a.x, a.y], [b.x, b.y])),
+ _ => None,
+ })
+ .collect()
+}
+
+fn assert_line(line: ([f64; 2], [f64; 2]), start: [f64; 2], end: [f64; 2]) {
+ let near = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6;
+ assert!(near(line.0, start) && near(line.1, end), "{line:?}, expected {start:?} to {end:?}");
+}
+
+/// The Undo button runs `edit.undo` and leaves the tool on: the chain goes back to the end of the
+/// last segment still there, and the next click draws from it (#10016).
+#[test]
+fn undo_in_a_line_chain_goes_back_to_the_last_segment_still_there() {
+ let mut h = Harness::new();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.settle();
+ line_chain(&mut h, &[[-20, 0, 0], [0, 0, 0], [0, 15, 0]]);
+ assert_eq!(lines(&h).len(), 2);
+
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.settle();
+ assert_eq!(lines(&h).len(), 1);
+ h.step(&json!({"click":{"world":[20, 15, 0]}})).unwrap();
+ h.settle();
+ let drawn = lines(&h);
+ assert_eq!(drawn.len(), 2, "the click drew a line: {:?}", h.app.session.log.last());
+ assert_line(drawn[0], [-20.0, 0.0], [0.0, 0.0]);
+ assert_line(drawn[1], [0.0, 0.0], [20.0, 15.0]);
+ let chain = h.app.tool.as_ref().unwrap();
+ assert_eq!(chain.cmd, "sketch.line", "the tool stays on");
+ assert_eq!(chain.pts.len(), 1, "and the chain carries on from the new end");
+
+ // Undo twice: back past the first segment, to where the chain began.
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.settle();
+ assert!(lines(&h).is_empty());
+ h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap();
+ h.settle();
+ let drawn = lines(&h);
+ assert_eq!(drawn.len(), 1, "{:?}", h.app.session.log.last());
+ assert_line(drawn[0], [-20.0, 0.0], [5.0, 5.0]);
+}
+
+/// A chain begun on a point Undo then takes away has nowhere to go back to: it ends, the tool
+/// stays on, and the next click begins a new chain with no error.
+#[test]
+fn a_chain_whose_first_point_is_undone_ends_and_the_tool_stays_on() {
+ let mut h = Harness::new();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.line", json!({"points":[[-20, -20], [-10, -20]]})).unwrap();
+ h.settle();
+ h.step(&json!({"start":"sketch.line"})).unwrap();
+ h.step(&json!({"click":{"world":[-10, -20, 0]}})).unwrap();
+ assert!(h.app.tool.as_ref().unwrap().pts[0].1.is_some(), "the click snapped to the line's end");
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.settle();
+ assert!(lines(&h).is_empty());
+ h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap();
+ h.settle();
+ let chain = h.app.tool.as_ref().unwrap();
+ assert_eq!(chain.cmd, "sketch.line");
+ assert_eq!(chain.pts.len(), 1, "the click began a new chain: {:?}", h.app.session.log.last());
+ assert!(chain.pts[0].1.is_none() && (chain.pts[0].0.x - 5.0).abs() < 1e-6, "{:?}", chain.pts);
+ assert!(lines(&h).is_empty());
+ h.step(&json!({"click":{"world":[15, 5, 0]}})).unwrap();
+ h.settle();
+ assert_eq!(lines(&h).len(), 1, "{:?}", h.app.session.log.last());
+}
+
+/// Esc ends a chain for good: a new chain begun on its last point doesn't fall back into the old
+/// one when Undo takes that point away.
+#[test]
+fn undo_does_not_bring_back_a_chain_ended_with_esc() {
+ let mut h = Harness::new();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.settle();
+ line_chain(&mut h, &[[-20, 0, 0], [0, 0, 0], [0, 15, 0]]);
+ press(&mut h, "Escape");
+ assert_eq!(tool(&h), Some("sketch.line"));
+ assert!(h.app.tool.as_ref().unwrap().pts.is_empty());
+ h.step(&json!({"click":{"world":[0, 15, 0]}})).unwrap();
+ assert!(h.app.tool.as_ref().unwrap().pts[0].1.is_some(), "the new chain begins on the old one's end");
+ h.app.run("edit.undo", json!({})).unwrap();
+ h.settle();
+ h.step(&json!({"click":{"world":[5, 5, 0]}})).unwrap();
+ h.settle();
+ let chain = h.app.tool.as_ref().unwrap();
+ assert_eq!(chain.pts.len(), 1);
+ assert!(chain.pts[0].1.is_none(), "a new chain, from the click, not the old one's point: {:?}", chain.pts);
+ assert_eq!(lines(&h).len(), 1, "only the first segment is left");
+}
diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs
index 7b134c477e..56b99357f5 100644
--- a/crates/ui-egui/src/tools.rs
+++ b/crates/ui-egui/src/tools.rs
@@ -215,6 +215,10 @@ fn midpoint_at(app: &SolveApp, proj: &Proj, pos: Pos2, radius: f32) -> Option<(V
pub fn on_hover(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) {
crate::sketch3d::on_hover(app, proj, pos);
+ if let Some(mut tool) = app.tool.take() {
+ crate::sketch_line_arc::rejoin(app, &mut tool);
+ app.tool = Some(tool);
+ }
let midpoint = (shift && app.tool.as_ref().is_some_and(|t| t.kind == Kind::HorizontalVertical))
.then(|| midpoint_at(app, proj, pos, f32::INFINITY))
.flatten();
@@ -501,6 +505,7 @@ fn dimension_picks(tool: &Tool, id: String, midpoint: Option<&str>, midpoint_tar
/// the typed values as dimension constraints).
pub fn place(app: &mut SolveApp, p: (Vec2, Option<String>)) {
let Some(mut tool) = app.tool.take() else { return };
+ crate::sketch_line_arc::rejoin(app, &mut tool);
if tool.kind == Kind::Ext {
crate::sketch_tools::place_point(app, &mut tool, p);
// A placed text opens Edit Text, which ends the tool.
@@ -551,7 +556,7 @@ pub fn place(app: &mut SolveApp, p: (Vec2, Option<String>)) {
tool.pts[last].1 = Some(format!("{id}.end"));
// The chain needs only its current endpoint, even after many segments.
tool.pts = vec![tool.pts[last].clone()];
- tool.chain.placed(id);
+ tool.chain.placed(id, a.clone());
}
crate::sketch_dims::reset(&mut tool);
} else {