Skip to main content

openbim_ifc_binding_core/
unreachable.rs

1//! Products no viewer will draw (feature `unreachable`).
2//!
3//! The facade's `unreachable_products` joins the spatial tree and the
4//! representation contexts. Its findings cross as plain records with a
5//! stable `reason` code; without the feature the operation refuses with
6//! `feature-disabled`.
7
8use crate::{BindingError, IfcModel};
9
10/// One product a viewer will not draw, and why.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct UnreachableProduct {
13    /// The product's entity id.
14    pub id: u64,
15    /// Stable reason code: `not-contained-in-spatial-structure`,
16    /// `no-representation-in-model-context` or
17    /// `representation-without-context`.
18    pub reason: String,
19    /// For `no-representation-in-model-context`: the target views the
20    /// product's geometry was found in instead. Empty otherwise.
21    pub found_views: Vec<String>,
22    /// A one-line explanation for a CLI or a UI warning.
23    pub message: String,
24}
25
26impl IfcModel {
27    /// Products that no viewer will draw, with the reason, in id order.
28    ///
29    /// Openings, assembly parts, spatial containers and products without a
30    /// representation are not reported: they legitimately sit outside the
31    /// containment tree.
32    pub fn unreachable_products(&self) -> Result<Vec<UnreachableProduct>, BindingError> {
33        #[cfg(feature = "unreachable")]
34        {
35            use ifc::Unreachable;
36            Ok(ifc::unreachable_products(&self.inner)
37                .into_iter()
38                .map(|(id, why)| {
39                    let message = why.message();
40                    let (reason, found_views) = match why {
41                        Unreachable::NotContainedInSpatialStructure => {
42                            ("not-contained-in-spatial-structure", Vec::new())
43                        }
44                        Unreachable::NoRepresentationInModelContext { found } => {
45                            ("no-representation-in-model-context", found)
46                        }
47                        Unreachable::RepresentationWithoutContext => {
48                            ("representation-without-context", Vec::new())
49                        }
50                        // A reason added after this binding: the message
51                        // still explains it.
52                        _ => ("other", Vec::new()),
53                    };
54                    UnreachableProduct {
55                        id: id.0,
56                        reason: reason.to_owned(),
57                        found_views,
58                        message,
59                    }
60                })
61                .collect())
62        }
63        #[cfg(not(feature = "unreachable"))]
64        {
65            Err(BindingError::FeatureDisabled("unreachable"))
66        }
67    }
68}
69
70/// The products as one tagged `list`, each a `list` of four values: id
71/// (`ref`), reason (text), found views (`list` of text) and
72/// message (text). The C binding's tape form.
73pub fn products_to_tagged(products: &[UnreachableProduct]) -> crate::value::Tagged {
74    use crate::value::Tagged;
75    Tagged::List(
76        products
77            .iter()
78            .map(|product| {
79                Tagged::List(vec![
80                    Tagged::Ref(product.id),
81                    Tagged::Text(product.reason.clone()),
82                    Tagged::List(
83                        product
84                            .found_views
85                            .iter()
86                            .map(|view| Tagged::Text(view.clone()))
87                            .collect(),
88                    ),
89                    Tagged::Text(product.message.clone()),
90                ])
91            })
92            .collect(),
93    )
94}