SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Insert Design: preserve source part properties (#6624) (#6691)
Colin Faulkingham7d7016b3569b
diff --git a/crates/engine/src/cmd/bom_tests.rs b/crates/engine/src/cmd/bom_tests.rsindex 68805cc2a1..47df03c581 100644--- a/crates/engine/src/cmd/bom_tests.rs+++ b/crates/engine/src/cmd/bom_tests.rs@@ -122,6 +122,61 @@ fn part_properties_survive_save_and_open_and_undo_in_one_step() {let _ = std::fs::remove_dir_all(&dir);}+#[test]+fn inserted_design_carries_its_part_properties_into_the_bom() {+ let dir = scratch("insert-design");+ let source = dir.join("plate.solidcraft");+ let metadata = json!({+ "part_number": "PL-6624", "description": "Mounting plate", "revision": "B",+ "author": "Example author", "license": "CC0-1.0", "com.example.catalog": {"stock": 12}+ });+ let mut part = Session::default();+ run(&mut part, "solid.box", json!({"length": 40, "width": 20, "height": 3}));+ run(&mut part, "document.metadata.set", json!({"values": metadata}));+ run(&mut part, "file.save", json!({"path": source}));++ for reuse in [false, true] {+ let mut s = Session::default();+ run(&mut s, "document.metadata.set", json!({"values": {"part_number": "ASM-1", "description": "Assembly"}}));+ let own_metadata = s.doc.metadata.clone();+ let undo = s.undo.len();+ let inserted = run(&mut s, "component.insert_design", json!({"path": source, "name": "Plate", "reuse": reuse}));+ let component = inserted["component"].as_u64().unwrap();+ assert_eq!(run(&mut s, "component.metadata", json!({"component": component}))["metadata"], metadata);+ assert_eq!(s.doc.metadata, own_metadata, "the assembly keeps its own properties");+ let bom = run(&mut s, "assembly.bom", json!({}));+ assert_eq!(bom["rows"][0]["part_number"], "PL-6624");+ assert_eq!(bom["rows"][0]["description"], "Mounting plate");+ assert_eq!(bom["rows"][0]["revision"], "B");+ assert_eq!(bom["rows"][0]["count"], 1);+ assert_eq!(s.undo.len(), undo + 1, "insertion including properties is one undo step");+ run(&mut s, "edit.undo", json!({}));+ assert!(s.doc.components.is_empty());+ assert!(s.doc.links.is_empty());+ run(&mut s, "edit.redo", json!({}));+ assert_eq!(run(&mut s, "component.metadata", json!({"component": component}))["metadata"], metadata);++ for (file, format) in [("assembly.json", "json"), ("assembly.solidcraft", "container")] {+ let path = dir.join(file);+ run(&mut s, "file.save_as", json!({"path": path, "format": format}));+ let mut back = Session::default();+ run(&mut back, "doc.open", json!({"path": path}));+ assert_eq!(run(&mut back, "component.metadata", json!({"component": component}))["metadata"], metadata, "{format}");+ assert_eq!(run(&mut back, "assembly.bom", json!({}))["rows"], bom["rows"], "{format}");+ }++ // Reusing the link shares its definition, including locally edited part properties.+ run(&mut s, "component.metadata.set", json!({"component": component, "description": "Assembly-specific description"}));+ run(&mut s, "component.insert_design", json!({"path": source, "reuse": true, "translate": [50, 0, 0]}));+ let bom = run(&mut s, "assembly.bom", json!({}));+ assert_eq!(bom["rows"].as_array().unwrap().len(), 1);+ assert_eq!(bom["rows"][0]["count"], 2);+ assert_eq!(bom["rows"][0]["part_number"], "PL-6624");+ assert_eq!(bom["rows"][0]["description"], "Assembly-specific description");+ }+ let _ = std::fs::remove_dir_all(&dir);+}+#[test]fn csv_quotes_every_field_and_json_writes_the_rows() {let dir = scratch("export");diff --git a/crates/engine/src/cmd/component_extract.rs b/crates/engine/src/cmd/component_extract.rsindex e813a47675..627dfca756 100644--- a/crates/engine/src/cmd/component_extract.rs+++ b/crates/engine/src/cmd/component_extract.rs@@ -566,7 +566,7 @@ pub(super) fn extract(s: &mut Session, p: &Value) -> Result<Value> {// This design: the component's history becomes an Import feature of the new design's bodieslet shown = file.display().to_string();- let (_, sf, faces) = session_bodies(&part, &shown)?;+ let (_, sf, faces, _) = session_bodies(&part, &shown)?;// The link has a body per piece: a body in pieces keeps its name on the firstlet mut solids: Vec<String> = Vec::new();for b in part.world_state().bodies.iter().filter(|b| !b.body.is_mesh()) {diff --git a/crates/engine/src/cmd/component_link.rs b/crates/engine/src/cmd/component_link.rsindex e84d37bf92..ed14937b82 100644--- a/crates/engine/src/cmd/component_link.rs+++ b/crates/engine/src/cmd/component_link.rs@@ -27,7 +27,7 @@ mod relink_tests;pub static COMMANDS: &[CommandSpec] = &[CommandSpec::new("component.insert_design", "Insert Design", insert_design).at("SOLID", "ASSEMBLE").icon("component").params(- "path: a .solidcraft design → a new component linked to it, holding its solid bodies as placed in that design; \+ "path: a .solidcraft 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]; \reuse?: bool (default false): insert a free occurrence, reusing an existing link to the same file when available",),@@ -133,8 +133,8 @@ pub fn resolve_design_links(doc: &mut Document, from: Option<&str>) {}/// 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>>);+/// the design gives their faces (`Feature::link_faces`), with the source's design properties.+type Linked = (String, solidcraft_io::StepFeature, Vec<Vec<String>>, solidcraft_doc::Metadata);/// The solid bodies of the design at `file`, evaluated, as an Import feature.fn linked_bodies(file: &Path, own: Option<&str>) -> Result<Linked> {@@ -167,7 +167,7 @@ fn session_bodies(part: &Session, shown: &str) -> Result<Linked> {// The STEP text keeps no face names: carry the design's own, so references into the linked// bodies follow them as they do inside the design (#3330).let faces = solidcraft_doc::naming::carried_face_names(&world, &feature.import.bodies);- Ok((name, feature, faces))+ Ok((name, feature, faces, part.doc.metadata.clone()))}fn component_key(s: &Session, p: &Value, key: &str, cmd: &str) -> Result<u64> {@@ -197,7 +197,7 @@ fn insert_design(s: &mut Session, p: &Value) -> Result<Value> {return super::component::copy_occurrence(s, &json!({"component": component, "parent": parent, "translate": translate}));}}- let (design_name, sf, faces) = linked_bodies(&file, s.path.as_deref())?;+ let (design_name, sf, faces, metadata) = linked_bodies(&file, s.path.as_deref())?;let name = str_(p, "name").filter(|n| !n.trim().is_empty()).map(str::to_string).unwrap_or(design_name);let mut create = json!({"name": name, "activate": false});// Clipboard insertion uses reuse even for the first paste, which must stay movable.@@ -210,6 +210,10 @@ fn insert_design(s: &mut Session, p: &Value) -> Result<Value> {let made = super::component::new_component(s, &create)?;let comp = made["component"].as_u64().ok_or_else(|| bad(cmd, "the component was not created"))?;let occ = made["occurrence"].as_u64();+ // Seed only a new definition: reused links keep any locally edited part properties.+ if let Some(c) = s.doc_mut().components.iter_mut().find(|c| c.id == comp) {+ c.metadata = metadata;+ }let id = s.doc_mut().add_feature(sf.kind, Some(&name))?;if let Some(f) = s.doc_mut().feature_mut(id) {f.component = comp;@@ -260,7 +264,7 @@ fn update_links(s: &mut Session, p: &Value) -> Result<Value> {};let old = old.clone();let file = resolve(s.path.as_deref(), &link.path);- let (_, sf, faces) = linked_bodies(&file, s.path.as_deref()).map_err(|e| bad(cmd, format!("{cname}: {e}")))?;+ let (_, sf, faces, _) = linked_bodies(&file, s.path.as_deref()).map_err(|e| bad(cmd, format!("{cname}: {e}")))?;warnings.extend(sf.warnings.iter().cloned());let same = matches!(&sf.kind, FeatureKind::Import { step, .. } if *step == old);let kept = s.doc.feature(link.feature).map(|f| &f.link_faces);@@ -339,7 +343,7 @@ fn relink(s: &mut Session, p: &Value) -> Result<Value> {}// Read/evaluate the candidate before changing anything. Session::execute owns the single// undo step and restores both the document and evaluated state on any later failure.- let (_, sf, faces) = linked_bodies(&file, s.path.as_deref()).map_err(|e| bad(cmd, e.to_string()))?;+ let (_, sf, faces, _) = linked_bodies(&file, s.path.as_deref()).map_err(|e| bad(cmd, e.to_string()))?;let geometry_changed = match (s.doc.feature(link.feature).map(|f| &f.kind), &sf.kind) {(Some(FeatureKind::Import { step: old, .. }), FeatureKind::Import { step, .. }) => old != step,_ => true,