sketch: points placed by expressions (#2864)

* Sketch points placed by expressions over the design's parameters

A point typed as an expression ("W", "H / 2") in sketch.point, sketch.line and
the other point arguments is fixed where its expressions come to, and moves
there again before each solve when the parameters change: an outline that
follows its parameters needs no dimensions and no solve per change. The
expressions are saved with the point (one pending design-format change), listed
with the sketch's other expressions, and renamed with their parameters; one
that cannot be worked out is refused.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* Sketch point expressions: accurate params doc, out-of-range regression test

The sketch.line params said every point argument takes expressions; only those that
also take a point id do (the rectangle corners, an arc's through point and the like
are plain [x, y]). PointExprs' comment called its expressions mm. A test covers a
parameter that puts a placed point out of range and a design file whose point
expression overflows: both are errors, not crashes, and the sketch recovers.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
NOWN-DEV
2026-10-19 19:05:28 +02:00
committed by GitHub
parent 3b4db52c10
commit e4d9015f68
10 changed files with 267 additions and 40 deletions
+24 -1
View File
@@ -3195,8 +3195,10 @@ impl Document {
}
}
/// Apply parameter values to every dimension in a sketch (in place).
/// Apply parameter values to every dimension in a sketch, and to its points placed by
/// parameters (in place).
pub fn apply_dimension_values(&self, vals: &std::collections::BTreeMap<String, expr::Value>, sk: &mut Sketch) -> Result<()> {
self.apply_point_values(vals, sk)?;
for c in &mut sk.constraints {
if let Some(p) = &c.param {
let kind = if c.kind.is_angle() { Kind::Angle } else { Kind::Length };
@@ -3210,6 +3212,27 @@ impl Document {
Ok(())
}
/// Move the points placed by parameters to where their expressions come to (in place). Such
/// a point is fixed, so the solve keeps it there.
pub fn apply_point_values(&self, vals: &std::collections::BTreeMap<String, expr::Value>, sk: &mut Sketch) -> Result<()> {
use crate::eval::val;
for p in &mut sk.points {
let Some(exprs) = p.exprs.as_deref() else { continue };
let mut at = p.pos;
for (e, slot, what) in [(&exprs.x, &mut at.x, "X"), (&exprs.y, &mut at.y, "Y")] {
if let Some(e) = e {
*slot = val(vals, e, Kind::Length).map_err(|error| DocError::Expr(format!("point {} {what}: {error}", p.id)))?;
}
}
if !(at.is_finite() && at.x.abs() < solidcraft_sketch::MAX_COORD && at.y.abs() < solidcraft_sketch::MAX_COORD) {
return Err(DocError::Invalid(format!("point {} is placed out of range", p.id)));
}
p.pos = at;
p.fixed = true;
}
Ok(())
}
/// Give the texts of a sketch the numbers their expressions come to (in place): a text's
/// height, place and angle that were typed as expressions. A text's outlines are rebuilt from
/// its numbers when the sketch's links are refreshed, so this comes before that. A number
@@ -0,0 +1,5 @@
//! A sketch point can be placed by parameters: `exprs` (`x` and/or `y`, expressions over the
//! design's parameters, in the design's units when a number in them has no unit) on a point that
//! is `fixed` where they come to, and moved there again before each solve when the parameters
//! change. Older designs need no transformation: their points have no `exprs`. The last release
//! asks for an update instead of opening a design with this format number.