ifc_constraint/
association.rs1use std::collections::HashSet;
8
9use ifc_model::guid::Guid;
10use ifc_model::{EntityId, Model, Transaction, Value};
11
12use crate::authoring::{final_type, optional_ref, optional_text, refs, text, validate_target_in};
13use crate::release::{bind, require_owner_history};
14use crate::{ConstraintError, ConstraintResult};
15
16const ASSIGNMENT: &str = "IFCRELASSOCIATESCONSTRAINT";
17
18#[derive(Debug, Clone, Copy)]
20#[non_exhaustive]
21pub struct ConstraintAssociationDraft<'a> {
22 pub global_id: &'a str,
24 pub name: Option<&'a str>,
26 pub description: Option<&'a str>,
28 pub related_objects: &'a [EntityId],
30 pub intent: Option<&'a str>,
32 pub relating_constraint: EntityId,
34}
35
36impl<'a> ConstraintAssociationDraft<'a> {
37 #[must_use]
39 pub fn new(
40 global_id: &'a str,
41 related_objects: &'a [EntityId],
42 relating_constraint: EntityId,
43 ) -> Self {
44 Self {
45 global_id,
46 name: None,
47 description: None,
48 related_objects,
49 intent: None,
50 relating_constraint,
51 }
52 }
53
54 #[must_use]
56 pub fn name(mut self, value: &'a str) -> Self {
57 self.name = Some(value);
58 self
59 }
60
61 #[must_use]
63 pub fn description(mut self, value: &'a str) -> Self {
64 self.description = Some(value);
65 self
66 }
67
68 #[must_use]
70 pub fn intent(mut self, value: &'a str) -> Self {
71 self.intent = Some(value);
72 self
73 }
74}
75
76pub fn associate_constraint(
98 tx: &mut Transaction,
99 model: &Model,
100 draft: ConstraintAssociationDraft<'_>,
101) -> ConstraintResult<EntityId> {
102 associate(tx, model, draft, None)
103}
104
105pub fn associate_constraint_with_owner_history(
121 tx: &mut Transaction,
122 model: &Model,
123 draft: ConstraintAssociationDraft<'_>,
124 owner_history: EntityId,
125) -> ConstraintResult<EntityId> {
126 associate(tx, model, draft, Some(owner_history))
127}
128
129fn associate(
131 tx: &mut Transaction,
132 model: &Model,
133 draft: ConstraintAssociationDraft<'_>,
134 owner_history: Option<EntityId>,
135) -> ConstraintResult<EntityId> {
136 let layout = bind(model)?;
137 if Guid::parse(draft.global_id).is_none() {
138 return Err(ConstraintError::AuthoringInvalid {
139 entity: ASSIGNMENT,
140 attribute: "GlobalId",
141 value: draft.global_id.into(),
142 });
143 }
144 validate_target_in(
145 layout.schema(),
146 tx,
147 model,
148 draft.relating_constraint,
149 "IfcConstraint",
150 )?;
151 if draft.related_objects.is_empty() {
152 return Err(ConstraintError::AuthoringInvalid {
153 entity: ASSIGNMENT,
154 attribute: "RelatedObjects",
155 value: "empty SET [1:?]".into(),
156 });
157 }
158 let mut seen = HashSet::new();
159 for &target in draft.related_objects {
160 if !seen.insert(target) {
161 return Err(ConstraintError::AuthoringInvalid {
162 entity: ASSIGNMENT,
163 attribute: "RelatedObjects",
164 value: format!("duplicate {target}"),
165 });
166 }
167 let actual =
168 final_type(tx, model, target).ok_or(ConstraintError::UnknownEntity { id: target })?;
169 if !layout.is_definition(actual) {
170 return Err(ConstraintError::AuthoringReferenceType {
171 target,
172 expected: "IfcDefinitionSelect",
173 actual: actual.into(),
174 });
175 }
176 }
177 if let Some(owner_history) = owner_history {
178 require_owner_history(tx, model, owner_history)?;
179 }
180 let record = layout.named_record(
181 ASSIGNMENT,
182 vec![
183 ("GlobalId", text(draft.global_id)),
184 ("OwnerHistory", optional_ref(owner_history)),
185 ("Name", optional_text(draft.name)),
186 ("Description", optional_text(draft.description)),
187 ("RelatedObjects", refs(draft.related_objects)),
188 ("Intent", optional_text(draft.intent)),
189 ("RelatingConstraint", Value::Ref(draft.relating_constraint)),
190 ],
191 )?;
192 Ok(tx.create(record))
193}