Skip to content

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

Assembly: moving a component without capturing its position marks the design unsaved (#9736) (#9851)

austin62jake7c89f7445fa4
diff --git a/crates/engine/src/cmd/component_relink_tests.rs b/crates/engine/src/cmd/component_relink_tests.rs
index 672279a872..82984ee45f 100644
--- a/crates/engine/src/cmd/component_relink_tests.rs
+++ b/crates/engine/src/cmd/component_relink_tests.rs
@@ -547,6 +547,8 @@ fn relink_repairs_a_moved_source_for_every_instance_in_one_undo_step() {
assert!((volume(&mut s) - 12000.0).abs() < 1e-5);
run(&mut s, "edit.redo", json!({}));
assert_eq!(serde_json::to_value(&*s.doc).unwrap(), after);
+ // A move not captured stops a save (#9736).
+ run(&mut s, "component.capture_position", json!({}));
run(&mut s, "file.save", json!({}));
let mut reopened = Session::default();
run(&mut reopened, "doc.open", json!({"path": assembly}));
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs
index 3ef6d18cd0..4be66d8b8e 100644
--- a/crates/engine/src/cmd/file.rs
+++ b/crates/engine/src/cmd/file.rs
@@ -520,6 +520,16 @@ fn write(s: &mut Session, cmd: &str, path: &str, format: &str, compression: &str
// A path outside the agent's folders says so first, whatever its name.
solidcraft_io::vfs::check(path).map_err(|e| EngineError::Other(e.to_string()))?;
check_design_name(cmd, path)?;
+ // A move not captured yet isn't in the document a file holds: refuse, rather than write the
+ // file without it and say nothing (#9736). The app asks the Capture Position question first.
+ // An exploded view shown saves as before: its placements aren't the design's.
+ if s.moved_uncaptured() {
+ let moved: Vec<&str> = s.doc.occurrences.iter().filter(|o| s.pending_moves.contains_key(&o.id)).map(|o| o.name.as_str()).collect();
+ return Err(EngineError::Other(format!(
+ "{path}: {} moved and not captured yet; component.capture_position keeps the move and component.revert_position puts it back, then save",
+ moved.join(", ")
+ )));
+ }
if s.path.as_deref() == Some(path) {
if s.read_only {
return Err(EngineError::Other(format!("{path}: the design was opened read-only; save it to another file with file.save_as")));
@@ -1432,6 +1442,53 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ /// #9736: a component moved and not captured yet is a change the file doesn't hold. The
+ /// design reads as unsaved, and Save and Save As refuse, naming the component, until the move
+ /// is captured (then the file has it) or reverted (then the design is as saved).
+ #[test]
+ fn a_move_not_captured_is_unsaved_and_saving_waits_for_it() {
+ let dir = std::env::temp_dir().join(format!("solidcraft-pending-move-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("two.sc").to_string_lossy().to_string();
+ let mut s = Session::default();
+ s.execute("solid.box", &json!({"length": 40, "width": 40, "height": 10, "corner": [0, 0, 0], "body_name": "BaseB"})).unwrap();
+ s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 30, "corner": [15, 15, 10], "body_name": "ArmB"})).unwrap();
+ s.execute("component.from_bodies", &json!({"bodies": ["BaseB", "ArmB"]})).unwrap();
+ s.execute("file.save_as", &json!({"path": path})).unwrap();
+ assert!(!s.is_dirty());
+ let arm = s.doc.occurrences.iter().find(|o| o.name.starts_with("ArmB")).unwrap().name.clone();
+ let arm_x = |s: &Session| {
+ let world = s.world_state().unwrap();
+ let body = world.bodies.iter().find(|b| b.name.starts_with("ArmB")).unwrap();
+ solidcraft_kernel::measure(&body.body).unwrap().centroid.x
+ };
+ for answer in ["component.revert_position", "component.capture_position"] {
+ s.execute("occurrence.move", &json!({"occurrence": arm, "translate": [7, 0, 0], "capture": false})).unwrap();
+ assert!(s.is_dirty(), "{answer}: the move is a change to save");
+ for (cmd, p) in [("file.save", json!({})), ("file.save_as", json!({"path": dir.join("other.sc").to_string_lossy()}))] {
+ let e = s.execute(cmd, &p).unwrap_err().to_string();
+ assert!(e.contains(&arm) && e.contains("not captured") && e.contains("component.capture_position"), "{cmd}: {e}");
+ }
+ assert!(!dir.join("other.sc").exists(), "nothing written");
+ s.execute(answer, &json!({})).unwrap();
+ s.execute("file.save", &json!({})).unwrap();
+ assert!(!s.is_dirty(), "{answer}");
+ }
+ let opened = Session::new(crate::io::read_design(&std::fs::read(&path).unwrap()).unwrap());
+ assert!((arm_x(&opened) - 27.0).abs() < 1e-6, "the captured move is in the file: {}", arm_x(&opened));
+ // An exploded view shown places the components only while it is shown: no change to
+ // save, and the design saves as it is. A move on top of it is one.
+ s.execute("explode.create", &json!({"name": "Apart", "moves": [{"occurrence": arm, "translate": [0, 0, 40]}]})).unwrap();
+ s.execute("file.save", &json!({})).unwrap();
+ s.execute("explode.show", &json!({"view": "Apart"})).unwrap();
+ assert!(!s.pending_moves.is_empty() && !s.is_dirty(), "an exploded view shown");
+ s.execute("file.save", &json!({})).unwrap();
+ s.execute("occurrence.move", &json!({"occurrence": arm, "translate": [5, 0, 0], "capture": false})).unwrap();
+ assert!(s.is_dirty());
+ assert!(s.execute("file.save", &json!({})).is_err());
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
#[ignore = "manual warmed engine save and provisional-open benchmark"]
fn benchmark_warm_design_save_with_preview() {
diff --git a/crates/engine/src/cmd/motion.rs b/crates/engine/src/cmd/motion.rs
index 44cf43dc2b..6f6927634c 100644
--- a/crates/engine/src/cmd/motion.rs
+++ b/crates/engine/src/cmd/motion.rs
@@ -1214,6 +1214,12 @@ fn explode_show(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"view": v.id, "name": v.name}))
}
+/// Whether the pending moves are an exploded view shown (`explode.show`), which places the
+/// occurrences only while it is shown: not a move to capture or save (#9736).
+pub(crate) fn shows_exploded_view(s: &Session) -> bool {
+ s.doc.assembly.exploded.iter().any(|v| exploded_at(&explode_moves(s, v), 1.0) == s.pending_moves)
+}
+
/// What exploded view `v` moves: (occurrence, its placement, the translation the view adds). Moves
/// of occurrences no longer in the design, or not finite (a hostile design file), are left out;
/// a later move of one occurrence replaces an earlier one.
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 26dfa31a5f..407206957e 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -960,9 +960,16 @@ impl Session {
}
}
- /// The design has changes not in its file: in the document, or in what is hidden.
+ /// The design has changes not in its file: in the document, in what is hidden, or a
+ /// component moved and not captured yet (#9736).
pub fn is_dirty(&self) -> bool {
- self.document_differs() || self.visibility != self.saved_visibility
+ self.document_differs() || self.visibility != self.saved_visibility || self.moved_uncaptured()
+ }
+
+ /// A component was moved and its position not captured yet (#9736). The placements of an
+ /// exploded view shown are not such a move: they last only while it is shown.
+ pub fn moved_uncaptured(&self) -> bool {
+ !self.pending_moves.is_empty() && !cmd::motion::shows_exploded_view(self)
}
/// The document is not the one its file holds: compared once per pair of documents and
diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs
index 568a133828..c1d5da0f7f 100644
--- a/crates/ui-egui/src/browser.rs
+++ b/crates/ui-egui/src/browser.rs
@@ -3634,6 +3634,18 @@ pub(crate) fn panel_pos(app: &SolveApp) -> Pos2 {
/// The Capture Position question stands for saving.
pub const SAVE: &str = "__save";
+/// It stands for Save As (`__save_as:<extension>:<name>`), which needs the question as Save
+/// does: a file can't hold a move not captured yet (#9736).
+const SAVE_AS: &str = "__save_as:";
+
+/// It stands for the browser build's Download design.
+pub const DOWNLOAD: &str = "__download";
+
+/// The question standing for Save As under `name` with `extension`.
+pub fn save_as_question(name: &str, extension: &str) -> String {
+ format!("{SAVE_AS}{extension}:{name}")
+}
+
/// Does starting `what` (a command id or [`SAVE`]) need the Capture Position question first?
/// Yes while components were moved without capturing their positions, except for the commands
/// that capture, revert or keep moving.
@@ -3661,6 +3673,8 @@ pub fn answer_capture(app: &mut SolveApp, action: &str) -> Result<(), String> {
"revert" => drop(app.run("component.revert_position", json!({}))),
"cancel" => {
app.tree.capture_prompt = None;
+ // A tab waiting to close after the save stays open (#9736).
+ crate::documents::saved_for_close(app);
return Ok(());
}
other => return Err(format!("unknown answer `{other}` (capture, revert, cancel)")),
@@ -3669,9 +3683,18 @@ pub fn answer_capture(app: &mut SolveApp, action: &str) -> Result<(), String> {
app.tree.occurrence_move = None;
if what == SAVE {
crate::toolbar::save(app);
+ } else if let Some((extension, name)) = what.strip_prefix(SAVE_AS).and_then(|rest| rest.split_once(':')) {
+ crate::toolbar::save_as_named(app, name, extension);
+ } else if what == DOWNLOAD {
+ crate::toolbar::download_design(app);
} else {
app.start(&what);
}
+ // A save that finished here (no thread, or nothing to write) answers a tab waiting to close,
+ // as the "save changes?" answer does; one under way answers when it ends.
+ if app.saving.is_none() && !app.file_picker_busy() {
+ crate::documents::saved_for_close(app);
+ }
Ok(())
}
diff --git a/crates/ui-egui/src/documents.rs b/crates/ui-egui/src/documents.rs
index 87c1c3585a..c59f39aafe 100644
--- a/crates/ui-egui/src/documents.rs
+++ b/crates/ui-egui/src/documents.rs
@@ -1216,6 +1216,11 @@ pub enum Answer {
pub fn prompt(app: &mut SolveApp, ctx: &egui::Context) {
let _language = crate::i18n::scope(&app.preferences.language);
quit_step(app);
+ // Saved, it asks the Capture Position question first (#9736): that question is the one to
+ // answer meanwhile, not this one again.
+ if app.tree.capture_prompt.is_some() {
+ return;
+ }
let Some(i) = app.docs.closing else { return };
let name = tabs_info(app).get(i).map(|x| x.0.clone()).unwrap_or_default();
let name_display = crate::i18n::tr_name(&name);
@@ -1274,7 +1279,7 @@ pub fn prompt(app: &mut SolveApp, ctx: &egui::Context) {
/// Act on the "save changes?" answer (also the control channel's `ui.documents {answer}`).
pub fn answer_prompt(app: &mut SolveApp, answer: Answer) {
- if app.file_picker_busy() {
+ if app.file_picker_busy() || app.tree.capture_prompt.is_some() {
return;
}
let Some(i) = app.docs.closing else { return };
@@ -1310,6 +1315,11 @@ pub fn answer_prompt(app: &mut SolveApp, answer: Answer) {
/// Resume Close/Quit only after Save As has answered and the write succeeded.
pub(crate) fn saved_for_close(app: &mut SolveApp) {
+ // Save asks the Capture Position question first while a move isn't captured (#9736): the
+ // close waits for its answer.
+ if app.tree.capture_prompt.is_some() {
+ return;
+ }
if !std::mem::take(&mut app.docs.saving) || app.docs.closing.is_none() {
return;
}
diff --git a/crates/ui-egui/src/menu_tests.rs b/crates/ui-egui/src/menu_tests.rs
index 0b9c167549..1884dcdc40 100644
--- a/crates/ui-egui/src/menu_tests.rs
+++ b/crates/ui-egui/src/menu_tests.rs
@@ -1057,6 +1057,50 @@ fn an_uncaptured_move_asks_before_the_next_command() {
assert!(crate::browser::answer_capture(&mut app, "capture").is_err(), "nothing waits now");
}
+/// #9736: a move not captured yet is a change to save. Closing the tab asks "save changes?";
+/// Save asks the Capture Position question before it writes, and the close waits for it.
+/// Cancel keeps the tab and the move; Capture saves the move and the tab closes. Save As asks
+/// the same question first.
+#[test]
+fn closing_with_an_uncaptured_move_asks_to_save_and_to_capture() {
+ let dir = std::env::temp_dir().join(format!("solidcraft-close-pending-move-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("two.sc");
+ let mut app = sample_app();
+ let b = body(&app);
+ app.run("component.from_bodies", json!({"bodies": [b]})).unwrap();
+ let occ = app.session.doc.occurrences[0].id;
+ app.run("occurrence.ground", json!({"occurrence": occ, "grounded": false})).unwrap();
+ app.run("file.save_as", json!({"path": path.to_string_lossy()})).unwrap();
+ assert!(!crate::documents::any_dirty(&app));
+ app.run("occurrence.move", json!({"occurrence": occ, "translate": [10, 0, 0], "capture": false})).unwrap();
+ assert!(crate::documents::any_dirty(&app), "the move is a change to save");
+
+ crate::toolbar::save_as(&mut app);
+ assert!(app.tree.capture_prompt.as_deref().is_some_and(|q| q.starts_with("__save_as:")), "{:?}", app.tree.capture_prompt);
+ assert!(!app.file_picker_busy(), "no file is picked before the answer");
+ crate::browser::answer_capture(&mut app, "cancel").unwrap();
+
+ crate::documents::close(&mut app, 0, false);
+ assert_eq!(app.docs.closing, Some(0), "asks to save");
+ crate::documents::answer_prompt(&mut app, crate::documents::Answer::Save);
+ assert_eq!(app.tree.capture_prompt.as_deref(), Some(crate::browser::SAVE));
+ assert_eq!(app.docs.closing, Some(0), "the close waits for the answer");
+ crate::documents::answer_prompt(&mut app, crate::documents::Answer::DontSave);
+ assert_eq!(app.docs.closing, Some(0), "the Capture Position question is the one to answer now");
+ crate::browser::answer_capture(&mut app, "cancel").unwrap();
+ assert!(app.docs.closing.is_none() && !app.session.pending_moves.is_empty(), "still open, with the move");
+
+ crate::documents::close(&mut app, 0, false);
+ crate::documents::answer_prompt(&mut app, crate::documents::Answer::Save);
+ crate::browser::answer_capture(&mut app, "capture").unwrap();
+ crate::save_job::tick(&mut app, true);
+ assert!(app.docs.closing.is_none() && app.session.path.is_none(), "saved and closed: a blank design is left");
+ let saved = solidcraft_engine::io::read_design(&std::fs::read(&path).unwrap()).unwrap();
+ assert_eq!(saved.occurrences[0].transform[3][0], 10.0, "the file holds the move");
+ let _ = std::fs::remove_dir_all(&dir);
+}
+
#[test]
fn f2_renames_the_selected_item() {
let mut app = sample_app();
diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs
index 71a5a8cc28..5ff5bf0948 100644
--- a/crates/ui-egui/src/toolbar.rs
+++ b/crates/ui-egui/src/toolbar.rs
@@ -939,7 +939,13 @@ fn save_as_with_extension(app: &mut SolveApp, extension: &str) {
save_as_named(app, &name, extension);
}
-fn save_as_named(app: &mut SolveApp, name: &str, extension: &str) {
+pub(crate) fn save_as_named(app: &mut SolveApp, name: &str, extension: &str) {
+ // The Capture Position question first, as Save asks it: the file can't hold a move not
+ // captured yet (#9736).
+ if crate::browser::needs_capture(app, crate::browser::SAVE) {
+ app.tree.capture_prompt = Some(crate::browser::save_as_question(name, extension));
+ return;
+ }
let name = format!("{name}.{extension}");
app.pick_save(&name, &[solidcraft_engine::io::DESIGN_EXT, "json"], |app, path| {
// A save under way answers a closing tab when it ends.
@@ -953,7 +959,11 @@ fn save_as_named(app: &mut SolveApp, name: &str, extension: &str) {
}
/// File > Download design (browser build): the open design as a `.sc` file download.
-fn download_design(app: &mut SolveApp) {
+pub(crate) fn download_design(app: &mut SolveApp) {
+ if crate::browser::needs_capture(app, crate::browser::SAVE) {
+ app.tree.capture_prompt = Some(crate::browser::DOWNLOAD.into());
+ return;
+ }
let name = format!("{}.{}", app.session.doc.name, solidcraft_engine::io::DESIGN_EXT);
let result = app
.session
diff --git a/docs/design-container.md b/docs/design-container.md
index 9c50c4ce71..884d514936 100644
--- a/docs/design-container.md
+++ b/docs/design-container.md
@@ -118,8 +118,9 @@ Engine Save/Save As write the evaluated model with the design by default
`cache/bodies.bin`. Plain JSON, recovery/autosave and browser downloads do not
carry it. Saving never forces placement, evaluation or tessellation: the model is
the session's own evaluation, and only when it is the evaluation of the document
-being saved (`Model::is_current`). An active sketch, uncaptured moves or feature
-errors leave the cache out; the design saves as before. A cache
+being saved (`Model::is_current`). An active sketch or feature errors leave the
+cache out; the design saves as before. A component moved and not captured yet
+stops the save instead, naming it: capture or revert the move first (#9736). A cache
over its budgets keeps fewer states (below) and is left out only when not even
the final state fits.