Skip to content

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

Smooth sketch dragging on large imported faces (#3222) (#3480)

Christian Svensson922768548300
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index f4d339121e..eef0f704e0 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -286,7 +286,15 @@ pub(super) fn edit_solved<T>(
if sk.patterns.iter().any(|p| p.count_expression.is_some()) {
doc.apply_pattern_quantities(&doc.param_values().0, &mut sk)?;
}
- super::sketch_project::refresh(s, &doc, id, &mut sk);
+ // The next frame of a live gesture on this sketch, nothing else run since the last one: its
+ // links are as that frame projected them, from a model that hasn't changed (#3222).
+ let fresh = s
+ .live_links
+ .as_ref()
+ .is_some_and(|(sketch, after, revision)| *sketch == id && std::sync::Arc::ptr_eq(after, &s.doc) && *revision == s.revision);
+ if !fresh {
+ super::sketch_project::refresh(s, &doc, id, &mut sk);
+ }
// The stored sketch is solved for its stored values.
let stored = sk.driving_values();
let (out, left) = f(&mut sk, &mut doc)?;
diff --git a/crates/engine/src/cmd/sketch_drag_live_tests.rs b/crates/engine/src/cmd/sketch_drag_live_tests.rs
index d55ea6fdec..2e46b658dc 100644
--- a/crates/engine/src/cmd/sketch_drag_live_tests.rs
+++ b/crates/engine/src/cmd/sketch_drag_live_tests.rs
@@ -6,6 +6,7 @@ use solidcraft_geom::Vec3;
use super::solve_counts;
use crate::Session;
+use crate::cmd::sketch_project::refresh_count;
#[test]
fn determined_pattern_geometry_does_not_attempt_conflicting_held_solves() {
@@ -366,3 +367,56 @@ fn a_drag_with_the_rest_of_a_selection_moves_it_as_one() {
assert!(s.execute("sketch.drag", &json!({"entity": "l1", "with": with, "from": [10, 30], "to": [20, 20]})).is_err(), "{with}");
}
}
+
+/// A 5 x 5 square drawn on the top face of a 30 x 20 x 10 box, the face's edges projected into
+/// its sketch; returns the session and a corner of the square (its id and where it is).
+fn on_a_face() -> (Session, String, solidcraft_geom::Vec2) {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 30, "width": 20, "height": 10}));
+ run(&mut s, "sketch.create", json!({"plane": {"face": [15, 10, 10]}}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [5, 5], "p1": [10, 10]}));
+ let sk = sketch_of(&s);
+ assert!(!sk.links.is_empty(), "the face's edges are projected");
+ let corner = sk.curves.iter().find_map(|c| match c.kind {
+ solidcraft_sketch::CurveKind::Line { a, .. } if c.link.is_none() => sk.points.get(a).map(|p| (p.id.clone(), p.pos)),
+ _ => None,
+ });
+ let (id, at) = corner.expect("a drawn line");
+ (s, id, at)
+}
+
+/// One live frame moving `point` from `at` by `dx`; returns where it went.
+fn live_frame(s: &mut Session, point: &str, at: solidcraft_geom::Vec2, dx: f64) -> solidcraft_geom::Vec2 {
+ let to = [at.x + dx, at.y];
+ run(s, "sketch.drag", json!({"entity": point, "from": [at.x, at.y], "to": to, "live": true}));
+ solidcraft_geom::Vec2::new(to[0], to[1])
+}
+
+#[test]
+fn a_live_gesture_projects_a_face_sketchs_links_once() {
+ // Every frame projected the face's edges again and solved the sketch once more for them,
+ // which on a face with many edges made each frame take a large fraction of a second (#3222).
+ let (mut s, point, mut at) = on_a_face();
+ refresh_count::take();
+ for _ in 0..5 {
+ at = live_frame(&mut s, &point, at, 0.5);
+ }
+ assert_eq!(refresh_count::take(), 1, "the gesture's first frame projects the links, the rest reuse them");
+ assert_eq!(pt(&s, &point), at, "the corner follows the drag");
+ assert!(sketch_of(&s).links.iter().all(|l| !l.lost), "the links are kept");
+ run(&mut s, "sketch.drag_end", json!({}));
+ assert_eq!(pt(&s, &point), at);
+
+ // Anything run between two frames (another command, an undo) may have changed the model the
+ // links come from: the next frame projects them again.
+ refresh_count::take();
+ at = live_frame(&mut s, &point, at, 0.5);
+ run(&mut s, "sketch.solve", json!({}));
+ live_frame(&mut s, &point, at, 0.5);
+ assert_eq!(refresh_count::take(), 2);
+ run(&mut s, "edit.undo", json!({}));
+ let at = pt(&s, &point);
+ refresh_count::take();
+ live_frame(&mut s, &point, at, 0.5);
+ assert_eq!(refresh_count::take(), 1);
+}
diff --git a/crates/engine/src/cmd/sketch_project.rs b/crates/engine/src/cmd/sketch_project.rs
index 07a3ee9233..15d508734f 100644
--- a/crates/engine/src/cmd/sketch_project.rs
+++ b/crates/engine/src/cmd/sketch_project.rs
@@ -178,6 +178,8 @@ pub(super) fn refresh(s: &Session, doc: &Document, id: u64, sk: &mut Sketch) ->
if sk.links.is_empty() {
return Vec::new();
}
+ #[cfg(test)]
+ refresh_count::record();
let doc = placed_candidate(s, doc);
let (vals, _) = doc.param_values();
// A text's outlines are rebuilt from its numbers: the ones its expressions give come first
@@ -209,6 +211,25 @@ pub(super) fn refresh(s: &Session, doc: &Document, id: u64, sk: &mut Sketch) ->
warnings
}
+/// Refreshes of a sketch's links run on this thread by [`refresh`].
+#[cfg(test)]
+pub(super) mod refresh_count {
+ use std::cell::Cell;
+
+ thread_local! {
+ static COUNT: Cell<usize> = const { Cell::new(0) };
+ }
+
+ pub fn record() {
+ COUNT.with(|c| c.set(c.get() + 1));
+ }
+
+ /// The count since the last call.
+ pub fn take() -> usize {
+ COUNT.with(|c| c.replace(0))
+ }
+}
+
/// Add a link for `src` to sketch `id` in `doc` (not stored in the session). Returns the link id.
pub(super) fn add_link(s: &Session, doc: &Document, id: u64, sk: &mut Sketch, kind: LinkKind, src: LinkSource) -> Result<String> {
let doc = placed_candidate(s, doc);
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index 86399cd9e2..1ae0021732 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -275,6 +275,10 @@ pub struct Session {
/// How calls that break a command's parameter contract are treated (off by default). It
/// belongs to the session, not the design, so it survives opening and starting designs.
pub param_check: schema::ParamCheck,
+ /// After a live frame: its sketch, the document it left and the revision. Its links were
+ /// brought up to date by that frame, so the gesture's next frame needn't project them again
+ /// while nothing else has run (#3222).
+ pub(crate) live_links: Option<(u64, Arc<Document>, u64)>,
/// The component new sketches and features go into (0 = the root).
pub active_component: u64,
/// The placement used when authoring shared component geometry.
@@ -444,6 +448,7 @@ impl Session {
revision: 1,
auto_project: true,
param_check: schema::ParamCheck::Off,
+ live_links: None,
active_component: 0,
active_occurrence_path: None,
pending_moves: Default::default(),
@@ -870,8 +875,11 @@ impl Session {
let host_jobs = self.render_jobs.host.clone();
let additional_jobs = self.render_jobs.additional.clone();
let live = spec.live.filter(|_| params.get("live").and_then(Value::as_bool) == Some(true));
- if live.is_none() && spec.reads_model && self.model.is_stale() {
- self.refresh();
+ if live.is_none() {
+ self.live_links = None;
+ if spec.reads_model && self.model.is_stale() {
+ self.refresh();
+ }
}
(spec.enabled)(self).map_err(|why| EngineError::Disabled(spec.id.to_string(), why))?;
let before = Snapshot {
@@ -943,7 +951,11 @@ impl Session {
self.redo.clear();
}
match live_edit {
- Some(edit) => self.refresh_live(edit),
+ Some(edit) => {
+ let sketch = edit.sketch;
+ self.refresh_live(edit);
+ self.live_links = Some((sketch, self.doc.clone(), self.revision));
+ }
None if render_only => self.refresh_render(&previous_doc),
None => self.refresh_from(&previous_doc),
}
@@ -1047,6 +1059,7 @@ impl Session {
revision: self.revision,
auto_project: self.auto_project,
param_check: self.param_check,
+ live_links: None,
active_component: self.active_component,
active_occurrence_path: self.active_occurrence_path.clone(),
pending_moves: self.pending_moves.clone(),