Skip to content

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

Rounded Rectangle: corners take parameter expressions, as a 2-point rectangle's do (#10956)

Rayf6e932f61af6
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 0c9444c10e..8aeee9c4bf 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -81,7 +81,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.at("SKETCH", "CREATE")
.icon("rect_rounded")
.enabled(in_sketch)
- .params(concat!("p0, p1: opposite corners [x,y]; radius: expr (the corner radius, less than half the shorter side); construction?. A 2-point rectangle whose four corners are filleted alike: one radius dimension (`param`), the other arcs equal to it. Returns sides {bottom, right, top, left} (line ids) and arcs (the corner arcs: bottom-left, bottom-right, top-right, top-left)", connect_doc!(), created_doc!())),
+ .params(concat!("p0, p1: opposite corners [x,y] (a coordinate may be an expression over the design's parameters: the corner follows them, as in `sketch.rectangle.two_point`); radius: expr (the corner radius, less than half the shorter side); construction?. A 2-point rectangle whose four corners are filleted alike: one radius dimension (`param`), the other arcs equal to it. Returns sides {bottom, right, top, left} (line ids) and arcs (the corner arcs: bottom-left, bottom-right, top-right, top-left)", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.rectangle.center", "Center Rectangle", rect_center)
.at("SKETCH", "CREATE")
.icon("rect_center")
@@ -1412,7 +1412,9 @@ fn rect_two_point(s: &mut Session, p: &Value) -> Result<Value> {
/// fillet run in a scratch session that replaces the design only when both succeed.
fn rect_rounded(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.rectangle.rounded";
- let (a, b) = (req_vec2(cmd, p, "p0")?, req_vec2(cmd, p, "p1")?);
+ // The corners may be typed as expressions: `sketch.rectangle.two_point` below places them, so
+ // here they are only worked out to check the radius against the sides.
+ let ((a, _), (b, _)) = (corner_arg(&s.doc, p, "p0", cmd)?, corner_arg(&s.doc, p, "p1", cmd)?);
let r_expr = req_expr(cmd, p, "radius")?;
let r = s.doc.eval(&r_expr, Kind::Length).map_err(|e| bad(cmd, format!("radius: {e}")))?;
if !(r > 1e-9 && r < 1e8) {
diff --git a/crates/engine/src/cmd/sketch_rounded_rect_tests.rs b/crates/engine/src/cmd/sketch_rounded_rect_tests.rs
index 023a6c1362..159230b1d2 100644
--- a/crates/engine/src/cmd/sketch_rounded_rect_tests.rs
+++ b/crates/engine/src/cmd/sketch_rounded_rect_tests.rs
@@ -146,3 +146,27 @@ fn typed_width_and_height_measure_the_whole_rounded_rectangle() {
assert!((area - want).abs() < 1e-6, "area {area}, want {want}: {si}");
assert_eq!(si["dof"], 2, "only the position is free: {si}");
}
+
+/// The corners take expressions over the design's parameters, as a 2-point rectangle's do: the
+/// rounded rectangle follows them, and the radius is checked against the sides they work out to.
+#[test]
+fn corners_typed_as_expressions_follow_their_parameters() {
+ let mut s = sketch();
+ run(&mut s, "parameters.add", json!({"name": "W", "expression": "40 mm"}));
+ run(&mut s, "parameters.add", json!({"name": "H", "expression": "20 mm"}));
+ let r = run(&mut s, "sketch.rectangle.rounded", json!({"p0": ["-W / 2", "-H / 2"], "p1": ["W / 2", "H / 2"], "radius": 3}));
+ assert_eq!(ids(&r["arcs"]).len(), 4, "{r}");
+ let area = |s: &mut Session| {
+ let si = run(s, "sketch.inspect", json!({}));
+ si["profiles"][0]["area_mm2"].as_f64().unwrap_or(f64::NAN)
+ };
+ let corners = (4.0 - std::f64::consts::PI) * 9.0;
+ let a = area(&mut s);
+ assert!((a - (40.0 * 20.0 - corners)).abs() < 1e-6, "area {a}");
+ run(&mut s, "parameters.change", json!({"name": "W", "expression": "70 mm"}));
+ let a = area(&mut s);
+ assert!((a - (70.0 * 20.0 - corners)).abs() < 1e-6, "area after W = 70 mm: {a}");
+ // A radius the worked-out sides cannot take is refused, as for numbers.
+ let too_big = s.execute("sketch.rectangle.rounded", &json!({"p0": [0, 0], "p1": ["W", "H / 4"], "radius": 3}));
+ assert!(too_big.is_err(), "radius 3 mm on a 5 mm side: {too_big:?}");
+}