Skip to content

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

Renaming a plane keeps its projections, canvases and hidden state (#4353) (#4354)

SS-EN5d92ad1e2316
diff --git a/crates/doc/src/clipboard.rs b/crates/doc/src/clipboard.rs
index ee2d2b616a..44ce0a109d 100644
--- a/crates/doc/src/clipboard.rs
+++ b/crates/doc/src/clipboard.rs
@@ -139,7 +139,7 @@ fn remap_plane(
Ok(())
}
-fn remap_link(
+pub(crate) fn remap_link(
source: &mut solidcraft_sketch::LinkSource,
ids: &BTreeMap<u64, u64>,
names: &BTreeMap<String, String>,
diff --git a/crates/doc/src/feature_refs.rs b/crates/doc/src/feature_refs.rs
index 8d57e518de..48ed05b2b0 100644
--- a/crates/doc/src/feature_refs.rs
+++ b/crates/doc/src/feature_refs.rs
@@ -69,3 +69,29 @@ impl FeatureKind {
});
}
}
+
+impl crate::Document {
+ /// Rename feature `old` to `new` where the design names it outside the features' own
+ /// references (`remap_refs`): sketch projections of a construction plane and the canvases
+ /// placed on it (#4353). (The features are the rename command's to change.)
+ pub fn rename_feature_keys(&mut self, old: &str, new: &str) {
+ let names = BTreeMap::from([(old.to_string(), new.to_string())]);
+ let (ids, bodies) = (BTreeMap::new(), BTreeMap::new());
+ for f in &mut self.features {
+ if let FeatureKind::Sketch { sketch, .. } = &mut f.kind {
+ for link in &mut sketch.links {
+ crate::clipboard::remap_link(&mut link.source, &ids, &names, &bodies, 0);
+ }
+ }
+ }
+ for c in &mut self.canvases {
+ c.plane.walk_refs_mut(&mut |r| {
+ if let Ref::Feature(name) = r
+ && name.as_ref() == old
+ {
+ *name = FeatureName(new.to_string());
+ }
+ });
+ }
+ }
+}
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index 713ae2c395..8205650fd6 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -207,6 +207,15 @@ fn rename(s: &mut Session, p: &Value) -> Result<Value> {
for f in doc.features.iter_mut() {
f.kind.remap_refs(&sketches, &names);
}
+ doc.rename_feature_keys(&old, name);
+ // The hidden list follows in `Session::refresh_from` (undo and redo too).
+ for sel in &mut s.selection {
+ if let Sel::Plane { name: n } = sel
+ && *n == old
+ {
+ *n = name.to_string();
+ }
+ }
Ok(json!({"feature": id, "name": name}))
}
diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs
index d9c9734759..6a481a0fe3 100644
--- a/crates/engine/src/cmd/edit_tests.rs
+++ b/crates/engine/src/cmd/edit_tests.rs
@@ -865,3 +865,58 @@ fn renaming_a_feature_evaluates_nothing() {
fresh.refresh();
assert_eq!(failed(&s), failed(&fresh));
}
+
+/// #4353: a construction plane is also named outside the features: by a sketch's projection of
+/// it, a canvas placed on it and the hidden list. Renaming it renames it there too, and undo
+/// takes the old name back.
+#[test]
+fn renaming_a_plane_keeps_its_projections_canvases_and_hidden_state() {
+ let mut s = Session::default();
+ run(&mut s, "construct.plane.offset", json!({"base": "XY", "offset": 20, "name": "Shelf"}));
+ run(&mut s, "construct.plane.offset", json!({"base": "YZ", "offset": -30, "name": "Guide"}));
+ run(&mut s, "browser.visibility", json!({"items": ["Guide"], "visible": false}));
+ let mut png = b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec();
+ png.extend([0, 0, 0, 4, 0, 0, 0, 2, 8, 6, 0, 0, 0, 0, 0, 0, 0]);
+ run(&mut s, "canvas.insert", json!({"data": solidcraft_doc::canvas::base64_encode(&png), "plane": "Shelf", "width": 40}));
+ run(&mut s, "sketch.create", json!({"plane": "XZ", "name": "Profile"}));
+ run(&mut s, "sketch.project", json!({"refs": [{"plane": "Shelf"}]}));
+ run(&mut s, "sketch.finish", json!({}));
+ let plane_link = |s: &Session| match &s.doc.find_feature("Profile").unwrap().kind {
+ solidcraft_doc::FeatureKind::Sketch { sketch, .. } => match &sketch.links[0].source {
+ solidcraft_sketch::LinkSource::Plane { name } => name.clone(),
+ other => panic!("{other:?}"),
+ },
+ other => panic!("{other:?}"),
+ };
+ let canvas_plane = |s: &Session| s.doc.canvases[0].plane.clone();
+ let projected_z = |s: &mut Session| {
+ let r = run(s, "sketch.inspect", json!({"sketch": "Profile"}));
+ r["points"].as_array().unwrap().iter().find(|p| p["link"] == "j1").unwrap()["world"][2].as_f64().unwrap()
+ };
+ let warnings = |s: &Session| s.model.results.iter().filter_map(|r| r.warning.clone()).collect::<Vec<_>>();
+ run(&mut s, "select.set", json!({"items": [{"type": "plane", "name": "Shelf"}]}));
+ run(&mut s, "timeline.rename", json!({"feature": "Shelf", "name": "Ledge"}));
+ run(&mut s, "timeline.rename", json!({"feature": "Guide", "name": "Rail"}));
+ assert_eq!(plane_link(&s), "Ledge");
+ assert_eq!(canvas_plane(&s), solidcraft_doc::PlaneRef::Construction { name: "Ledge".into() });
+ assert_eq!(s.visibility.hidden_origin, ["Rail"]);
+ assert_eq!(s.selection, [Sel::Plane { name: "Ledge".into() }]);
+ assert!(warnings(&s).is_empty(), "{:?}", warnings(&s));
+ // The projection follows the renamed plane.
+ run(&mut s, "timeline.edit", json!({"feature": "Ledge", "set": {"plane.distance": "35"}}));
+ assert!((projected_z(&mut s) - 35.0).abs() < 1e-9);
+ assert!(warnings(&s).is_empty(), "{:?}", warnings(&s));
+ // Undo takes the old name back everywhere in the design, and the sketch finds it again.
+ for _ in 0..3 {
+ run(&mut s, "edit.undo", json!({}));
+ }
+ assert_eq!(plane_link(&s), "Shelf");
+ assert_eq!(canvas_plane(&s), solidcraft_doc::PlaneRef::Construction { name: "Shelf".into() });
+ assert_eq!(s.visibility.hidden_origin, ["Guide"], "the hidden plane stays hidden");
+ assert!(warnings(&s).is_empty(), "{:?}", warnings(&s));
+ assert!((projected_z(&mut s) - 20.0).abs() < 1e-9);
+ run(&mut s, "edit.redo", json!({}));
+ run(&mut s, "edit.redo", json!({}));
+ assert_eq!(s.visibility.hidden_origin, ["Rail"]);
+ assert_eq!(plane_link(&s), "Ledge");
+}
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index baf93456ca..da2d3f4b93 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -671,6 +671,7 @@ impl Session {
/// a body or a feature (or undid or redid such a rename) renames it in the model instead,
/// which keeps every result and mesh (#3044).
fn refresh_from(&mut self, previous: &Arc<Document>) {
+ self.follow_plane_renames(previous);
if let Some((old, new)) = self.body_rename(previous)
&& self.model.rename_body(&self.doc, &old, &new)
{
@@ -690,6 +691,27 @@ impl Session {
self.refresh();
}
+ /// A hidden construction plane stays hidden when the design renames it, or undo or redo
+ /// gives it another name (#4353): the hidden list keeps planes by name.
+ fn follow_plane_renames(&mut self, previous: &Document) {
+ if self.visibility.hidden_origin.is_empty() {
+ return;
+ }
+ let before: std::collections::HashMap<u64, &str> = previous
+ .features
+ .iter()
+ .filter(|f| matches!(f.kind, solidcraft_doc::FeatureKind::ConstructionPlane { .. }))
+ .map(|f| (f.id, f.name.as_str()))
+ .collect();
+ for f in &self.doc.features {
+ if let Some(old) = before.get(&f.id).filter(|old| **old != f.name) {
+ for n in self.visibility.hidden_origin.iter_mut().filter(|n| *n == old) {
+ n.clone_from(&f.name);
+ }
+ }
+ }
+ }
+
/// The body rename that turned `previous` into the design (old name, new name), if that is
/// the whole change: one this session made (either way round), else one the designs show.
fn body_rename(&mut self, previous: &Arc<Document>) -> Option<(String, String)> {