Skip to content

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

Fix stored world-axis inputs for revolve (#7179) (#7406)

Colin Faulkinghamb1cf66b62800
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index e260ef51d6..7ff7c1e154 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -38,7 +38,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("solid.revolve", "Revolve", revolve)
.at("SOLID", "CREATE")
.icon("revolve")
- .params("axis: sketch line id | x | y (sketch axes) | X|Y|Z (world) | construction axis name (followed when edited) | {origin, dir} | {edge|face: [x,y,z]} (a straight or circular edge, or a cylinder, cone or torus face: about its axis, followed as the model changes); angle?: expr (default 360 deg); angle2?: expr (Two Sides: the other side's angle); direction?: symmetric (angle split either side, independent of axis direction); convention?: sketch_normal|legacy_axis_start (default sketch_normal; legacy retains older angular starts); to?: [x,y,z] (To Object: turn until that face, vertex or point); sketch?, profiles? | face: [x,y,z] (a planar body face as the profile) and face_body?: name (whose face, where faces of several bodies meet), operation?, targets?, name?, body_name?"),
+ .params("axis: sketch line id | x | y (sketch axes) | X|Y|Z (world) | {type: world, axis: X|Y|Z} (as returned by timeline.inspect) | construction axis name (followed when edited) | {origin, dir} | {edge|face: [x,y,z]} (a straight or circular edge, or a cylinder, cone or torus face: about its axis, followed as the model changes); angle?: expr (default 360 deg); angle2?: expr (Two Sides: the other side's angle); direction?: symmetric (angle split either side, independent of axis direction); convention?: sketch_normal|legacy_axis_start (default sketch_normal; legacy retains older angular starts); to?: [x,y,z] (To Object: turn until that face, vertex or point); sketch?, profiles? | face: [x,y,z] (a planar body face as the profile) and face_body?: name (whose face, where faces of several bodies meet), operation?, targets?, name?, body_name?"),
CommandSpec::new("solid.sweep", "Sweep", sweep)
.at("SOLID", "CREATE")
.icon("sweep")
@@ -1242,7 +1242,8 @@ fn revolve(s: &mut Session, p: &Value) -> Result<Value> {
}
/// A revolve axis of `sketch` from its parameter: a sketch line id, the sketch's x or y axis, a
-/// world axis, a construction axis by name (followed when it is edited), {origin, dir}, or an edge
+/// world axis (also as saved: {type: world, axis: X|Y|Z}), a construction axis by name
+/// (followed when it is edited), {origin, dir}, or an edge
/// or round face of the model ({edge|face: [x,y,z]}), which the revolve follows as the model
/// changes. A line of the sketch wins over a construction axis of the same name.
pub(super) fn axis_param(s: &Session, sketch: u64, p: &Value, cmd: &str) -> Result<AxisRef> {
@@ -1250,6 +1251,11 @@ pub(super) fn axis_param(s: &Session, sketch: u64, p: &Value, cmd: &str) -> Resu
return Ok(AxisRef::Model { geo });
}
if let Some(o) = p.get("axis").filter(|v| v.is_object()) {
+ if str_(o, "type") == Some("world") {
+ let axis =
+ str_(o, "axis").filter(|a| matches!(*a, "X" | "Y" | "Z")).ok_or_else(|| bad(cmd, "world axis requires `axis` to be X, Y or Z"))?;
+ return Ok(AxisRef::World { axis: axis.into() });
+ }
let origin = o.get("origin").and_then(vec3).unwrap_or(Vec3::ZERO);
let dir = o.get("dir").and_then(vec3).ok_or_else(|| bad(cmd, "axis needs `dir`"))?;
return Ok(AxisRef::Line { origin: Position(origin), dir: Dir(dir) });
diff --git a/crates/engine/src/cmd/revolve_convention_tests.rs b/crates/engine/src/cmd/revolve_convention_tests.rs
index 4e1d55de48..a41ff77189 100644
--- a/crates/engine/src/cmd/revolve_convention_tests.rs
+++ b/crates/engine/src/cmd/revolve_convention_tests.rs
@@ -360,3 +360,57 @@ fn construction_axis_revolve_rejects_invalid_axes_atomically() {
assert!(s.model.state().bodies.is_empty());
}
}
+
+#[test]
+fn world_axis_from_timeline_inspect_round_trips() {
+ for (axis, radial, axial) in [("X", Vec3::Y, Vec3::X), ("Y", Vec3::X, Vec3::Y), ("Z", Vec3::X, Vec3::Z)] {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"name":"Profile","plane":{"origin":[0,0,0],"x_dir":radial,"y_dir":axial}}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0":[2,0],"p1":[4,10]}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.revolve", json!({"sketch":"Profile","axis":axis,"angle":"90 deg","operation":"new","name":"Turn"}));
+ let inspected = run(&mut s, "timeline.inspect", json!({"feature":"Turn"}));
+ let stored_axis = inspected["definition"]["axis"].clone();
+ assert_eq!(stored_axis, json!({"type":"world","axis":axis}));
+ let original = s.doc.find_feature("Turn").unwrap().kind.clone();
+ let original_measure = solidcraft_kernel::measure(&s.model.state().bodies[0].body).unwrap();
+ run(&mut s, "edit.undo", json!({}));
+ assert!(s.model.state().bodies.is_empty());
+
+ run(&mut s, "solid.revolve", json!({"sketch":"Profile","axis":stored_axis,"angle":"90 deg","operation":"new","name":"Turn"}));
+ assert_eq!(s.doc.find_feature("Turn").unwrap().kind, original);
+ let state = s.model.state();
+ assert_eq!(state.bodies.len(), 1);
+ let measure = solidcraft_kernel::measure(&state.bodies[0].body).unwrap();
+ // A quarter of an annular cylinder: pi * (4^2 - 2^2) * 10 / 4.
+ assert!((measure.volume - 30.0 * PI).abs() < 0.03, "{axis}: {}", measure.volume);
+ assert!((measure.volume - original_measure.volume).abs() < 1e-6);
+ assert!(measure.centroid.dist(original_measure.centroid) < 1e-6);
+ }
+}
+
+#[test]
+fn invalid_world_axis_objects_are_rejected_atomically() {
+ let mut s = Session::default();
+ profile(&mut s, false);
+ let before = s.doc.to_json();
+ for axis in [
+ json!({"type":"world"}),
+ json!({"type":"world","axis":null}),
+ json!({"type":"world","axis":1}),
+ json!({"type":"world","axis":[0,1,0]}),
+ json!({"type":"world","axis":{}}),
+ json!({"type":"world","axis":""}),
+ json!({"type":"world","axis":"x"}),
+ json!({"type":"world","axis":"y"}),
+ json!({"type":"world","axis":"z"}),
+ json!({"type":"world","axis":"W","dir":[0,1,0]}),
+ ] {
+ let error = s.execute("solid.revolve", &json!({"sketch":"Profile","axis":axis,"angle":"90 deg","operation":"new"})).unwrap_err();
+ let message = error.to_string();
+ assert!(matches!(error, crate::EngineError::BadParams { .. }), "{axis}: {message}");
+ assert!(message.contains("world axis") && message.contains("X, Y or Z"), "{axis}: {message}");
+ assert_eq!(s.doc.to_json(), before);
+ assert!(s.model.state().bodies.is_empty());
+ }
+}
diff --git a/crates/engine/src/cmd/surface.rs b/crates/engine/src/cmd/surface.rs
index 24457190b9..4c1e4423f1 100644
--- a/crates/engine/src/cmd/surface.rs
+++ b/crates/engine/src/cmd/surface.rs
@@ -43,7 +43,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("surface.revolve", "Surface Revolve", surface_revolve)
.at("SURFACE", "CREATE")
.icon("surface_revolve")
- .params("sketch?: id|name; curves?: [curve ids] and/or profiles? (as surface.extrude); axis: sketch line id|x|y|X|Y|Z|construction axis name|{origin, dir} | {edge|face: [x,y,z]} (a straight or circular edge, or a cylinder, cone or torus face: about its axis, followed as the model changes); angle?: expr (default 360 deg); angle2?: expr (the other side); symmetric?: bool; name?, body_name? (a surface body, no caps; a solid where a full turn closes it)"),
+ .params("sketch?: id|name; curves?: [curve ids] and/or profiles? (as surface.extrude); axis: sketch line id|x|y|X|Y|Z|{type: world, axis: X|Y|Z}|construction axis name|{origin, dir} | {edge|face: [x,y,z]} (a straight or circular edge, or a cylinder, cone or torus face: about its axis, followed as the model changes); angle?: expr (default 360 deg); angle2?: expr (the other side); symmetric?: bool; name?, body_name? (a surface body, no caps; a solid where a full turn closes it)"),
CommandSpec::new("surface.sweep", "Surface Sweep", surface_sweep)
.at("SURFACE", "CREATE")
.icon("surface_sweep")