diff --git a/crates/doc/src/feature_refs.rs b/crates/doc/src/feature_refs.rs index 9c3045f72..8d57e518d 100644 --- a/crates/doc/src/feature_refs.rs +++ b/crates/doc/src/feature_refs.rs @@ -28,6 +28,18 @@ impl FeatureKind { v } + /// Whether the feature names the feature `name` (the construction plane a plane is built on, + /// a patterned or mirrored feature). + pub fn names_feature(&self, name: &str) -> bool { + let mut found = false; + self.walk_refs(&mut |r| { + if let Ref::Feature(n) = r { + found |= n.as_ref() == name; + } + }); + found + } + /// Point references to sketches (by id) and features (by name) at their copies, or at a /// feature's new name (`timeline.rename`). pub fn remap_refs(&mut self, sketches: &BTreeMap, names: &BTreeMap) { diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 04b39f5ef..59b229c28 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1253,6 +1253,11 @@ fn redefine(s: &mut Session, p: &Value) -> Result { if std::mem::discriminant(&new.kind) != std::mem::discriminant(&old.kind) && !(boundary(&new.kind) && boundary(&old.kind)) { return Err(bad(cmd, format!("{} is not a {} feature", old.name, cid))); } + // The trial still holds the old feature, so a plane offset from its own name was made there, + // and once swapped in it would be built on itself (#3567). + if new.kind.names_feature(&old.name) { + return Err(bad(cmd, format!("{} can't be built on itself: pick another plane or face", old.name))); + } // Optional inputs can move other inputs' positions (e.g. Distance 2 goes before Taper). // Keep each existing model parameter attached to its semantic input, rather than its // old index. Constructor-assigned names for new inputs cannot reuse the old dimensions. diff --git a/crates/engine/src/cmd/offset_plane_face_tests.rs b/crates/engine/src/cmd/offset_plane_face_tests.rs index 7985ce64b..16687bf10 100644 --- a/crates/engine/src/cmd/offset_plane_face_tests.rs +++ b/crates/engine/src/cmd/offset_plane_face_tests.rs @@ -80,3 +80,22 @@ fn offset_plane_rejects_bad_face_picks_without_panicking() { assert_eq!(s.doc.features.len(), features, "{base}"); } } + +/// #3567: an Offset Plane redefined with itself as its base was accepted while the old plane +/// stood beside the new one, and then failed with "plane offsets nested too deeply". It is +/// refused with a plain message, and the plane is left as it was. +#[test] +fn offset_plane_redefined_on_itself_is_refused() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20, "corner": [10, 0, 0]})); + run(&mut s, "construct.plane.offset", json!({"base": {"face": [30, 0, 10]}, "offset": 10, "name": "Side"})); + assert!((plane(&s, "Side").origin.y + 10.0).abs() < 1e-9, "{:?}", plane(&s, "Side")); + let saved = s.doc.to_json(); + let redefine = |base: Value| json!({"feature": "Side", "command": "construct.plane.offset", "params": {"base": base, "offset": "7"}}); + let e = s.execute("timeline.redefine", &redefine(json!("Side"))).unwrap_err().to_string(); + assert!(e.contains("can't be built on itself"), "{e}"); + assert_eq!(s.doc.to_json(), saved); + // The same edit on the face it was built on works. + run(&mut s, "timeline.redefine", redefine(json!({"face": [30, 0, 10]}))); + assert!((plane(&s, "Side").origin.y + 7.0).abs() < 1e-9, "{:?}", plane(&s, "Side")); +} diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a2dd5f67f..bbbbeea5f 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -4114,7 +4114,9 @@ pub(crate) fn plane_sel(s: &Session, pl: &PlaneRef) -> Option { match pl { PlaneRef::Origin { name } => Some(Sel::Plane { name: name.clone() }), PlaneRef::Construction { name } => Some(Sel::Plane { name: name.to_string() }), - PlaneRef::Custom { plane } | PlaneRef::Face { plane, .. } => face_sel(s, plane.origin), + PlaneRef::Custom { plane } => face_sel(s, plane.origin), + // The point picked on the face: the plane's origin can lie off the face (#3567). + PlaneRef::Face { at, .. } => face_sel(s, *at), PlaneRef::Flipped { base, .. } => plane_sel(s, base), _ => None, } diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index 9d62a25af..0a8893773 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -85,6 +85,27 @@ fn feature_mirror_and_patterns_reopen_as_feature_replays_even_for_body_creators( } } +/// #3567: an Offset Plane built on a face reopened with its Plane input empty when the face's +/// plane origin lay off the face (here the world origin's foot, beside the box), so OK asked to +/// pick a plane first. It reopens with the face, and OK keeps the plane on it. +#[test] +fn offset_plane_on_a_face_reopens_with_the_face() { + let mut app = app_with(json!([ + {"command":"solid.box","params":{"length":40,"width":30,"height":20,"corner":[10,0,0],"body_name":"Plate"}}, + {"command":"construct.plane.offset","params":{"base":{"face":[30,0,10]},"offset":10,"name":"Side"}} + ])); + let side = app.session.doc.find_feature("Side").unwrap().clone(); + let d = crate::dialogs::for_feature(&app, side.id, None).unwrap(); + assert!(matches!(d.inputs[0].items.as_slice(), [Sel::Face { body, .. }] if body == "Plate"), "{:?}", d.inputs[0].items); + let (edit, p) = apply_commands(&app, &d).unwrap().remove(0); + assert_eq!(edit, "timeline.redefine"); + app.session.execute(&edit, &p).unwrap(); + assert!(app.session.model.results.iter().all(|r| r.error.is_none()), "{:?}", app.session.model.results); + let state = app.session.model.state(); + let plane = state.construct.iter().find(|c| c.name == "Side").map(|c| c.geom.clone()); + assert!(matches!(plane, Some(solidcraft_engine::doc::construct::ConstructGeom::Plane(pl)) if (pl.origin.y + 10.0).abs() < 1e-9), "{plane:?}"); +} + #[test] fn feature_pattern_controls_preview_without_committing_and_cancel_cleanly() { for command in ["solid.pattern.rectangular", "solid.pattern.circular"] {