Skip to content

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

Merge origin/main (with #86): sketch.dimension reports the solved value in the dimension's unit; the spout test expects the solved 30 and 60 degrees

CypherNaugh_0xade30e9c4e43
diff --cc crates/engine/src/cmd/sketch.rs
index 4e38353276,e76bd36039..6b34a4fd58
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@@ -1441,26 -1460,23 +1460,32 @@@ fn dimension(s: &mut Session, p: &Value
let name = k.name();
let id = sk.add_constraint(k, Some(pname.clone()))?;
place_text(sk, &id, text_at);
- Ok((pname, name, cur))
+ Ok((pname, name, measured(is_angle, cur)))
})?;
+ // Report what the dimension measures once the sketch is solved, not the geometry before.
+ let current = solved_measure(s, p, cmd).unwrap_or(current);
if driven {
- return Ok(json!({"param": Value::Null, "driven": true, "type": kind_name, "measured": current, "sketch": info}));
+ return Ok(json!({"param": Value::Null, "driven": true, "type": kind_name, "measured": current.0, "unit": current.1, "sketch": info}));
}
if reject_redundant(before, &info, cmd).is_err() {
return Err(bad(cmd, "that dimension would over-constrain the sketch; add it as a driven (reference) dimension with driven: true"));
}
let v = s.doc.param(&param).map(|p| p.expr.clone()).unwrap_or_default();
- Ok(json!({"param": param, "type": kind_name, "expression": v, "measured": current, "sketch": info}))
+ Ok(json!({"param": param, "type": kind_name, "expression": v, "measured": current.0, "unit": current.1, "sketch": info}))
+ }
+
+ /// A dimension's measured value as reported, in the unit its expression is written in.
+ fn measured(is_angle: bool, v: f64) -> (f64, &'static str) {
+ if is_angle { (round6(v.to_degrees()), "deg") } else { (round6(v), "mm") }
}
+/// What the dimension just added (the sketch's last constraint) measures on the solved sketch.
- fn solved_measure(s: &Session, p: &Value, cmd: &str) -> Option<f64> {
++fn solved_measure(s: &Session, p: &Value, cmd: &str) -> Option<(f64, &'static str)> {
+ let sk = s.doc.sketch(target_sketch(s, p, cmd).ok()?).ok()?;
- solvecraft_sketch::measure_dimension(sk, &sk.constraints.last()?.kind)
++ let k = &sk.constraints.last()?.kind;
++ Some(measured(k.is_angle(), solvecraft_sketch::measure_dimension(sk, k)?))
+}
+
/// Keep a dimension's text where it was placed (`at`, sketch coordinates), relative to the
/// dimension so it follows the geometry.
fn place_text(sk: &mut Sketch, id: &str, at: Option<Vec2>) {
diff --cc crates/engine/src/cmd/sketch_constraints_tests.rs
index fe07607de2,8623eb7722..e600fd7267
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@@ -221,3 -201,65 +221,65 @@@ fn drawing_onto_a_curve_keeps_the_point
let cc = sk.center(sk.curve_index(&c).unwrap()).unwrap();
assert!((st.dist(cc) - 8.0).abs() < 1e-7, "{st:?} {cc:?}");
}
+
+ /// A faucet spout path: a vertical riser, a horizontal top and an outlet sloping down, joined
+ /// by fillets. The riser points up and the outlet down-left, so their directions are 150° apart
+ /// while the lines meet at 30°.
+ fn spout() -> Session {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": [[795, 955], [795, 1068], [689, 1068], [661, 1020]], "ids": ["l1", "l2", "l3"]}));
+ run(&mut s, "sketch.fillet", json!({"point": "l1.end", "radius": 50}));
+ run(&mut s, "sketch.fillet", json!({"point": "l2.end", "radius": 35}));
+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "l1", "mode": "vertical"}));
+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "l2", "mode": "horizontal"}));
+ run(&mut s, "sketch.dimension", json!({"entities": ["origin", "l1.start"], "type": "horizontal", "value": 795}));
+ run(&mut s, "sketch.dimension", json!({"entities": ["origin", "l1.start"], "type": "vertical", "value": 955}));
+ run(&mut s, "sketch.dimension", json!({"entities": ["origin", "l2.start"], "type": "vertical", "value": 1068}));
+ run(&mut s, "sketch.dimension", json!({"entities": ["l3.end", "l1.start"], "type": "horizontal", "value": 134}));
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["origin", "l3.end"], "type": "vertical", "value": 1020}));
+ assert_eq!(d["sketch"]["dof"], 1, "{d}");
+ s
+ }
+
+ /// The angle (degrees, 0..=90) between two lines of the active sketch.
+ fn line_angle(s: &Session, a: &str, b: &str) -> f64 {
+ let sk = sketch(s);
+ let dir = |l: &str| {
+ let c = sk.curve_index(l).unwrap();
+ let (p, q) = crate::cmd::sketch::line_pts(&sk, c).unwrap();
+ q - p
+ };
+ let (u, v) = (dir(a), dir(b));
+ let t = u.cross(v).abs().atan2(u.dot(v)).to_degrees();
+ t.min(180.0 - t)
+ }
+
+ #[test]
+ fn an_angle_between_lines_takes_the_reading_nearest_its_value_and_reports_degrees() {
+ // The riser and the outlet are not adjacent and meet at 30°; their directions are 150°
+ // apart. Asking for 30° used to swing the outlet round and was refused as over-constraining.
+ for ents in [["l1", "l3"], ["l3", "l1"]] {
+ let mut s = spout();
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ents, "type": "angle", "value": "30 deg"}));
+ assert_eq!(d["sketch"]["dof"], 0, "{d}");
+ assert_eq!(d["unit"], "deg", "{d}");
- assert!((d["measured"].as_f64().unwrap() - 30.3).abs() < 0.1, "{d}");
++ assert!((d["measured"].as_f64().unwrap() - 30.0).abs() < 1e-6, "{d}");
+ assert!((line_angle(&s, "l1", "l3") - 30.0).abs() < 1e-6);
+ }
+ // The other reading still works, and the adjacent top line measures 60° the same way.
+ let mut s = spout();
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l1", "l3"], "value": "150 deg"}));
+ assert_eq!(d["sketch"]["dof"], 0, "{d}");
+ assert!((line_angle(&s, "l1", "l3") - 30.0).abs() < 1e-6);
+ let mut s = spout();
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l2", "l3"], "value": "60 deg"}));
- assert!((d["measured"].as_f64().unwrap() - 59.74).abs() < 0.01, "{d}");
++ assert!((d["measured"].as_f64().unwrap() - 60.0).abs() < 1e-6, "{d}");
+ // Without a value the acute angle is kept as it is.
+ let mut s = spout();
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l1", "l3"]}));
+ let e = d["expression"].as_str().unwrap();
+ assert!(e.ends_with(" deg") && (e.trim_end_matches(" deg").parse::<f64>().unwrap() - 30.3).abs() < 0.1, "{d}");
+ // Lengths report millimetres.
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l2"], "driven": true}));
+ assert_eq!(d["unit"], "mm", "{d}");
+ }
diff --cc crates/sketch/src/lib.rs
index 2b4cc1d3a4,81d8af22b7..3079d6dbd5
--- a/crates/sketch/src/lib.rs
+++ b/crates/sketch/src/lib.rs
@@@ -25,9 -26,10 +26,10 @@@ pub use curves::
pub use dims::{DimFrame, DimLayout, chain_centres, default_text, dim_frame, dim_layout, encode_text};
pub use link::{Link, LinkGeom, LinkKind, LinkSource, MAX_LINK_ENTITIES};
pub use model::{Constraint, ConstraintKind, Curve, CurveKind, SPoint, Sketch, SketchError, SketchView};
+ pub use offset::OffsetChain;
pub use profiles::{Profile, find_drawn_profiles, find_profiles, merge_regions};
pub use shape::{Shape, intersections};
-pub use solver::{Hold, SolveReport, SolveStatus, solve, solve_holding};
+pub use solver::{Hold, SolveReport, SolveStatus, measure_dimension, solve, solve_holding};
pub use text::{MAX_TEXT_CHARS, text_geometry};
pub use wire::{MAX_FIT_POINTS, MAX_WIRE_POINTS, Wire, fit_polyline};