* 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.
This commit is contained in:
@@ -16,7 +16,7 @@ use crate::{
|
||||
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; 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 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.
|
||||
@@ -40,10 +40,18 @@ fn convert(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if !source.body.is_mesh() {
|
||||
return Err(bad(cmd, "select a mesh body"));
|
||||
}
|
||||
if s.doc.model_body_component(source) != s.active_component {
|
||||
return Err(bad(cmd, "activate the mesh body's component before converting it"));
|
||||
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")?;
|
||||
add_feature(s, p, FeatureKind::MeshConvert { body: body.into(), 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
|
||||
}
|
||||
|
||||
@@ -216,3 +216,61 @@ fn installed_slicers_are_listed() {
|
||||
let r = s.execute("file.slicers", &json!({})).unwrap();
|
||||
assert!(r["slicers"].is_array());
|
||||
}
|
||||
|
||||
fn insert_box_mesh(s: &mut Session, suffix: &str) -> (String, u64) {
|
||||
let mut source = imported_box();
|
||||
let path = std::env::temp_dir().join(format!("solidcraft-convert-component-{}-{suffix}.stl", std::process::id()));
|
||||
run(&mut source, "file.save_mesh", json!({"path":path}));
|
||||
let result = run(s, "file.insert_mesh", json!({"path":path,"name":"Mesh 1"}));
|
||||
std::fs::remove_file(path).unwrap();
|
||||
let component = result["component"].as_u64().unwrap();
|
||||
let state = s.model.state();
|
||||
let body = state.bodies.iter().find(|body| s.doc.model_body_component(body) == component).unwrap().name.clone();
|
||||
(body, component)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inserted_mesh_converts_in_its_owner_from_an_active_ancestor() {
|
||||
for nested in [false, true] {
|
||||
let mut s = Session::default();
|
||||
if nested {
|
||||
run(&mut s, "component.create", json!({"name":"Assembly"}));
|
||||
}
|
||||
let context = (s.active_component, s.active_occurrence_path.clone());
|
||||
let (body, owner) = insert_box_mesh(&mut s, if nested { "nested" } else { "root" });
|
||||
let before = s.doc.to_json();
|
||||
assert!(s.execute("mesh.convert", &json!({"body":body,"tolerance":"2 mm"})).is_err());
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
assert_eq!((s.active_component, s.active_occurrence_path.clone()), context);
|
||||
run(&mut s, "mesh.convert", json!({"body":body,"tolerance":"0.01 mm"}));
|
||||
assert_eq!((s.active_component, s.active_occurrence_path.clone()), context);
|
||||
let check = |s: &Session| {
|
||||
let state = s.model.state();
|
||||
let converted = state.body(&body).unwrap();
|
||||
assert!(!converted.body.is_mesh());
|
||||
assert_eq!(converted.body.faces(0.01).unwrap().len(), 6);
|
||||
assert_eq!(s.doc.model_body_component(converted), owner);
|
||||
assert_eq!(s.doc.feature(converted.feature).unwrap().component, owner);
|
||||
assert!(s.model.results.iter().all(|r| r.error.is_none()));
|
||||
};
|
||||
check(&s);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
run(&mut s, "edit.redo", json!({}));
|
||||
check(&s);
|
||||
check(&Session::new(Document::from_json(&s.doc.to_json()).unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conversion_from_another_branch_names_the_component_to_activate() {
|
||||
let mut s = Session::default();
|
||||
let (body, _) = insert_box_mesh(&mut s, "other-branch");
|
||||
run(&mut s, "component.create", json!({"name":"Other"}));
|
||||
let before = s.doc.to_json();
|
||||
let context = (s.active_component, s.active_occurrence_path.clone());
|
||||
let error = s.execute("mesh.convert", &json!({"body":body})).unwrap_err().to_string();
|
||||
assert!(error.contains("activate component 'Mesh 1' first"), "{error}");
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
assert_eq!((s.active_component, s.active_occurrence_path.clone()), context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user