Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

motion.list gives study keys in motion.study units (#9741) (#9760)

Dahak Builds646eb97eaf79
diff --git a/crates/engine/src/cmd/motion.rs b/crates/engine/src/cmd/motion.rs
index 2580e8c7b9..0e1e65a922 100644
--- a/crates/engine/src/cmd/motion.rs
+++ b/crates/engine/src/cmd/motion.rs
@@ -979,7 +979,30 @@ fn keep_sampled_trace(
}
fn list_studies(s: &mut Session, _p: &Value) -> Result<Value> {
- Ok(json!({"studies": s.doc.assembly.studies, "traces": associative::list(s)}))
+ let studies: Vec<Value> = s.doc.assembly.studies.iter().map(|m| study_json(s, m)).collect();
+ Ok(json!({"studies": studies, "traces": associative::list(s)}))
+}
+
+/// A study as `motion.list` gives it: key values in the units `motion.study` takes — degrees
+/// for angles, mm for distances, like `joint.list`'s values — each key with its joint's name,
+/// so a listed study can be fed back to `motion.study` unchanged.
+fn study_json(s: &Session, m: &MotionStudy) -> Value {
+ let keys: Vec<Value> = m
+ .keys
+ .iter()
+ .map(|k| {
+ let joint = s.doc.assembly.joints.iter().find(|j| j.id == k.joint);
+ let angle = joint.is_some_and(|j| j.kind.is_angle(k.index));
+ json!({
+ "joint": k.joint,
+ "joint_name": joint.map(|j| j.name.clone()).unwrap_or_else(|| k.joint.to_string()),
+ "index": k.index,
+ "unit": if angle { "deg" } else { "mm" },
+ "points": k.points.iter().map(|(step, v)| json!([step, if angle { v.to_degrees() } else { *v }])).collect::<Vec<_>>(),
+ })
+ })
+ .collect();
+ json!({"id": m.id, "name": m.name, "steps": m.steps, "keys": keys})
}
fn delete_study(s: &mut Session, p: &Value) -> Result<Value> {
diff --git a/crates/engine/src/cmd/motion_tests.rs b/crates/engine/src/cmd/motion_tests.rs
index f4e88fb36e..558dbb48d9 100644
--- a/crates/engine/src/cmd/motion_tests.rs
+++ b/crates/engine/src/cmd/motion_tests.rs
@@ -639,6 +639,35 @@ fn motion_study_plays_and_exports() {
assert_eq!(run(&mut s, "motion.list", json!({}))["studies"].as_array().map(Vec::len), Some(0));
}
+/// Regression (#9741): `motion.study` takes degrees for angle keys, so `motion.list` must give
+/// the keys back in those same units — feeding a listed study straight back to `motion.study`
+/// must not turn a 90° key into 1.57°.
+#[test]
+fn motion_list_round_trips_key_units_through_motion_study() {
+ let hinge_deg = |s: &mut Session| {
+ run(s, "joint.list", json!({}))["joints"]
+ .as_array()
+ .and_then(|a| a.iter().find(|j| j["name"] == "Hinge").cloned())
+ .and_then(|j| j["values"][0]["value"].as_f64())
+ .unwrap_or(f64::NAN)
+ };
+ let mut s = hinged_plates(0.0);
+ run(&mut s, "motion.study", json!({"name": "Turn", "steps": 10, "keys": [{"joint": "Hinge", "points": [[0, 0], [10, 90]]}]}));
+ run(&mut s, "motion.play", json!({"study": "Turn", "step": 10}));
+ assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "playing step 10 gave {}°", hinge_deg(&mut s));
+ let listed = run(&mut s, "motion.list", json!({}));
+ let key = &listed["studies"][0]["keys"][0];
+ assert_eq!(key["joint_name"], "Hinge");
+ assert_eq!(key["unit"], "deg");
+ assert!((key["points"][1][1].as_f64().unwrap_or(f64::NAN) - 90.0).abs() < 1e-6, "{listed}");
+ // The listed keys go back to `motion.study` unchanged and the study still plays 90°.
+ let keys = listed["studies"][0]["keys"].clone();
+ run(&mut s, "motion.study", json!({"study": "Turn", "keys": keys}));
+ run(&mut s, "motion.play", json!({"study": "Turn", "step": 0}));
+ run(&mut s, "motion.play", json!({"study": "Turn", "step": 10}));
+ assert!((hinge_deg(&mut s) - 90.0).abs() < 1e-6, "the round-tripped study played {}°", hinge_deg(&mut s));
+}
+
#[test]
fn exploded_views_show_without_changing_the_design() {
let mut s = rail();