Skip to main content

ifc_spatial/authoring/
owned.rs

1//! Containers, contexts and the two tree relationships in the model's
2//! declared release, with a caller-supplied `IfcOwnerHistory` (#202).
3//!
4//! From the EXPRESS sources: IFC2X3 TC1 requires
5//! `IfcSpatialStructureElement.CompositionType`, `IfcSpace.
6//! InteriorOrExteriorSpace` ([`SpatialDraft::interior_or_exterior`], #214),
7//! and `IfcProject.RepresentationContexts` and `UnitsInContext`; it declares
8//! no `IfcExternalSpatialElement` or `IfcProjectLibrary`.
9
10use ifc_model::guid::Guid;
11use ifc_model::{EntityId, Model, Transaction, Value};
12
13use super::external::{check_external, EXTERNAL};
14use super::owned_relationships::{refs, relate_owned};
15use super::release::{bind, stage};
16use super::{
17    container, invalid, optional_text, ExternalSpatialDraft, ProjectLibraryDraft,
18    SpatialAuthoringResult, SpatialDraft, INTERIOR_OR_EXTERIOR,
19};
20use crate::relation::slots::{AGGREGATES, CONTAINED_IN};
21use crate::tree::SpatialKind;
22
23/// [`create_spatial_element`](super::create_spatial_element) in the model's
24/// declared release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
25/// requires.
26///
27/// IFC2X3 also requires `CompositionType`, and an `IfcSpace`'s
28/// `InteriorOrExteriorSpace`, which
29/// [`SpatialDraft::interior_or_exterior`] carries (#214). That value is
30/// refused with `AuthoringNotInSchema` on any other container and in IFC4
31/// and IFC4X3, which do not declare it, and with `AuthoringValueType` for a
32/// token outside IFC2X3's `IfcInternalOrExternalEnum`.
33///
34/// # Errors
35///
36/// Those of [`create_spatial_element`](super::create_spatial_element), a
37/// value the release cannot hold, and the release and owner-history
38/// refusals of [`aggregate_with_owner_history`]. Nothing is staged on an
39/// error.
40pub fn create_spatial_element_with_owner_history(
41    tx: &mut Transaction,
42    model: &Model,
43    kind: SpatialKind,
44    global_id: &str,
45    draft: SpatialDraft<'_>,
46    owner_history: EntityId,
47) -> SpatialAuthoringResult<EntityId> {
48    let (type_name, _) = container(kind, global_id)?;
49    let values = vec![
50        ("GlobalId", Value::Text(global_id.into())),
51        ("Name", optional_text(draft.name)),
52        ("Description", optional_text(draft.description)),
53        (
54            "ObjectPlacement",
55            draft.placement.map_or(Value::Null, Value::Ref),
56        ),
57        ("LongName", optional_text(draft.long_name)),
58        (
59            "CompositionType",
60            draft
61                .composition
62                .map_or(Value::Null, |t| Value::Enum(t.into())),
63        ),
64        (
65            INTERIOR_OR_EXTERIOR,
66            draft
67                .interior_or_exterior
68                .map_or(Value::Null, |t| Value::Enum(t.into())),
69        ),
70    ];
71    stage(tx, model, type_name, values, Some(owner_history))
72}
73
74/// [`create_project`](super::create_project) in the model's declared
75/// release, with a caller-supplied `IfcOwnerHistory`, which IFC2X3
76/// requires.
77///
78/// `representation_contexts` fills `RepresentationContexts`, a
79/// `SET [1:?]`: empty leaves it `$`, which IFC4 and IFC4X3 allow and IFC2X3,
80/// which requires it (as it requires `UnitsInContext`), refuses with
81/// `AuthoringRequired` (#214). Each context must be an
82/// `IfcRepresentationContext` in the model or staged on `tx`, and not an
83/// `IfcGeometricRepresentationSubContext`, which every release's
84/// `IfcProject` rule (IFC2X3 `WR32`, IFC4 and IFC4X3 `CorrectContext`)
85/// forbids.
86///
87/// # Errors
88///
89/// Those of [`create_project`](super::create_project), the release and
90/// owner-history refusals of [`aggregate_with_owner_history`], and on
91/// `RepresentationContexts`: a duplicated context (`Invalid`), one that
92/// resolves nowhere (`MissingReference`), one that is not an
93/// `IfcRepresentationContext` (`WrongReferenceType`) and a sub-context
94/// (`Invalid`). Nothing is staged on an error.
95pub fn create_project_with_owner_history(
96    tx: &mut Transaction,
97    model: &Model,
98    global_id: &str,
99    name: Option<&str>,
100    units: Option<EntityId>,
101    representation_contexts: &[EntityId],
102    owner_history: EntityId,
103) -> SpatialAuthoringResult<EntityId> {
104    const ENTITY: &str = "IFCPROJECT";
105    if Guid::parse(global_id).is_none() {
106        return Err(invalid(ENTITY, "GlobalId", global_id));
107    }
108    bind(model)?.require_contexts(tx, model, ENTITY, representation_contexts)?;
109    let contexts = if representation_contexts.is_empty() {
110        Value::Null
111    } else {
112        refs(representation_contexts)
113    };
114    let values = vec![
115        ("GlobalId", Value::Text(global_id.into())),
116        ("Name", optional_text(name)),
117        ("RepresentationContexts", contexts),
118        ("UnitsInContext", units.map_or(Value::Null, Value::Ref)),
119    ];
120    stage(tx, model, ENTITY, values, Some(owner_history))
121}
122
123/// [`aggregate`](super::aggregate) in the model's declared release, with a
124/// caller-supplied `IfcOwnerHistory`, which IFC2X3 requires.
125///
126/// The release is bound from `FILE_SCHEMA` (none binds IFC4) and the record
127/// laid out by attribute name from its table. In IFC4 and IFC4X3 it is the
128/// record of the plain writer with the reference in the optional slot. The
129/// owner history is never invented: build it with `ifc-author`.
130///
131/// # Errors
132///
133/// Those of [`aggregate`](super::aggregate), and:
134/// [`MultipleSchemas`](super::SpatialAuthoringError::MultipleSchemas) or
135/// [`UnsupportedSchema`](super::SpatialAuthoringError::UnsupportedSchema)
136/// if the model binds no single known release;
137/// [`EntityNotInSchema`](super::SpatialAuthoringError::EntityNotInSchema),
138/// [`AuthoringNotInSchema`](super::SpatialAuthoringError::AuthoringNotInSchema),
139/// [`AuthoringValueType`](super::SpatialAuthoringError::AuthoringValueType)
140/// or [`AuthoringRequired`](super::SpatialAuthoringError::AuthoringRequired)
141/// for an entity or value the release cannot hold;
142/// [`MissingReference`](super::SpatialAuthoringError::MissingReference) if
143/// `owner_history` is neither in the model nor staged;
144/// [`WrongReferenceType`](super::SpatialAuthoringError::WrongReferenceType)
145/// if it is not an `IfcOwnerHistory`. Nothing is staged on an error.
146pub fn aggregate_with_owner_history(
147    tx: &mut Transaction,
148    model: &Model,
149    global_id: &str,
150    parent: EntityId,
151    children: &[EntityId],
152    owner_history: EntityId,
153) -> SpatialAuthoringResult<EntityId> {
154    let extra = Vec::new();
155    relate_owned(
156        tx,
157        model,
158        AGGREGATES,
159        global_id,
160        parent,
161        children,
162        extra,
163        Some(owner_history),
164    )
165}
166
167/// [`contain`](super::contain) in the model's declared release, with a
168/// caller-supplied `IfcOwnerHistory`, which IFC2X3 requires.
169///
170/// # Errors
171///
172/// Those of [`contain`](super::contain), and the release and owner-history
173/// refusals of [`aggregate_with_owner_history`]. Nothing is staged on an
174/// error.
175pub fn contain_with_owner_history(
176    tx: &mut Transaction,
177    model: &Model,
178    global_id: &str,
179    structure: EntityId,
180    elements: &[EntityId],
181    owner_history: EntityId,
182) -> SpatialAuthoringResult<EntityId> {
183    let extra = Vec::new();
184    relate_owned(
185        tx,
186        model,
187        CONTAINED_IN,
188        global_id,
189        structure,
190        elements,
191        extra,
192        Some(owner_history),
193    )
194}
195
196/// [`create_external_spatial_element`](super::create_external_spatial_element)
197/// in the model's declared release, with a caller-supplied
198/// `IfcOwnerHistory`. IFC2X3 declares no `IfcExternalSpatialElement`
199/// (`EntityNotInSchema`).
200///
201/// # Errors
202///
203/// Those of
204/// [`create_external_spatial_element`](super::create_external_spatial_element),
205/// and the release and owner-history refusals of
206/// [`aggregate_with_owner_history`]. Nothing is staged on an error.
207pub fn create_external_spatial_element_with_owner_history(
208    tx: &mut Transaction,
209    model: &Model,
210    global_id: &str,
211    draft: ExternalSpatialDraft<'_>,
212    owner_history: EntityId,
213) -> SpatialAuthoringResult<EntityId> {
214    check_external(global_id, &draft)?;
215    let values = vec![
216        ("GlobalId", Value::Text(global_id.into())),
217        ("Name", optional_text(draft.name)),
218        ("Description", optional_text(draft.description)),
219        ("ObjectType", optional_text(draft.object_type)),
220        (
221            "ObjectPlacement",
222            draft.placement.map_or(Value::Null, Value::Ref),
223        ),
224        (
225            "Representation",
226            draft.representation.map_or(Value::Null, Value::Ref),
227        ),
228        ("LongName", optional_text(draft.long_name)),
229        (
230            "PredefinedType",
231            draft
232                .predefined_type
233                .map_or(Value::Null, |t| Value::Enum(t.into())),
234        ),
235    ];
236    stage(tx, model, EXTERNAL, values, Some(owner_history))
237}
238
239/// [`create_project_library`](super::create_project_library) in the model's
240/// declared release, with a caller-supplied `IfcOwnerHistory`. IFC2X3
241/// declares no `IfcProjectLibrary` (`EntityNotInSchema`).
242///
243/// # Errors
244///
245/// Those of [`create_project_library`](super::create_project_library), and
246/// the release and owner-history refusals of
247/// [`aggregate_with_owner_history`]. Nothing is staged on an error.
248pub fn create_project_library_with_owner_history(
249    tx: &mut Transaction,
250    model: &Model,
251    global_id: &str,
252    draft: ProjectLibraryDraft<'_>,
253    representation_contexts: &[EntityId],
254    owner_history: EntityId,
255) -> SpatialAuthoringResult<EntityId> {
256    const ENTITY: &str = "IFCPROJECTLIBRARY";
257    if Guid::parse(global_id).is_none() {
258        return Err(invalid(ENTITY, "GlobalId", global_id));
259    }
260    let contexts = if representation_contexts.is_empty() {
261        Value::Null
262    } else {
263        refs(representation_contexts)
264    };
265    let values = vec![
266        ("GlobalId", Value::Text(global_id.into())),
267        ("Name", optional_text(draft.name)),
268        ("Description", optional_text(draft.description)),
269        ("ObjectType", optional_text(draft.object_type)),
270        ("LongName", optional_text(draft.long_name)),
271        ("Phase", optional_text(draft.phase)),
272        ("RepresentationContexts", contexts),
273        (
274            "UnitsInContext",
275            draft.units.map_or(Value::Null, Value::Ref),
276        ),
277    ];
278    stage(tx, model, ENTITY, values, Some(owner_history))
279}