Skip to content

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

Sketch: leaving a sketch opened inside a component gives the active component back (#8127) (#8157)

phanoobdfa9afd39f30
diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs
index c9d4eafeda..e794ea88f2 100644
--- a/crates/ui-egui/src/browser.rs
+++ b/crates/ui-egui/src/browser.rs
@@ -2478,10 +2478,7 @@ fn entry_row(
}
if r.double && folder == "sketches" {
if let Ok(id) = e.key.parse() {
- if let Some(path) = path {
- let _ = app.run("component.activate", json!({"component": comp,"occurrence":path.last(),"path":path}));
- }
- app.edit_sketch(id);
+ app.edit_sketch_in(id, path.map(|path| (comp, path)));
}
} else if r.clicked {
let shift = ui.input(|i| i.modifiers.shift);
diff --git a/crates/ui-egui/src/documents.rs b/crates/ui-egui/src/documents.rs
index c6ba36de0e..449af1df42 100644
--- a/crates/ui-egui/src/documents.rs
+++ b/crates/ui-egui/src/documents.rs
@@ -27,6 +27,7 @@ pub struct DocSlot {
pre_sketch_marker: Option<Option<usize>>,
pub(crate) pending_feature_edit: Option<crate::feature_edit::PendingFeatureEdit>,
pre_sketch_cam: Option<crate::PreSketchView>,
+ pre_sketch_component: Option<crate::PreSketchComponent>,
view: DocView,
occurrence_move: Option<crate::browser::OccMove>,
agent_chat: crate::agent_chat::AgentChat,
@@ -55,6 +56,7 @@ impl DocSlot {
pre_sketch_marker: None,
pending_feature_edit: None,
pre_sketch_cam: None,
+ pre_sketch_component: None,
view: DocView::default(),
occurrence_move: None,
agent_chat: Default::default(),
@@ -148,6 +150,7 @@ fn swap_in(app: &mut SolveApp, mut slot: DocSlot) -> DocSlot {
pre_sketch_marker: std::mem::replace(&mut app.pre_sketch_marker, slot.pre_sketch_marker),
pending_feature_edit: std::mem::replace(&mut app.pending_feature_edit, slot.pending_feature_edit),
pre_sketch_cam: std::mem::replace(&mut app.pre_sketch_cam, slot.pre_sketch_cam),
+ pre_sketch_component: std::mem::replace(&mut app.pre_sketch_component, slot.pre_sketch_component),
view: take_view(app),
occurrence_move: std::mem::replace(&mut app.tree.occurrence_move, slot.occurrence_move),
agent_chat: slot.agent_chat,
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index fcc89588a7..c1775c199f 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -430,6 +430,16 @@ pub struct PreSketchView {
pub facing: Camera,
}
+/// The active component (and placement) before a sketch opened inside another one (#8127), and
+/// the component the sketch opened in.
+#[derive(Clone, Debug, PartialEq)]
+pub struct PreSketchComponent {
+ pub sketch: u64,
+ pub component: u64,
+ pub path: Option<Vec<u64>>,
+ pub opened_in: u64,
+}
+
pub struct SolveApp {
/// Compact browser embed: retain model panels and joint controls without application chrome.
pub embedded: bool,
@@ -447,6 +457,9 @@ pub struct SolveApp {
/// The views around opening the open sketch. Leaving it goes back to the earlier one as the
/// "When a sketch ends" preference says (#7038).
pub pre_sketch_cam: Option<PreSketchView>,
+ /// The component active before Edit Sketch activated the one the sketch is in (#8127);
+ /// leaving the sketch makes it active again.
+ pub pre_sketch_component: Option<PreSketchComponent>,
pub services: Services,
file_picker: Option<file_picker::Pending>,
/// A slicer started alone and waited for before the file is sent (Send to Slicer).
@@ -665,6 +678,7 @@ impl SolveApp {
now: 0.0,
pre_sketch_marker: None,
pre_sketch_cam: None,
+ pre_sketch_component: None,
services,
file_picker: None,
slicer_wait: None,
@@ -1642,9 +1656,22 @@ impl SolveApp {
/// it, and remember the marker so Finish Sketch puts it back. Editing the sketch that is already open does nothing; editing another one
/// finishes the current sketch first, so edit sessions never nest.
pub fn edit_sketch(&mut self, id: u64) {
+ self.edit_sketch_in(id, None);
+ }
+
+ /// Edit a sketch as [`SolveApp::edit_sketch`] does, in one placement of its component (a
+ /// sketch row inside a component in the browser): that component is active while the sketch
+ /// is open, and the one active before comes back when the sketch ends (#8127).
+ pub fn edit_sketch_in(&mut self, id: u64, placement: Option<(u64, &[u64])>) {
+ let activate = |app: &mut Self| {
+ if let Some((component, path)) = placement {
+ let _ = app.run("component.activate", json!({"component": component, "occurrence": path.last(), "path": path}));
+ }
+ };
if let solidcraft_engine::doc::FaceAttachmentStatus::Lost { message, .. } =
self.session.placed_document().sketch_face_attachment(id, &self.session.model)
{
+ activate(self);
browser::redefine_sketch(self, id);
self.set_status(message, true);
return;
@@ -1655,19 +1682,39 @@ impl SolveApp {
let Some(idx) = self.session.doc.feature_index(id) else { return };
let marker = self.finish_sketch_for_edit();
let before = self.settled_cam();
+ let context = (self.session.active_component, self.session.active_occurrence_path.clone());
self.tool = None;
self.dialog = None;
+ activate(self);
let rolled = self.session.doc.marker != Some(idx + 1) && self.run("timeline.roll_to", json!({ "position": idx + 1 })).is_ok();
if self.run("sketch.edit", json!({ "sketch": id })).is_err() {
if rolled || marker != self.session.doc.marker {
self.restore_marker(marker);
}
+ self.restore_component(context.0, context.1);
return;
}
self.pre_sketch_marker = (rolled || marker != self.session.doc.marker).then_some(marker);
+ // `sketch.edit` itself activates the component a face-attached sketch was drawn in.
+ let opened_in = self.session.active_component;
+ let moved = (opened_in, &self.session.active_occurrence_path) != (context.0, &context.1);
+ self.pre_sketch_component = moved.then_some(PreSketchComponent { sketch: id, component: context.0, path: context.1, opened_in });
self.look_at_opened_sketch(before);
}
+ /// Make a component active again, in the placement it was active in.
+ fn restore_component(&mut self, component: u64, path: Option<Vec<u64>>) {
+ if (self.session.active_component, &self.session.active_occurrence_path) == (component, &path) {
+ return;
+ }
+ let mut p = json!({ "component": component });
+ if let Some(path) = path {
+ p["occurrence"] = json!(path.last());
+ p["path"] = json!(path);
+ }
+ let _ = self.run("component.activate", p);
+ }
+
/// Turn the view to face the sketch just opened, and remember the view from `before` it and
/// the one facing it for when the sketch ends (#7038).
pub(crate) fn look_at_opened_sketch(&mut self, before: Camera) {
@@ -1721,6 +1768,10 @@ impl SolveApp {
if let Some(marker) = self.pre_sketch_marker.take() {
self.restore_marker(marker);
}
+ // Unless another component was activated meanwhile: that one stays.
+ if let Some(pre) = self.pre_sketch_component.take().filter(|pre| Some(pre.sketch) == open && pre.opened_in == self.session.active_component) {
+ self.restore_component(pre.component, pre.path);
+ }
// Only the views saved when this sketch opened: one opened another way saved none.
if let Some(view) = self.pre_sketch_cam.take().filter(|v| Some(v.sketch) == open)
&& let Some(to) = self.preferences.sketch_exit_view.view_after(view.before, view.facing, self.settled_cam())
diff --git a/crates/ui-egui/src/sketch_attachment_ui_tests.rs b/crates/ui-egui/src/sketch_attachment_ui_tests.rs
index 7fa8ed3951..46f4472eca 100644
--- a/crates/ui-egui/src/sketch_attachment_ui_tests.rs
+++ b/crates/ui-egui/src/sketch_attachment_ui_tests.rs
@@ -229,6 +229,7 @@ fn actual_nested_second_placement_face_pick_keeps_target_and_author_paths() {
assert_eq!(h.app.session.active_component, child_component);
assert_eq!(h.app.session.active_occurrence_path.as_ref(), Some(&path));
h.app.finish_sketch();
+ assert_eq!((h.app.session.active_component, h.app.session.active_occurrence_path.as_ref()), (0, None), "the design is active again (#8127)");
assert_eq!(h.app.session.doc.feature(sketch).unwrap().kind, kind);
let file = h.dir.join("nested-face.solidcraft");
h.app.run("file.save", json!({"path":file})).unwrap();
diff --git a/crates/ui-egui/src/sketch_edit_tests.rs b/crates/ui-egui/src/sketch_edit_tests.rs
index 8c7fe7731f..882ee39ec0 100644
--- a/crates/ui-egui/src/sketch_edit_tests.rs
+++ b/crates/ui-egui/src/sketch_edit_tests.rs
@@ -367,3 +367,48 @@ mod exit_view_preference {
assert_eq!(SketchExitView::UnlessRotated.view_after(before, facing, turned), None);
}
}
+
+/// #8127: a sketch inside an inserted fastener, opened from its row in the browser, activates the
+/// fastener's component while it is open; Finish and Cancel make the design active again, so the
+/// other bodies are no longer drawn faded. Editing it with the fastener already active leaves the
+/// fastener active, and so does activating another component while the sketch is open.
+#[test]
+fn a_sketch_opened_inside_a_component_gives_the_active_component_back() {
+ let mut app = sample_app();
+ let bolt = app.run("parts.insert", serde_json::json!({"family": "socket_head_cap_screw", "size": "M6", "point": [0, 0, 0]})).unwrap();
+ let comp = bolt["component"].as_u64().unwrap();
+ let path = app.session.doc.first_path(comp);
+ let sketch = app.session.doc.features.iter().find(|f| f.component == comp && matches!(f.kind, FeatureKind::Sketch { .. })).map(|f| f.id).unwrap();
+ let marker = app.session.doc.marker;
+ for leave in [SolveApp::finish_sketch as fn(&mut SolveApp), SolveApp::cancel_sketch] {
+ app.edit_sketch_in(sketch, Some((comp, &path)));
+ assert_eq!(
+ (app.session.active_sketch, app.session.active_component),
+ (Some(sketch), comp),
+ "the fastener is active while its sketch is open"
+ );
+ leave(&mut app);
+ assert_eq!((app.session.active_sketch, app.session.active_component), (None, 0), "the design is active again");
+ assert_eq!(app.session.active_occurrence_path, None);
+ assert_eq!(app.session.doc.marker, marker);
+ }
+ // Switching to a sketch of the design from the fastener's leaves nothing of the fastener active.
+ app.edit_sketch_in(sketch, Some((comp, &path)));
+ app.edit_sketch(first_sketch(&app));
+ assert_eq!(app.session.active_component, 0);
+ app.finish_sketch();
+ assert_eq!(app.session.active_component, 0);
+ // The fastener was active already: it stays active.
+ app.run("component.activate", serde_json::json!({"component": comp})).unwrap();
+ app.edit_sketch_in(sketch, Some((comp, &path)));
+ app.finish_sketch();
+ assert_eq!(app.session.active_component, comp);
+ // Another component activated while the sketch is open stays active.
+ app.run("component.activate", serde_json::json!({"component": 0})).unwrap();
+ let washer = app.run("parts.insert", serde_json::json!({"family": "washer", "size": "M6", "point": [20, 0, 0]})).unwrap();
+ let washer = washer["component"].as_u64().unwrap();
+ app.edit_sketch_in(sketch, Some((comp, &path)));
+ app.run("component.activate", serde_json::json!({"component": washer})).unwrap();
+ app.finish_sketch();
+ assert_eq!(app.session.active_component, washer, "the component activated meanwhile stays active");
+}