* Rounded Rectangle sketch tool (#891) A new SKETCH > CREATE command, sketch.rectangle.rounded, in the Rectangle family: two opposite corners and a corner radius make a 2-point rectangle whose four corners are filleted alike, as one undo step. The radius is one parameter (one radius dimension, the other arcs equal to it), so changing it changes all four corners. While drawing, Width, Height and Radius boxes take typed values; typed width and height dimension the overall size between opposite sides. Without a typed radius the corners get a fifth of the shorter side. Fixes #891 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Rounded Rectangle in the Simplified Chinese catalog Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
19bf854dc9
commit
97ff7578c2
15 files changed
+330
-9
No files matched your search
@@ -52,6 +52,11 @@ pub static COMMANDS: &[CommandSpec] = &[
|
||||
.icon("rect3")
|
||||
.enabled(in_sketch)
|
||||
.params(concat!("p0, p1: first edge; p2: point on the opposite edge", connect_doc!(), created_doc!())),
|
||||
CommandSpec::new("sketch.rectangle.rounded", "Rounded Rectangle", rect_rounded)
|
||||
.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!())),
|
||||
CommandSpec::new("sketch.rectangle.center", "Center Rectangle", rect_center)
|
||||
.at("SKETCH", "CREATE")
|
||||
.icon("rect_center")
|
||||
@@ -930,6 +935,60 @@ fn rect_two_point(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(rect_result(out, names, info))
|
||||
}
|
||||
|
||||
/// A 2-point rectangle with its corners filleted, as one undo step: the rectangle and the
|
||||
/// 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")?);
|
||||
let r_expr = expr(p, "radius").ok_or_else(|| bad(cmd, "`radius` must be a number or an expression"))?;
|
||||
let r = s.doc.eval(&r_expr, Kind::Length).map_err(|e| bad(cmd, format!("radius: {e}")))?;
|
||||
if !(r > 1e-9 && r < 1e8) {
|
||||
return Err(bad(cmd, "radius must be positive"));
|
||||
}
|
||||
let half = 0.5 * (a.x - b.x).abs().min((a.y - b.y).abs());
|
||||
if half > 1e-9 && r >= half - 1e-9 {
|
||||
return Err(bad(cmd, format!("the radius must be less than half the shorter side ({half:.3} mm)")));
|
||||
}
|
||||
let mut rect = p.clone();
|
||||
if let Some(o) = rect.as_object_mut() {
|
||||
o.remove("radius");
|
||||
}
|
||||
let mut scratch = s.scratch();
|
||||
let mut out = scratch.execute("sketch.rectangle.two_point", &rect)?;
|
||||
let corners: Vec<Value> = ["bottom_left", "bottom_right", "top_right", "top_left"]
|
||||
.iter()
|
||||
.filter_map(|k| out["corners"].get(*k).filter(|v| v.is_string()).cloned())
|
||||
.collect();
|
||||
if corners.len() != 4 {
|
||||
return Err(bad(cmd, "the rectangle's corners are missing"));
|
||||
}
|
||||
let mut fillet = json!({"points": corners, "radius": r_expr});
|
||||
if let Some(sketch) = p.get("sketch") {
|
||||
fillet["sketch"] = sketch.clone();
|
||||
}
|
||||
let rounded = scratch.execute("sketch.fillet", &fillet)?;
|
||||
// Session::execute records the wrapper's before snapshot. Nested history must not leak.
|
||||
scratch.undo.clear();
|
||||
s.accept_computed(scratch);
|
||||
let arcs = rounded.get("curves").cloned().unwrap_or_else(|| json!([]));
|
||||
if let Some(o) = out.as_object_mut() {
|
||||
// The sharp corners are gone (the lines end at the arcs now).
|
||||
o.remove("corners");
|
||||
if let (Some(Value::Array(curves)), Value::Array(extra)) = (o.get_mut("curves"), &arcs) {
|
||||
curves.extend(extra.iter().cloned());
|
||||
}
|
||||
if let (Some(Value::Array(cons)), Some(Value::Array(extra))) = (o.get_mut("constraints"), rounded.get("constraints")) {
|
||||
cons.extend(extra.iter().cloned());
|
||||
}
|
||||
o.insert("arcs".into(), arcs);
|
||||
o.insert("param".into(), rounded.get("param").cloned().unwrap_or(Value::Null));
|
||||
if let Some(info) = rounded.get("sketch") {
|
||||
o.insert("sketch".into(), info.clone());
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn rect_center(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "sketch.rectangle.center";
|
||||
let (c, k) = (req_vec2(cmd, p, "center")?, req_vec2(cmd, p, "corner")?);
|
||||
@@ -2325,6 +2384,10 @@ fn solve_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
#[path = "sketch_rect_tests.rs"]
|
||||
mod rect_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "sketch_rounded_rect_tests.rs"]
|
||||
mod rounded_rect_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "sketch_ux_tests.rs"]
|
||||
mod ux_tests;
|
||||
|
||||
Reference in new issue
Block a user