Skip to content

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

Fix feature editor picks after upstream sketch resizing (#4083) (#4084)

raykholo14ccc7129154
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index f177a6b1f6..e72d3cf9e6 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -4024,6 +4024,41 @@ pub(crate) fn face_sel(s: &Session, p0: Vec3) -> Option<Sel> {
(d <= tolerance).then(|| Sel::Face { body: body.name.clone(), index, point })
}
+/// Resolve a stored name on the current input body before placing its pick in the viewport.
+/// Keep the resolved index: a face point can lie on a boundary shared with another face.
+fn named_refill(s: &Session, body: Option<&str>, name: &str, point: Vec3, edge: bool) -> Option<Sel> {
+ use solidcraft_engine::doc::naming;
+
+ if name.is_empty() {
+ return None;
+ }
+ let st = s.model.state();
+ let (source, index, point) = if edge {
+ let (b, hit) = st
+ .bodies
+ .iter()
+ .filter(|b| body.is_none_or(|name| b.name == name))
+ .filter_map(|b| naming::locate_edge(b, name, point).ok().map(|hit| (b, hit)))
+ .min_by(|(_, a), (_, b)| a.point.dist(point).total_cmp(&b.point.dist(point)))?;
+ (b, hit.edge, hit.point)
+ } else if let Some((b, (index, point, _))) = body.and_then(|n| st.body(n)).and_then(|b| naming::face_on_body(b, name, point).map(|hit| (b, hit)))
+ {
+ (b, index, point)
+ } else {
+ let hit = naming::locate_face(&st, name, point)?;
+ (st.bodies.get(hit.body)?, hit.face, hit.point)
+ };
+ let (body, point) = s
+ .doc
+ .body_frames_with_source(&st)
+ .into_iter()
+ .find(|(_, definition, _, _, _)| definition == source.definition_name())
+ .map(|(world, _, _, _, frame)| (world, solidcraft_engine::doc::apply_point(&frame, point)))
+ .unwrap_or_else(|| (source.name.clone(), point));
+ s.world_state().body(&body)?;
+ Some(if edge { Sel::Edge { body, index, point } } else { Sel::Face { body, index, point } })
+}
+
fn emboss_face_sel(s: &Session, component: u64, face: &solidcraft_engine::doc::EmbossFace, persistent: &str) -> Option<Sel> {
let source = s.model.state();
let body = source.body(&face.body)?;
@@ -4278,7 +4313,9 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
}
d
}
- FeatureKind::Fillet { edges, radius, .. } | FeatureKind::Chamfer { edges, distance: radius, .. } => {
+ FeatureKind::Fillet { edges, radius, body, .. } | FeatureKind::Chamfer { edges, distance: radius, body, .. } => {
+ let edge_pick =
+ |i: usize, p: Vec3| f.edge_names.get(i).and_then(|name| named_refill(s, body.as_deref(), name, p, true)).or_else(|| edge_sel(s, p));
let chamfer = matches!(f.kind, FeatureKind::Chamfer { .. });
let mut d = start(if chamfer { "solid.chamfer" } else { "solid.fillet" })?;
d.kind = fillet_kind(radius, chamfer);
@@ -4339,7 +4376,7 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
FilletStyle::Constant | FilletStyle::Chord | FilletStyle::Curvature => {}
}
// The start was the first edge's last point: ends swapped.
- let first_end = edges.first().and_then(|p| edge_sel(s, p.0)).and_then(|x| match x {
+ let first_end = edges.first().and_then(|p| edge_pick(0, p.0)).and_then(|x| match x {
Sel::Edge { body, index, .. } => {
s.world_state().body(&body).and_then(|b| b.mesh().edges.get(index).and_then(|e| e.last().copied()))
}
@@ -4351,7 +4388,7 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
}
}
if let Some(inp) = d.inputs.first_mut() {
- inp.items = edges.iter().filter_map(|p| edge_sel(s, p.0)).collect();
+ inp.items = edges.iter().enumerate().filter_map(|(i, p)| edge_pick(i, p.0)).collect();
d.picked_edges = inp.items.clone();
}
d.chain_applied = false;
@@ -4365,7 +4402,13 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
d.extra.insert("direction".into(), json!(direction));
}
if let Some(inp) = d.inputs.first_mut() {
- inp.items = faces.iter().filter_map(|p| face_sel(s, p.0)).collect();
+ inp.items = faces
+ .iter()
+ .enumerate()
+ .filter_map(|(i, p)| {
+ f.face_names.get(i).and_then(|name| named_refill(s, f.kind.face_body(), name, p.0, false)).or_else(|| face_sel(s, p.0))
+ })
+ .collect();
}
d
}
@@ -4373,7 +4416,13 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
let mut d = start("solid.draft")?;
d.kind = Kind::simple_restore(&f.kind)?;
if let Some(inp) = d.inputs.get_mut(0) {
- inp.items = faces.iter().filter_map(|p| face_sel(s, p.0)).collect();
+ inp.items = faces
+ .iter()
+ .enumerate()
+ .filter_map(|(i, p)| {
+ f.face_names.get(i).and_then(|name| named_refill(s, f.kind.face_body(), name, p.0, false)).or_else(|| face_sel(s, p.0))
+ })
+ .collect();
}
if let Some(inp) = d.inputs.get_mut(1) {
inp.items = plane_sel(s, neutral).into_iter().collect();
diff --git a/crates/ui-egui/src/dialogs_all_tests.rs b/crates/ui-egui/src/dialogs_all_tests.rs
index 4a50f8b7d9..253bf8a17b 100644
--- a/crates/ui-egui/src/dialogs_all_tests.rs
+++ b/crates/ui-egui/src/dialogs_all_tests.rs
@@ -337,3 +337,71 @@ fn remaining_dialogs_keep_kit_chrome_and_cancel_restores_preview() {
assert_eq!(h.app.session.pending_moves, moves, "{command}: cancel restores display-only moves");
}
}
+
+fn resized_box_editor(command: &str, params: Value) -> (Harness, u64) {
+ let mut h = Harness::new();
+ for (id, params) in [
+ ("sketch.create", json!({"plane":"XY"})),
+ ("sketch.rectangle.two_point", json!({"p0":[0,0],"p1":[40,20]})),
+ ("sketch.dimension", json!({"entities":["l1"],"value":40})),
+ ("sketch.dimension", json!({"entities":["l2"],"value":20})),
+ ("sketch.finish", json!({})),
+ ("solid.extrude", json!({"distance":20})),
+ (command, params),
+ ("parameters.change", json!({"name":"d2","expression":"30"})),
+ ] {
+ h.app.run(id, params).unwrap();
+ }
+ let id = h.app.session.doc.features.last().unwrap().id;
+ assert!(h.app.session.model.results.iter().all(|r| r.error.is_none()));
+ h.app.edit_feature(id);
+ (h, id)
+}
+
+#[test]
+fn blend_editor_refills_named_edges_after_sketch_resize() {
+ for command in ["solid.fillet", "solid.chamfer"] {
+ let (mut h, id) = resized_box_editor(command, json!({"edges":[[40,10,20],[20,20,20],[20,0,20],[0,10,20]],"radius":4,"distance":4}));
+ let d = h.app.dialog.as_ref().unwrap();
+ let picks = &d.inputs[0].items;
+ assert_eq!(picks.len(), 4);
+ assert_eq!(&d.picked_edges, picks);
+ let s = &h.app.session;
+ let body = s.model.state().bodies.first().unwrap().clone();
+ let names = &s.doc.feature(id).unwrap().edge_names;
+ for (pick, name) in picks.iter().zip(names) {
+ let Sel::Edge { point, index, .. } = pick else { panic!("{pick:?}") };
+ let hit = solidcraft_engine::doc::naming::locate_edge(&body, name, *point).unwrap();
+ assert_eq!(*index, hit.edge);
+ assert!(hit.on_point, "{command}: {point:?} must lie on the resized edge");
+ }
+ let commands = apply_commands(&h.app, d).unwrap();
+ crate::dialogs::roll_marker(&mut h.app, None).unwrap();
+ for (command, params) in commands {
+ h.app.run(&command, params).unwrap();
+ }
+ assert_eq!(h.app.session.doc.features.last().unwrap().id, id);
+ assert!(h.app.session.model.results.iter().all(|r| r.error.is_none()));
+ }
+}
+
+#[test]
+fn face_editor_refills_named_faces_after_sketch_resize() {
+ for (command, params) in
+ [("solid.shell", json!({"faces":[[20,20,10]],"thickness":2})), ("solid.draft", json!({"faces":[[20,20,10]],"neutral":"XY","angle":3}))]
+ {
+ let (mut h, id) = resized_box_editor(command, params);
+ let d = h.app.dialog.as_ref().unwrap();
+ let picks = &d.inputs[0].items;
+ assert_eq!(picks.len(), 1, "{command}: the moved face must refill");
+ let Sel::Face { point, .. } = picks.first().unwrap() else { panic!("{picks:?}") };
+ assert!((point.y - 30.0).abs() < 1e-6, "{command}: {point:?}");
+ let commands = apply_commands(&h.app, d).unwrap();
+ crate::dialogs::roll_marker(&mut h.app, None).unwrap();
+ for (command, params) in commands {
+ h.app.run(&command, params).unwrap();
+ }
+ assert_eq!(h.app.session.doc.features.last().unwrap().id, id);
+ assert!(h.app.session.model.results.iter().all(|r| r.error.is_none()));
+ }
+}