Skip to main content

ifc_properties/pset/
lining.rs

1//! Door and window lining properties.
2//!
3//! # Paired attributes
4//!
5//! The lining rules are pairing constraints, and door and window state
6//! them differently. A door's transom and casing pairs are XOR: both or
7//! neither. A window's transom and mullion offsets are ordered: the
8//! second may only appear when the first does. Writing one half of
9//! either pair produces a file that parses and reports a dimension
10//! nothing can interpret.
11//!
12//! # Release
13//!
14//! [`add_door_lining_properties`] and [`add_window_lining_properties`]
15//! take no model and write the IFC4 layout; the `*_with_owner_history`
16//! variants write the model's declared release (see `owned.rs`). IFC2X3
17//! declares neither `LiningToPanelOffsetX/Y` nor, on a window,
18//! `LiningOffset`, and types the thicknesses as positive lengths.
19
20use ifc_model::{EntityId, Model, Transaction, Value};
21
22use crate::PropertyResult;
23
24use super::owned::{stage_ifc4, stage_owned, Rooted};
25use super::predefined::{invalid, measure, ratio, require_guid, Measure};
26
27/// Attributes of an `IfcDoorLiningProperties`.
28#[derive(Debug, Clone, Copy, Default)]
29#[non_exhaustive]
30pub struct DoorLiningDraft<'a> {
31    /// `Name`.
32    pub name: Option<&'a str>,
33    /// `Description`.
34    pub description: Option<&'a str>,
35    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
36    pub lining_depth: Option<f64>,
37    /// `LiningThickness`, a non-negative length.
38    pub lining_thickness: Option<f64>,
39    /// `ThresholdDepth`, a positive length. Requires `threshold_thickness` (WR32).
40    pub threshold_depth: Option<f64>,
41    /// `ThresholdThickness`, a non-negative length.
42    pub threshold_thickness: Option<f64>,
43    /// `TransomThickness`, a non-negative length. Paired with `transom_offset` (WR33).
44    pub transom_thickness: Option<f64>,
45    /// `TransomOffset`, a length. Paired with `transom_thickness` (WR33).
46    pub transom_offset: Option<f64>,
47    /// `LiningOffset`, a length.
48    pub lining_offset: Option<f64>,
49    /// `ThresholdOffset`, a length.
50    pub threshold_offset: Option<f64>,
51    /// `CasingThickness`, a positive length. Paired with `casing_depth` (WR34).
52    pub casing_thickness: Option<f64>,
53    /// `CasingDepth`, a positive length. Paired with `casing_thickness` (WR34).
54    pub casing_depth: Option<f64>,
55    /// `ShapeAspectStyle`.
56    pub shape_aspect_style: Option<EntityId>,
57    /// `LiningToPanelOffsetX`, a length.
58    pub lining_to_panel_offset_x: Option<f64>,
59    /// `LiningToPanelOffsetY`, a length.
60    pub lining_to_panel_offset_y: Option<f64>,
61}
62
63impl<'a> DoorLiningDraft<'a> {
64    /// Starts a draft with every field unset.
65    #[must_use]
66    pub fn new() -> Self {
67        Self::default()
68    }
69
70    /// Sets `name`.
71    ///
72    /// `Name`.
73    #[must_use]
74    pub fn name(mut self, value: &'a str) -> Self {
75        self.name = Some(value);
76        self
77    }
78
79    /// Sets `description`.
80    ///
81    /// `Description`.
82    #[must_use]
83    pub fn description(mut self, value: &'a str) -> Self {
84        self.description = Some(value);
85        self
86    }
87
88    /// Sets `lining_depth`.
89    ///
90    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
91    #[must_use]
92    pub fn lining_depth(mut self, value: f64) -> Self {
93        self.lining_depth = Some(value);
94        self
95    }
96
97    /// Sets `lining_thickness`.
98    ///
99    /// `LiningThickness`, a non-negative length.
100    #[must_use]
101    pub fn lining_thickness(mut self, value: f64) -> Self {
102        self.lining_thickness = Some(value);
103        self
104    }
105
106    /// Sets `threshold_depth`.
107    ///
108    /// `ThresholdDepth`, a positive length. Requires `threshold_thickness` (WR32).
109    #[must_use]
110    pub fn threshold_depth(mut self, value: f64) -> Self {
111        self.threshold_depth = Some(value);
112        self
113    }
114
115    /// Sets `threshold_thickness`.
116    ///
117    /// `ThresholdThickness`, a non-negative length.
118    #[must_use]
119    pub fn threshold_thickness(mut self, value: f64) -> Self {
120        self.threshold_thickness = Some(value);
121        self
122    }
123
124    /// Sets `transom_thickness`.
125    ///
126    /// `TransomThickness`, a non-negative length. Paired with `transom_offset` (WR33).
127    #[must_use]
128    pub fn transom_thickness(mut self, value: f64) -> Self {
129        self.transom_thickness = Some(value);
130        self
131    }
132
133    /// Sets `transom_offset`.
134    ///
135    /// `TransomOffset`, a length. Paired with `transom_thickness` (WR33).
136    #[must_use]
137    pub fn transom_offset(mut self, value: f64) -> Self {
138        self.transom_offset = Some(value);
139        self
140    }
141
142    /// Sets `lining_offset`.
143    ///
144    /// `LiningOffset`, a length.
145    #[must_use]
146    pub fn lining_offset(mut self, value: f64) -> Self {
147        self.lining_offset = Some(value);
148        self
149    }
150
151    /// Sets `threshold_offset`.
152    ///
153    /// `ThresholdOffset`, a length.
154    #[must_use]
155    pub fn threshold_offset(mut self, value: f64) -> Self {
156        self.threshold_offset = Some(value);
157        self
158    }
159
160    /// Sets `casing_thickness`.
161    ///
162    /// `CasingThickness`, a positive length. Paired with `casing_depth` (WR34).
163    #[must_use]
164    pub fn casing_thickness(mut self, value: f64) -> Self {
165        self.casing_thickness = Some(value);
166        self
167    }
168
169    /// Sets `casing_depth`.
170    ///
171    /// `CasingDepth`, a positive length. Paired with `casing_thickness` (WR34).
172    #[must_use]
173    pub fn casing_depth(mut self, value: f64) -> Self {
174        self.casing_depth = Some(value);
175        self
176    }
177
178    /// Sets `shape_aspect_style`.
179    ///
180    /// `ShapeAspectStyle`.
181    #[must_use]
182    pub fn shape_aspect_style(mut self, value: EntityId) -> Self {
183        self.shape_aspect_style = Some(value);
184        self
185    }
186
187    /// Sets `lining_to_panel_offset_x`.
188    ///
189    /// `LiningToPanelOffsetX`, a length.
190    #[must_use]
191    pub fn lining_to_panel_offset_x(mut self, value: f64) -> Self {
192        self.lining_to_panel_offset_x = Some(value);
193        self
194    }
195
196    /// Sets `lining_to_panel_offset_y`.
197    ///
198    /// `LiningToPanelOffsetY`, a length.
199    #[must_use]
200    pub fn lining_to_panel_offset_y(mut self, value: f64) -> Self {
201        self.lining_to_panel_offset_y = Some(value);
202        self
203    }
204}
205
206/// Stage an `IfcDoorLiningProperties`.
207///
208/// Takes no model, so it writes the IFC4 layout with `OwnerHistory` `$`:
209/// valid IFC4 and IFC4X3, never valid IFC2X3. Use
210/// [`add_door_lining_properties_with_owner_history`] to write the model's
211/// declared release.
212///
213/// # Errors
214///
215/// Refuses a malformed GlobalId; a depth without its thickness (WR31,
216/// WR32); a transom or casing pair with exactly one half set (WR33,
217/// WR34); and any measure that violates its schema measure type.
218///
219/// WR35 requires the set to define an `IfcDoorType`. That is a property
220/// of the attachment, not of this record, so it is enforced where the
221/// set is attached rather than invented here.
222pub fn add_door_lining_properties(
223    tx: &mut Transaction,
224    global_id: &str,
225    draft: DoorLiningDraft<'_>,
226) -> PropertyResult<EntityId> {
227    stage_ifc4(tx, door_lining(global_id, draft)?)
228}
229
230/// [`add_door_lining_properties`] in the model's declared release, with a
231/// caller-supplied `IfcOwnerHistory`, which IFC2X3 requires (#202).
232///
233/// # Errors
234///
235/// Those of [`add_door_lining_properties`], and: a model that binds no
236/// single known release; an `owner_history` that is not in the model or
237/// staged ([`MissingEntity`](crate::PropertyError::MissingEntity)) or is
238/// not an `IfcOwnerHistory`
239/// ([`AuthoringInvalid`](crate::PropertyError::AuthoringInvalid)); in
240/// IFC2X3 a `lining_to_panel_offset_x/y`
241/// ([`AuthoringNotInSchema`](crate::PropertyError::AuthoringNotInSchema))
242/// and a zero thickness, which IFC2X3 types as `IfcPositiveLengthMeasure`.
243/// Nothing is staged on an error.
244pub fn add_door_lining_properties_with_owner_history(
245    tx: &mut Transaction,
246    model: &Model,
247    global_id: &str,
248    draft: DoorLiningDraft<'_>,
249    owner_history: EntityId,
250) -> PropertyResult<EntityId> {
251    stage_owned(tx, model, door_lining(global_id, draft)?, owner_history)
252}
253
254fn door_lining<'a>(global_id: &'a str, draft: DoorLiningDraft<'a>) -> PropertyResult<Rooted<'a>> {
255    const ENTITY: &str = "IFCDOORLININGPROPERTIES";
256    if draft.lining_depth.is_some() && draft.lining_thickness.is_none() {
257        return Err(invalid(
258            ENTITY,
259            "LiningThickness",
260            "WR31: a lining depth needs its thickness",
261        ));
262    }
263    if draft.threshold_depth.is_some() && draft.threshold_thickness.is_none() {
264        return Err(invalid(
265            ENTITY,
266            "ThresholdThickness",
267            "WR32: a threshold depth needs its thickness",
268        ));
269    }
270    if draft.transom_offset.is_some() != draft.transom_thickness.is_some() {
271        return Err(invalid(
272            ENTITY,
273            "TransomOffset",
274            "WR33: transom offset and thickness are all or nothing",
275        ));
276    }
277    if draft.casing_depth.is_some() != draft.casing_thickness.is_some() {
278        return Err(invalid(
279            ENTITY,
280            "CasingDepth",
281            "WR34: casing depth and thickness are all or nothing",
282        ));
283    }
284    require_guid(ENTITY, global_id)?;
285    let m = |attribute, value, kind| measure(ENTITY, attribute, value, kind);
286    let values = vec![
287        (
288            "LiningDepth",
289            m("LiningDepth", draft.lining_depth, Measure::Positive)?,
290        ),
291        (
292            "LiningThickness",
293            m(
294                "LiningThickness",
295                draft.lining_thickness,
296                Measure::NonNegative,
297            )?,
298        ),
299        (
300            "ThresholdDepth",
301            m("ThresholdDepth", draft.threshold_depth, Measure::Positive)?,
302        ),
303        (
304            "ThresholdThickness",
305            m(
306                "ThresholdThickness",
307                draft.threshold_thickness,
308                Measure::NonNegative,
309            )?,
310        ),
311        (
312            "TransomThickness",
313            m(
314                "TransomThickness",
315                draft.transom_thickness,
316                Measure::NonNegative,
317            )?,
318        ),
319        (
320            "TransomOffset",
321            m("TransomOffset", draft.transom_offset, Measure::Length)?,
322        ),
323        (
324            "LiningOffset",
325            m("LiningOffset", draft.lining_offset, Measure::Length)?,
326        ),
327        (
328            "ThresholdOffset",
329            m("ThresholdOffset", draft.threshold_offset, Measure::Length)?,
330        ),
331        (
332            "CasingThickness",
333            m("CasingThickness", draft.casing_thickness, Measure::Positive)?,
334        ),
335        (
336            "CasingDepth",
337            m("CasingDepth", draft.casing_depth, Measure::Positive)?,
338        ),
339        (
340            "ShapeAspectStyle",
341            draft.shape_aspect_style.map_or(Value::Null, Value::Ref),
342        ),
343        (
344            "LiningToPanelOffsetX",
345            m(
346                "LiningToPanelOffsetX",
347                draft.lining_to_panel_offset_x,
348                Measure::Length,
349            )?,
350        ),
351        (
352            "LiningToPanelOffsetY",
353            m(
354                "LiningToPanelOffsetY",
355                draft.lining_to_panel_offset_y,
356                Measure::Length,
357            )?,
358        ),
359    ];
360    Ok(Rooted {
361        entity: ENTITY,
362        global_id,
363        name: draft.name,
364        description: draft.description,
365        values,
366    })
367}
368
369/// Attributes of an `IfcWindowLiningProperties`.
370#[derive(Debug, Clone, Copy, Default)]
371#[non_exhaustive]
372pub struct WindowLiningDraft<'a> {
373    /// `Name`.
374    pub name: Option<&'a str>,
375    /// `Description`.
376    pub description: Option<&'a str>,
377    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
378    pub lining_depth: Option<f64>,
379    /// `LiningThickness`, a non-negative length.
380    pub lining_thickness: Option<f64>,
381    /// `TransomThickness`, a non-negative length.
382    pub transom_thickness: Option<f64>,
383    /// `MullionThickness`, a non-negative length.
384    pub mullion_thickness: Option<f64>,
385    /// `FirstTransomOffset`, a normalised ratio.
386    pub first_transom_offset: Option<f64>,
387    /// `SecondTransomOffset`, a normalised ratio. Needs the first (WR32).
388    pub second_transom_offset: Option<f64>,
389    /// `FirstMullionOffset`, a normalised ratio.
390    pub first_mullion_offset: Option<f64>,
391    /// `SecondMullionOffset`, a normalised ratio. Needs the first (WR33).
392    pub second_mullion_offset: Option<f64>,
393    /// `ShapeAspectStyle`.
394    pub shape_aspect_style: Option<EntityId>,
395    /// `LiningOffset`, a length.
396    pub lining_offset: Option<f64>,
397    /// `LiningToPanelOffsetX`, a length.
398    pub lining_to_panel_offset_x: Option<f64>,
399    /// `LiningToPanelOffsetY`, a length.
400    pub lining_to_panel_offset_y: Option<f64>,
401}
402
403impl<'a> WindowLiningDraft<'a> {
404    /// Starts a draft with every field unset.
405    #[must_use]
406    pub fn new() -> Self {
407        Self::default()
408    }
409
410    /// Sets `name`.
411    ///
412    /// `Name`.
413    #[must_use]
414    pub fn name(mut self, value: &'a str) -> Self {
415        self.name = Some(value);
416        self
417    }
418
419    /// Sets `description`.
420    ///
421    /// `Description`.
422    #[must_use]
423    pub fn description(mut self, value: &'a str) -> Self {
424        self.description = Some(value);
425        self
426    }
427
428    /// Sets `lining_depth`.
429    ///
430    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
431    #[must_use]
432    pub fn lining_depth(mut self, value: f64) -> Self {
433        self.lining_depth = Some(value);
434        self
435    }
436
437    /// Sets `lining_thickness`.
438    ///
439    /// `LiningThickness`, a non-negative length.
440    #[must_use]
441    pub fn lining_thickness(mut self, value: f64) -> Self {
442        self.lining_thickness = Some(value);
443        self
444    }
445
446    /// Sets `transom_thickness`.
447    ///
448    /// `TransomThickness`, a non-negative length.
449    #[must_use]
450    pub fn transom_thickness(mut self, value: f64) -> Self {
451        self.transom_thickness = Some(value);
452        self
453    }
454
455    /// Sets `mullion_thickness`.
456    ///
457    /// `MullionThickness`, a non-negative length.
458    #[must_use]
459    pub fn mullion_thickness(mut self, value: f64) -> Self {
460        self.mullion_thickness = Some(value);
461        self
462    }
463
464    /// Sets `first_transom_offset`.
465    ///
466    /// `FirstTransomOffset`, a normalised ratio.
467    #[must_use]
468    pub fn first_transom_offset(mut self, value: f64) -> Self {
469        self.first_transom_offset = Some(value);
470        self
471    }
472
473    /// Sets `second_transom_offset`.
474    ///
475    /// `SecondTransomOffset`, a normalised ratio. Needs the first (WR32).
476    #[must_use]
477    pub fn second_transom_offset(mut self, value: f64) -> Self {
478        self.second_transom_offset = Some(value);
479        self
480    }
481
482    /// Sets `first_mullion_offset`.
483    ///
484    /// `FirstMullionOffset`, a normalised ratio.
485    #[must_use]
486    pub fn first_mullion_offset(mut self, value: f64) -> Self {
487        self.first_mullion_offset = Some(value);
488        self
489    }
490
491    /// Sets `second_mullion_offset`.
492    ///
493    /// `SecondMullionOffset`, a normalised ratio. Needs the first (WR33).
494    #[must_use]
495    pub fn second_mullion_offset(mut self, value: f64) -> Self {
496        self.second_mullion_offset = Some(value);
497        self
498    }
499
500    /// Sets `shape_aspect_style`.
501    ///
502    /// `ShapeAspectStyle`.
503    #[must_use]
504    pub fn shape_aspect_style(mut self, value: EntityId) -> Self {
505        self.shape_aspect_style = Some(value);
506        self
507    }
508
509    /// Sets `lining_offset`.
510    ///
511    /// `LiningOffset`, a length.
512    #[must_use]
513    pub fn lining_offset(mut self, value: f64) -> Self {
514        self.lining_offset = Some(value);
515        self
516    }
517
518    /// Sets `lining_to_panel_offset_x`.
519    ///
520    /// `LiningToPanelOffsetX`, a length.
521    #[must_use]
522    pub fn lining_to_panel_offset_x(mut self, value: f64) -> Self {
523        self.lining_to_panel_offset_x = Some(value);
524        self
525    }
526
527    /// Sets `lining_to_panel_offset_y`.
528    ///
529    /// `LiningToPanelOffsetY`, a length.
530    #[must_use]
531    pub fn lining_to_panel_offset_y(mut self, value: f64) -> Self {
532        self.lining_to_panel_offset_y = Some(value);
533        self
534    }
535}
536
537/// Stage an `IfcWindowLiningProperties`.
538///
539/// Takes no model, so it writes the IFC4 layout with `OwnerHistory` `$`:
540/// valid IFC4 and IFC4X3, never valid IFC2X3. Use
541/// [`add_window_lining_properties_with_owner_history`] to write the
542/// model's declared release.
543///
544/// # Errors
545///
546/// Refuses a malformed GlobalId; a lining depth without its thickness
547/// (WR31); a second transom or mullion offset without the first (WR32,
548/// WR33); and any measure outside its schema type. The offsets are
549/// `IfcNormalisedRatioMeasure`, so they are bounded to `[0, 1]` rather
550/// than treated as free lengths.
551///
552/// Unlike the door rules, these are ordered rather than XOR: a first
553/// offset alone is legal, a second alone is not.
554pub fn add_window_lining_properties(
555    tx: &mut Transaction,
556    global_id: &str,
557    draft: WindowLiningDraft<'_>,
558) -> PropertyResult<EntityId> {
559    stage_ifc4(tx, window_lining(global_id, draft)?)
560}
561
562/// [`add_window_lining_properties`] in the model's declared release, with
563/// a caller-supplied `IfcOwnerHistory`, which IFC2X3 requires (#202).
564///
565/// # Errors
566///
567/// Those of [`add_window_lining_properties`] and the release and
568/// owner-history refusals of
569/// [`add_door_lining_properties_with_owner_history`]. IFC2X3 declares no
570/// `LiningOffset` and no `LiningToPanelOffsetX/Y` on a window lining.
571/// Nothing is staged on an error.
572pub fn add_window_lining_properties_with_owner_history(
573    tx: &mut Transaction,
574    model: &Model,
575    global_id: &str,
576    draft: WindowLiningDraft<'_>,
577    owner_history: EntityId,
578) -> PropertyResult<EntityId> {
579    stage_owned(tx, model, window_lining(global_id, draft)?, owner_history)
580}
581
582fn window_lining<'a>(
583    global_id: &'a str,
584    draft: WindowLiningDraft<'a>,
585) -> PropertyResult<Rooted<'a>> {
586    const ENTITY: &str = "IFCWINDOWLININGPROPERTIES";
587    if draft.lining_depth.is_some() && draft.lining_thickness.is_none() {
588        return Err(invalid(
589            ENTITY,
590            "LiningThickness",
591            "WR31: a lining depth needs its thickness",
592        ));
593    }
594    if draft.second_transom_offset.is_some() && draft.first_transom_offset.is_none() {
595        return Err(invalid(
596            ENTITY,
597            "SecondTransomOffset",
598            "WR32: a second transom offset needs the first",
599        ));
600    }
601    if draft.second_mullion_offset.is_some() && draft.first_mullion_offset.is_none() {
602        return Err(invalid(
603            ENTITY,
604            "SecondMullionOffset",
605            "WR33: a second mullion offset needs the first",
606        ));
607    }
608    require_guid(ENTITY, global_id)?;
609    let m = |attribute, value, kind| measure(ENTITY, attribute, value, kind);
610    let r = |attribute, value| ratio(ENTITY, attribute, value);
611    let values = vec![
612        (
613            "LiningDepth",
614            m("LiningDepth", draft.lining_depth, Measure::Positive)?,
615        ),
616        (
617            "LiningThickness",
618            m(
619                "LiningThickness",
620                draft.lining_thickness,
621                Measure::NonNegative,
622            )?,
623        ),
624        (
625            "TransomThickness",
626            m(
627                "TransomThickness",
628                draft.transom_thickness,
629                Measure::NonNegative,
630            )?,
631        ),
632        (
633            "MullionThickness",
634            m(
635                "MullionThickness",
636                draft.mullion_thickness,
637                Measure::NonNegative,
638            )?,
639        ),
640        (
641            "FirstTransomOffset",
642            r("FirstTransomOffset", draft.first_transom_offset)?,
643        ),
644        (
645            "SecondTransomOffset",
646            r("SecondTransomOffset", draft.second_transom_offset)?,
647        ),
648        (
649            "FirstMullionOffset",
650            r("FirstMullionOffset", draft.first_mullion_offset)?,
651        ),
652        (
653            "SecondMullionOffset",
654            r("SecondMullionOffset", draft.second_mullion_offset)?,
655        ),
656        (
657            "ShapeAspectStyle",
658            draft.shape_aspect_style.map_or(Value::Null, Value::Ref),
659        ),
660        (
661            "LiningOffset",
662            m("LiningOffset", draft.lining_offset, Measure::Length)?,
663        ),
664        (
665            "LiningToPanelOffsetX",
666            m(
667                "LiningToPanelOffsetX",
668                draft.lining_to_panel_offset_x,
669                Measure::Length,
670            )?,
671        ),
672        (
673            "LiningToPanelOffsetY",
674            m(
675                "LiningToPanelOffsetY",
676                draft.lining_to_panel_offset_y,
677                Measure::Length,
678            )?,
679        ),
680    ];
681    Ok(Rooted {
682        entity: ENTITY,
683        global_id,
684        name: draft.name,
685        description: draft.description,
686        values,
687    })
688}