Revolve: an axis origin written as an expression is no longer read as zero (#10252)
* fix: Revolve axis origin takes expressions instead of silently zero - AxisRef::Line origin is a PointExpr evaluated with parameter values in the design unit (format note in pending) - An unreadable origin or dir is a BadParams error instead of (0, 0, 0) - Edit Feature keeps the saved expressions of an untouched origin or dir, for solid and surface revolves, in moved components too - Face role names stay stable with a parameter-driven axis * chore: Re-run PR checks after the description fix * fix: Edit Feature of a revolve maps nothing where nothing is placed - unplaced designs skip world_state and the pick table, as before the merge - the untouched-edit test checks that OK applied the edit (CodeRabbit) --------- Co-authored-by: P'tite Tête <asso@ptitetete.com>
This commit is contained in:
@@ -43,7 +43,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) | {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?, touching?, 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} (origin: numbers or length expressions, followed when parameters change) | {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?, touching?, targets?, name?, body_name?"),
|
||||
CommandSpec::new("solid.sweep", "Sweep", sweep)
|
||||
.at("SOLID", "CREATE")
|
||||
.icon("sweep")
|
||||
@@ -1335,7 +1335,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 (also as saved: {type: world, axis: X|Y|Z}), a construction axis by name
|
||||
/// (followed when it is edited), {origin, dir}, or an edge
|
||||
/// (followed when it is edited), {origin, dir} (each origin coordinate a number or an expression over
|
||||
/// the parameters, which the axis follows), 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> {
|
||||
@@ -1348,9 +1349,12 @@ pub(super) fn axis_param(s: &Session, sketch: u64, p: &Value, cmd: &str) -> Resu
|
||||
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) });
|
||||
let origin = point_param(s, o, "origin", cmd)?.unwrap_or_else(|| solidcraft_doc::point_expr(Vec3::ZERO));
|
||||
let dir = match o.get("dir") {
|
||||
None => return Err(bad(cmd, "axis needs `dir`")),
|
||||
Some(v) => vec3(v).ok_or_else(|| bad(cmd, "axis `dir` must be a direction [x, y, z] of numbers"))?,
|
||||
};
|
||||
return Ok(AxisRef::Line { origin, dir: Dir(dir) });
|
||||
}
|
||||
match str_(p, "axis") {
|
||||
Some(a @ ("X" | "Y" | "Z")) => Ok(AxisRef::World { axis: a.into() }),
|
||||
|
||||
@@ -473,3 +473,74 @@ fn invalid_world_axis_objects_are_rejected_atomically() {
|
||||
assert!(s.model.state().bodies.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_axis_origin_takes_expressions_and_follows_parameters() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "parameters.add", json!({"name": "W", "expression": "40 mm"}));
|
||||
profile(&mut s, false);
|
||||
let volume = |s: &Session| solidcraft_kernel::measure(&s.model.state().bodies[0].body).unwrap().volume;
|
||||
let axis = json!({"origin": ["W / 2 - 20", 0, 0], "dir": [0, 1, 0]});
|
||||
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": axis, "name": "Turn", "operation": "new"}));
|
||||
// W / 2 - 20 is 0 mm: radii 2 to 4 about x = 0.
|
||||
assert!((volume(&s) - 120.0 * PI).abs() < 0.2, "{}", volume(&s));
|
||||
let FeatureKind::Revolve { axis: solidcraft_doc::AxisRef::Line { origin, .. }, .. } = &s.doc.find_feature("Turn").unwrap().kind else {
|
||||
panic!("not a line axis");
|
||||
};
|
||||
assert_eq!(origin[0].as_str(), "W / 2 - 20");
|
||||
let reopened = Document::from_json(&s.doc.to_json()).unwrap();
|
||||
assert_eq!(reopened.find_feature("Turn").unwrap().kind, s.doc.find_feature("Turn").unwrap().kind);
|
||||
// Moving the axis follows W: the origin is at x = 1, so the radii are 1 to 3.
|
||||
run(&mut s, "parameters.change", json!({"name": "W", "expression": "42 mm"}));
|
||||
assert!((volume(&s) - 80.0 * PI).abs() < 0.2, "{}", volume(&s));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_axis_refuses_an_origin_or_dir_it_cannot_read() {
|
||||
let mut s = Session::default();
|
||||
profile(&mut s, false);
|
||||
let before = s.doc.to_json();
|
||||
for axis in [
|
||||
json!({"origin": ["no_such_parameter", 0, 0], "dir": [0, 1, 0]}),
|
||||
json!({"origin": ["1 +", 0, 0], "dir": [0, 1, 0]}),
|
||||
json!({"origin": [0, 0], "dir": [0, 1, 0]}),
|
||||
json!({"origin": [null, 0, 0], "dir": [0, 1, 0]}),
|
||||
json!({"origin": [0, 0, 0], "dir": ["W", 1, 0]}),
|
||||
] {
|
||||
let error = s.execute("solid.revolve", &json!({"sketch": "Profile", "axis": axis, "operation": "new"})).unwrap_err();
|
||||
assert!(matches!(error, crate::EngineError::BadParams { .. }), "{axis}: {error}");
|
||||
assert!(!error.to_string().contains("sketch plane"), "{axis}: {error}");
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn surface_revolve_axis_origin_follows_a_parameter() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "parameters.add", json!({"name": "W", "expression": "40 mm"}));
|
||||
profile(&mut s, false);
|
||||
let axis = json!({"origin": ["W / 2 - 20", 0, 0], "dir": [0, 1, 0]});
|
||||
run(&mut s, "surface.revolve", json!({"sketch": "Profile", "axis": axis, "name": "Wall"}));
|
||||
let min_x = |s: &Session| s.model.state().bodies[0].body.mesh(&solidcraft_kernel::MeshTolerance::chord(1e-3)).unwrap().bounds().min.x;
|
||||
// W / 2 - 20 is 0 mm: the profile (x from 2 to 4) sweeps out to x = -4.
|
||||
assert!((min_x(&s) + 4.0).abs() < 0.05, "{}", min_x(&s));
|
||||
run(&mut s, "parameters.change", json!({"name": "W", "expression": "42 mm"}));
|
||||
// The axis is at x = 1: the farthest point is 3 from it.
|
||||
assert!((min_x(&s) + 2.0).abs() < 0.05, "{}", min_x(&s));
|
||||
let reopened = Document::from_json(&s.doc.to_json()).unwrap();
|
||||
assert_eq!(reopened.find_feature("Wall").unwrap().kind, s.doc.find_feature("Wall").unwrap().kind);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_parameter_driven_axis_origin_names_revolve_faces_as_the_numeric_origin_does() {
|
||||
let names = |origin: Value| {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "parameters.add", json!({"name": "W", "expression": "40 mm"}));
|
||||
profile(&mut s, false);
|
||||
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"origin": origin, "dir": [0, 1, 0]}, "operation": "new"}));
|
||||
solidcraft_doc::naming::face_names(&s.model.state().bodies[0]).to_vec()
|
||||
};
|
||||
let numeric = names(json!([20, 0, 0]));
|
||||
assert!(numeric.iter().all(|n| !n.contains(":face:")), "index names: {numeric:?}");
|
||||
assert_eq!(names(json!(["W / 2", 0, 0])), numeric);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,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|{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)"),
|
||||
.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} (origin: numbers or length expressions, followed when parameters change) | {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")
|
||||
|
||||
Reference in New Issue
Block a user