ifc-properties 0.5.3

Property sets, quantities, and unit resolution. No geometry.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Door and window lining properties.
//!
//! # Paired attributes
//!
//! The lining rules are pairing constraints, and door and window state
//! them differently. A door's transom and casing pairs are XOR: both or
//! neither. A window's transom and mullion offsets are ordered: the
//! second may only appear when the first does. Writing one half of
//! either pair produces a file that parses and reports a dimension
//! nothing can interpret.
//!
//! # Release
//!
//! [`add_door_lining_properties`] and [`add_window_lining_properties`]
//! take no model and write the IFC4 layout; the `*_with_owner_history`
//! variants write the model's declared release (see `owned.rs`). IFC2X3
//! declares neither `LiningToPanelOffsetX/Y` nor, on a window,
//! `LiningOffset`, and types the thicknesses as positive lengths.

use ifc_model::{EntityId, Model, Transaction, Value};

use crate::PropertyResult;

use super::owned::{stage_ifc4, stage_owned, Rooted};
use super::predefined::{invalid, measure, ratio, require_guid, Measure};

/// Attributes of an `IfcDoorLiningProperties`.
#[derive(Debug, Clone, Copy, Default)]
pub struct DoorLiningDraft<'a> {
    /// `Name`.
    pub name: Option<&'a str>,
    /// `Description`.
    pub description: Option<&'a str>,
    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
    pub lining_depth: Option<f64>,
    /// `LiningThickness`, a non-negative length.
    pub lining_thickness: Option<f64>,
    /// `ThresholdDepth`, a positive length. Requires `threshold_thickness` (WR32).
    pub threshold_depth: Option<f64>,
    /// `ThresholdThickness`, a non-negative length.
    pub threshold_thickness: Option<f64>,
    /// `TransomThickness`, a non-negative length. Paired with `transom_offset` (WR33).
    pub transom_thickness: Option<f64>,
    /// `TransomOffset`, a length. Paired with `transom_thickness` (WR33).
    pub transom_offset: Option<f64>,
    /// `LiningOffset`, a length.
    pub lining_offset: Option<f64>,
    /// `ThresholdOffset`, a length.
    pub threshold_offset: Option<f64>,
    /// `CasingThickness`, a positive length. Paired with `casing_depth` (WR34).
    pub casing_thickness: Option<f64>,
    /// `CasingDepth`, a positive length. Paired with `casing_thickness` (WR34).
    pub casing_depth: Option<f64>,
    /// `ShapeAspectStyle`.
    pub shape_aspect_style: Option<EntityId>,
    /// `LiningToPanelOffsetX`, a length.
    pub lining_to_panel_offset_x: Option<f64>,
    /// `LiningToPanelOffsetY`, a length.
    pub lining_to_panel_offset_y: Option<f64>,
}

/// Stage an `IfcDoorLiningProperties`.
///
/// Takes no model, so it writes the IFC4 layout with `OwnerHistory` `$`:
/// valid IFC4 and IFC4X3, never valid IFC2X3. Use
/// [`add_door_lining_properties_with_owner_history`] to write the model's
/// declared release.
///
/// # Errors
///
/// Refuses a malformed GlobalId; a depth without its thickness (WR31,
/// WR32); a transom or casing pair with exactly one half set (WR33,
/// WR34); and any measure that violates its schema measure type.
///
/// WR35 requires the set to define an `IfcDoorType`. That is a property
/// of the attachment, not of this record, so it is enforced where the
/// set is attached rather than invented here.
pub fn add_door_lining_properties(
    tx: &mut Transaction,
    global_id: &str,
    draft: DoorLiningDraft<'_>,
) -> PropertyResult<EntityId> {
    stage_ifc4(tx, door_lining(global_id, draft)?)
}

/// [`add_door_lining_properties`] in the model's declared release, with a
/// caller-supplied `IfcOwnerHistory`, which IFC2X3 requires (#202).
///
/// # Errors
///
/// Those of [`add_door_lining_properties`], and: a model that binds no
/// single known release; an `owner_history` that is not in the model or
/// staged ([`MissingEntity`](crate::PropertyError::MissingEntity)) or is
/// not an `IfcOwnerHistory`
/// ([`AuthoringInvalid`](crate::PropertyError::AuthoringInvalid)); in
/// IFC2X3 a `lining_to_panel_offset_x/y`
/// ([`AuthoringNotInSchema`](crate::PropertyError::AuthoringNotInSchema))
/// and a zero thickness, which IFC2X3 types as `IfcPositiveLengthMeasure`.
/// Nothing is staged on an error.
pub fn add_door_lining_properties_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    draft: DoorLiningDraft<'_>,
    owner_history: EntityId,
) -> PropertyResult<EntityId> {
    stage_owned(tx, model, door_lining(global_id, draft)?, owner_history)
}

