Skip to main content

ifc_cost/
item.rs

1//! `IfcCostItem` — one line in a cost schedule.
2//!
3//! Attribute positions come from the IFC4 schema in
4//! `references/ifc-spec/ifc4-add2-tc1/IFC4.exp`. They are read defensively:
5//! real files are routinely short a trailing optional attribute, so every
6//! accessor returns an `Option` rather than indexing blindly.
7
8use ifc_model::{Entity, EntityId, Value};
9
10/// A borrowed view of an `IfcCostItem` entity.
11#[derive(Debug, Clone, Copy)]
12pub struct CostItem<'m> {
13    id: EntityId,
14    entity: &'m Entity,
15}
16
17/// `IfcCostItem` attribute slots, from `IfcRoot` down.
18///
19/// The inheritance chain is `IfcRoot` -> `IfcObjectDefinition` -> `IfcObject`
20/// -> `IfcControl` -> `IfcCostItem`, which contributes, in order:
21///
22/// ```text
23/// 0 GlobalId        IfcRoot
24/// 1 OwnerHistory    IfcRoot
25/// 2 Name            IfcRoot
26/// 3 Description     IfcRoot
27/// 4 ObjectType      IfcObject      <- contributes a slot
28/// 5 Identification  IfcControl
29/// 6 PredefinedType  IfcCostItem
30/// 7 CostValues      IfcCostItem
31/// 8 CostQuantities  IfcCostItem
32/// ```
33///
34/// `IfcObjectDefinition` adds only INVERSE attributes, which are not stored
35/// positionally. Verified against IFC4 EXPRESS and cross-checked by writing
36/// the entity with IfcOpenShell and reading back its attribute order.
37pub(crate) mod slot {
38    /// `GlobalId` (from `IfcRoot`).
39    pub const GLOBAL_ID: usize = 0;
40    /// `Name` (from `IfcRoot`).
41    pub const NAME: usize = 2;
42    /// `Description` (from `IfcRoot`).
43    pub const DESCRIPTION: usize = 3;
44    /// `Identification` (from `IfcControl`).
45    pub const IDENTIFICATION: usize = 5;
46    /// `PredefinedType`.
47    pub const PREDEFINED_TYPE: usize = 6;
48    /// `CostValues`.
49    pub const COST_VALUES: usize = 7;
50    /// `CostQuantities`.
51    pub const COST_QUANTITIES: usize = 8;
52}
53
54impl<'m> CostItem<'m> {
55    /// Wrap an entity known to be an `IfcCostItem`.
56    pub fn new(id: EntityId, entity: &'m Entity) -> Self {
57        Self { id, entity }
58    }
59
60    /// The entity id in the file.
61    pub fn id(&self) -> EntityId {
62        self.id
63    }
64
65    /// The `GlobalId` string.
66    pub fn global_id(&self) -> Option<&'m str> {
67        self.entity.text(slot::GLOBAL_ID)
68    }
69
70    /// The human-readable name.
71    pub fn name(&self) -> Option<&'m str> {
72        self.entity.text(slot::NAME)
73    }
74
75    /// The user-facing identification code.
76    pub fn identification(&self) -> Option<&'m str> {
77        self.entity.text(slot::IDENTIFICATION)
78    }
79
80    /// The description.
81    pub fn description(&self) -> Option<&'m str> {
82        self.entity.text(slot::DESCRIPTION)
83    }
84
85    /// The predefined type token, e.g. `MATERIAL`, without its dots.
86    pub fn predefined_type(&self) -> Option<&'m str> {
87        match self.entity.attribute(slot::PREDEFINED_TYPE)? {
88            Value::Enum(e) => Some(e),
89            _ => None,
90        }
91    }
92
93    /// Ids of the `IfcCostValue`s attached to this item.
94    pub fn value_refs(&self) -> Vec<EntityId> {
95        refs_in(self.entity.attribute(slot::COST_VALUES))
96    }
97
98    /// Ids of the quantities this cost is computed against.
99    pub fn quantity_refs(&self) -> Vec<EntityId> {
100        refs_in(self.entity.attribute(slot::COST_QUANTITIES))
101    }
102}
103
104/// Collect entity references from an optional aggregate attribute.
105fn refs_in(value: Option<&Value>) -> Vec<EntityId> {
106    let mut out = Vec::new();
107    if let Some(v) = value {
108        v.for_each_ref(&mut |id| out.push(id));
109    }
110    out
111}