Assembly: Relink Source takes a .json design, as Insert Design does (#10394)

`component.insert_design` links a design saved as a .sc container or as plain
JSON (`.json`), but `component.relink` refused every path that was not a
container ("choose a saved .sc design"), so a component linked to a JSON
design could not be pointed at another one. Relink now takes any design path
Insert Design takes (`is_design_path`: .sc, a legacy extension or .json); the
file is read by the same `design_bodies`, which already reads both. The Relink
Source file dialog opens on a filter that lists .json among the designs, too.

Assisted-by: LLM
This commit is contained in:
Christian Glombek
2026-11-01 04:18:49 +00:00
committed by GitHub
parent 9d01f6e005
commit 5c1aad8a09
3 changed files with 41 additions and 5 deletions
+4 -3
View File
@@ -41,7 +41,7 @@ pub static COMMANDS: &[CommandSpec] = &[
joints whose faces are gone or that no longer solve)",
),
CommandSpec::new("component.relink", "Relink Source", relink).icon("update").params(
"component: id or name of a linked component; path: a saved .sc design — replaces the source and linked bodies for all its occurrences, preserving identities and placements; returns path, previous_path, warnings, joint_errors and joint_conflicts",
"component: id or name of a linked component; path: a saved design (.sc or .json) — replaces the source and linked bodies for all its occurrences, preserving identities and placements; returns path, previous_path, warnings, joint_errors and joint_conflicts",
),
CommandSpec::new("component.break_link", "Break Link", break_link)
.icon("break")
@@ -721,8 +721,9 @@ fn relink(s: &mut Session, p: &Value) -> Result<Value> {
}
let given = str_(p, "path").filter(|x| !x.trim().is_empty() && x.len() < 4096).ok_or_else(|| bad(cmd, "`path` is required"))?;
let file = absolute(Path::new(given));
if !solidcraft_io::is_design_container_path(&file) {
return Err(bad(cmd, "choose a saved .sc design"));
// Any design Insert Design takes: a .sc container (or a legacy one) or a plain JSON design.
if !solidcraft_io::is_design_path(given) {
return Err(bad(cmd, "choose a saved design (.sc or .json)"));
}
// A network or device path is judged from its text before the disk is asked anything.
link_refused(s.path.as_deref(), given).map_err(|e| bad(cmd, e.to_string()))?;
@@ -509,6 +509,24 @@ fn relink_rejects_bad_sources_and_targets_without_touching_the_design_or_undo()
let _ = std::fs::remove_dir_all(dir);
}
/// Relink takes any design Insert Design takes: a plain JSON design too, and back to a container.
#[test]
fn relink_takes_a_json_design_as_insert_design_does() {
let dir = folder("json");
let (container, plain) = (dir.join("source.sc"), dir.join("source.json"));
part(&container, 30);
part(&plain, 45);
assert!(!solidcraft_io::is_design_container(&std::fs::read(&plain).unwrap()), "file.save writes a .json design as plain JSON");
let mut s = Session::default();
let component = run(&mut s, "component.insert_design", json!({"path": container}))["component"].as_u64().unwrap();
let to_json = run(&mut s, "component.relink", json!({"component": component, "path": plain}));
assert_eq!(to_json["geometry_changed"], true);
assert!((volume(&mut s) - 9000.0).abs() < 1e-5);
run(&mut s, "component.relink", json!({"component": component, "path": container}));
assert!((volume(&mut s) - 6000.0).abs() < 1e-5);
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn relink_keeps_joint_definitions_and_reports_solver_diagnostics() {
let dir = folder("joints");
+19 -2
View File
@@ -36,7 +36,10 @@ pub(crate) fn start(app: &mut SolveApp, component: u64) {
return;
}
let document = app.session.doc.clone();
app.pick_open_for(&solidcraft_engine::io::DESIGN_EXTS, move |app, path| {
// A design of any release, or a plain JSON design: what Insert Design takes.
let mut extensions = solidcraft_engine::io::DESIGN_EXTS.to_vec();
extensions.push("json");
app.pick_open_for(&extensions, move |app, path| {
let Some(path) = path else { return };
// A host or programmatic caller can replace/edit a design while a native picker is
// pending. Never apply that old component id to a different design or newer revision.
@@ -295,7 +298,7 @@ mod tests {
fn answer(app: &mut SolveApp, path: Option<String>) {
app.services.pick_open = Some(Box::new(move |extensions| {
assert_eq!(extensions, &["sc", "solidcraft", "solvecraft"], "a design of any release");
assert_eq!(extensions, &["sc", "solidcraft", "solvecraft", "json"], "a design of any release, or a plain JSON design");
let result = path.clone();
Box::pin(async move { result })
}));
@@ -368,6 +371,20 @@ mod tests {
let _ = std::fs::remove_dir_all(folder);
}
#[test]
fn relink_picks_a_plain_json_design_as_insert_design_does() {
let (mut app, component, _, folder, _) = fixture("json");
let plain = folder.join("plain.json");
let mut part = Session::default();
part.execute("solid.box", &json!({"length": 10, "width": 20, "height": 45})).unwrap();
part.execute("file.save", &json!({"path": plain})).unwrap();
answer(&mut app, Some(plain.to_string_lossy().into_owned()));
start(&mut app, component);
crate::file_picker::tick(&mut app, &egui::Context::default());
assert!(app.session.doc.links[&component].path.ends_with("plain.json"));
let _ = std::fs::remove_dir_all(folder);
}
#[test]
fn extract_menu_asks_where_to_save_and_links_the_component_there() {
let folder = std::env::temp_dir().join(format!("solidcraft-extract-ui-{}", std::process::id()));