use std::collections::HashSet;
use crate::feature_pipeline::features::common;
use crate::feature_pipeline::{AddedSolid, FeatureContext, FeatureResult};
use crate::delete_faces_and_heal;
pub fn execute(ctx: &FeatureContext) -> FeatureResult {
match build(ctx) {
Ok(result) => result,
Err(error) => ctx.fail(error),
}
}
fn build(ctx: &FeatureContext) -> Result<FeatureResult, String> {
let mut result = FeatureResult::empty(ctx.id.clone(), ctx.feature_type.clone());
let names = common::reference_name_array(ctx.param("faces"));
if names.is_empty() {
return Ok(result); }
let mut resolved: Vec<(u32, u64)> = Vec::new();
for name in &names {
match ctx.scene.resolve_face(name) {
Some(face_ref) => resolved.push((face_ref.handle, face_ref.face_id)),
None => result.unresolved.push(name.clone()),
}
}
if resolved.is_empty() {
return Ok(result); }
let handles: HashSet<u32> = resolved.iter().map(|(handle, _)| *handle).collect();
if handles.len() > 1 {
return Ok(result); }
let target_handle = resolved[0].0;
let mut seen: HashSet<u64> = HashSet::new();
let face_ids: Vec<u64> = resolved
.iter()
.map(|(_, face_id)| *face_id)
.filter(|face_id| seen.insert(*face_id))
.collect();
let target_name = ctx
.scene
.solids
.iter()
.find(|(_, handle)| **handle == target_handle)
.map(|(name, _)| name.clone())
.ok_or("delete_face: target solid has no scene-map name")?;
let solid = crate::with_registered_solid_str(target_handle, |solid| Ok(solid.clone()))?;
let solid = delete_faces_and_heal(&solid, &face_ids)?;
let face_names = common::collect_face_names(&solid);
let edge_names = common::collect_edge_names(&solid);
let handle = crate::register_solid_value(solid);
result.added.push(AddedSolid {
handle,
name: target_name.clone(),
face_names,
edge_names,
..AddedSolid::default()
});
result.removed.push(target_name);
Ok(result)
}
pub fn context_applicable(probe: &crate::feature_pipeline::SelectionProbe) -> bool {
probe.faces > 0
}
pub fn schema() -> serde_json::Value {
serde_json::json!({
"type": "DF",
"shortName": "DF",
"longName": "Delete Face",
"displayBuilder": false,
"inputParamsSchema": {
"id": {
"type": "string",
"default_value": null,
"hint": "Optional identifier for the delete face feature"
},
"faces": {
"type": "reference_selection",
"selectionFilter": [
"FACE"
],
"timestampDependency": "parentSolid",
"multiple": true,
"default_value": [],
"hint": "Select the face(s) to remove: a transition face (chamfer / fillet / simple 4-sided face), whose neighbours re-intersect to heal the hole, or every face of a pocket / boss / bore, which is capped by the face it was sunk through. Both kinds can be picked together — each feature in the selection is healed the way its own shape asks for"
}
}
})
}