Skip to main content

brep_render/engine_state/
mount_ops.rs

1//! The MOUNT FRAME helpers that read the model: a part's mount set from a
2//! pick (`brep_kernel::part_mount`).
3//!
4//! A pick is a helper that computes numbers ONCE. The mount block stores
5//! the resolved pose and never the name, so nothing re-resolves when the part
6//! changes. Each pick composes onto the current mount and is one undo step,
7//! written through `History::set_mount_block`.
8
9use super::*;
10
11/// Which helper a mount pick is for.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum MountPick {
14    /// One vertex, edge or face point: it goes to the footprint origin, in
15    /// the footprint's plane (`brep_kernel::mount_origin_to`).
16    Origin,
17    /// One planar face (or datum plane): it becomes the seating plane, its
18    /// outward normal down and its plane at z = 0
19    /// (`brep_kernel::mount_seated_on`).
20    Seat,
21}
22
23impl MountPick {
24    /// The picker's heading.
25    pub fn label(self) -> &'static str {
26        match self {
27            MountPick::Origin => "Put this point on the footprint origin",
28            MountPick::Seat => "Seat the part on this face",
29        }
30    }
31
32    /// The pick kinds the helper admits. SOLID is offered to neither: a
33    /// click meant for a face that landed on the body would name its centre.
34    pub fn filter(self) -> Vec<String> {
35        let kinds: &[&str] = match self {
36            MountPick::Origin => &["VERTEX", "EDGE", "FACE"],
37            MountPick::Seat => &["FACE", "PLANE"],
38        };
39        kinds.iter().map(|kind| (*kind).to_string()).collect()
40    }
41}
42
43impl EngineState {
44    /// Enter the reference picker for a mount helper. Nothing rolls back: a
45    /// mount names the finished part, which is what is on screen.
46    pub fn begin_ref_select_for_mount(&mut self, pick: MountPick) {
47        self.disarm_transform();
48        let filter = pick.filter();
49        self.selection_filter = SelectionFilter::from_ref_filter(&filter);
50        self.ref_select = Some(RefSelectState {
51            feature_id: brep_kernel::MOUNT_BLOCK.to_string(),
52            path: vec![brep_kernel::MOUNT_BLOCK.to_string()],
53            label: pick.label().to_string(),
54            filter,
55            multiple: false,
56            names: Vec::new(),
57            restore_index: self.history.rollback(),
58            target: RefSelectTarget::Mount { pick },
59            picks: 0,
60            claimed: false,
61            finish_requested: false,
62        });
63        self.sync_ref_select_emphasis();
64    }
65
66    /// The mount a pick of `name` asks for, composed onto the current one.
67    /// The name is resolved against the scene on screen (the rolled-to run).
68    pub fn mount_from_pick(&self, pick: MountPick, name: &str) -> Result<Option<serde_json::Value>, String> {
69        let current = self.history.mount_block().map(brep_kernel::mount_from_block).transpose()?;
70        let request: HistoryRequest =
71            serde_json::from_value(self.history.prefix_request()).map_err(|error| error.to_string())?;
72        let _trace = crate::run_trace::span("mount_from_pick");
73        let result = self.execute_plugin_history(&request);
74        let mount = match pick {
75            MountPick::Origin => {
76                let point = brep_kernel::face_transform_reference_point(&result.results, name)?;
77                brep_kernel::mount_origin_to(current.as_ref(), point)?
78            }
79            MountPick::Seat => {
80                let (origin, normal) = brep_kernel::mount_reference_plane(&result.results, name)?;
81                brep_kernel::mount_seated_on(current.as_ref(), origin, normal)?
82            }
83        };
84        Ok(brep_kernel::mount_block_of(&mount))
85    }
86
87    /// Finish a mount pick: write what it asks for as one undo step, or say
88    /// why it could not. A pick of nothing changes nothing.
89    pub(super) fn mount_commit_pick(&mut self, pick: MountPick, name: Option<&str>) {
90        let Some(name) = name.filter(|name| !name.trim().is_empty()) else {
91            return;
92        };
93        match self.mount_from_pick(pick, name) {
94            Ok(block) if block.as_ref() != self.history.mount_block() => self.history.set_mount_block(block, None),
95            Ok(_) => {}
96            Err(error) => self.push_notice(format!("{}: {error}", pick.label())),
97        }
98    }
99}
100