diff --git a/crates/engine/src/cmd/component.rs b/crates/engine/src/cmd/component.rs index 2203e0d4d..9caeb0c5e 100644 --- a/crates/engine/src/cmd/component.rs +++ b/crates/engine/src/cmd/component.rs @@ -175,7 +175,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .params("component (or occurrence): what to place again; path?: validates selected occurrence; parent?: component (default: the same parent); translate?: [x,y,z]; in_place?: bool (default false: the selected transform within the parent; true: where the selected placement is in the world, translate in world axes; a parent placed more than once is placed by parent_path?, else the active placement, and every instance gets the copy). Copies the selected effective transform, including pending moves"), CommandSpec::new("component.paste_new", "Paste New", paste_new).icon("paste").params("component: independently copy its nested definition graph and history, materials and styles; occurrence?, path?: selected source placement; parent?: component the copy goes in (default: the source placement's; not the component or one inside it); in_place?, parent_path?: as occurrence.copy; translate?: [x,y,z]. Reassigned external bodies become base geometry; retained external links and omitted boundary joints are reported"), CommandSpec::new("occurrence.move", "Move Occurrence", move_occurrence).icon("move") - .params("occurrence; path?: exact placement; space?: local|world (default local); translate?: [x,y,z]; axis?: [x,y,z], angle?: radians or \"30 deg\", origin?: [x,y,z]; matrix?: [12 or 16 numbers] (instead of translate/axis/angle/origin: a rigid move as a row-major 3x4 rotation-and-translation or 4x4 matrix, flat or as rows; one that is a rotation only up to rounding is orthonormalised, a scale, shear or mirror is refused); tolerance?: largest accepted |RᵀR − I| (default 1e-9, at most 1e-3); capture?: bool (default true; false keeps it pending until Capture Position); move_grounded?: bool (default false: a grounded occurrence refuses; true moves it and it stays grounded). Nested occurrence definitions remain shared"), + .params("occurrence; path?: exact placement; space?: local|world (default local); translate?: [x,y,z]; axis?: [x,y,z], angle?: radians or \"30 deg\", origin?: [x,y,z]; matrix?: [12 or 16 numbers] (instead of translate/axis/angle/origin: a rigid move as a row-major 3x4 rotation-and-translation or 4x4 matrix, flat or as rows; one that is a rotation only up to rounding is orthonormalised, a scale, shear or mirror is refused); tolerance?: largest accepted |RᵀR − I| (default 1e-9, at most 1e-3); capture?: bool (default true; false keeps it pending until Capture Position); move_grounded?: bool (default false: a grounded occurrence refuses; true moves it and it stays grounded). Nested occurrence definitions remain shared. Replies {occurrence, transform, pending}; when joints hold it to something fixed and put it back, transform is where it is and the reply adds reverted: true and a warning naming the joint"), CommandSpec::new("occurrence.drag", "Drag Occurrence", drag::drag).icon("move").params( "occurrence or path: exact placement; at: [x,y,z] world point on it; point: [x,y,z] where it goes. Moves the placement by \ point - at (with any chain of joints it is in that is joined to nothing grounded) and captures its position; other pending \ @@ -730,6 +730,9 @@ fn move_occurrence(s: &mut Session, p: &Value) -> Result { if let Some(x) = s.doc_mut().occurrences.iter_mut().find(|x| x.id == id) { x.transform = next; } + if !s.doc.assembly.joints.is_empty() { + return Ok(after_joints(s, p, id, next, cmd)); + } } else { s.pending_moves.insert(id, next); s.revision += 1; @@ -737,6 +740,46 @@ fn move_occurrence(s: &mut Session, p: &Value) -> Result { Ok(json!({"occurrence": id, "transform": next, "pending": !bool_(p, "capture").unwrap_or(true)})) } +/// The reply to a captured move once the joints are solved (#11723): a part its joints hold to +/// something fixed is put back by that solve, so the reply gives where it is now, says +/// `reverted`, and names the joint and what to change instead. +fn after_joints(s: &mut Session, p: &Value, id: u64, moved: Mat, cmd: &str) -> Value { + s.refresh(); + let now = s.doc.occurrences.iter().find(|o| o.id == id).map_or(moved, |o| o.transform); + let same = now.iter().flatten().zip(moved.iter().flatten()).all(|(a, b)| (a - b).abs() <= 1e-6); + if same { + return json!({"occurrence": id, "transform": now, "pending": false}); + } + let name = s.doc.occurrences.iter().find(|o| o.id == id).map(|o| o.name.clone()).unwrap_or_default(); + // The placement the caller named, else the first one: a part in a component placed more + // than once has no path of its own, and its joints hold it alike in every placement. + let path = if p.get("path").is_some() { + occurrence_path(s, p, id, cmd).ok() + } else { + let parent = s.doc.occurrences.iter().find(|o| o.id == id).map_or(0, |o| o.parent); + let mut path = s.doc.first_path(parent); + path.push(id); + Some(path) + }; + let why = path.and_then(|path| s.drag_group(&path).err()).map(|e| format!("{e}. ")).unwrap_or_else(|| { + // No one joint to blame (a hinge's stored value, say): name the joints on the part. + let on: Vec<&str> = s + .doc + .assembly + .joints + .iter() + .filter(|j| !j.suppressed && (j.a.occurrence == id || j.b.occurrence == id)) + .map(|j| j.name.as_str()) + .collect(); + if on.is_empty() { String::new() } else { format!("Joints on {name}: {}. ", on.join(", ")) } + }); + let warning = format!( + "{why}The joints put {name} back where they hold it. To move it, change the joint (joint.edit for its offset or angle, \ + joint.drive for a hinge or slider value), move what it is held to, or use joint.reach" + ); + json!({"occurrence": id, "transform": now, "pending": false, "reverted": true, "warning": warning}) +} + fn ground(s: &mut Session, p: &Value) -> Result { let cmd = "occurrence.ground"; let id = occurrence(s, p, cmd)?; diff --git a/crates/engine/src/cmd/component_drag_tests.rs b/crates/engine/src/cmd/component_drag_tests.rs index bc0629fa7..599598c88 100644 --- a/crates/engine/src/cmd/component_drag_tests.rs +++ b/crates/engine/src/cmd/component_drag_tests.rs @@ -287,3 +287,66 @@ fn a_unique_part_can_drag_a_chain_through_a_shared_peer() { run(&mut s, "joint.solve", json!({})); assert_eq!(s.doc.to_json(), before); } + +/// #11723: Move Occurrence on a part that rigid joints hold to something grounded is put back by +/// the joint solve after it. The move still succeeds, but its reply says it was put back, gives +/// where the part is, and names the joint. A free chain follows the move, as before, and says +/// nothing. +#[test] +fn a_move_its_joints_undo_says_so() { + let setups: [(&str, fn(&mut Session, u64, u64, u64)); 4] = [ + ("free chain", |_, _, _, _| {}), + ("plate held to base", |s, base, plate, _| { + run(s, "joint.as_built", json!({"type": "rigid", "a": base, "b": plate, "name": "Mount"})); + }), + ("motor held to base", |s, base, _, motor| { + run(s, "joint.as_built", json!({"type": "rigid", "a": base, "b": motor, "name": "Mount"})); + }), + ("motor grounded", |s, _, _, motor| { + run(s, "occurrence.ground", json!({"occurrence": motor, "grounded": true})); + }), + ]; + for (case, setup) in setups { + let mut s = Session::default(); + let (_, base) = part(&mut s, "Base", [0.0, 0.0, 0.0]); + let (_, plate) = part(&mut s, "Plate", [20.0, 0.0, 0.0]); + let (_, motor) = part(&mut s, "Motor", [20.0, 0.0, 10.0]); + run(&mut s, "joint.as_built", json!({"type": "rigid", "a": plate, "b": motor, "name": "Bolted"})); + setup(&mut s, base, plate, motor); + let (plate_at, motor_at) = (origin(&s, &[plate]), origin(&s, &[motor])); + let r = run(&mut s, "occurrence.move", json!({"occurrence": plate, "space": "world", "translate": [0, 0, 30]})); + let reply_at = [r["transform"][3][0].as_f64().unwrap(), r["transform"][3][1].as_f64().unwrap(), r["transform"][3][2].as_f64().unwrap()]; + near(reply_at, origin(&s, &[plate])); + if case == "free chain" { + near(origin(&s, &[plate]), [plate_at[0], plate_at[1], plate_at[2] + 30.0]); + near(origin(&s, &[motor]), [motor_at[0], motor_at[1], motor_at[2] + 30.0]); + assert!(r.get("reverted").is_none() && r.get("warning").is_none(), "{case}: {r}"); + continue; + } + near(origin(&s, &[plate]), plate_at); + assert_eq!(r["reverted"], json!(true), "{case}: {r}"); + let warning = r["warning"].as_str().unwrap_or_default(); + let holder = if case == "motor grounded" { "Bolted" } else { "Mount" }; + assert!(warning.contains(holder) && warning.contains("joint.reach") && warning.contains("joint.edit"), "{case}: {warning}"); + } +} + +/// #11723: a part inside an assembly placed twice has no path of its own. When its joints put it +/// back, the warning still names the joint that holds it. +#[test] +fn a_move_its_joints_undo_names_the_joint_in_a_repeated_assembly() { + let mut s = Session::default(); + let (_, base) = part(&mut s, "Base", [0.0, 0.0, 0.0]); + let arm = run(&mut s, "component.create", json!({"name": "Arm"})); + let (arm_component, arm_at) = (arm["component"].as_u64().unwrap(), arm["occurrence"].as_u64().unwrap()); + run(&mut s, "component.activate", json!({"component": arm_component})); + let plate = run(&mut s, "component.create", json!({"name": "Plate"}))["occurrence"].as_u64().unwrap(); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [20, 0, 0]})); + run(&mut s, "component.activate", json!({"component": "root"})); + run(&mut s, "joint.as_built", json!({"type": "rigid", "a": base, "b": plate, "name": "Mount"})); + run(&mut s, "occurrence.copy", json!({"occurrence": arm_at, "translate": [0, 50, 0]})); + let r = run(&mut s, "occurrence.move", json!({"occurrence": plate, "translate": [0, 0, 30]})); + assert_eq!(r["reverted"], json!(true), "{r}"); + let warning = r["warning"].as_str().unwrap_or_default(); + assert!(warning.contains("Mount"), "the warning names the joint: {warning}"); +}