* claim: #4609 * Mesh Convert works without activating the component first (#4609) Inserting a mesh puts it in a new component while the parent stays active, so Convert Mesh always refused the first time. Convert now resolves the mesh body's owning component and adds the conversion there when the active component is that component or an ancestor, leaving the active component unchanged. From another branch it still refuses, and the error names the component to activate.
58 lines
2.6 KiB
Rust
58 lines
2.6 KiB
Rust
//! Parametric reconstruction of imported mechanical mesh bodies.
|
|
|
|
use serde_json::Value;
|
|
use solidcraft_doc::FeatureKind;
|
|
use solidcraft_doc::expr::Kind;
|
|
|
|
use super::{
|
|
CommandSpec,
|
|
features::{add_feature, check_expr},
|
|
};
|
|
use crate::{
|
|
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 (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 in the mesh body's owning component (the active component must be that component or an ancestor; otherwise activate the named component first); the active component is unchanged; 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 state = s.model.state();
|
|
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"));
|
|
}
|
|
let owner = s.doc.model_body_component(source);
|
|
if !s.doc.component_within(owner, s.active_component) {
|
|
let name = s.doc.components.iter().find(|c| c.id == owner).map(|c| c.name.as_str()).unwrap_or(s.doc.name.as_str());
|
|
return Err(bad(cmd, format!("activate component '{name}' first")));
|
|
}
|
|
let tolerance = expr(p, "tolerance").unwrap_or_else(|| "0.05 mm".into());
|
|
check_expr(s, &tolerance, Kind::Length, cmd, "tolerance")?;
|
|
// Conversion edits the component definition, independent of its occurrence placement.
|
|
let active_component = std::mem::replace(&mut s.active_component, owner);
|
|
let active_occurrence_path = s.active_occurrence_path.take();
|
|
let result = add_feature(s, p, FeatureKind::MeshConvert { body: body.into(), tolerance });
|
|
s.active_component = active_component;
|
|
s.active_occurrence_path = active_occurrence_path;
|
|
result
|
|
}
|