ifc_resource/resource/base.rs
1//! Construction-resource occurrence projections.
2//!
3//! IFC4 and IFC4X3 share one layout. IFC2X3 TC1 declares a reduced one
4//! (`ResourceIdentifier`, `ResourceGroup`, `ResourceConsumption`,
5//! `BaseQuantity : IfcMeasureWithUnit` on `IfcConstructionResource`, plus
6//! `SkillSet`, `Suppliers`/`UsageRatio` and `SubContractor`/`JobDescription`
7//! on three subtypes); the IFC2X3-only accessors below read it, and refuse
8//! with [`ResourceError::NotInSchema`] under IFC4 and IFC4X3.
9
10use ifc_model::EntityId;
11
12use crate::error::{ResourceError, ResourceResult};
13use crate::usage::ResourceTime;
14use crate::view::Record;
15
16/// Concrete `IfcConstructionResource` specialization, one of the six
17/// resource-occurrence entities this crate projects (all six exist in
18/// IFC2X3, IFC4 and IFC4X3).
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum ResourceKind {
22 /// `IfcLaborResource`: a person or crew role.
23 Labor,
24 /// `IfcConstructionEquipmentResource`: machinery or tooling.
25 Equipment,
26 /// `IfcCrewResource`: a composed group of resources.
27 Crew,
28 /// `IfcConstructionMaterialResource`: a consumed material.
29 Material,
30 /// `IfcConstructionProductResource`: a resource realized as a product.
31 Product,
32 /// `IfcSubContractResource`: work delegated to a subcontractor.
33 Subcontract,
34}
35
36impl ResourceKind {
37 fn from_type(type_name: &str) -> Option<Self> {
38 if type_name.eq_ignore_ascii_case("IfcLaborResource") {
39 Some(Self::Labor)
40 } else if type_name.eq_ignore_ascii_case("IfcConstructionEquipmentResource") {
41 Some(Self::Equipment)
42 } else if type_name.eq_ignore_ascii_case("IfcCrewResource") {
43 Some(Self::Crew)
44 } else if type_name.eq_ignore_ascii_case("IfcConstructionMaterialResource") {
45 Some(Self::Material)
46 } else if type_name.eq_ignore_ascii_case("IfcConstructionProductResource") {
47 Some(Self::Product)
48 } else if type_name.eq_ignore_ascii_case("IfcSubContractResource") {
49 Some(Self::Subcontract)
50 } else {
51 None
52 }
53 }
54}
55
56#[derive(Debug, Clone, Copy)]
57/// Borrowed projection of a concrete `IfcConstructionResource` occurrence.
58pub struct ConstructionResource<'m, 's> {
59 record: Record<'m, 's>,
60 kind: ResourceKind,
61}
62
63impl<'m, 's> ConstructionResource<'m, 's> {
64 pub(crate) fn from_record(record: Record<'m, 's>) -> ResourceResult<Self> {
65 let kind = ResourceKind::from_type(&record.entity.type_name).ok_or_else(|| {
66 ResourceError::WrongType {
67 id: record.id,
68 expected: "concrete IfcConstructionResource occurrence",
69 actual: record.entity.type_name.to_string(),
70 }
71 })?;
72 Ok(Self { record, kind })
73 }
74
75 /// The entity id of the projected occurrence.
76 #[must_use]
77 pub fn id(&self) -> EntityId {
78 self.record.id
79 }
80
81 /// Which concrete `IfcConstructionResource` subtype this occurrence is.
82 #[must_use]
83 pub fn kind(&self) -> ResourceKind {
84 self.kind
85 }
86
87 /// The `Name` attribute, when authored.
88 pub fn name(&self) -> ResourceResult<Option<&'m str>> {
89 self.record.optional_text("Name")
90 }
91
92 /// The `Identification` attribute, when authored.
93 ///
94 /// Under IFC2X3 this reads `ResourceIdentifier`, the `IfcIdentifier`
95 /// IFC2X3 declares in the same position.
96 pub fn identification(&self) -> ResourceResult<Option<&'m str>> {
97 if self.record.is_ifc2x3() {
98 self.record.optional_text("ResourceIdentifier")
99 } else {
100 self.record.optional_text("Identification")
101 }
102 }
103
104 /// The `LongDescription` attribute, when authored; IFC2X3 declares none
105 /// ([`ResourceError::NotInSchema`]).
106 pub fn long_description(&self) -> ResourceResult<Option<&'m str>> {
107 self.record.optional_text("LongDescription")
108 }
109
110 /// The `PredefinedType` enumeration, when authored; no IFC2X3 resource
111 /// declares one ([`ResourceError::NotInSchema`]).
112 ///
113 /// A `USERDEFINED` value requires `ObjectType` to be set; that
114 /// constraint is enforced here rather than left to the caller.
115 pub fn predefined_type(&self) -> ResourceResult<Option<&'m str>> {
116 let value = self.record.optional_enum("PredefinedType")?;
117 self.record.require_object_type_if(
118 value.is_some_and(|value| value.eq_ignore_ascii_case("USERDEFINED")),
119 "USERDEFINED resource PredefinedType requires ObjectType",
120 )?;
121 Ok(value)
122 }
123
124 /// The `Usage` attribute: authored `IfcResourceTime`, when present;
125 /// IFC2X3 declares neither ([`ResourceError::NotInSchema`]).
126 pub fn usage(&self) -> ResourceResult<Option<ResourceTime<'m, 's>>> {
127 self.record
128 .optional_ref("Usage", "IfcResourceTime")?
129 .map(|id| Record::new(self.record.model, self.record.schema, id, "IfcResourceTime"))
130 .transpose()
131 .map(|record| record.map(ResourceTime::from_record))
132 }
133
134 /// The `BaseCosts` attribute: applied-value references, when authored;
135 /// IFC2X3 declares none ([`ResourceError::NotInSchema`]).
136 pub fn base_costs(&self) -> ResourceResult<Vec<EntityId>> {
137 self.record
138 .refs("BaseCosts", "IfcAppliedValue", 1, true, false)
139 }
140
141 /// The `BaseQuantity` attribute: the physical-quantity reference, when
142 /// authored.
143 ///
144 /// IFC2X3 declares `BaseQuantity` as an `IfcMeasureWithUnit`, not a
145 /// physical quantity, so this refuses with
146 /// [`ResourceError::NotInSchema`] there; read it with
147 /// [`ConstructionResource::base_quantity_measure`].
148 pub fn base_quantity(&self) -> ResourceResult<Option<EntityId>> {
149 if self.record.is_ifc2x3() {
150 return Err(self.record.not_in_schema("BaseQuantity"));
151 }
152 self.record
153 .optional_ref("BaseQuantity", "IfcPhysicalQuantity")
154 }
155
156 /// IFC2X3 `IfcConstructionResource.BaseQuantity`: the
157 /// `IfcMeasureWithUnit` reference, when authored. Project it with
158 /// [`ResourceView::measure_with_unit`](crate::ResourceView::measure_with_unit).
159 ///
160 /// IFC4 and IFC4X3 declare `BaseQuantity` as an `IfcPhysicalQuantity`,
161 /// read by [`ConstructionResource::base_quantity`]; this refuses there
162 /// with [`ResourceError::NotInSchema`].
163 pub fn base_quantity_measure(&self) -> ResourceResult<Option<EntityId>> {
164 if !self.record.is_ifc2x3() {
165 return Err(self.record.not_in_schema("BaseQuantity"));
166 }
167 self.record
168 .optional_ref("BaseQuantity", "IfcMeasureWithUnit")
169 }
170
171 /// IFC2X3 `IfcConstructionResource.ResourceGroup` (`IfcLabel`), when
172 /// authored. [`ResourceError::NotInSchema`] under IFC4 and IFC4X3.
173 pub fn resource_group(&self) -> ResourceResult<Option<&'m str>> {
174 self.record.optional_text("ResourceGroup")
175 }
176
177 /// IFC2X3 `IfcConstructionResource.ResourceConsumption`
178 /// (`IfcResourceConsumptionEnum`), when authored, checked against the
179 /// release's declared members. [`ResourceError::NotInSchema`] under
180 /// IFC4 and IFC4X3.
181 pub fn resource_consumption(&self) -> ResourceResult<Option<&'m str>> {
182 self.record.optional_enum("ResourceConsumption")
183 }
184
185 /// IFC2X3 `IfcLaborResource.SkillSet` (`IfcText`), when authored.
186 /// [`ResourceError::NotInSchema`] on any other kind and under IFC4 and
187 /// IFC4X3.
188 pub fn skill_set(&self) -> ResourceResult<Option<&'m str>> {
189 self.record.optional_text("SkillSet")
190 }
191
192 /// IFC2X3 `IfcConstructionMaterialResource.Suppliers`
193 /// (`SET [1:?] OF IfcActorSelect`): person, organization or
194 /// person-and-organization references, empty when unset.
195 /// [`ResourceError::NotInSchema`] on any other kind and under IFC4 and
196 /// IFC4X3.
197 pub fn suppliers(&self) -> ResourceResult<Vec<EntityId>> {
198 self.record
199 .refs_select("Suppliers", "IfcActorSelect", ACTOR_SELECT, 1, true, true)
200 }
201
202 /// IFC2X3 `IfcConstructionMaterialResource.UsageRatio`
203 /// (`IfcRatioMeasure`), when authored; a non-finite value is refused.
204 /// [`ResourceError::NotInSchema`] on any other kind and under IFC4 and
205 /// IFC4X3.
206 pub fn usage_ratio(&self) -> ResourceResult<Option<f64>> {
207 self.record.optional_finite_number("UsageRatio")
208 }
209
210 /// IFC2X3 `IfcSubContractResource.SubContractor` (`IfcActorSelect`),
211 /// when authored. [`ResourceError::NotInSchema`] on any other kind and
212 /// under IFC4 and IFC4X3.
213 pub fn sub_contractor(&self) -> ResourceResult<Option<EntityId>> {
214 self.record
215 .optional_ref_select("SubContractor", "IfcActorSelect", ACTOR_SELECT)
216 }
217
218 /// IFC2X3 `IfcSubContractResource.JobDescription` (`IfcText`), when
219 /// authored. [`ResourceError::NotInSchema`] on any other kind and under
220 /// IFC4 and IFC4X3.
221 pub fn job_description(&self) -> ResourceResult<Option<&'m str>> {
222 self.record.optional_text("JobDescription")
223 }
224}
225
226/// `IfcActorSelect` members, identical in IFC2X3, IFC4 and IFC4X3.
227const ACTOR_SELECT: &[&str] = &["IfcPerson", "IfcOrganization", "IfcPersonAndOrganization"];