ifc_lite_export/plan/
add_builders.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Constructors for [`super::Disposition::Add`]'s public vocabulary; see
3//! `builders.rs` for why the plan's types are built this way.
4
5use super::add_assoc_types::{
6    ClassificationLink, ClassificationSystem, LayerMaterial, MaterialLink, NewClassification, NewClassificationReference,
7    NewLayerSet, NewMaterial, NewMaterialLayer, NewProperty, NewPropertySet, PropertyValue, ValueUnit,
8};
9use super::add_types::{
10    ClassSpec, Extrusion, GlobalId, IfcClass, MappedBody, NewObject, NewOpening, NewPlacement, NewType, Representation, TypeLink,
11};
12use super::types::{ObjectRef, PsetKind, SurfaceColour};
13impl ClassSpec {
14    /// `class` with no `PredefinedType`.
15    pub fn new(class: IfcClass) -> Self {
16        Self { class, predefined_type: None }
17    }
18
19    pub fn with_predefined_type(mut self, predefined_type: impl Into<String>) -> Self {
20        self.predefined_type = Some(predefined_type.into());
21        self
22    }
23}
24
25impl From<IfcClass> for ClassSpec {
26    fn from(class: IfcClass) -> Self {
27        Self::new(class)
28    }
29}
30
31impl NewPlacement {
32    /// At `matrix`, relative to the placement of the product the object is
33    /// nested in, else its container's (or absolute with neither).
34    pub fn new(matrix: [[f64; 4]; 4]) -> Self {
35        Self { matrix, relative_to: None }
36    }
37
38    pub fn with_relative_to(mut self, relative_to: ObjectRef) -> Self {
39        self.relative_to = Some(relative_to);
40        self
41    }
42}
43
44impl Extrusion {
45    /// `profile` swept along `direction` for `depth`, with no voids.
46    pub fn new(profile: Vec<[f64; 2]>, direction: [f64; 3], depth: f64) -> Self {
47        Self { profile, voids: Vec::new(), direction, depth }
48    }
49
50    pub fn with_voids(mut self, voids: Vec<Vec<[f64; 2]>>) -> Self {
51        self.voids = voids;
52        self
53    }
54}
55
56impl PropertyValue {
57    /// A numeric measure by its IFC defined-type name, e.g.
58    /// `PropertyValue::measure("IfcLengthMeasure", 3.0)`.
59    pub fn measure(measure_type: impl Into<String>, value: f64) -> Self {
60        PropertyValue::Measure { measure_type: measure_type.into(), value }
61    }
62}
63
64impl ValueUnit {
65    /// A new `IfcSIUnit` by name (`"PASCAL"`), with no prefix.
66    pub fn si(name: impl Into<String>) -> Self {
67        ValueUnit::Si { prefix: None, name: name.into() }
68    }
69
70    /// A new prefixed `IfcSIUnit`, e.g. `ValueUnit::si_prefixed("KILO", "PASCAL")`.
71    pub fn si_prefixed(prefix: impl Into<String>, name: impl Into<String>) -> Self {
72        ValueUnit::Si { prefix: Some(prefix.into()), name: name.into() }
73    }
74}
75
76impl NewProperty {
77    pub fn new(name: impl Into<String>, value: PropertyValue) -> Self {
78        Self { name: name.into(), value, unit: None }
79    }
80
81    pub fn with_unit(mut self, unit: ValueUnit) -> Self {
82        self.unit = Some(unit);
83        self
84    }
85}
86
87impl NewPropertySet {
88    pub fn new(name: impl Into<String>, kind: PsetKind, properties: Vec<NewProperty>) -> Self {
89        Self { name: name.into(), kind, properties }
90    }
91}
92
93impl NewType {
94    /// A type of `class`, its GlobalId derived from the occurrence that
95    /// creates it.
96    pub fn new(class: impl Into<ClassSpec>) -> Self {
97        Self { global_id: None, class: class.into(), name: None, tag: None, element_type: None, psets: Vec::new(), representation: Representation::None, style: None }
98    }
99
100    pub fn with_global_id(mut self, global_id: GlobalId) -> Self {
101        self.global_id = Some(global_id);
102        self
103    }
104
105    pub fn with_name(mut self, name: impl Into<String>) -> Self {
106        self.name = Some(name.into());
107        self
108    }
109
110    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
111        self.tag = Some(tag.into());
112        self
113    }
114
115    pub fn with_element_type(mut self, element_type: impl Into<String>) -> Self {
116        self.element_type = Some(element_type.into());
117        self
118    }
119
120    pub fn with_psets(mut self, psets: Vec<NewPropertySet>) -> Self {
121        self.psets = psets;
122        self
123    }
124
125    /// The geometry every occurrence with a mapped body shares.
126    pub fn with_representation(mut self, representation: Representation) -> Self {
127        self.representation = representation;
128        self
129    }
130
131    pub fn with_style(mut self, style: SurfaceColour) -> Self {
132        self.style = Some(style);
133        self
134    }
135}
136
137impl MappedBody {
138    pub fn new(transform: [[f64; 4]; 4]) -> Self {
139        Self { transform }
140    }
141}
142
143impl NewOpening {
144    pub fn new(host: ObjectRef, matrix: [[f64; 4]; 4], shape: Extrusion) -> Self {
145        Self { host, matrix, shape }
146    }
147}
148
149impl NewObject {
150    /// An object with nothing but its identity, class and placement: no
151    /// name, container, type, geometry, colour, sets, opening or
152    /// associations until a setter adds them.
153    pub fn new(global_id: GlobalId, class: impl Into<ClassSpec>, placement: NewPlacement) -> Self {
154        Self {
155            global_id,
156            class: class.into(),
157            name: None,
158            long_name: None,
159            tag: None,
160            object_type: None,
161            placement,
162            container: None,
163            nest_in: None,
164            type_link: None,
165            representation: Representation::None,
166            style: None,
167            psets: Vec::new(),
168            opening: None,
169            material: None,
170            classifications: Vec::new(),
171            elevation: None,
172        }
173    }
174
175    pub fn with_name(mut self, name: impl Into<String>) -> Self {
176        self.name = Some(name.into());
177        self
178    }
179
180    pub fn with_long_name(mut self, long_name: impl Into<String>) -> Self {
181        self.long_name = Some(long_name.into());
182        self
183    }
184
185    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
186        self.tag = Some(tag.into());
187        self
188    }
189
190    pub fn with_object_type(mut self, object_type: impl Into<String>) -> Self {
191        self.object_type = Some(object_type.into());
192        self
193    }
194
195    pub fn with_container(mut self, container: ObjectRef) -> Self {
196        self.container = Some(container);
197        self
198    }
199
200    pub fn with_nest_in(mut self, host: ObjectRef) -> Self {
201        self.nest_in = Some(host);
202        self
203    }
204
205    pub fn with_type(mut self, type_link: TypeLink) -> Self {
206        self.type_link = Some(type_link);
207        self
208    }
209
210    pub fn with_representation(mut self, representation: Representation) -> Self {
211        self.representation = representation;
212        self
213    }
214
215    pub fn with_style(mut self, style: SurfaceColour) -> Self {
216        self.style = Some(style);
217        self
218    }
219
220    pub fn with_psets(mut self, psets: Vec<NewPropertySet>) -> Self {
221        self.psets = psets;
222        self
223    }
224
225    pub fn with_opening(mut self, opening: NewOpening) -> Self {
226        self.opening = Some(opening);
227        self
228    }
229
230    pub fn with_material(mut self, material: MaterialLink) -> Self {
231        self.material = Some(material);
232        self
233    }
234
235    pub fn with_classifications(mut self, classifications: Vec<ClassificationLink>) -> Self {
236        self.classifications = classifications;
237        self
238    }
239
240    /// A storey's `Elevation`, in the project's length unit.
241    pub fn with_elevation(mut self, elevation: f64) -> Self {
242        self.elevation = Some(elevation);
243        self
244    }
245}
246
247impl NewMaterial {
248    pub fn new(name: impl Into<String>) -> Self {
249        Self { name: name.into(), category: None }
250    }
251
252    pub fn with_category(mut self, category: impl Into<String>) -> Self {
253        self.category = Some(category.into());
254        self
255    }
256}
257
258impl NewMaterialLayer {
259    /// A layer `thickness` thick with no material.
260    pub fn new(thickness: f64) -> Self {
261        Self { material: None, thickness }
262    }
263
264    pub fn with_material(mut self, material: LayerMaterial) -> Self {
265        self.material = Some(material);
266        self
267    }
268}
269
270impl NewLayerSet {
271    pub fn new(layers: Vec<NewMaterialLayer>) -> Self {
272        Self { name: None, layers }
273    }
274
275    pub fn with_name(mut self, name: impl Into<String>) -> Self {
276        self.name = Some(name.into());
277        self
278    }
279}
280
281impl NewClassification {
282    pub fn new(name: impl Into<String>) -> Self {
283        Self { name: name.into(), source: None, edition: None }
284    }
285
286    pub fn with_source(mut self, source: impl Into<String>) -> Self {
287        self.source = Some(source.into());
288        self
289    }
290
291    pub fn with_edition(mut self, edition: impl Into<String>) -> Self {
292        self.edition = Some(edition.into());
293        self
294    }
295}
296
297impl NewClassificationReference {
298    /// A reference into `system`. IFC requires at least one of an
299    /// identification, a name or a location.
300    pub fn new(system: ClassificationSystem) -> Self {
301        Self { system, identification: None, name: None, location: None }
302    }
303
304    pub fn with_identification(mut self, identification: impl Into<String>) -> Self {
305        self.identification = Some(identification.into());
306        self
307    }
308
309    pub fn with_name(mut self, name: impl Into<String>) -> Self {
310        self.name = Some(name.into());
311        self
312    }
313
314    pub fn with_location(mut self, location: impl Into<String>) -> Self {
315        self.location = Some(location.into());
316        self
317    }
318}
319
320#[cfg(test)]
321mod tests {
322    use super::*;
323    use crate::plan::IDENTITY;
324
325    /// Every setter lands in its own field; the constructors' defaults are
326    /// the empty description.
327    #[test]
328    fn each_setter_sets_its_field() {
329        let colour = SurfaceColour { rgb: [0.1, 0.2, 0.3], transparency: 0.5 };
330        let opening = NewOpening::new(ObjectRef::Id(10), IDENTITY, Extrusion::new(vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], [0.0, 0.0, 1.0], 1.0));
331        let set = NewPropertySet::new("P", PsetKind::Property, vec![NewProperty::new("L", PropertyValue::measure("IfcLengthMeasure", 2.0))]);
332        let o = NewObject::new(GlobalId(7), IfcClass::Wall, NewPlacement::new(IDENTITY).with_relative_to(ObjectRef::Id(3)))
333            .with_name("W")
334            .with_container(ObjectRef::Id(70))
335            .with_type(TypeLink::Existing(ObjectRef::Id(40)))
336            .with_representation(Representation::Extrusion(Extrusion::new(vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], [0.0, 0.0, 1.0], 1.0)))
337            .with_style(colour)
338            .with_psets(vec![set])
339            .with_opening(opening)
340            .with_material(MaterialLink::New(NewMaterial::new("C").with_category("concrete")))
341            .with_classifications(vec![ClassificationLink::Existing(ObjectRef::Id(9))])
342            .with_elevation(2.5);
343        assert_eq!(o.elevation, Some(2.5));
344        assert_eq!((o.global_id, o.class.class.keyword(), o.name.as_deref()), (GlobalId(7), "IFCWALL".to_string(), Some("W")));
345        assert_eq!(o.container, Some(ObjectRef::Id(70)));
346        assert_eq!(o.placement.relative_to, Some(ObjectRef::Id(3)));
347        assert!(matches!(o.type_link, Some(TypeLink::Existing(ObjectRef::Id(40)))));
348        let Representation::Extrusion(body) = &o.representation else { panic!("extrusion body") };
349        assert_eq!((body.profile.len(), body.direction, body.depth), (3, [0.0, 0.0, 1.0], 1.0));
350        assert_eq!(o.class.predefined_type, None);
351        assert_eq!(o.style.map(|c| (c.rgb, c.transparency)), Some(([0.1, 0.2, 0.3], 0.5)));
352        let op = o.opening.as_ref().unwrap();
353        assert_eq!((&op.host, op.matrix, op.shape.direction, op.shape.depth), (&ObjectRef::Id(10), IDENTITY, [0.0, 0.0, 1.0], 1.0));
354        assert_eq!(op.shape.profile, vec![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
355        let (set, prop) = (&o.psets[0], &o.psets[0].properties[0]);
356        assert_eq!((o.psets.len(), set.name.as_str(), set.kind, prop.name.as_str()), (1, "P", PsetKind::Property, "L"));
357        assert_eq!(prop.unit, None);
358        assert!(matches!(&prop.value, PropertyValue::Measure { measure_type, value, .. } if measure_type == "IfcLengthMeasure" && *value == 2.0));
359        assert!(matches!(&o.material, Some(MaterialLink::New(m)) if m.name == "C" && m.category.as_deref() == Some("concrete")));
360        assert!(matches!(o.classifications.as_slice(), [ClassificationLink::Existing(ObjectRef::Id(9))]));
361
362        let bare = NewObject::new(GlobalId(8), IfcClass::Wall, NewPlacement::new(IDENTITY));
363        assert_eq!((bare.global_id, bare.class.class.keyword()), (GlobalId(8), "IFCWALL".to_string()));
364        assert!(bare.name.is_none() && bare.container.is_none() && bare.type_link.is_none() && bare.style.is_none());
365        assert!(matches!(bare.representation, Representation::None) && bare.psets.is_empty() && bare.classifications.is_empty());
366        assert!(bare.elevation.is_none() && bare.opening.is_none() && bare.material.is_none() && bare.placement.relative_to.is_none() && bare.class.predefined_type.is_none());
367
368        let type_set = NewPropertySet::new("TS", PsetKind::Quantity, Vec::new());
369        let t = NewType::new(IfcClass::Named("IfcWallType".into())).with_global_id(GlobalId(5)).with_name("T").with_psets(vec![type_set]);
370        assert_eq!((t.global_id, t.name.as_deref()), (Some(GlobalId(5)), Some("T")));
371        assert_eq!((t.psets[0].name.as_str(), t.psets[0].kind, t.psets[0].properties.len()), ("TS", PsetKind::Quantity, 0));
372        assert_eq!((t.class.class.keyword(), t.class.predefined_type.clone()), ("IFCWALLTYPE".to_string(), None));
373        let r = NewClassificationReference::new(ClassificationSystem::New(NewClassification::new("U").with_source("s").with_edition("1")))
374            .with_identification("A")
375            .with_name("N")
376            .with_location("L");
377        assert_eq!((r.identification.as_deref(), r.name.as_deref(), r.location.as_deref()), (Some("A"), Some("N"), Some("L")));
378        let ClassificationSystem::New(system) = &r.system else { panic!("new system") };
379        assert_eq!((system.name.as_str(), system.source.as_deref(), system.edition.as_deref()), ("U", Some("s"), Some("1")));
380        let layers = NewLayerSet::new(vec![NewMaterialLayer::new(0.2).with_material(LayerMaterial::Existing(ObjectRef::Id(1)))]).with_name("S");
381        assert_eq!(layers.name.as_deref(), Some("S"));
382        assert_eq!(layers.layers[0].thickness, 0.2);
383        assert!(matches!(layers.layers[0].material, Some(LayerMaterial::Existing(ObjectRef::Id(1)))));
384        assert_eq!(ValueUnit::si_prefixed("KILO", "PASCAL"), ValueUnit::Si { prefix: Some("KILO".into()), name: "PASCAL".into() });
385        assert_eq!(ClassSpec::new(IfcClass::Wall).with_predefined_type("STANDARD").predefined_type.as_deref(), Some("STANDARD"));
386        assert_eq!(Extrusion::new(Vec::new(), [0.0, 0.0, 1.0], 1.0).with_voids(vec![vec![[1.0, 2.0]]]).voids, vec![vec![[1.0, 2.0]]]);
387        let unit = NewProperty::new("P", PropertyValue::Integer(1)).with_unit(ValueUnit::si("METRE")).unit;
388        assert_eq!(unit, Some(ValueUnit::Si { prefix: None, name: "METRE".into() }));
389    }
390}