* claim: #7878 * Sketch: the rounded rectangle completes from a vertex start point (#7878)
This commit is contained in:
@@ -1159,14 +1159,24 @@ fn rect_rounded(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
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"));
|
||||
// The fillet rounds each corner by the two sides meeting there, not by the corner point: a
|
||||
// corner that lands on another curve's end is shared with it, and rounding the point would
|
||||
// weld the two together, leaving the corner on three curves (#7878).
|
||||
let sides: Vec<String> =
|
||||
["bottom", "right", "top", "left"].iter().filter_map(|k| out["sides"].get(*k).and_then(Value::as_str).map(str::to_string)).collect();
|
||||
if sides.len() != 4 {
|
||||
return Err(bad(cmd, "the rectangle's sides are missing"));
|
||||
}
|
||||
let mut fillet = json!({"points": corners, "radius": r_expr});
|
||||
let (bottom, right, top, left) = (&sides[0], &sides[1], &sides[2], &sides[3]);
|
||||
let mut fillet = json!({
|
||||
"points": [
|
||||
{"a": bottom, "b": left},
|
||||
{"a": bottom, "b": right},
|
||||
{"a": right, "b": top},
|
||||
{"a": top, "b": left},
|
||||
],
|
||||
"radius": r_expr,
|
||||
});
|
||||
if let Some(sketch) = p.get("sketch") {
|
||||
fillet["sketch"] = sketch.clone();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ pub static COMMANDS: &[CommandSpec] = &[
|
||||
.at("SKETCH", "MODIFY")
|
||||
.icon("sketch_fillet")
|
||||
.enabled(in_sketch)
|
||||
.params(concat!("point: corner shared by two curves (\"l1.end\") | points: [corners] (all rounded alike: one radius, the rest equal) | a, b: two lines or arcs meeting at a corner; radius: expr. A corner that dimensions or constraints measure to stays as a virtual sharp corner on both curves' extensions (`virtual_corners`; the moved ones in `moved_to_virtual_corner`)", created_doc!())),
|
||||
.params(concat!("point: corner shared by two curves (\"l1.end\") | points: [corners] (all rounded alike: one radius, the rest equal; a corner is a point reference or {a, b}, the two curves meeting there) | a, b: two lines or arcs meeting at a corner; radius: expr. A corner that dimensions or constraints measure to stays as a virtual sharp corner on both curves' extensions (`virtual_corners`; the moved ones in `moved_to_virtual_corner`)", created_doc!())),
|
||||
CommandSpec::new("sketch.chamfer.equal_distance", "Equal Distance Chamfer", chamfer_equal)
|
||||
.at("SKETCH", "MODIFY")
|
||||
.icon("sketch_chamfer")
|
||||
@@ -1305,12 +1305,21 @@ fn cut_corner(
|
||||
Ok((fix(ta), fix(tb), pc, kept.unwrap_or_default()))
|
||||
}
|
||||
|
||||
/// A `points` entry: the two curves meeting at the corner (`a` and `b`) as given, or a point
|
||||
/// reference to round.
|
||||
fn corner_arg(q: &Value) -> Value {
|
||||
match q {
|
||||
Value::Object(o) if o.contains_key("a") || o.contains_key("b") || o.contains_key("point") => q.clone(),
|
||||
_ => json!({ "point": q }),
|
||||
}
|
||||
}
|
||||
|
||||
fn fillet(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cmd = "sketch.fillet";
|
||||
let r_expr = req_expr(cmd, p, "radius")?;
|
||||
// Several corners at once (`points`), each like a single `point`.
|
||||
let corners: Vec<Value> = match p.get("points").and_then(Value::as_array) {
|
||||
Some(a) if !a.is_empty() && a.len() <= 500 => a.iter().map(|q| json!({ "point": q })).collect(),
|
||||
Some(a) if !a.is_empty() && a.len() <= 500 => a.iter().map(corner_arg).collect(),
|
||||
Some(_) => return Err(bad(cmd, "`points` must list 1…500 corners")),
|
||||
None => vec![p.clone()],
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Rounded Rectangle (#891): a 2-point rectangle whose corners are filleted alike, in one step.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use solidcraft_sketch::ConstraintKind;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
@@ -100,6 +101,34 @@ fn a_radius_too_large_or_not_positive_is_refused_and_draws_nothing() {
|
||||
assert!(s.execute("sketch.rectangle.rounded", &json!({"p0": [0, 0], "p1": [40, 20]})).is_err(), "radius is required");
|
||||
}
|
||||
|
||||
/// #7878: a rounded rectangle whose corner lands on an existing point (the far end of a line)
|
||||
/// completes, as the plain rectangle does, and the vertex stays tied to the corner.
|
||||
#[test]
|
||||
fn a_rounded_rectangle_completes_from_an_existing_vertex() {
|
||||
let mut s = sketch();
|
||||
let line = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0]]}))["curves"])[0].clone();
|
||||
let r = match s.execute("sketch.rectangle.rounded", &json!({"p0": [40, 0], "p1": [60, 20], "radius": 2})) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("the rounded rectangle refused the vertex: {e}"),
|
||||
};
|
||||
assert_eq!(ids(&r["arcs"]).len(), 4, "{r}");
|
||||
assert_eq!(ids(&r["curves"]).len(), 8, "{r}");
|
||||
let sk = solved(&s);
|
||||
// The vertex stays where it was, and the line's end stays tied to the corner.
|
||||
let at_vertex = |i: usize| sk.point(i).is_some_and(|p| (p.x - 40.0).abs() < 1e-6 && p.y.abs() < 1e-6);
|
||||
let end = sk.resolve_point(&format!("{line}.end")).expect("the line's end");
|
||||
assert!(at_vertex(end), "the line's end stays at the vertex");
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Coincident { p, q } if at_vertex(p) && at_vertex(q))),
|
||||
"the vertex stays tied to the corner"
|
||||
);
|
||||
// The shape is whole: a 20 × 20 rectangle with 2 mm corners.
|
||||
let si = run(&mut s, "sketch.inspect", json!({}));
|
||||
let area = si["profiles"][0]["area_mm2"].as_f64().unwrap();
|
||||
let want = 20.0 * 20.0 - (4.0 - std::f64::consts::PI) * 4.0;
|
||||
assert!((area - want).abs() < 1e-6, "area {area}, want {want}: {si}");
|
||||
}
|
||||
|
||||
/// Typed width and height (the drawing tool's boxes) dimension the overall size, between
|
||||
/// opposite sides, not the straight part of a side.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user