Convert Mesh uses the selected mesh body (#2482)

Convert Mesh uses the selected mesh body.
This commit is contained in:
plexi32
2026-10-19 23:22:44 +02:00
committed by GitHub
parent 3d105fb281
commit a1badaed17
2 changed files with 45 additions and 4 deletions
+17 -4
View File
@@ -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"));
}
+28
View File
@@ -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();