Skip to main content

ifc_style/assignment/
styled_item.rs

1//! `IfcStyledItem` projection.
2
3use ifc_model::EntityId;
4use ifc_schema::SchemaVersion;
5
6use crate::error::{StyleError, StyleResult};
7use crate::view::Record;
8
9/// Borrowed projection of `IfcStyledItem`: binds one or more presentation
10/// styles to a single `IfcRepresentationItem`.
11#[derive(Debug, Clone, Copy)]
12pub struct StyledItem<'m, 's> {
13    record: Record<'m, 's>,
14}
15
16impl<'m, 's> StyledItem<'m, 's> {
17    pub(crate) fn from_record(record: Record<'m, 's>) -> Self {
18        Self { record }
19    }
20
21    /// The entity id of this `IfcStyledItem`.
22    pub fn id(&self) -> EntityId {
23        self.record.id
24    }
25
26    /// The `Item` reference to the styled `IfcRepresentationItem`. `None`
27    /// for an `IfcStyledItem` that only carries a `Name` with no target item.
28    pub fn item(&self) -> StyleResult<Option<EntityId>> {
29        self.record.optional_ref("Item", "IfcRepresentationItem")
30    }
31
32    /// The `Styles` select. IFC2x3 requires exactly one
33    /// `IfcPresentationStyleAssignment` member; IFC4, IFC4X1 and IFC4X2 also
34    /// permit direct `IfcPresentationStyle` references alongside the legacy
35    /// wrapper (`IfcStyleAssignmentSelect`); IFC4X3, and a schema of no
36    /// recognised release, accept only direct `IfcPresentationStyle`
37    /// references. A recognised release not listed here is refused with
38    /// [`StyleError::UnsupportedSchema`] rather than read by a neighbour's
39    /// rule.
40    ///
41    /// [`StyleError::UnsupportedSchema`]: crate::StyleError::UnsupportedSchema
42    pub fn styles(&self) -> StyleResult<Vec<EntityId>> {
43        let (members, maximum): (&[&str], Option<usize>) = match self.record.schema.version() {
44            Some(SchemaVersion::Ifc2x3) => (&["IfcPresentationStyleAssignment"], Some(1)),
45            Some(SchemaVersion::Ifc4 | SchemaVersion::Ifc4x1 | SchemaVersion::Ifc4x2) => (
46                &["IfcPresentationStyle", "IfcPresentationStyleAssignment"],
47                None,
48            ),
49            Some(SchemaVersion::Ifc4x3) | None => (&["IfcPresentationStyle"], None),
50            Some(_) => {
51                return Err(StyleError::UnsupportedSchema {
52                    schema: self.record.schema.name().to_owned(),
53                })
54            }
55        };
56        self.record
57            .required_refs_select("Styles", "IfcStyleAssignmentSelect", members, 1, maximum)
58    }
59
60    /// The `Name` attribute, when authored.
61    pub fn name(&self) -> StyleResult<Option<&'m str>> {
62        self.record.optional_text("Name")
63    }
64}