Sketch: an x distance beside a y distance on the same points isn't refused (#4352) (#4387)

This commit is contained in:
raykholo
2026-10-25 18:27:40 +00:00
committed by GitHub
parent 3045d0787e
commit df01e59716
4 changed files with 83 additions and 1 deletions
+3 -1
View File
@@ -120,7 +120,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.params(concat!("p0, p1: overall ends, width", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.point", "Point", draw_point).at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them) or a snap: \"mid:<line>\" (held at the line's midpoint), \"on:<curve>:x,y\" (on the curve near x,y), {vertex: [x,y,z]} or {on_edge: [x,y,z]} of the model; id?", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params(
"entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"; also `#name = expression`), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other); dimension their centre points for the distance between centres. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",
"entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|distance_x|vertical|distance_y|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"; also `#name = expression`), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other); dimension their centre points for the distance between centres. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",
),
CommandSpec::new("sketch.constraint.horizontal_vertical", "Horizontal/Vertical", c_horizontal_vertical)
.at("SKETCH", "CONSTRAINTS")
@@ -2319,6 +2319,8 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
let ty = match (ty.as_str(), str_(p, "orientation").map(str::to_ascii_lowercase).as_deref()) {
("distance" | "auto", Some("horizontal")) => "horizontal".to_string(),
("distance" | "auto", Some("vertical")) => "vertical".to_string(),
("distance_x", _) => "horizontal".to_string(),
("distance_y", _) => "vertical".to_string(),
("offset", _) => "distance".to_string(),
_ => ty,
};
@@ -1247,6 +1247,49 @@ fn equal_over_a_list_of_curves_makes_them_all_equal_in_one_step() {
}
}
fn offset_lines(vertical: bool) -> Session {
let mut s = new_sketch();
let points = if vertical { json!([[0, 0], [0, 40]]) } else { json!([[0, 0], [40, 0]]) };
run(&mut s, "sketch.line", json!({"points": points, "ids": ["base"], "infer": false}));
run(&mut s, "sketch.constraint.fix", json!({"entity": "base"}));
let points = if vertical { json!([[10, 43], [10, 48]]) } else { json!([[43, 10], [48, 10]]) };
run(&mut s, "sketch.line", json!({"points": points, "ids": ["slider"], "infer": false, "connect": false}));
run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "slider", "mode": if vertical { "vertical" } else { "horizontal" }}));
run(&mut s, "sketch.dimension", json!({"entities": ["slider"], "value": 5}));
run(
&mut s,
"sketch.dimension",
json!({"entities": ["base.end", "slider.start"], "type": if vertical { "horizontal" } else { "vertical" }, "value": 10}),
);
assert_eq!(run(&mut s, "sketch.inspect", json!({}))["dof"], 1);
s
}
#[test]
fn component_dimensions_beside_a_perpendicular_dimension_solve() {
use solidcraft_sketch::ConstraintKind;
for vertical in [false, true] {
for ty in if vertical { ["vertical", "distance_y"] } else { ["horizontal", "distance_x"] } {
for value in [0.0, 0.5, 1.0] {
let mut s = offset_lines(vertical);
let out = run(&mut s, "sketch.dimension", json!({"entities": ["base.end", "slider.start"], "type": ty, "value": value}));
assert_eq!(out["sketch"]["dof"], 0, "{ty}, {value}: {out}");
let sk = sketch(&s);
let k = &sk.constraints.last().unwrap().kind;
assert!(
if vertical { matches!(k, ConstraintKind::DistanceY { .. }) } else { matches!(k, ConstraintKind::DistanceX { .. }) },
"{k:?}"
);
let a = sk.point(sk.resolve_point("base.end").unwrap()).unwrap();
let b = sk.point(sk.resolve_point("slider.start").unwrap()).unwrap();
let d = b - a;
assert!((if vertical { d.y } else { d.x }.abs() - value).abs() < 1e-6, "{d:?}");
assert!((if vertical { d.x } else { d.y }.abs() - 10.0).abs() < 1e-6, "{d:?}");
}
}
}
}
#[test]
fn equal_over_a_list_refuses_bad_lists_and_conflicts_without_changing_the_sketch() {
let mut s = four_circles();
@@ -1286,3 +1329,13 @@ fn midpoint_moves_the_point_whichever_is_picked_first() {
run(&mut s, "sketch.constraint.midpoint", json!({"a":"p","b":"l1"}));
assert!(line_midpoint(&sketch(&s), "l1").dist(Vec2::new(5.0, 5.0)) < 1e-6);
}
#[test]
fn conflicting_component_dimension_is_still_refused() {
let mut s = offset_lines(false);
let before = serde_json::to_value(sketch(&s)).unwrap();
let error =
s.execute("sketch.dimension", &json!({"entities": ["base.end", "slider.start"], "type": "vertical", "value": 11})).unwrap_err().to_string();
assert!(error.contains("would over-constrain the sketch (conflicts with"), "{error}");
assert_eq!(serde_json::to_value(sketch(&s)).unwrap(), before);
}
+4
View File
@@ -579,6 +579,10 @@ fn residuals(s: &State, k: &ConstraintKind, out: &mut Vec<f64>) {
out.extend([d.x, d.y]);
}
Distance { p, q, value } => out.push(s.p(p).dist(s.p(q)) - value),
// At zero the signed component has the same solutions as its magnitude, but keeps
// a nonzero derivative when the points line up for freedom and redundancy analysis.
DistanceX { p, q, value: 0.0 } => out.push(s.p(q).x - s.p(p).x),
DistanceY { p, q, value: 0.0 } => out.push(s.p(q).y - s.p(p).y),
DistanceX { p, q, value } => out.push((s.p(q).x - s.p(p).x).abs() - value),
DistanceY { p, q, value } => out.push((s.p(q).y - s.p(p).y).abs() - value),
PointLineDistance { p, l, value } => {
+23
View File
@@ -1158,3 +1158,26 @@ fn profiles_crossing_next_to_an_overshooting_end_keeps_the_corner_beside_it() {
assert_eq!(areas.len(), 1, "{areas:?}");
assert!(areas.iter().all(|a| (a - (2.0 - x)).abs() < 1e-4), "{areas:?}");
}
#[test]
fn zero_component_dimensions_keep_independent_rank() {
for vertical in [false, true] {
for value in [0.0, 0.5, 1.0] {
let mut sk = Sketch::new();
let q = sk.add_point(if vertical { v(10.0, 3.0) } else { v(3.0, 10.0) }, None).unwrap();
let existing =
if vertical { ConstraintKind::DistanceX { p: 0, q, value: 10.0 } } else { ConstraintKind::DistanceY { p: 0, q, value: 10.0 } };
sk.add_constraint(existing, None).unwrap();
assert_eq!(solve(&mut sk).dof, 1);
let added = if vertical { ConstraintKind::DistanceY { p: 0, q, value } } else { ConstraintKind::DistanceX { p: 0, q, value } };
let id = sk.add_constraint(added, None).unwrap();
let report = solve(&mut sk);
assert!(report.ok(), "{report:?}");
assert_eq!(report.dof, 0, "{vertical}, {value}: {report:?}");
assert!(!report.redundant.contains(&id), "{report:?}");
let ci = sk.constraints.iter().position(|c| c.id == id).unwrap();
assert!(determined_by(&sk, ci).is_empty());
assert!(constraint_residual(&sk, ci).unwrap() < 1e-6);
}
}
}