Skip to main content

geop_ops/part/
datum.rs

1//! A [`Part`]'s datums (see [`Datum`]): reference geometry the part is
2//! built *with*, not *of*. A sketch is placed on a datum plane, a datum
3//! axis is picked as a direction, a datum is built from another one. Named
4//! like any other entity.
5
6use geop_core_math::{
7    geop_error::{GeopError, GeopResult},
8    primitives::Datum,
9    scalars::Scalar,
10};
11
12use super::Part;
13use super::ids::DatumId;
14
15impl<S: Scalar> Part<S> {
16    /// Adds `datum` to the part under `name`. Fails, leaving the part
17    /// unchanged, if `name` is already taken.
18    pub fn add_datum(&mut self, datum: Datum<S>, name: impl Into<String>) -> GeopResult<DatumId> {
19        let id = DatumId(self.fresh_id());
20        self.names.insert(id, name)?;
21        self.datums.insert(id, datum);
22        Ok(id)
23    }
24
25    pub fn datum(&self, id: DatumId) -> GeopResult<&Datum<S>> {
26        self.datums
27            .get(&id)
28            .ok_or_else(|| GeopError::new(format!("Part has no datum {id}")))
29    }
30
31    /// Every datum, in the order they were added.
32    pub fn datums(&self) -> impl Iterator<Item = (DatumId, &Datum<S>)> {
33        self.datums.iter().map(|(&id, d)| (id, d))
34    }
35}