SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
perf: solve the joints once per parameter change (#6372)
NOWN-DEV2b620338dfa5
diff --git a/crates/engine/src/cmd/joints_tests.rs b/crates/engine/src/cmd/joints_tests.rsindex 5c2b4d2f47..095f5856f8 100644--- a/crates/engine/src/cmd/joints_tests.rs+++ b/crates/engine/src/cmd/joints_tests.rs@@ -2070,3 +2070,47 @@ fn joint_drive_and_refresh_do_not_repeat_dof_analysis() {assert_eq!(*s.doc, *before);}}++/// A parameter change solves the joints once: the refresh that follows the command finds the+/// design and the model as the command's own solve left them (the sketch the parameter drives+/// has its new solution stored after that solve). A move the joints undo is still solved.+#[test]+fn a_parameter_change_solves_the_joints_once() {+ let mut s = Session::default();+ run(&mut s, "parameters.change", json!({"name": "W", "expression": "300 mm"}));+ run(&mut s, "component.create", json!({"name": "Plate"}));+ run(&mut s, "solid.box", json!({"length": "W", "width": 100, "height": 1, "corner": [0, 0, 0]}));+ run(&mut s, "solid.hole", json!({"position": ["W / 2", 50, 1], "diameter": 6}));+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S"}));+ let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 30]}));+ let bottom = r["curves"][0].as_str().unwrap_or_default().to_string();+ run(&mut s, "sketch.dimension", json!({"entities": [bottom], "value": "W / 10"}));+ run(&mut s, "sketch.finish", json!({}));+ let plate = occ(&s, "Plate");+ run(&mut s, "occurrence.ground", json!({"occurrence": plate, "grounded": true}));+ run(&mut s, "component.activate", json!({"component": "root"}));+ run(&mut s, "component.create", json!({"name": "Bracket"}));+ run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 2, "corner": [100, 200, 0]}));+ run(&mut s, "solid.hole", json!({"position": [110, 210, 2], "diameter": 6}));+ let r = run(+ &mut s,+ "joint.create",+ json!({"type": "rigid", "a": {"occurrence": "Plate", "circle": [153, 50, 0.8], "x": [1, 0, 0]},+ "b": {"occurrence": "Bracket", "circle": [113, 210, 0.4], "x": [1, 0, 0]}}),+ );+ assert_eq!(r["errors"].as_array().map(Vec::len), Some(0), "{r}");+ run(&mut s, "component.activate", json!({"component": "root"}));+ for w in [400.0, 360.0] {+ super::solve_counts::take();+ run(&mut s, "parameters.change", json!({"name": "W", "expression": format!("{w} mm")}));+ assert_eq!(super::solve_counts::take().0, 1, "W = {w}");+ let at = place(&s, "Bracket", Vec3::new(110.0, 210.0, 0.0));+ assert!(near(at, Vec3::new(w / 2.0, 50.0, 1.0)), "W = {w}: the bracket follows the hole, {at:?}");+ }+ // Moving the bracket changes no feature, so the model stays; the joint still takes it back.+ let bracket = occ(&s, "Bracket");+ super::solve_counts::take();+ run(&mut s, "occurrence.move", json!({"occurrence": bracket, "translate": [5, 0, 0], "capture": true}));+ assert!(super::solve_counts::take().0 > 0);+ assert!(near(place(&s, "Bracket", Vec3::new(110.0, 210.0, 0.0)), Vec3::new(180.0, 50.0, 1.0)));+}diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 9fe13e714c..ffd8c13bd7 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -291,6 +291,9 @@ pub struct Session {/// Occurrence moves not captured yet (Capture Position keeps them, a recompute drops them).pub pending_moves: std::collections::BTreeMap<u64, solidcraft_doc::Mat>,world_cache: std::sync::Mutex<Option<WorldCache>>,+ /// The design the joints were last solved for, as the solve left it, and the model it read+ /// (see [`Session::solve_joints`]).+ joints_settled: Option<(Arc<Document>, std::sync::Weak<solidcraft_doc::ModelState>)>,occurrence_visibility_cache: std::sync::Mutex<Option<occurrences::VisibilityCache>>,removed_later_cache: std::sync::Mutex<Option<occurrences::RemovedLaterCache>>,occurrence_bodies_cache: std::sync::Mutex<Option<occurrences::OccurrenceBodiesCache>>,@@ -461,6 +464,7 @@ impl Session {active_occurrence_path: None,pending_moves: Default::default(),world_cache: Default::default(),+ joints_settled: None,occurrence_visibility_cache: Default::default(),removed_later_cache: Default::default(),occurrence_bodies_cache: Default::default(),@@ -702,7 +706,7 @@ impl Session {self.prune_occurrence_state();// Joints follow the geometry their origins snap to.if !self.doc.assembly.joints.is_empty() {- cmd::joints::place(self);+ self.solve_joints();}cmd::motion::associative::refresh(self);if let Some(id) = self.active_sketch@@ -712,6 +716,23 @@ impl Session {}}+ /// Solve the joints, unless nothing they read changed since this session last solved them:+ /// the model is the one that solve read and the design is as the solve left it, apart from+ /// its features (the joints read those only through the model). A command that refreshes on+ /// its own (a parameter change) is refreshed again after it, and that second solve would+ /// find every occurrence where it is.+ fn solve_joints(&mut self) {+ let state = self.model.state();+ let settled = self.joints_settled.as_ref().is_some_and(|(doc, model)| {+ std::sync::Weak::as_ptr(model) == Arc::as_ptr(&state) && (Arc::ptr_eq(doc, &self.doc) || same_but_features(doc, &self.doc))+ });+ if settled {+ return;+ }+ cmd::joints::place(self);+ self.joints_settled = Some((self.doc.clone(), Arc::downgrade(&self.model.state())));+ }+/// [`Session::refresh`] after the design changed from `previous`; a change that only renamed/// a body or a feature (or undid or redid such a rename) renames it in the model instead,/// which keeps every result and mesh (#3044).@@ -723,7 +744,7 @@ impl Session {{self.revision += 1;if !self.doc.assembly.joints.is_empty() {- cmd::joints::place(self);+ self.solve_joints();}return;}@@ -1101,6 +1122,7 @@ impl Session {active_occurrence_path: self.active_occurrence_path.clone(),pending_moves: self.pending_moves.clone(),world_cache: std::sync::Mutex::new(self.world_cache.lock().ok().and_then(|cache| cache.clone())),+ joints_settled: self.joints_settled.clone(),occurrence_visibility_cache: Default::default(),removed_later_cache: Default::default(),occurrence_bodies_cache: Default::default(),@@ -1379,3 +1401,73 @@ fn image_host_availability(host: Option<&render::job::ImageHost>, expected: &renNone => render::Availability::Unavailable { reason: "image host is unavailable".into() },}}++/// Whether two designs differ at most in their features. Every field is named, so a new one has+/// to be decided on here.+fn same_but_features(a: &Document, b: &Document) -> bool {+ let Document {+ render,+ format,+ format_pending,+ name,+ units,+ metadata,+ params,+ features: _,+ marker,+ next_id,+ reserved_body_names,+ components,+ occurrences,+ assembly,+ sheet,+ plastic,+ parts,+ links,+ configs,+ favorites,+ canvases,+ param_comments,+ materials,+ user_materials,+ appearances,+ body_components,+ body_offsets,+ browser_groups,+ timeline_groups,+ browser_order,+ named_views,+ visibility,+ } = a;+ *render == b.render+ && *format == b.format+ && *format_pending == b.format_pending+ && *name == b.name+ && *units == b.units+ && *metadata == b.metadata+ && *params == b.params+ && *marker == b.marker+ && *next_id == b.next_id+ && *reserved_body_names == b.reserved_body_names+ && *components == b.components+ && *occurrences == b.occurrences+ && *assembly == b.assembly+ && *sheet == b.sheet+ && *plastic == b.plastic+ && *parts == b.parts+ && *links == b.links+ && *configs == b.configs+ && *favorites == b.favorites+ && *canvases == b.canvases+ && *param_comments == b.param_comments+ && *materials == b.materials+ && *user_materials == b.user_materials+ && *appearances == b.appearances+ && *body_components == b.body_components+ && *body_offsets == b.body_offsets+ && *browser_groups == b.browser_groups+ && *timeline_groups == b.timeline_groups+ && *browser_order == b.browser_order+ && *named_views == b.named_views+ && *visibility == b.visibility+}