ifc_cost/quantity.rs
1//! Quantities a cost is computed against.
2//!
3//! `IfcCostItem.CostQuantities` points at `IfcPhysicalQuantity` subtypes
4//! (`IfcQuantityVolume`, `IfcQuantityArea`, ...). The generic reading lives
5//! here; the full quantity model belongs to `ifc-properties`, and this module
6//! deliberately does not duplicate it.
7
8use ifc_model::{Entity, EntityId};
9
10/// A borrowed view of a physical quantity referenced by a cost item.
11#[derive(Debug, Clone, Copy)]
12pub struct CostQuantity<'m> {
13 id: EntityId,
14 entity: &'m Entity,
15}
16
17impl<'m> CostQuantity<'m> {
18 /// Wrap a quantity entity.
19 pub fn new(id: EntityId, entity: &'m Entity) -> Self {
20 Self { id, entity }
21 }
22
23 /// The entity id in the file.
24 pub fn id(&self) -> EntityId {
25 self.id
26 }
27
28 /// The quantity name, always attribute 0 on `IfcPhysicalSimpleQuantity`.
29 pub fn name(&self) -> Option<&'m str> {
30 self.entity.text(0)
31 }
32
33 /// The numeric value.
34 ///
35 /// The slot differs per subtype (`IfcQuantityVolume.VolumeValue` is 3,
36 /// `IfcQuantityCount.CountValue` is 3 as well), so this scans for the
37 /// first numeric attribute after the unit rather than hard-coding a
38 /// position per type.
39 pub fn value(&self) -> Option<f64> {
40 self.entity
41 .attributes
42 .iter()
43 .skip(2)
44 .find_map(|v| v.unwrap_typed().as_f64())
45 }
46}