Skip to main content

ifc_material/usage/
assignment.rs

1//! Borrowed `IfcRelAssociatesMaterial` projection.
2
3use ifc_model::EntityId;
4
5use crate::usage::index::{Lookup, ASSOCIATES_MATERIAL};
6use crate::view::{
7    borrowed_entity, optional_text, required_ref, required_refs, required_text, MaterialView,
8};
9use crate::MaterialResult;
10
11borrowed_entity!(MaterialAssignment, "IFCRELASSOCIATESMATERIAL");
12
13impl<'m> MaterialAssignment<'m> {
14    /// `IfcRelAssociatesMaterial.GlobalId`. Required.
15    pub fn global_id(self) -> MaterialResult<&'m str> {
16        required_text(
17            "IFCRELASSOCIATESMATERIAL",
18            self.id(),
19            self.entity(),
20            self.slot("GlobalId")?,
21            "GlobalId",
22        )
23    }
24
25    /// `IfcRelAssociatesMaterial.Name`, if given.
26    pub fn name(self) -> MaterialResult<Option<&'m str>> {
27        optional_text(
28            "IFCRELASSOCIATESMATERIAL",
29            self.id(),
30            self.entity(),
31            self.slot("Name")?,
32            "Name",
33        )
34    }
35
36    /// `IfcRelAssociatesMaterial.Description`, if given.
37    pub fn description(self) -> MaterialResult<Option<&'m str>> {
38        optional_text(
39            "IFCRELASSOCIATESMATERIAL",
40            self.id(),
41            self.entity(),
42            self.slot("Description")?,
43            "Description",
44        )
45    }
46
47    /// `IfcRelAssociatesMaterial.RelatedObjects`. Required and must be
48    /// non-empty.
49    pub fn related_object_ids(self) -> MaterialResult<Vec<EntityId>> {
50        required_refs(
51            "IFCRELASSOCIATESMATERIAL",
52            self.id(),
53            self.entity(),
54            self.slot("RelatedObjects")?,
55            "RelatedObjects",
56            1,
57        )
58    }
59
60    /// `IfcRelAssociatesMaterial.RelatingMaterial`, an `IfcMaterialSelect`
61    /// branch reference. Required.
62    pub fn relating_material_id(self) -> MaterialResult<EntityId> {
63        required_ref(
64            "IFCRELASSOCIATESMATERIAL",
65            self.id(),
66            self.entity(),
67            self.slot("RelatingMaterial")?,
68            "RelatingMaterial",
69        )
70    }
71}
72
73impl<'m> MaterialView<'m> {
74    /// Iterates every `IfcRelAssociatesMaterial` instance in the model.
75    pub fn assignments(self) -> impl Iterator<Item = MaterialAssignment<'m>> + 'm {
76        let release = self.release();
77        self.model()
78            .of_type("IFCRELASSOCIATESMATERIAL")
79            .map(move |(id, entity)| MaterialAssignment::from_known(id, entity, release))
80    }
81
82    /// Collects every `IfcRelAssociatesMaterial` whose `RelatedObjects`
83    /// includes `object`, ascending by id. Several hits naming one
84    /// material resolve; see `assigned_material`.
85    ///
86    /// Reads only the relationships that reference `object` in
87    /// `RelatedObjects` (through the model's reverse index when built), so
88    /// it costs those and not every relationship in the model; a malformed
89    /// relationship elsewhere does not refuse it (#608). For many objects
90    /// use [`crate::MaterialIndex`].
91    pub fn assignments_for(self, object: EntityId) -> MaterialResult<Vec<MaterialAssignment<'m>>> {
92        self.assignments_in(self.lookup(), object)
93    }
94
95    pub(crate) fn assignments_in(
96        self,
97        lookup: Lookup<'_>,
98        object: EntityId,
99    ) -> MaterialResult<Vec<MaterialAssignment<'m>>> {
100        let release = self.release();
101        let mut matches = Vec::new();
102        for (id, entity) in self.naming(
103            lookup.referrers,
104            ASSOCIATES_MATERIAL,
105            "RelatedObjects",
106            object,
107        )? {
108            if self.names(lookup, ASSOCIATES_MATERIAL, id, entity, object)? {
109                matches.push(MaterialAssignment::from_known(id, entity, release));
110            }
111        }
112        Ok(matches)
113    }
114}