Skip to content

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

Sketch slots: provide typed dimension inputs for every variant (#2423) (#4318)

Colin Faulkingham89c1f02850b1
diff --git a/crates/engine/src/cmd/sketch_create.rs b/crates/engine/src/cmd/sketch_create.rs
index b97d7dd4a4..6cbc9fd3c5 100644
--- a/crates/engine/src/cmd/sketch_create.rs
+++ b/crates/engine/src/cmd/sketch_create.rs
@@ -59,7 +59,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.at("SKETCH", "CREATE")
.icon("arc_slot_center")
.enabled(in_sketch)
- .params(concat!("center, start: centre arc start, end: direction of the centre arc end (counter-clockwise), width", created_doc!())),
+ .params(concat!("center, start: centre arc start, end: direction of the centre arc end (counter-clockwise) or sweep: degrees (0 < sweep < 360), width; returns centre_arc", created_doc!())),
CommandSpec::new("sketch.ellipse", "Ellipse", ellipse).at("SKETCH", "CREATE").icon("ellipse").enabled(in_sketch).params(concat!(
"center, major: end of the major axis [x,y], minor: a point the ellipse passes through on the minor side [x,y] | minor_radius",
created_doc!()
@@ -937,6 +937,7 @@ fn arc_slot(s: &mut Session, p: &Value, cmd: &str, o: Vec2, r: f64, t0: f64, swe
}
let t1 = t0 + sweep;
let at = |t: f64, rr: f64| o + Vec2::from_angle(t) * rr;
+ let mut centre_arc = None;
let (out, info) = edit(s, p, cmd, false, |sk, _| {
let oc = sk.add_point(o, None)?;
let ps = sk.add_point(at(t0, r), None)?;
@@ -950,6 +951,7 @@ fn arc_slot(s: &mut Session, p: &Value, cmd: &str, o: Vec2, r: f64, t0: f64, swe
let centre = sk.add_arc(o, at(t0, r), at(t1, r), [Some(oc), Some(ps), Some(pe)], None)?;
if let Some(c) = sk.curves.get_mut(centre) {
c.construction = true;
+ centre_arc = Some(c.id.clone());
}
let mut cons = Vec::new();
for (a, b) in [(outer, cap_e), (inner, cap_e), (outer, cap_s), (inner, cap_s)] {
@@ -958,7 +960,9 @@ fn arc_slot(s: &mut Session, p: &Value, cmd: &str, o: Vec2, r: f64, t0: f64, swe
cons.push(add_c(sk, ConstraintKind::Equal { a: cap_s, b: cap_e })?);
Ok((ids_of(sk, &[outer, cap_e, inner, cap_s]), cons))
})?;
- Ok(result(out, info))
+ let mut out = result(out, info);
+ out["centre_arc"] = json!(centre_arc);
+ Ok(out)
}
fn arc_slot_three(s: &mut Session, p: &Value) -> Result<Value> {
@@ -975,14 +979,22 @@ fn arc_slot_three(s: &mut Session, p: &Value) -> Result<Value> {
fn arc_slot_center(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.slot.arc_center";
- let (o, a, b) = (req_vec2(cmd, p, "center")?, req_vec2(cmd, p, "start")?, req_vec2(cmd, p, "end")?);
+ let (o, a) = (req_vec2(cmd, p, "center")?, req_vec2(cmd, p, "start")?);
let w = width(p, cmd)?;
let r = o.dist(a);
- if r < 1e-9 || o.dist(b) < 1e-9 {
+ if r < 1e-9 {
return Err(bad(cmd, "the points must differ from the centre"));
}
let t0 = (a - o).angle();
- let sweep = ((b - o).angle() - t0).rem_euclid(std::f64::consts::TAU);
+ let sweep = if p.get("sweep").is_some() {
+ num(p, "sweep").ok_or_else(|| bad(cmd, "`sweep` must be finite degrees"))?.to_radians()
+ } else {
+ let b = req_vec2(cmd, p, "end")?;
+ if o.dist(b) < 1e-9 {
+ return Err(bad(cmd, "the points must differ from the centre"));
+ }
+ ((b - o).angle() - t0).rem_euclid(std::f64::consts::TAU)
+ };
arc_slot(s, p, cmd, o, r, t0, sweep, w)
}
diff --git a/crates/engine/src/cmd/sketch_dimensioned.rs b/crates/engine/src/cmd/sketch_dimensioned.rs
index 62d0897dc0..75e924577b 100644
--- a/crates/engine/src/cmd/sketch_dimensioned.rs
+++ b/crates/engine/src/cmd/sketch_dimensioned.rs
@@ -17,7 +17,9 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
}
"sketch.circle.center" | "sketch.circle.two_point" | "sketch.circle.three_point" => &["radius", "diameter"],
"sketch.arc.center_point" => &["radius", "sweep"],
- "sketch.slot.center_to_center" | "sketch.slot.overall" => &["length", "width"],
+ "sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point" => &["length", "width"],
+ "sketch.slot.arc_center" => &["radius", "sweep", "width"],
+ "sketch.slot.arc_three_point" => &["width"],
"sketch.polygon.inscribed" | "sketch.polygon.circumscribed" | "sketch.polygon.edge" => &["radius"],
_ => return Err(bad(cmd, "unsupported sketch creator")),
};
@@ -49,6 +51,9 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
if role == "sweep" && !(evaluated.abs() > 1e-9 && evaluated.abs() < std::f64::consts::TAU - 1e-9) {
return Err(bad(cmd, "sweep must be nonzero and less than a full turn"));
}
+ if role == "sweep" && create == "sketch.slot.arc_center" && evaluated <= 0.0 {
+ return Err(bad(cmd, "arc slot sweep must be greater than zero and less than a full turn"));
+ }
if role == "sweep" {
params["sweep"] = json!(evaluated.to_degrees());
if let Some(o) = params.as_object_mut() {
@@ -94,9 +99,13 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
(c, "width" | "height") if c.starts_with("sketch.rectangle.") => (vec![curve(usize::from(role == "height"))?], "length"),
(c, "radius" | "diameter") if c.starts_with("sketch.circle.") => (vec![curve(0)?], role.as_str()),
("sketch.arc.center_point", "radius" | "sweep") => (vec![curve(0)?], role.as_str()),
- ("sketch.slot.center_to_center", "length") => (vec![curve(0)?], "length"),
+ ("sketch.slot.center_to_center" | "sketch.slot.center_point", "length") => (vec![curve(0)?], "length"),
("sketch.slot.overall", "length") => (vec![curve(1)?, curve(3)?], "overall_length"),
- ("sketch.slot.center_to_center" | "sketch.slot.overall", "width") => (vec![curve(1)?], "diameter"),
+ (c, "width") if c.starts_with("sketch.slot.") => (vec![curve(1)?], "diameter"),
+ ("sketch.slot.arc_center", "radius" | "sweep") => {
+ let centre = out.get("centre_arc").filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "slot centre arc missing"))?;
+ (vec![centre], role.as_str())
+ }
(c, "radius") if c.starts_with("sketch.polygon.") => {
let support =
out.get("support_circle").filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "polygon support circle missing"))?;
diff --git a/crates/engine/src/cmd/sketch_dimensioned_tests.rs b/crates/engine/src/cmd/sketch_dimensioned_tests.rs
index b949794c46..391f8e0e5e 100644
--- a/crates/engine/src/cmd/sketch_dimensioned_tests.rs
+++ b/crates/engine/src/cmd/sketch_dimensioned_tests.rs
@@ -74,9 +74,14 @@ fn signed_arc_sweep_retains_radius_and_logical_endpoint_identity() {
#[test]
fn slots_and_polygon_keep_permanent_typed_dimensions() {
- for command in ["sketch.slot.center_to_center", "sketch.slot.overall"] {
+ for command in ["sketch.slot.center_to_center", "sketch.slot.overall", "sketch.slot.center_point"] {
let mut s = session();
- let out = draw(&mut s, command, json!({"p0":[2,3],"p1":[22,3],"width":4,"connect":false}), json!({"length":20,"width":4}));
+ let out = draw(
+ &mut s,
+ command,
+ json!({"p0":[2,3],"p1":[22,3],"center":[12,3],"end":[22,3],"width":4,"connect":false}),
+ json!({"length":20,"width":4}),
+ );
let length = out["dimensions"]["length"]["param"].as_str().unwrap();
let result = run(&mut s, "parameters.change", json!({"name":length,"expression":30}));
assert_eq!(result["errors"], json!([]));
@@ -99,6 +104,28 @@ fn slots_and_polygon_keep_permanent_typed_dimensions() {
}
}
+#[test]
+fn arc_slot_dimensions_survive_parameter_edits_and_reopening() {
+ let mut s = session();
+ let out = draw(
+ &mut s,
+ "sketch.slot.arc_center",
+ json!({"center":[0,0],"start":[20,0],"end":[0,20],"width":6}),
+ json!({"radius":20,"sweep":90,"width":6}),
+ );
+ for (role, value) in [("radius", 25), ("sweep", 120), ("width", 8)] {
+ let name = out["dimensions"][role]["param"].as_str().unwrap();
+ let result = run(&mut s, "parameters.change", json!({"name":name,"expression":value}));
+ assert_eq!(result["errors"], json!([]), "{role}: {result}");
+ }
+ let doc = serde_json::from_value(serde_json::to_value(s.design()).unwrap()).unwrap();
+ let mut reopened = Session::new(doc);
+ reopened.active_sketch = s.active_sketch;
+ close(measure(&reopened, |k| matches!(k, ConstraintKind::Radius { .. })), 25.0);
+ close(measure(&reopened, |k| matches!(k, ConstraintKind::ArcSweep { .. })), 120f64.to_radians());
+ close(measure(&reopened, |k| matches!(k, ConstraintKind::Diameter { .. })), 8.0);
+}
+
#[test]
fn invalid_dimensioned_shapes_are_atomic_including_late_constraint_failure() {
let mut s = session();
diff --git a/crates/ui-egui/src/sketch_dims.rs b/crates/ui-egui/src/sketch_dims.rs
index e1af83ec9b..571d7a0b3c 100644
--- a/crates/ui-egui/src/sketch_dims.rs
+++ b/crates/ui-egui/src/sketch_dims.rs
@@ -43,12 +43,13 @@ pub fn boxes_for(cmd: &str, n: usize) -> Vec<DimBox> {
("sketch.circle.center", 1) => vec![b("Diameter", Length)],
// The two points are a diameter's ends (#3278).
("sketch.circle.two_point", 1) => vec![b("Diameter", Length)],
- ("sketch.arc.center_point", 1) => vec![b("Radius", Length)],
- ("sketch.arc.center_point", 2) => vec![b("Sweep", Angle)],
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) => vec![b("Radius", Length)],
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => vec![b("Sweep", Angle)],
("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => vec![b("Radius", Length), b("Sides", Unitless)],
- ("sketch.slot.center_to_center" | "sketch.slot.overall", 1) => vec![b("Length", Length)],
+ ("sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point", 1) => vec![b("Length", Length)],
("sketch.conic", 2) => vec![b("Rho", Unitless)],
- ("sketch.slot.center_to_center" | "sketch.slot.overall", 2) => vec![b("Width", Length)],
+ ("sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point", 2)
+ | ("sketch.slot.arc_center" | "sketch.slot.arc_three_point", 3) => vec![b("Width", Length)],
_ => Vec::new(),
}
}
@@ -88,8 +89,9 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
}
("sketch.circle.center", 1) => a + dir_or_x(h - a) * lk("Diameter").map_or((h - a).len(), |d| d * 0.5),
("sketch.circle.two_point", 1) => a + dir_or_x(h - a) * lk("Diameter").unwrap_or((h - a).len()),
- ("sketch.arc.center_point", 1) => a + dir_or_x(h - a) * lk("Radius").unwrap_or((h - a).len()),
- ("sketch.arc.center_point", 2) => {
+ ("sketch.slot.center_point", 1) => a + dir_or_x(h - a) * lk("Length").map_or((h - a).len(), |l| l * 0.5),
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) => a + dir_or_x(h - a) * lk("Radius").unwrap_or((h - a).len()),
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => {
let c = t.pts.first().map(|p| p.0).unwrap_or(a);
let r = (a - c).len();
let a0 = (a - c).angle();
@@ -97,7 +99,7 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
c + Vec2::from_angle(a0 + sweep) * r
}
("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => a + dir_or_x(h - a) * lk("Radius").unwrap_or((h - a).len()),
- ("sketch.slot.center_to_center" | "sketch.slot.overall", 2) => match lk("Width") {
+ ("sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point", 2) => match lk("Width") {
Some(w) => {
let c0 = t.pts.first().map(|p| p.0).unwrap_or(a);
let dir = dir_or_x(a - c0);
@@ -107,6 +109,12 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
}
None => h,
},
+ ("sketch.slot.arc_center", 3) => {
+ let Some(c) = t.pts.first().map(|p| p.0) else { return h };
+ let Some(start) = t.pts.get(1).map(|p| p.0) else { return h };
+ lk("Width").map_or(h, |w| c + dir_or_x(h - c) * (c.dist(start) + w * 0.5))
+ }
+ ("sketch.slot.arc_three_point", 3) => lk("Width").map_or(h, |w| a + dir_or_x(h - a) * (w * 0.5)),
_ => h,
}
}
@@ -209,6 +217,7 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> {
let n = d.perp();
vec![(v.len(), (a + e) * 0.5 + n * off), (v.angle(), a + d * (v.len() * 0.3).min(60.0 * px) - n * off)]
}
+ ("sketch.slot.center_point", 1) => vec![(2.0 * (e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)],
("sketch.rectangle.two_point", 1) => {
let (sx, sy) = (sign(e.x - a.x), sign(e.y - a.y));
vec![
@@ -233,10 +242,10 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> {
}
("sketch.circle.center", 1) => vec![(2.0 * (e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)],
("sketch.circle.two_point", 1) => vec![((e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)],
- ("sketch.arc.center_point", 1) | ("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => {
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) | ("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => {
vec![((e - a).len(), (a + e) * 0.5), (6.0, a + (e - a) * 0.5 + (e - a).perp() * 0.3)]
}
- ("sketch.arc.center_point", 2) => {
+ ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => {
let a0 = (a - first).angle();
// Out along the radius, clear of the cursor: a box on it would take the hover and
// the click meant for a point there (#2201).
@@ -252,12 +261,17 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> {
let d = e - mid;
vec![(rho, mid + d * rho + dir_or_x(d).perp() * off)]
}
- ("sketch.slot.center_to_center" | "sketch.slot.overall", 2) => {
+ ("sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point", 2) => {
let dir = dir_or_x(a - first);
let side = if dir.cross(e - first) < 0.0 { -1.0 } else { 1.0 };
// Beyond the cursor, away from the centre line, as for the arc (#2201).
vec![(2.0 * dir.cross(e - first).abs(), e + dir.perp() * (side * 2.5 * off))]
}
+ ("sketch.slot.arc_center", 3) => {
+ let Some(start) = t.pts.get(1).map(|p| p.0) else { return Vec::new() };
+ vec![(2.0 * (e.dist(first) - start.dist(first)).abs(), e + dir_or_x(e - first) * (2.5 * off))]
+ }
+ ("sketch.slot.arc_three_point", 3) => vec![(2.0 * e.dist(a), e + dir_or_x(e - a) * (2.5 * off))],
_ => Vec::new(),
}
}
@@ -279,9 +293,15 @@ fn fmt(app: &SolveApp, label: &str, kind: ValueKind, v: f64) -> String {
/// Make the tool's boxes match its stage (new boxes when a point was placed).
pub fn sync(t: &mut Tool) {
if t.dims_stage != t.pts.len() {
- if t.dims_stage == 1
- && t.pts.len() == 2
- && matches!(t.cmd, "sketch.arc.center_point" | "sketch.slot.center_to_center" | "sketch.slot.overall")
+ if t.pts.len() == t.dims_stage.saturating_add(1)
+ && matches!(
+ t.cmd,
+ "sketch.arc.center_point"
+ | "sketch.slot.center_to_center"
+ | "sketch.slot.overall"
+ | "sketch.slot.center_point"
+ | "sketch.slot.arc_center"
+ )
{
t.dim_history.extend(t.dims.iter().filter(|dimension| dimension.locked).cloned());
} else if t.pts.len() <= t.dims_stage || t.cmd == "sketch.line" {
@@ -332,6 +352,9 @@ pub fn validate(app: &SolveApp, t: &Tool) -> Result<(), String> {
}
match dimension.label {
"Angle" => {}
+ "Sweep" if t.cmd == "sketch.slot.arc_center" && value <= 0.0 => {
+ return Err("Arc slot Sweep must be greater than zero and less than 360 degrees".into());
+ }
"Sweep" if value.abs() > 1e-9 && value.abs() < std::f64::consts::TAU - 1e-9 => {}
"Sweep" => return Err("Sweep must lie strictly between -360 and 360 degrees, and cannot be zero".into()),
"Sides" if (3.0..=200.0).contains(&value) && value.fract() == 0.0 => {}
diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs
index cca2047858..117fd95615 100644
--- a/crates/ui-egui/src/sketch_tools.rs
+++ b/crates/ui-egui/src/sketch_tools.rs
@@ -1008,16 +1008,22 @@ pub(crate) fn place_point(app: &mut SolveApp, tool: &mut Tool, p: (Vec2, Option<
app.set_status(error, true);
return;
}
+ let p = crate::sketch_dims::placed(app, tool, &p);
tool.pts.push(p);
if n > 0 && tool.pts.len() >= n {
- run_points(app, tool);
- tool.pts.clear();
+ if run_points(app, tool) {
+ tool.pts.clear();
+ crate::sketch_dims::reset(tool);
+ } else {
+ tool.pts.pop();
+ }
}
}
-fn run_points(app: &mut SolveApp, t: &Tool) {
+fn run_points(app: &mut SolveApp, t: &Tool) -> bool {
let p = &t.pts;
let width = |a: Vec2, b: Vec2, c: Vec2| -> f64 { 2.0 * (b - a).normalized().map(|d| d.cross(c - a).abs()).unwrap_or(1.0) };
+ let slot_width = |fallback: f64| crate::sketch_dims::locked_value(app, t, "Width").unwrap_or_else(|| fallback.max(1e-3));
let params = match (t.cmd, p.as_slice()) {
("sketch.line.midpoint", [m, e]) => json!({"mid": arg(m), "end": xy(e.0)}),
("sketch.arc.tangent", [s, e]) => json!({"start": arg(s), "end": xy(e.0)}),
@@ -1027,28 +1033,28 @@ fn run_points(app: &mut SolveApp, t: &Tool) {
let rho = crate::sketch_dims::locked_value(app, t, "Rho").unwrap_or(0.5);
json!({"start": arg(a), "end": arg(b), "apex": arg(x), "rho": rho})
}
- ("sketch.slot.center_point", [c, e, w]) => json!({"center": xy(c.0), "end": xy(e.0), "width": width(c.0, e.0, w.0).max(1e-3)}),
+ ("sketch.slot.center_point", [c, e, w]) => json!({"center": xy(c.0), "end": xy(e.0), "width": slot_width(width(c.0, e.0, w.0))}),
("sketch.slot.arc_three_point", [a, th, b, w]) => {
- json!({"start": xy(a.0), "through": xy(th.0), "end": xy(b.0), "width": (2.0 * w.0.dist(b.0)).max(1e-3)})
+ json!({"start": xy(a.0), "through": xy(th.0), "end": xy(b.0), "width": slot_width(2.0 * w.0.dist(b.0))})
}
("sketch.slot.arc_center", [c, a, b, w]) => {
let r = c.0.dist(a.0);
- json!({"center": xy(c.0), "start": xy(a.0), "end": xy(b.0), "width": (2.0 * (w.0.dist(c.0) - r).abs()).max(1e-3)})
+ json!({"center": xy(c.0), "start": xy(a.0), "end": xy(b.0), "width": slot_width(2.0 * (w.0.dist(c.0) - r).abs())})
}
("sketch.spline.fit_point" | "sketch.spline.control_point" | "sketch.spline.control_point_5", pts) if pts.len() >= 2 => {
json!({"points": pts.iter().map(arg).collect::<Vec<_>>()})
}
("sketch.blend_curve", [a, b]) => match (&a.1, &b.1) {
(Some(x), Some(y)) => json!({"a": x, "b": y}),
- _ => return,
+ _ => return false,
},
("sketch.text", [at]) => {
crate::sketch_text::place(app, at.0, round_nice(app.cam.half_height() * 0.1));
- return;
+ return true;
}
- _ => return,
+ _ => return false,
};
- let _ = app.run(t.cmd, params);
+ crate::tools::draw(app, t, params).is_ok()
}
fn run_entities(app: &mut SolveApp, t: &Tool) {
@@ -1182,8 +1188,14 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) {
crate::tools::paint_sketch_path(&[ss.plane.to_world(last.0), hw], mode, painter, proj, stroke);
}
}
- if let Some((h, _)) = t.hover.as_ref()
- && let Some(curve) = crate::tools::preview_curve(t, *h, None, crate::sketch_dims::locked_value(app, t, "Rho"), None)
+ if let Some(hover) = t.hover.as_ref()
+ && let Some(curve) = crate::tools::preview_curve(
+ t,
+ crate::sketch_dims::shown(app, t, hover),
+ None,
+ crate::sketch_dims::locked_value(app, t, "Rho"),
+ crate::sketch_dims::locked_value(app, t, "Width"),
+ )
{
crate::tools::paint_preview_curve(curve, &ss.plane, painter, proj, stroke, crate::sketch_palette::mode(app));
}
diff --git a/crates/ui-egui/src/sketch_typed_intent_tests.rs b/crates/ui-egui/src/sketch_typed_intent_tests.rs
index 3321611b0f..ca21bfe47b 100644
--- a/crates/ui-egui/src/sketch_typed_intent_tests.rs
+++ b/crates/ui-egui/src/sketch_typed_intent_tests.rs
@@ -53,7 +53,7 @@ fn typed_arc_radius_and_signed_sweep_survive_stages_parameter_edit_and_reopen()
#[test]
fn slots_keep_typed_length_and_width_as_dimensions_and_start_fresh() {
- for command in ["sketch.slot.center_to_center", "sketch.slot.overall"] {
+ for command in ["sketch.slot.center_to_center", "sketch.slot.overall", "sketch.slot.center_point"] {
let mut h = start(command);
type_value(&mut h, "30");
enter(&mut h);
@@ -80,6 +80,62 @@ fn slots_keep_typed_length_and_width_as_dimensions_and_start_fresh() {
}
}
+#[test]
+fn arc_slots_offer_typed_dimensions_and_keep_them_after_creation() {
+ for command in ["sketch.slot.arc_center", "sketch.slot.arc_three_point"] {
+ let mut h = start(command);
+ if command == "sketch.slot.arc_center" {
+ assert_eq!(h.app.tool.as_ref().unwrap().dims[0].label, "Radius");
+ type_value(&mut h, "20 mm");
+ enter(&mut h);
+ assert_eq!(h.app.tool.as_ref().unwrap().dims[0].label, "Sweep");
+ type_value(&mut h, "90 deg");
+ enter(&mut h);
+ } else {
+ h.step(&json!({"click":{"world":[20,20,0]}})).unwrap();
+ h.step(&json!({"click":{"world":[40,0,0]}})).unwrap();
+ }
+ h.step(&json!({"move":{"world":[40,5,0]}})).unwrap();
+ assert_eq!(h.app.tool.as_ref().unwrap().dims[0].label, "Width");
+ type_value(&mut h, "6 mm");
+ let undo = h.app.session.undo.len();
+ enter(&mut h);
+ assert_eq!(h.app.session.undo.len(), undo + 1, "one undo step for {command}");
+ let kinds = constraints(&h);
+ assert!(kinds.iter().any(|k| matches!(k, ConstraintKind::Diameter { value, .. } if (*value - 6.0).abs() < 1e-7)));
+ if command == "sketch.slot.arc_center" {
+ assert!(kinds.iter().any(|k| matches!(k, ConstraintKind::Radius { value, .. } if (*value - 20.0).abs() < 1e-7)));
+ assert!(kinds.iter().any(|k| matches!(k, ConstraintKind::ArcSweep { value, .. } if (*value - std::f64::consts::FRAC_PI_2).abs() < 1e-7)));
+ }
+ assert!(h.app.tool.as_ref().unwrap().pts.is_empty());
+ assert!(h.app.tool.as_ref().unwrap().dim_history.is_empty());
+ h.app.run("edit.undo", json!({})).unwrap();
+ assert!(constraints(&h).is_empty());
+ }
+}
+
+#[test]
+fn arc_slot_invalid_width_keeps_inputs_and_click_uses_corrected_value() {
+ let mut h = start("sketch.slot.arc_center");
+ type_value(&mut h, "10 mm");
+ enter(&mut h);
+ type_value(&mut h, "90 deg");
+ enter(&mut h);
+ h.step(&json!({"move":{"world":[0,15,0]}})).unwrap();
+ type_value(&mut h, "25 mm");
+ let undo = h.app.session.undo.len();
+ enter(&mut h);
+ assert_eq!(h.app.session.undo.len(), undo);
+ assert_eq!(h.app.tool.as_ref().unwrap().pts.len(), 3);
+ assert_eq!(h.app.tool.as_ref().unwrap().dims[0].value, "25 mm");
+ assert!(constraints(&h).is_empty());
+ type_value(&mut h, "4 mm");
+ h.step(&json!({"click":{"world":[30,10,0]}})).unwrap();
+ assert_eq!(h.app.session.undo.len(), undo + 1);
+ assert!(h.app.tool.as_ref().unwrap().pts.is_empty());
+ assert!(constraints(&h).iter().any(|k| matches!(k, ConstraintKind::Diameter { value, .. } if (*value - 4.0).abs() < 1e-7)));
+}
+
#[test]
fn invalid_stage_values_remain_editable_without_partial_geometry() {
let mut h = start("sketch.arc.center_point");
diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs
index 8d45da6905..3bf1a155d9 100644
--- a/crates/ui-egui/src/tools.rs
+++ b/crates/ui-egui/src/tools.rs
@@ -455,7 +455,7 @@ pub fn place(app: &mut SolveApp, p: (Vec2, Option<String>)) {
}
/// Geometry and typed constraints are committed atomically, as one undo step.
-fn draw(app: &mut SolveApp, t: &Tool, params: Value) -> Result<Value, String> {
+pub(crate) fn draw(app: &mut SolveApp, t: &Tool, params: Value) -> Result<Value, String> {
let dimensions = crate::sketch_dims::dimensions(t);
if dimensions.as_object().is_none_or(|values| values.is_empty()) {
app.run(t.cmd, params)
@@ -799,6 +799,7 @@ pub(crate) fn preview_curve(
let (start, end) = if span(*b) < span(*c) { (*a, *c) } else { (*c, *a) };
(centre, start, end, centre.dist(*a), (2.0 * hover.dist(*c)).max(1e-3))
};
+ let width = slot_width.unwrap_or(width);
let t0 = (start - centre).angle();
let sweep = ((end - centre).angle() - t0).rem_euclid(std::f64::consts::TAU);
let half = width * 0.5;