Skip to main content

ifc_spatial/authoring/
owned_relationships.rs

1//! The objectified relationships in the model's declared release, with a
2//! caller-supplied `IfcOwnerHistory` (#202).
3//!
4//! Each writer runs the checks of its plain counterpart in
5//! [`super::relationships`], then lays the record out by attribute name
6//! from the bound release's table (`release.rs`). The names come from the
7//! reader's slot constants (`RelSlots::relating_name`), so a writer and a
8//! reader cannot disagree about which end is which.
9//!
10//! Release differences the table carries, from the EXPRESS sources:
11//! IFC2X3 TC1 declares no `IfcRelDeclares`, `IfcRelDefinesByObject`,
12//! `IfcRelAssignsToGroupByFactor` or `IfcRelInterferesElements`, and calls
13//! `IfcRelCoversSpaces.RelatingSpace` `RelatedSpace`; only IFC4X3 ADD2
14//! declares `IfcRelAdheresToElement`, `IfcRelPositions` and
15//! `IfcRelAssociatesProfileDef`; IFC4X3 adds `InterferenceSpace` to
16//! `IfcRelInterferesElements`, which IFC4 declares with nine attributes.
17
18use ifc_model::{EntityId, Model, Transaction, Value};
19
20use super::connections::{check_realizing, distinct};
21use super::relationships::check_factor;
22use super::release::stage;
23use super::{check_relate, check_relate_one, SpatialAuthoringResult};
24use crate::relation::slots::{
25    RelSlots, ADHERES_TO_ELEMENT, ASSIGNS_TO_ACTOR, ASSIGNS_TO_GROUP_BY_FACTOR, ASSIGNS_TO_PROCESS,
26    ASSIGNS_TO_PRODUCT, ASSIGNS_TO_RESOURCE, ASSOCIATES_PROFILE_DEF, CONNECTS_ELEMENTS,
27    CONNECTS_WITH_REALIZING, COVERS_ELEMENTS, COVERS_SPACES, DECLARES, DEFINES_BY_OBJECT,
28    FILLS_ELEMENT, FLOW_CONTROL_ELEMENTS, INTERFERES_ELEMENTS, POSITIONS, PROJECTS_ELEMENT,
29    VOIDS_ELEMENT,
30};
31
32pub(super) fn refs(ids: &[EntityId]) -> Value {
33    Value::List(ids.iter().copied().map(Value::Ref).collect())
34}
35
36/// A set-valued relationship: [`super::relate`] by name.
37#[allow(clippy::too_many_arguments)]
38pub(super) fn relate_owned(
39    tx: &mut Transaction,
40    model: &Model,
41    rel: RelSlots,
42    global_id: &str,
43    parent: EntityId,
44    children: &[EntityId],
45    extra: Vec<(&'static str, Value)>,
46    owner_history: Option<EntityId>,
47) -> SpatialAuthoringResult<EntityId> {
48    check_relate(rel, global_id, parent, children)?;
49    let mut values = vec![
50        ("GlobalId", Value::Text(global_id.into())),
51        (rel.relating_name(), Value::Ref(parent)),
52        (rel.related_name(), refs(children)),
53    ];
54    values.extend(extra);
55    stage(tx, model, rel.type_name, values, owner_history)
56}
57
58/// A single-valued relationship: [`super::relate_one`] by name.
59fn relate_one_owned(
60    tx: &mut Transaction,
61    model: &Model,
62    rel: RelSlots,
63    global_id: &str,
64    relating: EntityId,
65    related: EntityId,
66    owner_history: EntityId,
67) -> SpatialAuthoringResult<EntityId> {
68    check_relate_one(rel, global_id, relating, related)?;
69    let values = vec![
70        ("GlobalId", Value::Text(global_id.into())),
71        (rel.relating_name(), Value::Ref(relating)),
72        (rel.related_name(), Value::Ref(related)),
73    ];
74    stage(tx, model, rel.type_name, values, Some(owner_history))
75}
76
77/// Two distinct elements by name; `None` leaves `OwnerHistory` `$`.
78#[allow(clippy::too_many_arguments)]
79pub(super) fn pair_owned(
80    tx: &mut Transaction,
81    model: &Model,
82    rel: RelSlots,
83    global_id: &str,
84    relating: EntityId,
85    related: EntityId,
86    extra: Vec<(&'static str, Value)>,
87    owner_history: Option<EntityId>,
88) -> SpatialAuthoringResult<EntityId> {
89    distinct(rel, global_id, relating, related)?;
90    let mut values = vec![
91        ("GlobalId", Value::Text(global_id.into())),
92        (rel.relating_name(), Value::Ref(relating)),
93        (rel.related_name(), Value::Ref(related)),
94    ];
95    values.extend(extra);
96    stage(tx, model, rel.type_name, values, owner_history)
97}
98
99macro_rules! set_valued {
100    ($(#[$doc:meta])* $name:ident, $plain:ident, $rel:ident, $parent:ident, $children:ident) => {
101        $(#[$doc])*
102        ///
103        /// # Errors
104        ///
105        #[doc = concat!("Those of [`", stringify!($plain), "`](super::", stringify!($plain), "),")]
106        /// and the release and owner-history refusals of
107        /// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
108        /// Nothing is staged on an error.
109        pub fn $name(
110            tx: &mut Transaction,
111            model: &Model,
112            global_id: &str,
113            $parent: EntityId,
114            $children: &[EntityId],
115            owner_history: EntityId,
116        ) -> SpatialAuthoringResult<EntityId> {
117            relate_owned(
118                tx,
119                model,
120                $rel,
121                global_id,
122                $parent,
123                $children,
124                Vec::new(),
125                Some(owner_history),
126            )
127        }
128    };
129}
130
131macro_rules! single_valued {
132    ($(#[$doc:meta])* $name:ident, $plain:ident, $rel:ident, $relating:ident, $related:ident) => {
133        $(#[$doc])*
134        ///
135        /// # Errors
136        ///
137        #[doc = concat!("Those of [`", stringify!($plain), "`](super::", stringify!($plain), "),")]
138        /// and the release and owner-history refusals of
139        /// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
140        /// Nothing is staged on an error.
141        pub fn $name(
142            tx: &mut Transaction,
143            model: &Model,
144            global_id: &str,
145            $relating: EntityId,
146            $related: EntityId,
147            owner_history: EntityId,
148        ) -> SpatialAuthoringResult<EntityId> {
149            relate_one_owned(tx, model, $rel, global_id, $relating, $related, owner_history)
150        }
151    };
152}
153
154set_valued!(
155    /// [`cover_elements`](super::cover_elements) in the model's declared
156    /// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
157    /// requires.
158    cover_elements_with_owner_history, cover_elements, COVERS_ELEMENTS, element, coverings
159);
160set_valued!(
161    /// [`cover_spaces`](super::cover_spaces) in the model's declared release,
162    /// with a caller-supplied `IfcOwnerHistory`, which IFC2X3 requires. In
163    /// IFC2X3 the space is written as `RelatedSpace`, IFC4's
164    /// `RelatingSpace` under its IFC2X3 name.
165    cover_spaces_with_owner_history, cover_spaces, COVERS_SPACES, space, coverings
166);
167set_valued!(
168    /// [`declare`](super::declare) in the model's declared release, with a
169    /// caller-supplied `IfcOwnerHistory`. IFC2X3 declares no
170    /// `IfcRelDeclares` (`EntityNotInSchema`).
171    declare_with_owner_history, declare, DECLARES, context, definitions
172);
173set_valued!(
174    /// [`define_by_object`](super::define_by_object) in the model's declared
175    /// release, with a caller-supplied `IfcOwnerHistory`. IFC2X3 declares no
176    /// `IfcRelDefinesByObject` (`EntityNotInSchema`).
177    define_by_object_with_owner_history, define_by_object, DEFINES_BY_OBJECT, defining, defined
178);
179set_valued!(
180    /// [`control_flow_element`](super::control_flow_element) in the model's
181    /// declared release, with a caller-supplied `IfcOwnerHistory`, which
182    /// IFC2X3 requires.
183    control_flow_element_with_owner_history,
184    control_flow_element,
185    FLOW_CONTROL_ELEMENTS,
186    flow_element,
187    controls
188);
189set_valued!(
190    /// [`assign_to_actor`](super::assign_to_actor) in the model's declared
191    /// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
192    /// requires. The record has the release's eight attributes, `ActingRole`
193    /// unset, as the plain writer's does (#213).
194    assign_to_actor_with_owner_history, assign_to_actor, ASSIGNS_TO_ACTOR, actor, objects
195);
196set_valued!(
197    /// [`assign_to_product`](super::assign_to_product) in the model's
198    /// declared release, with a caller-supplied `IfcOwnerHistory`, which
199    /// IFC2X3 requires.
200    assign_to_product_with_owner_history, assign_to_product, ASSIGNS_TO_PRODUCT, product, objects
201);
202set_valued!(
203    /// [`assign_to_process`](super::assign_to_process) in the model's
204    /// declared release, with a caller-supplied `IfcOwnerHistory`, which
205    /// IFC2X3 requires. The record has the release's eight attributes,
206    /// `QuantityInProcess` unset, as the plain writer's does (#213).
207    assign_to_process_with_owner_history, assign_to_process, ASSIGNS_TO_PROCESS, process, objects
208);
209set_valued!(
210    /// [`adhere_to_element`](super::adhere_to_element) in the model's
211    /// declared release, with a caller-supplied `IfcOwnerHistory`. Only
212    /// IFC4X3 declares `IfcRelAdheresToElement` (`EntityNotInSchema`
213    /// elsewhere).
214    adhere_to_element_with_owner_history, adhere_to_element, ADHERES_TO_ELEMENT, element, features
215);
216set_valued!(
217    /// [`position_products`](super::position_products) in the model's
218    /// declared release, with a caller-supplied `IfcOwnerHistory`. Only
219    /// IFC4X3 declares `IfcRelPositions` (`EntityNotInSchema` elsewhere).
220    position_products_with_owner_history, position_products, POSITIONS, positioning, products
221);
222set_valued!(
223    /// [`assign_to_resource`](super::assign_to_resource) in the model's
224    /// declared release, with a caller-supplied `IfcOwnerHistory`, which
225    /// IFC2X3 requires.
226    assign_to_resource_with_owner_history, assign_to_resource, ASSIGNS_TO_RESOURCE, resource, objects
227);
228set_valued!(
229    /// [`associate_profile_def`](super::associate_profile_def) in the model's
230    /// declared release, with a caller-supplied `IfcOwnerHistory`. Only
231    /// IFC4X3 declares `IfcRelAssociatesProfileDef` (`EntityNotInSchema`
232    /// elsewhere).
233    associate_profile_def_with_owner_history,
234    associate_profile_def,
235    ASSOCIATES_PROFILE_DEF,
236    profile,
237    objects
238);
239single_valued!(
240    /// [`void_element`](super::void_element) in the model's declared
241    /// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
242    /// requires.
243    void_element_with_owner_history, void_element, VOIDS_ELEMENT, element, opening
244);
245single_valued!(
246    /// [`fill_element`](super::fill_element) in the model's declared
247    /// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
248    /// requires.
249    fill_element_with_owner_history, fill_element, FILLS_ELEMENT, opening, filling
250);
251single_valued!(
252    /// [`project_element`](super::project_element) in the model's declared
253    /// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
254    /// requires.
255    project_element_with_owner_history, project_element, PROJECTS_ELEMENT, element, feature
256);
257
258/// [`assign_to_group_by_factor`](super::assign_to_group_by_factor) in the
259/// model's declared release, with a caller-supplied `IfcOwnerHistory`.
260/// IFC2X3 declares no `IfcRelAssignsToGroupByFactor` (`EntityNotInSchema`).
261///
262/// # Errors
263///
264/// Those of [`assign_to_group_by_factor`](super::assign_to_group_by_factor),
265/// and the release and owner-history refusals of
266/// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
267/// Nothing is staged on an error.
268pub fn assign_to_group_by_factor_with_owner_history(
269    tx: &mut Transaction,
270    model: &Model,
271    global_id: &str,
272    group: EntityId,
273    members: &[EntityId],
274    factor: f64,
275    owner_history: EntityId,
276) -> SpatialAuthoringResult<EntityId> {
277    check_factor(factor)?;
278    relate_owned(
279        tx,
280        model,
281        ASSIGNS_TO_GROUP_BY_FACTOR,
282        global_id,
283        group,
284        members,
285        vec![("Factor", Value::Real(factor))],
286        Some(owner_history),
287    )
288}
289
290/// [`connect_elements`](super::connect_elements) in the model's declared
291/// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
292/// requires.
293///
294/// # Errors
295///
296/// Those of [`connect_elements`](super::connect_elements), and the release
297/// and owner-history refusals of
298/// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
299/// Nothing is staged on an error.
300pub fn connect_elements_with_owner_history(
301    tx: &mut Transaction,
302    model: &Model,
303    global_id: &str,
304    relating: EntityId,
305    related: EntityId,
306    owner_history: EntityId,
307) -> SpatialAuthoringResult<EntityId> {
308    pair_owned(
309        tx,
310        model,
311        CONNECTS_ELEMENTS,
312        global_id,
313        relating,
314        related,
315        Vec::new(),
316        Some(owner_history),
317    )
318}
319
320/// [`connect_with_realizing_elements`](super::connect_with_realizing_elements)
321/// in the model's declared release, with a caller-supplied
322/// `IfcOwnerHistory`, which IFC2X3 requires. The record has the release's
323/// nine attributes, `ConnectionType` unset, as the plain writer's does
324/// (#213).
325///
326/// # Errors
327///
328/// Those of
329/// [`connect_with_realizing_elements`](super::connect_with_realizing_elements),
330/// and the release and owner-history refusals of
331/// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
332/// Nothing is staged on an error.
333pub fn connect_with_realizing_elements_with_owner_history(
334    tx: &mut Transaction,
335    model: &Model,
336    global_id: &str,
337    relating: EntityId,
338    related: EntityId,
339    realizing: &[EntityId],
340    owner_history: EntityId,
341) -> SpatialAuthoringResult<EntityId> {
342    check_realizing(realizing)?;
343    pair_owned(
344        tx,
345        model,
346        CONNECTS_WITH_REALIZING,
347        global_id,
348        relating,
349        related,
350        vec![("RealizingElements", refs(realizing))],
351        Some(owner_history),
352    )
353}
354
355/// [`interfere_elements`](super::interfere_elements) in the model's
356/// declared release, with a caller-supplied `IfcOwnerHistory`. IFC2X3
357/// declares no `IfcRelInterferesElements` (`EntityNotInSchema`).
358///
359/// The record has the release's own arity: nine attributes in IFC4, ten
360/// in IFC4X3 (`InterferenceSpace`, unset), as the plain writer's does
361/// (#213).
362///
363/// # Errors
364///
365/// Those of [`interfere_elements`](super::interfere_elements), and the
366/// release and owner-history refusals of
367/// [`aggregate_with_owner_history`](super::aggregate_with_owner_history).
368/// Nothing is staged on an error.
369pub fn interfere_elements_with_owner_history(
370    tx: &mut Transaction,
371    model: &Model,
372    global_id: &str,
373    relating: EntityId,
374    related: EntityId,
375    implied_order: Option<bool>,
376    owner_history: EntityId,
377) -> SpatialAuthoringResult<EntityId> {
378    pair_owned(
379        tx,
380        model,
381        INTERFERES_ELEMENTS,
382        global_id,
383        relating,
384        related,
385        vec![(
386            "ImpliedOrder",
387            implied_order.map_or(Value::LogicalUnknown, Value::Bool),
388        )],
389        Some(owner_history),
390    )
391}