Keep linked components valid through Undo after Save As (#6860) (#6877)

* claim: #6860

* fix: preserve linked component targets across Save As history (#6860)
This commit is contained in:
Colin Faulkingham
2026-10-26 11:08:32 +00:00
committed by GitHub
parent 25ad6c7266
commit 03bf992bc1
8 changed files with 183 additions and 44 deletions
+108 -9
View File
@@ -2,9 +2,9 @@
//!
//! The linked design is opened and evaluated on its own, and its solid bodies, as placed in it,
//! join a new component through an Import feature (exact B-rep, kept in this design so it opens
//! without the part file). The link remembers the file, relative to this design's folder once it
//! is saved; Update Links reads the file again and replaces the Import feature's bodies, and
//! Break Link keeps the bodies and forgets the file. The bodies' faces are named after the names
//! without the part file). The link remembers the absolute file in a session and a path relative
//! to the design's folder on disk; Update Links reads it again and replaces the Import feature's
//! bodies, and Break Link keeps the bodies and forgets the file. The bodies' faces are named after the names
//! the design gives them (`Feature::link_faces`), so a joint on a linked face stays on that face
//! when the design changes, as it would inside the design, and reports it when the face is gone.
@@ -124,6 +124,14 @@ pub(super) fn rebased(doc: &Document, from: Option<&str>, to: &str) -> std::coll
doc.links.iter().map(|(c, l)| (*c, DesignLink { path: stored(Some(to), &resolve(from, &l.path)), feature: l.feature })).collect()
}
/// Resolve a design's component links against its original file, before opening it from a
/// different location (for example a recovery file) or keeping it in session history.
pub fn resolve_design_links(doc: &mut Document, from: Option<&str>) {
for link in doc.links.values_mut() {
link.path = absolute(&resolve(from, &link.path)).to_string_lossy().into_owned();
}
}
/// The bodies of a linked design: its name, the Import feature that holds them and the names
/// the design gives their faces (`Feature::link_faces`).
type Linked = (String, solidcraft_io::StepFeature, Vec<Vec<String>>);
@@ -136,7 +144,7 @@ fn linked_bodies(file: &Path, own: Option<&str>) -> Result<Linked> {
}
let bytes = solidcraft_io::vfs::read(&shown, solidcraft_io::MAX_DESIGN_BYTES).map_err(|e| EngineError::Other(format!("{shown}: {e}")))?;
let doc = solidcraft_io::read_design(&bytes).map_err(|e| EngineError::Other(format!("{shown}: {e}")))?;
session_bodies(&Session::new(doc), &shown)
session_bodies(&Session::opened_at(doc, None, Some(&shown)), &shown)
}
/// The solid bodies of `part`, the design saved as `shown`, as an Import feature.
@@ -212,7 +220,7 @@ fn insert_design(s: &mut Session, p: &Value) -> Result<Value> {
o.transform[3] = [translate.x, translate.y, translate.z, 1.0];
}
let path = stored(s.path.as_deref(), &file);
s.doc_mut().links.insert(comp, DesignLink { path: path.clone(), feature: id });
s.doc_mut().links.insert(comp, DesignLink { path: file.to_string_lossy().into_owned(), feature: id });
s.refresh();
if let Some(e) = s.model.result(id).and_then(|r| r.error.clone()) {
return Err(EngineError::Other(e));
@@ -354,7 +362,7 @@ fn relink(s: &mut Session, p: &Value) -> Result<Value> {
f.kind = sf.kind;
f.link_faces = faces;
}
s.doc_mut().links.insert(comp, DesignLink { path: path.clone(), feature: link.feature });
s.doc_mut().links.insert(comp, DesignLink { path: file.to_string_lossy().into_owned(), feature: link.feature });
s.refresh();
if let Some(error) = s.model.result(link.feature).and_then(|r| r.error.clone()) {
return Err(bad(cmd, error));
@@ -367,7 +375,8 @@ fn relink(s: &mut Session, p: &Value) -> Result<Value> {
for (_, name, why) in sol.errors.iter().chain(&sol.conflicts) {
warnings.push(format!("joint {name}: {why}"));
}
Ok(json!({"component": comp, "feature": link.feature, "path": path, "previous_path": link.path, "geometry_changed": geometry_changed,
let previous_path = stored(s.path.as_deref(), &resolve(s.path.as_deref(), &link.path));
Ok(json!({"component": comp, "feature": link.feature, "path": path, "previous_path": previous_path, "geometry_changed": geometry_changed,
"warnings": warnings, "joint_errors": diagnostics(&sol.errors), "joint_conflicts": diagnostics(&sol.conflicts)}))
}
@@ -375,7 +384,7 @@ fn break_link(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "component.break_link";
let comp = component_key(s, p, "component", cmd)?;
let link = s.doc_mut().links.remove(&comp).ok_or_else(|| bad(cmd, "that component is not linked to a design"))?;
Ok(json!({"component": comp, "path": link.path}))
Ok(json!({"component": comp, "path": stored(s.path.as_deref(), &resolve(s.path.as_deref(), &link.path))}))
}
fn list_links(s: &mut Session, _p: &Value) -> Result<Value> {
@@ -386,8 +395,9 @@ fn list_links(s: &mut Session, _p: &Value) -> Result<Value> {
.map(|(c, l)| {
let file = resolve(s.path.as_deref(), &l.path);
let name = s.doc.components.iter().find(|x| x.id == *c).map(|x| x.name.clone());
let path = stored(s.path.as_deref(), &file);
let file = file.to_string_lossy();
json!({"component": c, "name": name, "path": l.path, "file": file, "found": solidcraft_io::vfs::is_file(&file)})
json!({"component": c, "name": name, "path": path, "file": file, "found": solidcraft_io::vfs::is_file(&file)})
})
.collect();
Ok(json!({"links": links}))
@@ -436,6 +446,95 @@ mod tests {
run(s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap_or(f64::NAN)
}
/// #6860: Save As changes the file's folder, not the targets kept in the edit history.
#[test]
fn save_as_keeps_link_targets_through_undo_redo_and_checkpoints() {
for on_copy in [false, true] {
let dir = std::env::temp_dir().join(format!("solidcraft-link-save-as-{}-{on_copy}", std::process::id()));
std::fs::create_dir_all(dir.join("parts")).unwrap();
std::fs::create_dir_all(dir.join("asm/sub/deeper")).unwrap();
let part = dir.join("parts/plate.solidcraft");
let case = dir.join("asm/case.solidcraft");
save_box(&part, 30.0);
let mut s = Session::default();
run(&mut s, "component.insert_design", json!({"path": part}));
run(&mut s, "file.save", json!({"path": case}));
// Opening starts from the relative paths actually written to disk.
run(&mut s, "doc.open", json!({"path": case}));
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [60, 0, 0]}));
let checkpoint = s.checkpoint();
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [80, 0, 0]}));
run(&mut s, "edit.undo", json!({}));
let before = s.doc.clone();
let undo_before = s.undo.last().unwrap().doc.clone();
let missing = dir.join("missing/case.solidcraft");
assert!(s.execute("file.save_as", &json!({"path": missing})).is_err());
assert_eq!(s.path.as_deref(), case.to_str());
for folder in ["asm/sub", "asm/sub/deeper"] {
let path = dir.join(folder).join("case.solidcraft");
if on_copy {
let mut copy = s.scratch();
run(&mut copy, "file.save_as", json!({"path": path}));
s.adopt_save(&copy);
} else {
run(&mut s, "file.save_as", json!({"path": path}));
}
assert!(!s.is_dirty());
for command in ["edit.redo", "edit.undo", "edit.undo", "edit.redo"] {
run(&mut s, command, json!({}));
let links = run(&mut s, "component.links", json!({}));
assert_eq!(links["links"][0]["found"], true, "{on_copy}, {folder}, {command}: {links}");
assert_eq!(absolute(Path::new(links["links"][0]["file"].as_str().unwrap())), absolute(&part));
}
}
assert!(std::sync::Arc::ptr_eq(&s.doc, &before), "saving does not rewrite the live document");
assert!(std::sync::Arc::ptr_eq(&s.undo.last().unwrap().doc, &undo_before), "saving does not copy the edit history");
s.restore_checkpoint(checkpoint).unwrap();
assert_eq!(run(&mut s, "component.links", json!({}))["links"][0]["found"], true);
let path = s.path.clone().unwrap();
for format in ["json", "container"] {
run(&mut s, "file.save", json!({"format": format}));
let disk = solidcraft_io::read_design(&std::fs::read(&path).unwrap()).unwrap();
assert_eq!(disk.links.values().next().unwrap().path, "../../../parts/plate.solidcraft");
run(&mut s, "doc.open", json!({"path": path}));
assert!(!s.is_dirty());
assert_eq!(run(&mut s, "component.links", json!({}))["links"][0]["found"], true);
}
save_box(&part, 45.0);
assert_eq!(run(&mut s, "component.update_links", json!({}))["updated"], json!(["plate"]));
assert!((volume(&mut s) - 9125.0).abs() < 1e-6);
let _ = std::fs::remove_dir_all(&dir);
}
}
#[test]
fn recovered_relative_links_keep_their_targets_after_save_as() {
let dir = std::env::temp_dir().join(format!("solidcraft-link-recovery-{}", std::process::id()));
std::fs::create_dir_all(dir.join("parts")).unwrap();
std::fs::create_dir_all(dir.join("asm/sub")).unwrap();
let part = dir.join("parts/plate.solidcraft");
save_box(&part, 30.0);
let mut s = Session::default();
run(&mut s, "component.insert_design", json!({"path": part}));
run(&mut s, "file.save", json!({"path": dir.join("asm/case.solidcraft")}));
run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [60, 0, 0]}));
let recovery = dir.join("recovery");
let mut autosaver = crate::recovery::Autosaver::new(&recovery, std::time::Duration::ZERO).unwrap();
assert!(autosaver.save(&s).unwrap());
drop(autosaver);
let entry = crate::recovery::orphans(&recovery).pop().unwrap();
// Older autosaves hold paths relative to the design, not the recovery folder.
std::fs::write(recovery.join(format!("{}.solidcraft", entry.id)), solidcraft_io::write_design(&s.design())).unwrap();
let mut recovered = Session::default();
run(&mut recovered, "doc.recover", json!({"dir": recovery, "id": entry.id}));
assert_eq!(run(&mut recovered, "component.links", json!({}))["links"][0]["found"], true);
run(&mut recovered, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [80, 0, 0]}));
run(&mut recovered, "file.save_as", json!({"path": dir.join("asm/sub/case.solidcraft")}));
run(&mut recovered, "edit.undo", json!({}));
assert_eq!(run(&mut recovered, "component.links", json!({}))["links"][0]["found"], true);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_saved_assembly_reopens_with_its_link_updates_it_and_breaks_it() {
let dir = std::env::temp_dir().join(format!("solidcraft-link-reopen-{}", std::process::id()));