ifc_properties/quantity/by_object.rs
1//! Quantity sets by the object that carries them.
2//!
3//! The same two routes as property sets (`IfcRelDefinesByProperties` for an
4//! occurrence, `IfcTypeObject.HasPropertySets` for a type), followed by the
5//! traversal in `pset/attach.rs`. A quantity set is an `IfcElementQuantity`,
6//! an `IfcPropertySetDefinition` like `IfcPropertySet`, so a type holds both
7//! kinds in one attribute; each view reads only its own kind.
8
9use std::collections::BTreeMap;
10
11use ifc_model::{EntityId, Model};
12
13use crate::error::PropertyAnomaly;
14use crate::pset::attach::sets_by_object;
15use crate::pset::Attachment;
16use crate::quantity::set::{quantity_set, QuantitySet};
17
18/// Quantity sets found on each object, with how each one was attached.
19pub type AttachedQuantitySets = BTreeMap<EntityId, Vec<(Attachment, QuantitySet)>>;
20
21/// Every quantity set in the file, keyed by the object carrying it.
22///
23/// Both routes are followed. A set reachable by both appears once per object,
24/// with [`Attachment`] recording how it arrived: precedence is a caller
25/// decision, so the reader must not collapse the distinction here. Sets of
26/// one object are ordered by id. Property sets are never returned here, and
27/// quantity sets are never returned by
28/// [`property_sets_by_object`](crate::property_sets_by_object).
29///
30/// Anomalies report a type object attached by the forbidden relationship,
31/// relationships whose targets are missing from the file, the members
32/// [`quantity_set`] could not resolve (once per set), and, once per set, each
33/// top-level quantity whose name an earlier one in the same set already has
34/// ([`PropertyAnomaly::DuplicatePropertyName`]).
35pub fn quantity_sets_by_object(model: &Model) -> (AttachedQuantitySets, Vec<PropertyAnomaly>) {
36 let (out, mut anomalies) = sets_by_object(model, quantity_set, |set: &QuantitySet| set.id);
37 // A set shared by many objects is checked once.
38 let mut unique: BTreeMap<EntityId, &QuantitySet> = BTreeMap::new();
39 for (_, set) in out.values().flatten() {
40 unique.entry(set.id).or_insert(set);
41 }
42 for set in unique.values() {
43 duplicate_quantity_names(set, &mut anomalies);
44 }
45 (out, anomalies)
46}
47
48/// `UniqueQuantityNames` (IFC4 and later): report each quantity whose name an
49/// earlier one in the same set already has.
50fn duplicate_quantity_names(set: &QuantitySet, anomalies: &mut Vec<PropertyAnomaly>) {
51 let mut first: BTreeMap<&str, EntityId> = BTreeMap::new();
52 for quantity in &set.quantities {
53 let Some(name) = quantity.name() else {
54 continue;
55 };
56 match first.get(name) {
57 Some(&kept) => anomalies.push(PropertyAnomaly::DuplicatePropertyName {
58 set: set.id,
59 kept,
60 rejected: quantity.id(),
61 }),
62 None => {
63 first.insert(name, quantity.id());
64 }
65 }
66 }
67}