ifc_properties/exact/material.rs
1//! Material property sets in exact resolution (#218).
2//!
3//! A material's property sets are no `IfcPropertySetDefinition` assigned by
4//! a relationship. Each points at its material itself:
5//!
6//! ```text
7//! IFC4 ADD2 TC1 / IFC4X3 ADD2
8//! IfcMaterialProperties 0 OPT Name 1 OPT Description
9//! 2 Properties 3 Material : IfcMaterialDefinition
10//! (read back through the inverse IfcMaterialDefinition.HasProperties)
11//!
12//! IFC2X3 TC1
13//! IfcMaterialProperties 0 Material : IfcMaterial (ABSTRACT)
14//! IfcExtendedMaterialProperties 1 ExtendedProperties 2 OPT Description 3 Name
15//! IfcGeneralMaterialProperties, IfcMechanicalMaterialProperties, ...
16//! own attributes after Material
17//! ```
18//!
19//! Positions are read by name from the bound release's table. Named
20//! properties resolve as those of an `IfcPropertySet` do; the attributes of
21//! an IFC2X3 typed subtype resolve as those of a predefined set do, keyed
22//! by its entity name (`IfcGeneralMaterialProperties`), since it has no
23//! `Name`. An IFC4 set whose optional `Name` is `$` is keyed by its entity
24//! name too.
25//!
26//! Every material property set in the file is validated, not only those of
27//! the queried material, as every property relationship is for an object:
28//! a malformed one could otherwise hide a set.
29
30use ifc_model::{EntityId, Model};
31
32use super::enumerate::{collect, list_sets, Selector};
33use super::refs::{ref_at, require_ref};
34use super::release::{validate_model, Release};
35use super::set::find_property;
36use super::{
37 ExactPropertyEntry, ExactPropertyError, ExactPropertySetEntry, ExactResolution, ExactSource,
38};
39
40/// Validate `material` as a query target and collect its material property
41/// sets in file order.
42///
43/// # Errors
44///
45/// A missing, foreign or mis-sized material; a material the release's
46/// `IfcMaterialProperties.Material` does not accept
47/// ([`ExactPropertyError::InvalidQueryObject`]); or any material property
48/// set in the file with the wrong arity or a missing `Material`.
49fn material_sets(
50 model: &Model,
51 release: Release,
52 material: EntityId,
53) -> Result<Vec<EntityId>, ExactPropertyError> {
54 let schema = release.schema;
55 let entity = model
56 .get(material)
57 .ok_or(ExactPropertyError::MissingReference {
58 from: material,
59 to: material,
60 })?;
61 if schema.entity(entity.type_name.as_ref()).is_none() {
62 return Err(release.not_in_schema(material, entity.type_name.clone()));
63 }
64 let (material_slot, _) = release
65 .attribute("IFCMATERIALPROPERTIES", "Material")
66 .expect("IfcMaterialProperties.Material is declared in every bundled release");
67 if !release.slot_accepts(
68 "IFCMATERIALPROPERTIES",
69 material_slot,
70 entity.type_name.as_ref(),
71 ) {
72 return Err(ExactPropertyError::InvalidQueryObject {
73 object: material,
74 type_name: entity.type_name.clone(),
75 });
76 }
77 release.require_exact_slots(material, entity)?;
78 let mut ids: Vec<EntityId> = model
79 .type_histogram()
80 .into_iter()
81 .filter(|(type_name, _)| {
82 schema.entity(type_name).is_some() && schema.is_a(type_name, "IFCMATERIALPROPERTIES")
83 })
84 .flat_map(|(type_name, _)| model.ids_of_type(type_name).iter().copied())
85 .collect();
86 ids.sort_unstable();
87 let mut sets = Vec::new();
88 for id in ids {
89 let set = model.get(id).expect("type index is current");
90 release.require_exact_slots(id, set)?;
91 // `Material` is inherited, so it keeps its supertype slot.
92 let target = ref_at(id, set.attributes.get(material_slot), "Material")?;
93 require_ref(model, id, target)?;
94 if target == material {
95 sets.push(id);
96 }
97 }
98 Ok(sets)
99}
100
101/// Resolve a property of material definition `material` by exact set and
102/// property name (#218).
103///
104/// The material's property sets are its IFC4 and IFC4X3
105/// `IfcMaterialProperties` (the inverse `HasProperties`), or its IFC2X3
106/// `IfcExtendedMaterialProperties` and typed `IfcMaterialProperties`
107/// subtypes. A named property resolves as in [`exact_property`], with the
108/// same values, units and refusals, and [`ExactSource::Material`] of
109/// `material`. An attribute of an IFC2X3 typed subtype resolves as a
110/// predefined set's attribute does, in the set named by its entity
111/// (`IfcMechanicalMaterialProperties.YoungModulus`).
112///
113/// `material` is an `IfcMaterialDefinition` in IFC4 and IFC4X3 (a material,
114/// layer, layer set, profile, profile set, constituent or constituent set)
115/// and an `IfcMaterial` in IFC2X3. [`ExactResolution::Absent`] is a proven
116/// absence, also when the material has no property sets at all. Material
117/// sets do not inherit from anywhere: a layer's properties are the layer's
118/// own, not its material's.
119///
120/// # Errors
121///
122/// Any [`ExactPropertyError`], as for [`exact_property`]; a query target of
123/// another kind is [`ExactPropertyError::InvalidQueryObject`].
124///
125/// [`exact_property`]: super::exact_property
126pub fn exact_material_property(
127 model: &Model,
128 material: EntityId,
129 set_name: Option<&str>,
130 property_name: &str,
131) -> Result<ExactResolution, ExactPropertyError> {
132 let release = validate_model(model)?;
133 let sets = material_sets(model, release, material)?;
134 Ok(find_property(
135 model,
136 release,
137 &sets,
138 ExactSource::Material(material),
139 set_name,
140 property_name,
141 )?
142 .map_or(ExactResolution::Absent, ExactResolution::Present))
143}
144
145/// The properties of material definition `material` in the sets
146/// `select_set` picks, whose names `select_property` picks, resolved
147/// exactly (#218).
148///
149/// It answers what [`exact_material_property`] answers, for every selected
150/// property at once, as [`exact_properties_where`] does for an object. The
151/// result is in file order of the sets, each set's members in file or
152/// schema order. An empty result is a proven absence of every selected
153/// property.
154///
155/// # Errors
156///
157/// Any [`ExactPropertyError`], as for [`exact_material_property`], and
158/// the ambiguity refusals of [`exact_properties_where`].
159///
160/// [`exact_properties_where`]: super::exact_properties_where
161pub fn exact_material_properties_where<S, P>(
162 model: &Model,
163 material: EntityId,
164 mut select_set: S,
165 mut select_property: P,
166) -> Result<Vec<ExactPropertyEntry>, ExactPropertyError>
167where
168 S: FnMut(&str) -> bool,
169 P: FnMut(&str) -> bool,
170{
171 let release = validate_model(model)?;
172 let sets = material_sets(model, release, material)?;
173 let mut selector = Selector {
174 set: &mut select_set,
175 property: &mut select_property,
176 };
177 collect(
178 model,
179 release,
180 &sets,
181 ExactSource::Material(material),
182 &mut selector,
183 )
184}
185
186/// The property sets of material definition `material` whose names
187/// `select_set` picks, empty ones included (#218).
188///
189/// The material counterpart of [`exact_property_sets_where`]: an empty
190/// result proves the material has no selected set, and an empty set is
191/// listed with `members == 0`.
192///
193/// # Errors
194///
195/// Any [`ExactPropertyError`], as for [`exact_material_property`].
196///
197/// [`exact_property_sets_where`]: super::exact_property_sets_where
198pub fn exact_material_property_sets_where<S>(
199 model: &Model,
200 material: EntityId,
201 mut select_set: S,
202) -> Result<Vec<ExactPropertySetEntry>, ExactPropertyError>
203where
204 S: FnMut(&str) -> bool,
205{
206 let release = validate_model(model)?;
207 let sets = material_sets(model, release, material)?;
208 list_sets(
209 model,
210 release,
211 &sets,
212 ExactSource::Material(material),
213 &mut select_set,
214 )
215}