Skip to content

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

Sketch: scale profiles symmetric about a sketch axis (#11625) (#11708)

PaddleStrokee9efcf4c0b72
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index fa63f36fa2..a6c0130b93 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -3208,10 +3208,15 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
let want = vals.get(&pname).and_then(|v| v.to_kind(if is_angle { Kind::Angle } else { Kind::Length }).ok());
residual = want.map(|w| (w - cur).abs()).filter(|r| r.is_finite());
// The first length dimension of a sketch with nothing sized yet scales it (#1365).
+ // A sketch axis can also be measured by its fixed endpoints. In that case, only use
+ // the scaled sketch if it satisfies the dimension with those endpoints left in place.
if scale_sketch
&& !is_angle
- && let Some(factor) = want.map(|w| w / cur).filter(|f| f.is_finite() && (f - 1.0).abs() > 1e-9)
+ && let Some(want) = want
+ && let Some(factor) = Some(want / cur).filter(|f| f.is_finite() && (f - 1.0).abs() > 1e-9)
&& let Some(big) = sk.scaled_for_first_dimension(factor)
+ && (sk.links.is_empty()
+ || solidcraft_sketch::measure_dimension(&big, &k).is_some_and(|v| (v - want).abs() <= solidcraft_sketch::CONSISTENT))
{
*sk = big;
scaled = Some(factor);
diff --git a/crates/engine/src/cmd/sketch_ux_tests.rs b/crates/engine/src/cmd/sketch_ux_tests.rs
index 378687353c..8986834e6e 100644
--- a/crates/engine/src/cmd/sketch_ux_tests.rs
+++ b/crates/engine/src/cmd/sketch_ux_tests.rs
@@ -2018,6 +2018,58 @@ fn the_first_dimension_scales_a_sketch_drawn_by_eye() {
run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "vc"}));
}
+#[test]
+fn the_first_dimension_scales_a_profile_symmetric_about_the_sketch_axis() {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-10, 20], "p1": [10, 30]}));
+ run(&mut s, "sketch.constraint.symmetry", json!({"a": "l1.start", "b": "l1.end", "line": "y"}));
+ let before = sketch(&s);
+
+ let out = run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": "40 mm", "scale_sketch": true}));
+ assert_eq!(out["scaled"], 2.0, "{out}");
+ assert_eq!(out["measured"], 40.0, "{out}");
+ let after = sketch(&s);
+ for (p, q) in before.points.iter().zip(&after.points) {
+ let want = if p.fixed { p.pos } else { p.pos * 2.0 };
+ assert!(q.pos.dist(want) < 1e-6, "{}: {:?} -> {:?}, expected {want:?}", p.id, p.pos, q.pos);
+ }
+}
+
+#[test]
+fn the_first_dimension_leaves_another_profile_on_the_same_sketch_axis_alone() {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-10, 20], "p1": [10, 30]}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-6, 50], "p1": [6, 65]}));
+ for [a, b] in [["l1.start", "l1.end"], ["l5.start", "l5.end"]] {
+ run(&mut s, "sketch.constraint.symmetry", json!({"a": a, "b": b, "line": "y"}));
+ }
+ let before = sketch(&s);
+
+ let out = run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": "40 mm", "scale_sketch": true}));
+ assert!(out.get("scaled").is_none(), "{out}");
+ let after = sketch(&s);
+ // The second rectangle's four points remain at their original positions.
+ for (p, q) in before.points.iter().zip(&after.points).skip(5).take(4) {
+ assert!(q.pos.dist(p.pos) < 1e-6, "{}: {:?} -> {:?}", p.id, p.pos, q.pos);
+ }
+}
+
+#[test]
+fn the_first_dimension_measuring_to_a_sketch_axis_endpoint_uses_its_fixed_position() {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-10, 20], "p1": [10, 30]}));
+ run(&mut s, "sketch.constraint.symmetry", json!({"a": "l1.start", "b": "l1.end", "line": "y"}));
+ let before = sketch(&s);
+ let axis = before.sketch_axis_curve("y").unwrap();
+ let end = format!("{}.end", before.curves[axis].id);
+ let end_pos = pt(&s, &end);
+
+ let out = run(&mut s, "sketch.dimension", json!({"entities": ["l1.start", end], "type": "vertical", "value": "60 mm", "scale_sketch": true}));
+ assert!(out.get("scaled").is_none(), "{out}");
+ assert_eq!(out["measured"], 60.0, "{out}");
+ assert_eq!(pt(&s, &end), end_pos);
+}
+
#[test]
fn the_first_dimension_does_not_scale_a_sketch_that_has_a_size() {
// A sketch with projected or fixed geometry, or another length dimension, already has a
diff --git a/crates/sketch/src/model.rs b/crates/sketch/src/model.rs
index 938db93617..a73d37af09 100644
--- a/crates/sketch/src/model.rs
+++ b/crates/sketch/src/model.rs
@@ -2,7 +2,9 @@ use serde::{Deserialize, Serialize};
use solidcore_record::{Plane, Seg2, Vec2, Vec3};
use solidcraft_geom::Aabb2;
use solidcraft_geom::prelude::*;
+use std::collections::BTreeMap;
+use crate::link::{LinkGeom, LinkKind, LinkSource, sketch_axis_geom};
use crate::union_find::{find, union};
/// Hard caps that keep hostile input from exhausting memory or time.
@@ -1222,9 +1224,10 @@ impl Sketch {
/// geometry has no size of its own until a dimension gives it one, so the first length
/// dimension scales the whole sketch to its value instead of distorting it. Only a sketch
/// with nothing that fixes a size: no other driving length dimension, nothing fixed,
- /// projected or patterned, no offsets and no 3D curves. Every geometric constraint
- /// (coincident, horizontal, parallel, tangent, equal, symmetric, …) and every angle holds
- /// on the scaled sketch as it held before. `None` when the sketch does not qualify, also
+ /// projected or patterned, no offsets and no 3D curves. A linked sketch axis can give a
+ /// direction or symmetry line through the origin without giving the drawing a size. Every
+ /// geometric constraint and angle holds on the scaled sketch as it held before.
+ /// `None` when the sketch does not qualify, also
/// when its curves form several separate chains (#3786): the dimension sizes only its own.
pub fn scaled_for_first_dimension(&self, factor: f64) -> Option<Sketch> {
if !(factor.is_finite() && (1e-6..=1e6).contains(&factor)) {
@@ -1237,21 +1240,41 @@ impl Sketch {
ConstraintKind::Pierce { .. } => true,
ref k => k.is_dimension() && !k.is_angle() && !c.driven,
});
- if sizes
- || !self.links.is_empty()
- || !self.patterns.is_empty()
- || !self.offsets.is_empty()
- || !self.freeform_offsets.is_empty()
- || !self.wires.is_empty()
- || self.curves.iter().any(|c| c.fixed || c.link.is_some())
- || self.points.iter().enumerate().any(|(i, p)| (p.fixed || p.link.is_some()) && (i != 0 || p.pos != Vec2::ZERO))
- {
+ if sizes || !self.patterns.is_empty() || !self.offsets.is_empty() || !self.freeform_offsets.is_empty() || !self.wires.is_empty() {
+ return None;
+ }
+ let mut axes = BTreeMap::new();
+ for link in &self.links {
+ let LinkSource::SketchAxis { name } = &link.source else { return None };
+ let LinkGeom::Line(a, b) = sketch_axis_geom(name)? else { return None };
+ if link.kind != LinkKind::Project || link.lost {
+ return None;
+ }
+ axes.insert(link.id.as_str(), [a, b]);
+ }
+ let axis_point = |i: usize| self.points.get(i).is_some_and(|p| p.link.as_deref().is_some_and(|id| axes.contains_key(id)));
+ if self.points.iter().enumerate().any(|(i, p)| (p.fixed || p.link.is_some()) && (i != 0 || p.pos != Vec2::ZERO) && !axis_point(i)) {
return None;
}
+ let drawn_curves = || self.curves.iter().filter(|c| c.link.is_none());
+ for curve in &self.curves {
+ if let Some(link) = curve.link.as_deref() {
+ let &[from, to] = axes.get(link)?;
+ let CurveKind::Line { a, b } = curve.kind else { return None };
+ let endpoint = |i: usize| self.points.get(i).filter(|p| p.link.as_deref() == Some(link)).map(|p| p.pos);
+ let ends = [endpoint(a)?, endpoint(b)?];
+ if ends != [from, to] && ends != [to, from] {
+ return None;
+ }
+ } else if curve.fixed || !axes.is_empty() && curve.kind.point_ids().into_iter().any(axis_point) {
+ return None;
+ }
+ }
// Shapes nothing joins (#3786) are not one drawing: only one connected chain of curves
- // scales. Curves and driving constraints join their points; the origin is a point too.
+ // scales. A sketch axis is shared by references to its direction, and does not by
+ // itself connect separate drawings.
let mut parent: Vec<usize> = (0..self.points.len()).collect();
- for c in &self.curves {
+ for c in drawn_curves() {
let ids = c.kind.point_ids();
for w in ids.windows(2) {
if let [a, b] = *w {
@@ -1260,21 +1283,29 @@ impl Sketch {
}
}
for c in self.constraints.iter().filter(|c| !c.driven) {
- let ids = self.constraint_points(&c.kind);
- for w in ids.windows(2) {
+ let (mut points, curves) = c.kind.indices();
+ // An explicit endpoint or the length of the axis segment does give a size.
+ if !axes.is_empty()
+ && (points.iter().copied().any(axis_point)
+ || matches!(c.kind, ConstraintKind::Equal { .. }) && curves.iter().any(|&i| self.curves.get(i).is_some_and(|c| c.link.is_some())))
+ {
+ return None;
+ }
+ points.extend(curves.into_iter().filter_map(|i| self.curves.get(i)).filter(|c| c.link.is_none()).flat_map(|c| c.kind.point_ids()));
+ for w in points.windows(2) {
if let [a, b] = *w {
union(&mut parent, a, b);
}
}
}
- let mut roots = self.curves.iter().filter_map(|c| c.kind.point_ids().first().map(|&p| find(&mut parent, p)));
+ let mut roots = drawn_curves().filter_map(|c| c.kind.point_ids().first().map(|&p| find(&mut parent, p)));
if let Some(first) = roots.next()
&& roots.any(|r| r != first)
{
return None;
}
let mut out = self.clone();
- for p in out.points.iter_mut().filter(|p| !p.fixed) {
+ for p in out.points.iter_mut().filter(|p| !p.fixed && p.link.is_none()) {
p.pos = p.pos * factor;
if !in_range(p.pos) {
return None;