diff --git a/crates/engine/src/cmd/component_link.rs b/crates/engine/src/cmd/component_link.rs index 8c1cc9992..ce7c78157 100644 --- a/crates/engine/src/cmd/component_link.rs +++ b/crates/engine/src/cmd/component_link.rs @@ -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 { } 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()))?; diff --git a/crates/engine/src/cmd/component_relink_tests.rs b/crates/engine/src/cmd/component_relink_tests.rs index 16c9b049a..4044b5b76 100644 --- a/crates/engine/src/cmd/component_relink_tests.rs +++ b/crates/engine/src/cmd/component_relink_tests.rs @@ -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"); diff --git a/crates/ui-egui/src/linked_source.rs b/crates/ui-egui/src/linked_source.rs index b3c39c22b..74d8f3f81 100644 --- a/crates/ui-egui/src/linked_source.rs +++ b/crates/ui-egui/src/linked_source.rs @@ -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) { 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()));