fn door_lining<'a>(global_id: &'a str, draft: DoorLiningDraft<'a>) -> PropertyResult<Rooted<'a>> {
    const ENTITY: &str = "IFCDOORLININGPROPERTIES";
    if draft.lining_depth.is_some() && draft.lining_thickness.is_none() {
        return Err(invalid(
            ENTITY,
            "LiningThickness",
            "WR31: a lining depth needs its thickness",
        ));
    }
    if draft.threshold_depth.is_some() && draft.threshold_thickness.is_none() {
        return Err(invalid(
            ENTITY,
            "ThresholdThickness",
            "WR32: a threshold depth needs its thickness",
        ));
    }
    if draft.transom_offset.is_some() != draft.transom_thickness.is_some() {
        return Err(invalid(
            ENTITY,
            "TransomOffset",
            "WR33: transom offset and thickness are all or nothing",
        ));
    }
    if draft.casing_depth.is_some() != draft.casing_thickness.is_some() {
        return Err(invalid(
            ENTITY,
            "CasingDepth",
            "WR34: casing depth and thickness are all or nothing",
        ));
    }
    require_guid(ENTITY, global_id)?;
    let m = |attribute, value, kind| measure(ENTITY, attribute, value, kind);
    let values = vec![
        (
            "LiningDepth",
            m("LiningDepth", draft.lining_depth, Measure::Positive)?,
        ),
        (
            "LiningThickness",
            m(
                "LiningThickness",
                draft.lining_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "ThresholdDepth",
            m("ThresholdDepth", draft.threshold_depth, Measure::Positive)?,
        ),
        (
            "ThresholdThickness",
            m(
                "ThresholdThickness",
                draft.threshold_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "TransomThickness",
            m(
                "TransomThickness",
                draft.transom_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "TransomOffset",
            m("TransomOffset", draft.transom_offset, Measure::Length)?,
        ),
        (
            "LiningOffset",
            m("LiningOffset", draft.lining_offset, Measure::Length)?,
        ),
        (
            "ThresholdOffset",
            m("ThresholdOffset", draft.threshold_offset, Measure::Length)?,
        ),
        (
            "CasingThickness",
            m("CasingThickness", draft.casing_thickness, Measure::Positive)?,
        ),
        (
            "CasingDepth",
            m("CasingDepth", draft.casing_depth, Measure::Positive)?,
        ),
        (
            "ShapeAspectStyle",
            draft.shape_aspect_style.map_or(Value::Null, Value::Ref),
        ),
        (
            "LiningToPanelOffsetX",
            m(
                "LiningToPanelOffsetX",
                draft.lining_to_panel_offset_x,
                Measure::Length,
            )?,
        ),
        (
            "LiningToPanelOffsetY",
            m(
                "LiningToPanelOffsetY",
                draft.lining_to_panel_offset_y,
                Measure::Length,
            )?,
        ),
    ];
    Ok(Rooted {
        entity: ENTITY,
        global_id,
        name: draft.name,
        description: draft.description,
        values,
    })
}

/// Attributes of an `IfcWindowLiningProperties`.
#[derive(Debug, Clone, Copy, Default)]
pub struct WindowLiningDraft<'a> {
    /// `Name`.
    pub name: Option<&'a str>,
    /// `Description`.
    pub description: Option<&'a str>,
    /// `LiningDepth`, a positive length. Requires `lining_thickness` (WR31).
    pub lining_depth: Option<f64>,
    /// `LiningThickness`, a non-negative length.
    pub lining_thickness: Option<f64>,
    /// `TransomThickness`, a non-negative length.
    pub transom_thickness: Option<f64>,
    /// `MullionThickness`, a non-negative length.
    pub mullion_thickness: Option<f64>,
    /// `FirstTransomOffset`, a normalised ratio.
    pub first_transom_offset: Option<f64>,
    /// `SecondTransomOffset`, a normalised ratio. Needs the first (WR32).
    pub second_transom_offset: Option<f64>,
    /// `FirstMullionOffset`, a normalised ratio.
    pub first_mullion_offset: Option<f64>,
    /// `SecondMullionOffset`, a normalised ratio. Needs the first (WR33).
    pub second_mullion_offset: Option<f64>,
    /// `ShapeAspectStyle`.
    pub shape_aspect_style: Option<EntityId>,
    /// `LiningOffset`, a length.
    pub lining_offset: Option<f64>,
    /// `LiningToPanelOffsetX`, a length.
    pub lining_to_panel_offset_x: Option<f64>,
    /// `LiningToPanelOffsetY`, a length.
    pub lining_to_panel_offset_y: Option<f64>,
}

/// Stage an `IfcWindowLiningProperties`.
///
/// Takes no model, so it writes the IFC4 layout with `OwnerHistory` `$`:
/// valid IFC4 and IFC4X3, never valid IFC2X3. Use
/// [`add_window_lining_properties_with_owner_history`] to write the
/// model's declared release.
///
/// # Errors
///
/// Refuses a malformed GlobalId; a lining depth without its thickness
/// (WR31); a second transom or mullion offset without the first (WR32,
/// WR33); and any measure outside its schema type. The offsets are
/// `IfcNormalisedRatioMeasure`, so they are bounded to `[0, 1]` rather
/// than treated as free lengths.
///
/// Unlike the door rules, these are ordered rather than XOR: a first
/// offset alone is legal, a second alone is not.
pub fn add_window_lining_properties(
    tx: &mut Transaction,
    global_id: &str,
    draft: WindowLiningDraft<'_>,
) -> PropertyResult<EntityId> {
    stage_ifc4(tx, window_lining(global_id, draft)?)
}

/// [`add_window_lining_properties`] in the model's declared release, with
/// a caller-supplied `IfcOwnerHistory`, which IFC2X3 requires (#202).
///
/// # Errors
///
/// Those of [`add_window_lining_properties`] and the release and
/// owner-history refusals of
/// [`add_door_lining_properties_with_owner_history`]. IFC2X3 declares no
/// `LiningOffset` and no `LiningToPanelOffsetX/Y` on a window lining.
/// Nothing is staged on an error.
pub fn add_window_lining_properties_with_owner_history(
    tx: &mut Transaction,
    model: &Model,
    global_id: &str,
    draft: WindowLiningDraft<'_>,
    owner_history: EntityId,
) -> PropertyResult<EntityId> {
    stage_owned(tx, model, window_lining(global_id, draft)?, owner_history)
}

fn window_lining<'a>(
    global_id: &'a str,
    draft: WindowLiningDraft<'a>,
) -> PropertyResult<Rooted<'a>> {
    const ENTITY: &str = "IFCWINDOWLININGPROPERTIES";
    if draft.lining_depth.is_some() && draft.lining_thickness.is_none() {
        return Err(invalid(
            ENTITY,
            "LiningThickness",
            "WR31: a lining depth needs its thickness",
        ));
    }
    if draft.second_transom_offset.is_some() && draft.first_transom_offset.is_none() {
        return Err(invalid(
            ENTITY,
            "SecondTransomOffset",
            "WR32: a second transom offset needs the first",
        ));
    }
    if draft.second_mullion_offset.is_some() && draft.first_mullion_offset.is_none() {
        return Err(invalid(
            ENTITY,
            "SecondMullionOffset",
            "WR33: a second mullion offset needs the first",
        ));
    }
    require_guid(ENTITY, global_id)?;
    let m = |attribute, value, kind| measure(ENTITY, attribute, value, kind);
    let r = |attribute, value| ratio(ENTITY, attribute, value);
    let values = vec![
        (
            "LiningDepth",
            m("LiningDepth", draft.lining_depth, Measure::Positive)?,
        ),
        (
            "LiningThickness",
            m(
                "LiningThickness",
                draft.lining_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "TransomThickness",
            m(
                "TransomThickness",
                draft.transom_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "MullionThickness",
            m(
                "MullionThickness",
                draft.mullion_thickness,
                Measure::NonNegative,
            )?,
        ),
        (
            "FirstTransomOffset",
            r("FirstTransomOffset", draft.first_transom_offset)?,
        ),
        (
            "SecondTransomOffset",
            r("SecondTransomOffset", draft.second_transom_offset)?,
        ),
        (
            "FirstMullionOffset",
            r("FirstMullionOffset", draft.first_mullion_offset)?,
        ),
        (
            "SecondMullionOffset",
            r("SecondMullionOffset", draft.second_mullion_offset)?,
        ),
        (
            "ShapeAspectStyle",
            draft.shape_aspect_style.map_or(Value::Null, Value::Ref),
        ),
        (
            "LiningOffset",
            m("LiningOffset", draft.lining_offset, Measure::Length)?,
        ),
        (
            "LiningToPanelOffsetX",
            m(
                "LiningToPanelOffsetX",
                draft.lining_to_panel_offset_x,
                Measure::Length,
            )?,
        ),
        (
            "LiningToPanelOffsetY",
            m(
                "LiningToPanelOffsetY",
                draft.lining_to_panel_offset_y,
                Measure::Length,
            )?,
        ),
    ];
    Ok(Rooted {
        entity: ENTITY,
        global_id,
        name: draft.name,
        description: draft.description,
        values,
    })
}