Sketch: a changed length dimension keeps its start point, so a side grows from its first corner (#6070) (#6139)
* claim: #6070 * Sketch: a changed length dimension keeps its start point, so a side grows from its first corner (#6070) Changing a dimension's value re-solved its sketch as the smallest move from the stored positions, which splits the change between both ends: a rectangle side set from 40 to 60 mm grew 10 mm each way. parameters.change now first solves a sketch whose length dimension (distance, horizontal or vertical distance, line length) the parameter drives with that dimension's first point held, and stores the result, so evaluation has nothing left to move. Where holding it can't solve (a symmetry moves both ends), evaluation solves as before. 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:
@@ -1490,6 +1490,13 @@ fn adopt_solution(stored: &mut solidcraft_sketch::Sketch, solved: &solidcraft_sk
|
||||
true
|
||||
}
|
||||
|
||||
impl Document {
|
||||
/// The values features are evaluated with: the parameters, and the design's length unit.
|
||||
pub fn evaluation_values(&self) -> BTreeMap<String, Value> {
|
||||
evaluation_values(self).0
|
||||
}
|
||||
}
|
||||
|
||||
/// The values features are evaluated with: the parameters, and the design's length unit.
|
||||
fn evaluation_values(doc: &Document) -> (BTreeMap<String, Value>, BTreeMap<String, String>) {
|
||||
let (mut vals, perr) = doc.param_values();
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
//! graph, CSV/JSON import and export, expression evaluation and autocomplete.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use solidcraft_doc::FeatureKind;
|
||||
use solidcraft_doc::expr::{self, Kind};
|
||||
use solidcraft_sketch::{Analysis, ConstraintKind, CurveKind, Hold, Sketch, solve_with};
|
||||
|
||||
use super::CommandSpec;
|
||||
use crate::params::{bad, bool_, str_};
|
||||
@@ -128,6 +130,9 @@ fn change_param(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
s.doc_mut().set_favorite(&name, f)?;
|
||||
}
|
||||
let switched = switch_by_rules(s, &before);
|
||||
if e.is_some() {
|
||||
keep_dimension_start(s, &name);
|
||||
}
|
||||
s.settle_value_edit(&solved)?;
|
||||
let (vals, _) = s.doc.param_values();
|
||||
if !switched.is_empty() {
|
||||
@@ -146,6 +151,64 @@ fn change_param(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The point a length dimension keeps when its value changes: the first it measures from (a
|
||||
/// line's start). None for other dimensions.
|
||||
fn dimension_start(sk: &Sketch, kind: &ConstraintKind) -> Option<usize> {
|
||||
match kind {
|
||||
ConstraintKind::Distance { p, .. } | ConstraintKind::DistanceX { p, .. } | ConstraintKind::DistanceY { p, .. } => Some(*p),
|
||||
ConstraintKind::Length { l, .. } => match sk.curves.get(*l)?.kind {
|
||||
CurveKind::Line { a, .. } => Some(a),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A length dimension driven by `name` keeps its first point where it is when the value
|
||||
/// changes, so a rectangle's side grows from its first corner, not from both ends (#6070). The
|
||||
/// sketch is solved here with that point held, before the design evaluates from its stored
|
||||
/// positions. Where holding it can't solve (a symmetry moves both ends), evaluation solves as
|
||||
/// before.
|
||||
fn keep_dimension_start(s: &mut Session, name: &str) {
|
||||
let targets: Vec<(u64, usize)> = s
|
||||
.doc
|
||||
.features
|
||||
.iter()
|
||||
.filter_map(|f| match &f.kind {
|
||||
FeatureKind::Sketch { sketch, .. } => sketch
|
||||
.constraints
|
||||
.iter()
|
||||
.find(|c| !c.driven && c.param.as_deref() == Some(name))
|
||||
.and_then(|c| dimension_start(sketch, &c.kind))
|
||||
.map(|p| (f.id, p)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
if targets.is_empty() {
|
||||
return;
|
||||
}
|
||||
let vals = s.doc.evaluation_values();
|
||||
for (id, start) in targets {
|
||||
let Ok(stored) = s.doc.sketch(id) else { continue };
|
||||
let mut trial = stored.clone();
|
||||
if s.doc.apply_dimension_values(&vals, &mut trial).is_err() {
|
||||
continue;
|
||||
}
|
||||
let reference: Vec<_> = stored.points.iter().map(|p| p.pos).collect();
|
||||
let hold = Hold { points: vec![start], radii: Vec::new() };
|
||||
if !solve_with(&mut trial, &hold, Some(&reference), Analysis::StatusOnly, None).keeps_orientation() {
|
||||
continue;
|
||||
}
|
||||
if let Ok(stored) = s.doc_mut().sketch_mut(id)
|
||||
&& stored.points.len() == trial.points.len()
|
||||
{
|
||||
for (p, q) in stored.points.iter_mut().zip(&trial.points) {
|
||||
p.pos = q.pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `parameters.change {changes}`: several existing parameters set at once, so a design is
|
||||
/// rebuilt once for all of them instead of once per parameter.
|
||||
fn change_many(s: &mut Session, changes: &Value, solved: &crate::SketchValues) -> Result<Value> {
|
||||
@@ -162,6 +225,9 @@ fn change_many(s: &mut Session, changes: &Value, solved: &crate::SketchValues) -
|
||||
let before = s.doc.param_values().0;
|
||||
s.doc_mut().change_params_atomic(&list)?;
|
||||
let switched = switch_by_rules(s, &before);
|
||||
for (name, _) in &list {
|
||||
keep_dimension_start(s, name);
|
||||
}
|
||||
s.settle_value_edit(solved)?;
|
||||
let (vals, _) = s.doc.param_values();
|
||||
let values: serde_json::Map<String, Value> = list.iter().map(|(n, _)| (n.clone(), json!(vals.get(n).map(|v| v.v)))).collect();
|
||||
|
||||
@@ -1850,3 +1850,36 @@ fn a_driven_dimension_made_driving_and_autoconstrain_dimensions_use_the_design_u
|
||||
assert!(!lengths.is_empty(), "AutoConstrain dimensioned the line");
|
||||
assert!(lengths.iter().all(|p| p.unit == "in" && p.expr.ends_with(" in")), "{lengths:?}");
|
||||
}
|
||||
|
||||
/// #6070: changing a rectangle side's dimension keeps the side's first corner where it is, so
|
||||
/// the side grows from where it was drawn, not from both ends. A centred rectangle (Symmetric
|
||||
/// about the axis) still grows both ways, as it must.
|
||||
#[test]
|
||||
fn a_changed_length_dimension_keeps_its_first_point() {
|
||||
let at = |s: &Session, p: &str| {
|
||||
let sk = sketch(s);
|
||||
sk.point(sk.resolve_point(p).unwrap()).unwrap()
|
||||
};
|
||||
let mut s = new_sketch();
|
||||
let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [10, 10], "p1": [50, 30]}));
|
||||
let bottom = r["sides"]["bottom"].as_str().unwrap().to_string();
|
||||
let right = r["sides"]["right"].as_str().unwrap().to_string();
|
||||
let width = run(&mut s, "sketch.dimension", json!({"entities": [bottom]}))["param"].as_str().unwrap().to_string();
|
||||
let height = run(&mut s, "sketch.dimension", json!({"entities": [right]}))["param"].as_str().unwrap().to_string();
|
||||
run(&mut s, "parameters.change", json!({"name": width, "expression": "60 mm"}));
|
||||
assert!(at(&s, "p1").dist(solidcraft_geom::Vec2::new(10.0, 10.0)) < 1e-6, "the first corner stays: {:?}", at(&s, "p1"));
|
||||
assert!(at(&s, "p2").dist(solidcraft_geom::Vec2::new(70.0, 10.0)) < 1e-6, "the far corner moves: {:?}", at(&s, "p2"));
|
||||
// Several at once: the right side grows up from its start, the bottom-right corner.
|
||||
run(&mut s, "parameters.change", json!({"changes": {height: "25 mm"}}));
|
||||
assert!(at(&s, "p2").dist(solidcraft_geom::Vec2::new(70.0, 10.0)) < 1e-6, "{:?}", at(&s, "p2"));
|
||||
assert!(at(&s, "p3").dist(solidcraft_geom::Vec2::new(70.0, 35.0)) < 1e-6, "{:?}", at(&s, "p3"));
|
||||
|
||||
// Centred on the Y axis: holding a corner can't keep it symmetric, so both ends move.
|
||||
let mut s = new_sketch();
|
||||
let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-20, 0], "p1": [20, 10]}));
|
||||
let bottom = r["sides"]["bottom"].as_str().unwrap().to_string();
|
||||
run(&mut s, "sketch.constraint.symmetry", json!({"a": "p1", "b": "p2", "line": "y"}));
|
||||
let width = run(&mut s, "sketch.dimension", json!({"entities": [bottom]}))["param"].as_str().unwrap().to_string();
|
||||
run(&mut s, "parameters.change", json!({"name": width, "expression": "60 mm"}));
|
||||
assert!((at(&s, "p1").x + 30.0).abs() < 1e-6 && (at(&s, "p2").x - 30.0).abs() < 1e-6, "{:?} {:?}", at(&s, "p1"), at(&s, "p2"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user