Skip to content

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

sketch.create: reject a non-numeric custom plane origin (#3248) (#3252)

rlartot885d2aafbb01
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index bbee1a0e56..96d9bc5b00 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -29,7 +29,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("sketch.create", "Create Sketch", create_sketch)
.at("SOLID", "CREATE")
.icon("sketch")
- .params("plane: XY|XZ|YZ | construction plane name or id | {origin, x_dir, y_dir} | {face: [world x,y,z], body?: exact world body, index?: face index, path?: complete occurrence ids, fixed?: bool}; new face attachments require a unique certified source (a flat face of a sheet metal body attaches to its panel instead, in the flat pattern's coordinates), fixed:true explicitly freezes the selected plane; offset?: expr, flip?: bool | \"x\" | \"y\" (not on a face: sketch the plane from its other side, so the normal and the default extrude direction are reversed; a half turn about the plane's own x or y axis, true picks the one keeping the sketch's y axis upward; offset still moves along the unflipped normal), project_edges?: bool, name?"),
+ .params("plane: XY|XZ|YZ | construction plane name or id | {origin?: [x,y,z] numbers in mm, default [0,0,0] (not an expression), x_dir, y_dir} | {face: [world x,y,z], body?: exact world body, index?: face index, path?: complete occurrence ids, fixed?: bool}; new face attachments require a unique certified source (a flat face of a sheet metal body attaches to its panel instead, in the flat pattern's coordinates), fixed:true explicitly freezes the selected plane; offset?: expr, flip?: bool | \"x\" | \"y\" (not on a face: sketch the plane from its other side, so the normal and the default extrude direction are reversed; a half turn about the plane's own x or y axis, true picks the one keeping the sketch's y axis upward; offset still moves along the unflipped normal), project_edges?: bool, name?"),
CommandSpec::new("sketch.edit", "Edit Sketch", edit_sketch).noundo().icon("sketch").params("sketch: id or name"),
CommandSpec::new("sketch.finish", "Finish Sketch", finish_sketch).at("SKETCH", "FINISH SKETCH").icon("finish").enabled(in_sketch).noundo(),
CommandSpec::new("sketch.line", "Line", draw_line)
@@ -626,7 +626,13 @@ pub(super) fn plane_ref(s: &Session, p: &Value, cmd: &str) -> Result<PlaneRef> {
if o.contains_key("face") {
face::capture(s, v, cmd)?
} else {
- let origin = v.get("origin").and_then(vec3).unwrap_or(Vec3::ZERO);
+ // A missing origin is the world origin; one that is given but is not three numbers
+ // (an expression, a number with a unit) is an error, not a silent [0, 0, 0].
+ let origin = match v.get("origin") {
+ None | Some(Value::Null) => Vec3::ZERO,
+ Some(o) => vec3(o)
+ .ok_or_else(|| bad(cmd, "plane `origin` must be three numbers [x, y, z] in mm (expressions are not evaluated here)"))?,
+ };
let x = v.get("x_dir").and_then(vec3).ok_or_else(|| bad(cmd, "plane needs x_dir"))?;
let y = v.get("y_dir").and_then(vec3).ok_or_else(|| bad(cmd, "plane needs y_dir"))?;
PlaneRef::Custom { plane: Plane::new(origin, x, y).ok_or_else(|| bad(cmd, "degenerate plane"))? }
diff --git a/crates/engine/src/cmd/sketch_face_tests.rs b/crates/engine/src/cmd/sketch_face_tests.rs
index 42a0e129b7..23ee3ee471 100644
--- a/crates/engine/src/cmd/sketch_face_tests.rs
+++ b/crates/engine/src/cmd/sketch_face_tests.rs
@@ -698,3 +698,28 @@ fn a_face_next_to_plane_is_refused_not_ignored() {
let r = run(&mut s, "sketch.create", json!({"plane": {"face": [10, 10, 10]}}));
assert_eq!(r["attachment"], "exact_face", "{r}");
}
+
+/// #3248: a custom plane's `origin` that is present but not three numbers (an expression such as
+/// "w/2", a number with a unit, too few values) became [0, 0, 0] without a word. It is refused,
+/// like a bad `x_dir`, and nothing is added. A missing origin is still the world origin.
+#[test]
+fn a_custom_plane_origin_that_is_not_numbers_is_refused_not_zeroed() {
+ let mut s = Session::default();
+ run(&mut s, "parameters.add", json!({"name": "w", "expression": "40 mm"}));
+ let features = s.doc.features.len();
+ for origin in [json!(["w/2", 0, 0]), json!(["20 mm", 0, 0]), json!([20, 0]), json!("w"), json!([20, 0, null])] {
+ let p = json!({"plane": {"origin": origin, "x_dir": [0, 1, 0], "y_dir": [0, 0, 1]}});
+ let e = s.execute("sketch.create", &p).unwrap_err().to_string();
+ assert!(e.contains("origin"), "{p}: {e}");
+ }
+ assert_eq!(s.doc.features.len(), features);
+ assert!(s.active_sketch.is_none());
+ let origin_of = |s: &mut Session, plane: Value| {
+ run(s, "sketch.create", json!({"plane": plane}));
+ let r = run(s, "sketch.inspect", json!({}));
+ run(s, "sketch.finish", json!({}));
+ r["plane"]["origin"].clone()
+ };
+ assert_eq!(origin_of(&mut s, json!({"origin": [20, 0, 0], "x_dir": [0, 1, 0], "y_dir": [0, 0, 1]})), json!([20.0, 0.0, 0.0]));
+ assert_eq!(origin_of(&mut s, json!({"x_dir": [0, 1, 0], "y_dir": [0, 0, 1]})), json!([0.0, 0.0, 0.0]));
+}