Skip to content

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

Sketch: Delete on a picked profile deletes the curves around it (#7107) (#7120)

yhamil0400e31c24fd5d
diff --git a/crates/engine/src/cmd/delete.rs b/crates/engine/src/cmd/delete.rs
index d7479ae804..291d696202 100644
--- a/crates/engine/src/cmd/delete.rs
+++ b/crates/engine/src/cmd/delete.rs
@@ -114,6 +114,28 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
None => faces.push((body.clone(), vec![*point])),
},
Sel::Edge { .. } | Sel::Vertex { .. } => skip("edges and vertices are deleted with their body or face".into()),
+ // In the sketch being edited, a profile stands for the curves around it (#7107);
+ // its holes are shapes of their own and stay. A letter's outline stands for its
+ // whole text, so no counters are left behind.
+ Sel::Profile { sketch, index } if s.active_sketch == Some(*sketch) => {
+ let st = s.model.state();
+ let (Some(ss), Ok(sk)) = (st.sketch(*sketch), s.doc.sketch(*sketch)) else {
+ skip("nothing around that profile can be deleted".into());
+ continue;
+ };
+ let outer = ss.profiles.get(*index).map(|pr| solidcraft_sketch::distinct_curves(&pr.outer_curves)).unwrap_or_default();
+ let before = entities.len();
+ for c in outer.iter().filter_map(|id| sk.curves.iter().find(|c| &c.id == id)) {
+ let text = c.link.as_ref().filter(|l| sk.link(l).is_some_and(|l| l.kind == solidcraft_sketch::LinkKind::Text));
+ let id = text.unwrap_or(&c.id);
+ if !entities.contains(id) {
+ entities.push(id.clone());
+ }
+ }
+ if entities.len() == before {
+ skip("nothing around that profile can be deleted".into());
+ }
+ }
Sel::Profile { .. } => skip("a profile goes with its sketch".into()),
Sel::ExternalSketchCurve { .. } | Sel::ExternalSketchPoint { .. } => skip("edit the source sketch to delete its geometry".into()),
}
@@ -494,4 +516,55 @@ mod tests {
run(&mut s, "selection.delete", json!({"items": [{"type": "sketch_curve", "id": id}]}));
assert!(s.model.state().sketch(sk).unwrap().sketch.curves.is_empty());
}
+
+ /// A rectangle picked by clicking inside it (a profile) goes with Delete while sketching
+ /// (#7107): its four lines go, the circle inside it (its hole) stays, and undo brings them
+ /// back. Outside the sketch a profile still goes only with its sketch.
+ #[test]
+ fn a_profile_deletes_the_curves_around_it_while_sketching() {
+ let mut s = Session::default();
+ let sk = run(&mut s, "sketch.create", json!({"plane": "XY"}))["sketch"].as_u64().unwrap();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 20]}));
+ run(&mut s, "sketch.circle.center", json!({"center": [10, 10], "radius": 3, "id": "hole"}));
+ let index = {
+ let st = s.model.state();
+ solidcraft_sketch::profile_at_point(&st.sketch(sk).unwrap().profiles, solidcraft_geom::Vec2::new(30.0, 10.0)).unwrap()
+ };
+ let before = curves(&s, sk);
+ assert_eq!(before.len(), 5, "four lines and a circle: {before:?}");
+ let item = json!({"items": [{"type": "profile", "sketch": sk, "index": index}]});
+ run(&mut s, "selection.delete", item.clone());
+ assert_eq!(curves(&s, sk), vec!["hole".to_string()], "only the rectangle goes");
+ run(&mut s, "edit.undo", json!({}));
+ assert_eq!(curves(&s, sk), before);
+ run(&mut s, "sketch.finish", json!({}));
+ let err = s.execute("selection.delete", &item).unwrap_err().to_string();
+ assert!(err.contains("a profile goes with its sketch"), "{err}");
+ assert_eq!(curves(&s, sk), before);
+ }
+
+ /// A picked letter goes with its whole text, counters too (an O's inside is a profile of
+ /// its own), and the rectangle beside it stays.
+ #[test]
+ fn a_letter_profile_deletes_its_whole_text_while_sketching() {
+ let mut s = Session::default();
+ let sk = run(&mut s, "sketch.create", json!({"plane": "XY"}))["sketch"].as_u64().unwrap();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 20]}));
+ run(&mut s, "sketch.text", json!({"text": "OB", "at": [60, 0], "height": 10}));
+ let (index, rect) = {
+ let st = s.model.state();
+ let ss = st.sketch(sk).unwrap();
+ let text = |id: &String| ss.sketch.curves.iter().any(|c| &c.id == id && c.link.is_some());
+ let index = ss.profiles.iter().position(|p| p.outer_curves.iter().all(text)).unwrap();
+ (index, ss.sketch.curves.iter().filter(|c| c.link.is_none()).map(|c| c.id.clone()).collect::<Vec<_>>())
+ };
+ assert_eq!(rect.len(), 4, "{rect:?}");
+ assert!(curves(&s, sk).len() > 4, "the text has outlines");
+ run(&mut s, "selection.delete", json!({"items": [{"type": "profile", "sketch": sk, "index": index}]}));
+ assert_eq!(curves(&s, sk), rect, "the whole text goes, the rectangle stays");
+ }
+
+ fn curves(s: &Session, sk: u64) -> Vec<String> {
+ s.model.state().sketch(sk).unwrap().sketch.curves.iter().map(|c| c.id.clone()).collect()
+ }
}