Skip to content

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

Extrude preview: a cut from an empty root is no longer refused (#3085) (#3363)

raykholo2daf22923cb3
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index d441131b67..2bd92b1eeb 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -182,6 +182,16 @@ pub fn auto_operation(s: &Session, p: &Value) -> Option<&'static str> {
auto_operation_in(s, p, true)
}
+impl Session {
+ /// Whether a cut or intersect that names no bodies takes part in every body of `st`: the
+ /// active component has no body of its own while the model has some (a sketch in the
+ /// assembly's root cutting through its parts, #1234). The command and its preview both ask
+ /// this, so the preview never refuses a cut the command makes (#3085).
+ pub fn cut_takes_every_body(&self, st: &solidcraft_doc::ModelState) -> bool {
+ !st.bodies.is_empty() && !st.bodies.iter().any(|b| self.doc.model_body_component(b) == self.active_component)
+ }
+}
+
fn auto_operation_in(s: &Session, p: &Value, world_faces: bool) -> Option<&'static str> {
let symmetric = p.get("to").is_none() && str_(p, "direction").is_some_and(|d| d.eq_ignore_ascii_case("symmetric"));
// Imported face groups can belong to a child of an empty active component.
@@ -503,10 +513,8 @@ fn add_feature_in(s: &mut Session, p: &Value, kind: FeatureKind, world: bool) ->
&& !primitive
&& (matches!(str_(p, "operation").and_then(Operation::parse), Some(Operation::Cut | Operation::Intersect))
|| matches!(&kind, FeatureKind::Extrude { operation: Operation::Cut | Operation::Intersect, .. }))
- && {
- let st = s.model.state();
- !st.bodies.is_empty() && !st.bodies.iter().any(|b| s.doc.model_body_component(b) == s.active_component)
- } {
+ && s.cut_takes_every_body(&s.model.state())
+ {
vec!["*".to_string()]
} else {
participants
diff --git a/crates/ui-egui/src/preview.rs b/crates/ui-egui/src/preview.rs
index 7b0161da26..b1bfee4b75 100644
--- a/crates/ui-egui/src/preview.rs
+++ b/crates/ui-egui/src/preview.rs
@@ -484,8 +484,13 @@ pub(crate) fn extrusion_target_error(s: &Session, cmds: &[(String, Value)], befo
}
}
} else {
+ // A cut or intersect from a component with no body of its own takes part in every
+ // body: the command decides that (#1234), and the preview asks it the same (#3085).
found = params.get("faces").and_then(|_| params.get("face_body")).and_then(Value::as_str).map_or_else(
- || before.bodies.iter().any(|body| s.doc.model_body_component(body) == s.active_component),
+ || {
+ before.bodies.iter().any(|body| s.doc.model_body_component(body) == s.active_component)
+ || (matches!(op, "cut" | "intersect") && s.cut_takes_every_body(before))
+ },
|name| s.world_state().body(name).is_some(),
);
}
@@ -1192,6 +1197,27 @@ mod tests {
assert_eq!(whole.scene.xray.len(), tool.scene.xray.len());
}
+ /// #3085: a cut sketched in the root, where every body lives in a component, cuts every body
+ /// it meets (#1234). The preview said "there is no body to cut or intersect" all the same,
+ /// while the cut itself went through.
+ #[test]
+ fn a_cut_from_an_empty_root_is_not_refused_by_its_preview() {
+ let mut s = session_of(&[
+ ("component.create", json!({"name": "Part"})),
+ ("solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Block"})),
+ ("component.activate", json!({"component": "root"})),
+ ("sketch.create", json!({"plane": "XY"})),
+ ("sketch.rectangle.two_point", json!({"p0": [3, 3], "p1": [7, 7]})),
+ ("sketch.finish", json!({})),
+ ]);
+ let params = json!({"distance": 20, "operation": "cut"});
+ let cmds = vec![("solid.extrude".to_string(), params.clone())];
+ assert_eq!(extrusion_target_error(&s, &cmds, &s.model.state()), None);
+ s.execute("solid.extrude", &params).unwrap();
+ let volume = s.model.state().body("Block").map(|b| solidcraft_engine::kernel::measure(&b.body).unwrap().volume).unwrap_or(0.0);
+ assert!((volume - (1000.0 - 160.0)).abs() < 1e-6, "{volume}");
+ }
+
/// While an arrow is dragged the inputs change faster than a tool is built: a tool built
/// for inputs since changed is shown anyway (then the latest computed), an error only for
/// the inputs it belongs to.