Skip to content

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

Convert Mesh uses the selected mesh body (#2482)

plexi32a1badaed1768
diff --git a/crates/engine/src/cmd/mesh.rs b/crates/engine/src/cmd/mesh.rs
index 702270f73b..d7e22bcd9b 100644
--- a/crates/engine/src/cmd/mesh.rs
+++ b/crates/engine/src/cmd/mesh.rs
@@ -9,21 +9,34 @@ use super::{
features::{add_feature, check_expr},
};
use crate::{
- Result, Session,
+ Result, Sel, Session,
params::{bad, expr, str_},
};
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("mesh.convert", "Convert Mesh", convert)
.icon("import")
- .params("body: mesh body name; tolerance?: length expression (default 0.05 mm): reconstruct a closed constant-section mechanical mesh as editable planar and cylindrical faces, with a parametric timeline feature; name?"),
+ .params("body?: mesh body name (default: the selected mesh body); tolerance?: length expression (default 0.05 mm): reconstruct a closed constant-section mechanical mesh as editable planar and cylindrical faces, with a parametric timeline feature; name?"),
];
+/// The mesh body to convert: `body`, else the one selected body.
+fn target(s: &Session, p: &Value, cmd: &str) -> Result<String> {
+ if let Some(name) = str_(p, "body").filter(|b| !b.trim().is_empty()) {
+ return Ok(name.to_owned());
+ }
+ let selected: Vec<&str> = s.selection.iter().filter_map(|x| if let Sel::Body { name } = x { Some(name.as_str()) } else { None }).collect();
+ match selected.as_slice() {
+ [name] => Ok((*name).to_owned()),
+ [] => Err(bad(cmd, "select a mesh body")),
+ _ => Err(bad(cmd, "select one mesh body")),
+ }
+}
+
fn convert(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "mesh.convert";
- let body = str_(p, "body").filter(|b| !b.trim().is_empty()).ok_or_else(|| bad(cmd, "select a mesh body"))?;
let state = s.model.state();
- let source = state.body(body).ok_or_else(|| bad(cmd, format!("no body `{body}`")))?;
+ let body = target(s, p, cmd)?;
+ let source = state.body(&body).ok_or_else(|| bad(cmd, format!("no body `{body}`")))?;
if !source.body.is_mesh() {
return Err(bad(cmd, "select a mesh body"));
}
diff --git a/crates/engine/src/tests/mesh_convert.rs b/crates/engine/src/tests/mesh_convert.rs
index 881d4ef395..c7c3e91fe7 100644
--- a/crates/engine/src/tests/mesh_convert.rs
+++ b/crates/engine/src/tests/mesh_convert.rs
@@ -70,6 +70,34 @@ fn failed_mesh_conversions_leave_the_design_unchanged() {
}
}
+#[test]
+fn convert_uses_the_selected_mesh_body_when_body_is_omitted() {
+ let mut s = imported_box();
+ s.selection = vec![crate::Sel::Body { name: "Mesh".into() }];
+ run(&mut s, "mesh.convert", json!({"tolerance":"0.01 mm"}));
+ let state = s.model.state();
+ let body = &state.body("Mesh").unwrap().body;
+ assert!(!body.is_mesh());
+ assert_eq!(body.faces(0.01).unwrap().len(), 6);
+}
+
+#[test]
+fn convert_without_body_or_selection_is_refused() {
+ let mut s = imported_box();
+ let before = s.doc.to_json();
+ assert!(s.execute("mesh.convert", &json!({"tolerance":"0.01 mm"})).is_err());
+ assert_eq!(s.doc.to_json(), before);
+ assert!(s.model.state().body("Mesh").unwrap().body.is_mesh());
+}
+
+#[test]
+fn an_explicit_body_wins_over_the_selection() {
+ let mut s = imported_box();
+ s.selection = vec![crate::Sel::Body { name: "Missing".into() }];
+ run(&mut s, "mesh.convert", json!({"body":"Mesh","tolerance":"0.01 mm"}));
+ assert!(!s.model.state().body("Mesh").unwrap().body.is_mesh());
+}
+
#[test]
fn exported_binary_stl_with_a_bore_reconstructs_analytic_faces() {
let mut s = Session::default();