Skip to main content

brep_app/
delete_selection.rs

1//! THE delete-selection command: what the Delete / Backspace key, the context
2//! bar's Delete entries and MCP `selection_delete` all run.
3//!
4//! One question — what does "delete the selection" mean right now? — answered
5//! once (`target`), then one dispatch (`apply`) to the delete each kind
6//! already has: the same call the row ✕ / menu Delete makes, so the same (no)
7//! confirmation and the same single undo step. The modes are exclusive, in the
8//! order a user is inside them: an open sketch owns the key, then a 3D sketch,
9//! an eCAD editor, an open drawing sheet, a selected PMI annotation; otherwise the 3D
10//! selection is an assembly constraint (label-picked), one component (its
11//! member solids picked in the view or its BOM row), or one feature (its
12//! history-tree row). A sheet or PMI mode with nothing selected deletes
13//! nothing — it never falls through to a feature the user cannot see.
14
15use crate::document::Documents;
16use brep_render::engine_state::EngineState;
17
18/// What the delete command acts on.
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub enum DeleteTarget {
21    /// The open sketch's selected geometry / points / constraints.
22    Sketch,
23    /// The open 3D sketch's selection.
24    Sketch3d,
25    /// The eCAD editor on the central tile (its own selection).
26    Ecad(crate::workbench::ecad::Target),
27    /// A held object on the open drawing sheet (view, dimension, note…).
28    SheetObject(String),
29    /// The selected PMI annotation.
30    PmiAnnotation(String),
31    /// The label-selected assembly constraint.
32    Constraint(String),
33    /// One assembly component (its ACOMP feature).
34    Component(String),
35    /// One history feature.
36    Feature(String),
37}
38
39impl DeleteTarget {
40    /// The kind word MCP reports.
41    pub fn kind(&self) -> &'static str {
42        match self {
43            DeleteTarget::Sketch => "sketch",
44            DeleteTarget::Sketch3d => "sketch3d",
45            DeleteTarget::Ecad(_) => "ecad",
46            DeleteTarget::SheetObject(_) => "sheetObject",
47            DeleteTarget::PmiAnnotation(_) => "pmiAnnotation",
48            DeleteTarget::Constraint(_) => "constraint",
49            DeleteTarget::Component(_) => "component",
50            DeleteTarget::Feature(_) => "feature",
51        }
52    }
53
54    /// The id deleted, for the kinds that name one.
55    pub fn id(&self) -> Option<&str> {
56        match self {
57            DeleteTarget::SheetObject(id)
58            | DeleteTarget::PmiAnnotation(id)
59            | DeleteTarget::Constraint(id)
60            | DeleteTarget::Component(id)
61            | DeleteTarget::Feature(id) => Some(id),
62            _ => None,
63        }
64    }
65}
66
67/// What "delete the selection" means for `docs` now; `None` when nothing is
68/// deletable.
69pub fn target(docs: &Documents) -> Option<DeleteTarget> {
70    let engine = docs.engine();
71    if engine.ref_select_active() {
72        // A reference pick owns the selection: Delete never edits the model
73        // under a picking form.
74        return None;
75    }
76    if engine.sketch_mode() {
77        return Some(DeleteTarget::Sketch);
78    }
79    if engine.sketch3d_mode() {
80        return Some(DeleteTarget::Sketch3d);
81    }
82    if let Some(target) = crate::workbench::ecad::Target::of_workbench(&engine.settings.workbench) {
83        return Some(DeleteTarget::Ecad(target));
84    }
85    if engine.sheet_open().is_some() {
86        return engine.sheet_selected_object().map(|id| DeleteTarget::SheetObject(id.to_string()));
87    }
88    // A selected annotation is what Delete means while the PMI panel is up.
89    // With none selected the key falls through to the model: the panel is
90    // shown beside the history in Modeling, Sheet Metal and Assembly, so it
91    // must not swallow Delete for features, constraints and components.
92    if engine.pmi_workbench_entered() {
93        if let Some(id) = engine.pmi_selected_annotation() {
94            return Some(DeleteTarget::PmiAnnotation(id.to_string()));
95        }
96    }
97    model_target(engine)
98}
99
100/// The 3D-model part of [`target`]: constraint, then component, then feature.
101pub(crate) fn model_target(engine: &EngineState) -> Option<DeleteTarget> {
102    let constraints_shown = crate::workbench::panel_visible(
103        &engine.settings.workbench,
104        crate::workbench::assembly::CONSTRAINTS_PANEL_ID,
105        &crate::workbench::ButtonState::of(engine),
106    );
107    if let Some(id) = engine.selected_constraint().filter(|_| constraints_shown) {
108        return Some(DeleteTarget::Constraint(id));
109    }
110    if let Some(id) = crate::panels::context_bar::component_action_target_of(engine) {
111        return Some(DeleteTarget::Component(id));
112    }
113    engine.selected_feature().map(|id| DeleteTarget::Feature(id.to_string()))
114}
115
116/// Delete `target` through the delete its kind already has. `Err` carries the
117/// refusal the row action would have shown.
118pub fn apply(docs: &mut Documents, target: &DeleteTarget) -> Result<(), String> {
119    match target {
120        DeleteTarget::Ecad(target) => {
121            docs.active_mut().ecad.selection_key(*target, true);
122            Ok(())
123        }
124        other => apply_engine(docs.engine_mut(), other),
125    }
126}
127
128/// [`apply`] for every kind the engine alone holds (all but eCAD, which lives
129/// on the document) — what an engine-only surface such as the context bar
130/// calls, so its Delete entries are this command too.
131pub(crate) fn apply_engine(engine: &mut EngineState, target: &DeleteTarget) -> Result<(), String> {
132    match target {
133        DeleteTarget::Sketch => {
134            engine.sketch_delete_selection();
135            Ok(())
136        }
137        DeleteTarget::Sketch3d => {
138            engine.sketch3d_delete_selection();
139            Ok(())
140        }
141        DeleteTarget::Ecad(_) => Err("the eCAD editor's selection is deleted in its editor".into()),
142        DeleteTarget::SheetObject(id) => crate::panels::sheets::delete_held_object(engine, id),
143        DeleteTarget::PmiAnnotation(id) => crate::panels::pmi::delete_annotation(engine, id),
144        DeleteTarget::Constraint(id) => {
145            let result = engine.assembly_remove_constraint(id);
146            engine.constraint_deselect();
147            result
148        }
149        DeleteTarget::Component(id) => {
150            if id.contains(':') {
151                return Err(format!(
152                    "'{id}' is a part inside a sub-assembly: open that sub-assembly to delete it"
153                ));
154            }
155            crate::panels::component_actions::run_component_action(
156                engine,
157                crate::panels::component_actions::ComponentAction::Delete,
158                id,
159            );
160            Ok(())
161        }
162        DeleteTarget::Feature(id) => {
163            engine.delete_feature(id);
164            Ok(())
165        }
166    }
167}
168
169/// [`target`] then [`apply`]; `Ok(None)` when nothing was deletable.
170pub fn delete_selection(docs: &mut Documents) -> Result<Option<DeleteTarget>, String> {
171    let Some(target) = target(docs) else {
172        return Ok(None);
173    };
174    apply(docs, &target)?;
175    Ok(Some(target))
176}
177