* claim: #10813 * fix: place reused linked designs from parent origin (#10813)
This commit is contained in:
committed by
GitHub
parent
a1dc849e8d
commit
b6aabb88ff
@@ -31,7 +31,8 @@ mod relink_tests;
|
||||
pub static COMMANDS: &[CommandSpec] = &[
|
||||
CommandSpec::new("component.insert_design", "Insert Design", insert_design).at("SOLID", "ASSEMBLE").icon("component").params(
|
||||
"path: a .sc design → a new component linked to it, holding its solid bodies as placed in that design and a copy of its design properties; \
|
||||
name? (default: the design's name); parent?: component id or name (default: the active one); translate?: [x,y,z]; \
|
||||
name? (default: the design's name); parent?: component id or name (default: the active one); \
|
||||
translate?: [x,y,z] in mm from the destination parent's origin (default [0,0,0], also with reuse); \
|
||||
reuse?: bool (default false): insert a free occurrence, reusing an existing link to the same file when available",
|
||||
),
|
||||
CommandSpec::new("component.update_links", "Update Links", update_links).icon("update").params(
|
||||
@@ -506,8 +507,13 @@ fn insert_design(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if p.get("reuse").and_then(Value::as_bool).unwrap_or(false) {
|
||||
let existing = s.doc.links.iter().find(|(_, link)| absolute(&resolve(s.path.as_deref(), &link.path)) == file).map(|(id, _)| *id);
|
||||
if let Some(component) = existing {
|
||||
let parent = p.get("parent").cloned().unwrap_or_else(|| json!(s.active_component));
|
||||
return super::component::copy_occurrence(s, &json!({"component": component, "parent": parent, "translate": translate}));
|
||||
let parent = if p.get("parent").is_some() { component_key(s, p, "parent", cmd)? } else { s.active_component };
|
||||
// Reuse the definition, not another placement's captured or pending move.
|
||||
// Insert Design places from the parent origin whether the link is new or reused.
|
||||
let mut transform = solidcraft_doc::IDENTITY;
|
||||
transform[3] = [translate.x, translate.y, translate.z, 1.0];
|
||||
let occurrence = s.doc_mut().add_occurrence(component, parent, transform)?;
|
||||
return Ok(json!({"occurrence": occurrence, "component": component}));
|
||||
}
|
||||
}
|
||||
let ((design_name, sf, faces, metadata, properties), read) = linked_bodies_read(&file, s.path.as_deref())?;
|
||||
|
||||
@@ -161,6 +161,90 @@ fn reused_design_insertion_is_free_even_before_a_link_exists() {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reused_design_insertion_uses_the_requested_position_and_survives_undo_and_reopen() {
|
||||
let dir = folder("reuse-position");
|
||||
let file = dir.join("part.sc");
|
||||
part(&file, 30);
|
||||
for first_reuses in [false, true] {
|
||||
let mut s = Session::default();
|
||||
let first = run(&mut s, "component.insert_design", json!({"path": file, "translate": [20, 0, 0], "reuse": first_reuses}));
|
||||
let before = s.doc.to_json();
|
||||
let undo = s.undo.len();
|
||||
let second = run(&mut s, "component.insert_design", json!({"path": file, "translate": [40, 5, 7], "reuse": true}));
|
||||
assert_eq!(first["component"], second["component"]);
|
||||
assert_eq!(s.doc.links.len(), 1);
|
||||
assert_eq!(s.doc.features.len(), 1);
|
||||
assert_eq!(s.undo.len(), undo + 1);
|
||||
let occurrence = second["occurrence"].as_u64().unwrap();
|
||||
assert_linked_bounds(&mut s, occurrence, [40.0, 5.0, 7.0], [50.0, 25.0, 37.0]);
|
||||
assert_linked_bounds(&mut s, first["occurrence"].as_u64().unwrap(), [20.0, 0.0, 0.0], [30.0, 20.0, 30.0]);
|
||||
assert!(!s.doc.occurrences.iter().find(|o| o.id == occurrence).unwrap().grounded);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
run(&mut s, "edit.redo", json!({}));
|
||||
assert_linked_bounds(&mut s, occurrence, [40.0, 5.0, 7.0], [50.0, 25.0, 37.0]);
|
||||
let assembly = dir.join("assembly.sc");
|
||||
run(&mut s, "file.save", json!({"path": assembly}));
|
||||
let mut cold = Session::default();
|
||||
run(&mut cold, "doc.open", json!({"path": assembly}));
|
||||
assert_linked_bounds(&mut cold, occurrence, [40.0, 5.0, 7.0], [50.0, 25.0, 37.0]);
|
||||
let at_origin = run(&mut cold, "component.insert_design", json!({"path": file, "reuse": true}))["occurrence"].as_u64().unwrap();
|
||||
assert_linked_bounds(&mut cold, at_origin, [0.0, 0.0, 0.0], [10.0, 20.0, 30.0]);
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
fn assert_linked_bounds(s: &mut Session, occurrence: u64, min: [f64; 3], max: [f64; 3]) {
|
||||
let p = run(s, "occurrence.properties", json!({"occurrence": occurrence}));
|
||||
assert_eq!(p["body_count"], 1);
|
||||
assert!((p["total"]["volume_mm3"].as_f64().unwrap() - 6000.0).abs() < 1e-6, "{p}");
|
||||
for (key, expected) in [("min", min), ("max", max)] {
|
||||
for (axis, expected) in expected.into_iter().enumerate() {
|
||||
assert!((p["total"]["bbox"][key][axis].as_f64().unwrap() - expected).abs() < 1e-6, "{key}[{axis}] != {expected}: {p}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reused_design_insertion_ignores_existing_moves_and_uses_the_destination_parent() {
|
||||
let dir = folder("reuse-parent");
|
||||
let file = dir.join("part.sc");
|
||||
part(&file, 30);
|
||||
for capture in [false, true] {
|
||||
let mut s = Session::default();
|
||||
let first = run(&mut s, "component.insert_design", json!({"path": file, "translate": [20, 0, 0]}));
|
||||
run(
|
||||
&mut s,
|
||||
"occurrence.move",
|
||||
json!({"occurrence": first["occurrence"], "translate": [0, 10, 0], "axis": [0, 0, 1], "angle": "90 deg", "capture": capture, "move_grounded": true}),
|
||||
);
|
||||
let original = s.doc.occurrences.clone();
|
||||
let pending = s.pending_moves.clone();
|
||||
let parent = run(&mut s, "component.create", json!({"name": "Parent", "activate": false}));
|
||||
run(&mut s, "occurrence.move", json!({"occurrence": parent["occurrence"], "translate": [100, 0, 0], "axis": [0, 0, 1], "angle": "90 deg"}));
|
||||
for explicit_parent in [true, false] {
|
||||
let mut params = json!({"path": file, "translate": [40, 5, 7], "reuse": true});
|
||||
if explicit_parent {
|
||||
params["parent"] = json!("Parent");
|
||||
} else {
|
||||
run(&mut s, "component.activate", json!({"component": parent["component"]}));
|
||||
}
|
||||
let inserted = run(&mut s, "component.insert_design", params);
|
||||
assert_eq!(inserted["component"], first["component"]);
|
||||
assert_linked_bounds(&mut s, inserted["occurrence"].as_u64().unwrap(), [75.0, 40.0, 7.0], [95.0, 50.0, 37.0]);
|
||||
assert_eq!(s.doc.occurrences.first(), original.first());
|
||||
assert_eq!(s.pending_moves, pending);
|
||||
}
|
||||
let before = s.doc.to_json();
|
||||
for parent in [json!("missing"), first["component"].clone()] {
|
||||
assert!(s.execute("component.insert_design", &json!({"path": file, "parent": parent, "reuse": true})).is_err());
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_design_properties_update_density_without_geometry_and_keep_edited_cards() {
|
||||
let dir = folder("properties-density");
|
||||
|
||||
@@ -394,6 +394,11 @@ off. A STEP or mesh file picked there is inserted as **Insert STEP** or **Insert
|
||||
Mesh** would. The part's solid bodies come
|
||||
in as they sit in that design, and they are kept in the assembly, so it opens even without the
|
||||
part file. The link is stored relative to the assembly's folder once the assembly is saved.
|
||||
For scripts, `translate: [x,y,z]` places the design's origin in millimetres from the destination
|
||||
parent's origin (the active component unless `parent` is supplied). It defaults to `[0,0,0]`.
|
||||
With `reuse: true`, insertion reuses the existing linked component but uses the same coordinates;
|
||||
it does not inherit another occurrence's position or rotation. For example, inserts at
|
||||
`[20,0,0]` and `[40,0,0]` place the two origins at 20 and 40 mm, even when the second reuses the link.
|
||||
After the part file changes, **Update Links** (`component.update_links`) reads it again.
|
||||
An assembly that opens, or whose tab comes back to the front, does that by itself for the parts whose files
|
||||
changed since it last read them, whether the part was saved in another tab, another window or by
|
||||
|
||||
Reference in New Issue
Block a user