SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Timeline folders: editing a feature in a folder leaves no stale id, and such designs open again (#9373) (#9393)
austin62jake5ad250350368
diff --git a/crates/doc/src/format.rs b/crates/doc/src/format.rsindex 46cc22376c..da9560f8dd 100644--- a/crates/doc/src/format.rs+++ b/crates/doc/src/format.rs@@ -724,6 +724,13 @@ pub fn read(s: &str) -> Result<Document> {let mut grouped_features = std::collections::BTreeSet::new();let joint_ids: std::collections::BTreeSet<_> = d.assembly.joints.iter().map(|joint| joint.id).collect();let mut grouped_joints = std::collections::BTreeSet::new();+ // A folder entry naming a feature or joint the design doesn't have, or one a folder already+ // holds, is dropped, and the design opens: folders only group the timeline, so nothing else+ // is lost. Editing a feature inside a folder used to leave such entries (#9373).+ for group in &mut d.timeline_groups {+ group.joints.retain(|joint| joint_ids.contains(joint) && grouped_joints.insert(*joint));+ group.features.retain(|feature| feature_ids.contains(feature) && grouped_features.insert(*feature));+ }for group in &d.timeline_groups {if group.name.trim().is_empty() || group.name.len() > 128 || !group_ids.insert(group.id) {return Err(DocError::Invalid("damaged design: invalid timeline folder".into()));@@ -731,12 +738,6 @@ pub fn read(s: &str) -> Result<Document> {if group.parent.is_some_and(|parent| parent == group.id) {return Err(DocError::Invalid("damaged design: timeline folder cannot contain itself".into()));}- if group.joints.iter().any(|joint| !joint_ids.contains(joint) || !grouped_joints.insert(*joint)) {- return Err(DocError::Invalid("damaged design: invalid joint in a timeline folder".into()));- }- if group.features.iter().any(|feature| !feature_ids.contains(feature) || !grouped_features.insert(*feature)) {- return Err(DocError::Invalid("damaged design: invalid feature in a timeline folder".into()));- }}for group in &d.timeline_groups {if let Some(parent) = group.parent {diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rsindex 3141559b67..3a7b2594ce 100644--- a/crates/doc/src/tests.rs+++ b/crates/doc/src/tests.rs@@ -596,6 +596,33 @@ fn feature_inserted_at_a_marker_inside_a_timeline_folder_joins_it() {assert!(Document::from_json(&doc.to_json()).is_ok(), "the inserted feature must leave a reopenable design");}+/// #9373: a timeline folder that names a feature or joint the design doesn't have, or a feature+/// a folder already lists, no longer stops the design opening. Those entries are dropped and+/// everything else is kept. Editing a feature inside a folder used to save such entries.+#[test]+fn a_folder_naming_missing_or_repeated_features_still_opens() {+ let mut doc = Document::new("stale folder entries");+ let sketch = || FeatureKind::Sketch { plane: PlaneRef::Origin { name: "XY".into() }, sketch: Sketch::new() };+ let a = doc.add_feature(sketch(), Some("A")).unwrap();+ let b = doc.add_feature(sketch(), Some("B")).unwrap();+ let c = doc.add_feature(sketch(), Some("C")).unwrap();+ let missing = doc.next_id + 10;+ doc.timeline_groups.push(TimelineGroup { id: 100, name: "Repeats".into(), parent: None, features: vec![a, b, b], joints: vec![] });+ doc.timeline_groups.push(TimelineGroup {+ id: 101,+ name: "Stale".into(),+ parent: None,+ features: vec![b, c, missing, missing, missing],+ joints: vec![missing, missing],+ });+ let opened = Document::from_json(&doc.to_json()).expect("the design opens");+ assert_eq!(opened.features.iter().map(|f| f.id).collect::<Vec<_>>(), vec![a, b, c]);+ assert_eq!(opened.timeline_groups[0].features, vec![a, b], "listed once");+ assert_eq!(opened.timeline_groups[1].features, vec![c], "no missing feature, nor one the first folder holds");+ assert!(opened.timeline_groups[1].joints.is_empty());+ assert_eq!(opened.timeline_groups.iter().map(|g| g.name.as_str()).collect::<Vec<_>>(), ["Repeats", "Stale"], "both folders are kept");+}+/// A marker after the last feature leaves every feature active, as no marker does, so a feature/// added then is active too, not added rolled back (#7741: editing the last feature from the/// History runs its command with the marker there).diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rsindex fce8f62cb5..b2e38945be 100644--- a/crates/engine/src/cmd/edit.rs+++ b/crates/engine/src/cmd/edit.rs@@ -1794,6 +1794,11 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {}).collect();trial.doc_mut().features.retain(|f| f.id != made);+ // Made right after the edited feature, the trial joined the timeline folder holding both:+ // it leaves it too, or its id stays there, and is given out again below (#9373).+ for group in &mut trial.doc_mut().timeline_groups {+ group.features.retain(|f| *f != made);+ }let added = trial.doc.features.len().saturating_sub(s.doc.features.len());trial.doc_mut().marker = s.doc.marker.map(|marker| if marker > idx { marker.saturating_add(added) } else { marker });if made == s.doc.next_id && trial.doc.next_id == made.saturating_add(1) {diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rsindex 45c4db6d06..2a039e0209 100644--- a/crates/engine/src/cmd/edit_tests.rs+++ b/crates/engine/src/cmd/edit_tests.rs@@ -643,6 +643,29 @@ fn timeline_folders_are_command_backed_saved_undoable_and_follow_feature_ids() {assert!(s.doc.timeline_groups.is_empty());}+/// #9373: editing a feature inside a timeline folder left the id of the edit's trial feature in+/// the folder and gave that id out again, so every edit there added it once more, and a feature+/// made later took it and was listed twice: the saved design then would not open. Edited, the+/// folder keeps its own features, once each, and the design opens again.+#[test]+fn editing_a_feature_inside_a_folder_leaves_the_folder_as_it_was() {+ let mut s = Session::default();+ for (name, height) in [("Base", 10), ("Post", 20), ("Pin", 30)] {+ run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": height, "operation": "new", "name": name}));+ }+ let ids: Vec<u64> = s.doc.features.iter().map(|f| f.id).collect();+ run(&mut s, "timeline.group", json!({"name": "Parts", "features": ids}));+ for width in [5, 6, 7] {+ run(&mut s, "timeline.redefine", json!({"feature": "Post", "command": "solid.box", "params": {"length": 4, "width": width, "height": 20}}));+ }+ assert_eq!(s.doc.timeline_groups[0].features, ids, "no trial feature is left in the folder");+ let made =+ run(&mut s, "solid.box", json!({"length": 2, "width": 2, "height": 2, "operation": "new", "name": "Later"}))["feature"].as_u64().unwrap();+ assert!(!s.doc.timeline_groups[0].features.contains(&made), "a feature made after the folder is not in it");+ let reopened = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap();+ assert_eq!(reopened.timeline_groups, s.doc.timeline_groups);+}+#[test]fn timeline_reorders_selected_features_as_one_block_and_removes_folders_in_one_step() {let mut s = Session::default();@@ -1559,11 +1582,13 @@ fn timeline_joint_folders_are_mixed_saved_and_undoable() {let saved = s.doc.to_json();let reopened = solidcraft_doc::Document::from_json(&saved).unwrap();assert_eq!(reopened.to_json(), saved);+ // A folder naming a joint the design doesn't have, or one twice, opens without that entry+ // (#9373).let mut damaged: Value = serde_json::from_str(&saved).unwrap();damaged["timeline_groups"][0]["joints"] = json!([u64::MAX]);- assert!(solidcraft_doc::Document::from_json(&damaged.to_string()).is_err());+ assert!(solidcraft_doc::Document::from_json(&damaged.to_string()).unwrap().timeline_groups[0].joints.is_empty());damaged["timeline_groups"][0]["joints"] = json!([joint, joint]);- assert!(solidcraft_doc::Document::from_json(&damaged.to_string()).is_err());+ assert_eq!(solidcraft_doc::Document::from_json(&damaged.to_string()).unwrap().timeline_groups[0].joints, [joint]);let destination = run(&mut s, "timeline.group", json!({"name":"Destination"}))["group"].as_u64().unwrap();run(&mut s, "timeline.move_to_group", json!({"joints":[joint], "group":destination}));assert!(s.doc.timeline_groups[0].joints.is_empty());