SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Extrude dialog: work out the automatic operation once, not every frame (#10491)
kaltenbrunnermarkus74eda80b5d2c
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex a261d14c22..0cf27ab749 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -3105,7 +3105,29 @@ fn auto_operation(app: &SolveApp, d: &mut Dialog) {nothing_shown = shown.is_empty();params["participants"] = json!(shown);}- let fresh = if nothing_shown { Some("new") } else { solidcraft_engine::auto_operation(&app.session, ¶ms) };+ let fresh = if nothing_shown {+ Some("new")+ } else {+ // Asked again only when the parameters or the design change: it casts rays through+ // every triangle of the bodies, milliseconds a frame on a large part.+ let key = auto_operation_key(app, ¶ms);+ let cached = match &d.kind {+ Kind::Extrude { smart, .. } => smart.auto.filter(|(k, _)| *k == key).map(|(_, op)| op),+ _ => None,+ };+ match cached {+ Some(op) => op,+ None => {+ #[cfg(test)]+ AUTO_OPERATION_ASKED.with(|n| n.set(n.get() + 1));+ let op = solidcraft_engine::auto_operation(&app.session, ¶ms);+ if let Kind::Extrude { smart, .. } = &mut d.kind {+ smart.auto = Some((key, op));+ }+ op+ }+ }+ };let Some(fresh) = fresh.and_then(|op| OPS.iter().position(|o| *o == op)) else {return;};@@ -3114,6 +3136,31 @@ fn auto_operation(app: &SolveApp, d: &mut Dialog) {}}+/// What [`solidcraft_engine::auto_operation`] depends on besides its parameters: the design+/// (its revision counts every change, shown bodies too), which design it is, and the active+/// component the extrude goes into.+fn auto_operation_key(app: &SolveApp, params: &Value) -> u64 {+ use std::hash::{Hash, Hasher};+ let mut h = std::collections::hash_map::DefaultHasher::new();+ params.to_string().hash(&mut h);+ app.session.revision.hash(&mut h);+ app.session.document_id.hash(&mut h);+ app.session.active_component.hash(&mut h);+ app.session.active_occurrence_path.hash(&mut h);+ h.finish()+}++#[cfg(test)]+thread_local! {+ static AUTO_OPERATION_ASKED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };+}++/// How often the extrude window asked the engine for its automatic operation on this thread.+#[cfg(test)]+pub(crate) fn auto_operation_asked() -> usize {+ AUTO_OPERATION_ASKED.with(std::cell::Cell::get)+}+/// The one-side direction a new extrude takes for its first pick: toward the side of a sketch/// profile (or curve) the camera looks from; None for picks that keep their own way.fn picked_side(app: &SolveApp, d: &Dialog, first: &Sel) -> Option<usize> {diff --git a/crates/ui-egui/src/smart_extrude.rs b/crates/ui-egui/src/smart_extrude.rsindex 677142f450..ec8989d77e 100644--- a/crates/ui-egui/src/smart_extrude.rs+++ b/crates/ui-egui/src/smart_extrude.rs@@ -40,6 +40,9 @@ pub struct Smart {pub shown: Option<Vec<String>>,/// Picks off the profile plane become the To Object target (Preferences › Design).pub auto_to_object: bool,+ /// The engine's automatic operation for the last parameters and design: a key over all it+ /// depends on, and its answer. It probes the bodies' triangles, too slow to ask every frame.+ pub auto: Option<(u64, Option<&'static str>)>,}/// One-side direction for a new pick: 0 (along the normal) or 1 (reversed). `ray` is the viewdiff --git a/crates/ui-egui/src/smart_extrude_ui_tests.rs b/crates/ui-egui/src/smart_extrude_ui_tests.rsindex aa15dbb944..cac58c3914 100644--- a/crates/ui-egui/src/smart_extrude_ui_tests.rs+++ b/crates/ui-egui/src/smart_extrude_ui_tests.rs@@ -243,3 +243,24 @@ fn pulling_a_face_down_into_the_body_cuts() {h.frames(4);assert_eq!(state(&h).1, CUT, "pushed into the box: Cut");}++/// The automatic operation probes the bodies' triangles: an open window asks for it once per+/// change of its inputs or the design, not every frame.+#[test]+fn an_open_extrude_asks_for_its_operation_only_when_something_changes() {+ let mut h = box_with_top_extrude(false);+ let asked = crate::dialogs::auto_operation_asked();+ h.frames(20);+ assert_eq!(crate::dialogs::auto_operation_asked(), asked, "nothing changed: not asked again");+ drag_to(&mut h, 5.0);+ assert_eq!(state(&h), (0, CUT, true), "into the box: Cut");+ let after_drag = crate::dialogs::auto_operation_asked();+ assert!(after_drag > asked, "a new distance is asked about");+ h.frames(20);+ assert_eq!(crate::dialogs::auto_operation_asked(), after_drag, "held still: not asked again");+ // A change to the design (another body, out of the extrude's way) is asked about again.+ h.app.run("solid.box", json!({"length":5,"width":5,"height":5,"corner":[50,0,0]})).unwrap();+ h.frames(2);+ assert!(crate::dialogs::auto_operation_asked() > after_drag, "a changed design is asked about");+ assert_eq!(state(&h), (0, CUT, true), "still into the box: Cut");+}