1use ifc_model::guid::Guid;
36use ifc_model::{Entity, EntityId, Transaction, Value};
37
38use crate::connectivity::relation::slot as connects_slot;
39use crate::port::definition::slot as port_slot;
40use crate::system::group::slot as group_slot;
41use crate::zone::spatial_group::slot as placement_slot;
42
43mod distribution;
44mod error;
45mod owned;
46mod release;
47mod services;
48mod system_kind;
49
50pub use distribution::{
51 create_distribution_element, create_distribution_element_with_owner_history,
52 create_spatial_zone, create_spatial_zone_with_owner_history, create_zone,
53 create_zone_with_owner_history, DistributionElementKind, ElementAttributes,
54};
55pub use error::{SystemAuthoringError, SystemAuthoringResult};
56pub use owned::{
57 assign_to_group_with_owner_history, connect_port_to_element_with_owner_history,
58 connect_ports_with_owner_history, create_group_with_owner_history,
59 create_port_with_owner_history, create_system_with_owner_history,
60 nest_ports_with_owner_history, reference_in_spatial_structure_with_owner_history,
61};
62#[allow(deprecated)]
64pub use owned::contain_in_spatial_structure_with_owner_history;
65#[allow(deprecated)]
66pub use services::{serve_buildings, serve_buildings_with_owner_history};
67pub use system_kind::{
68 create_classified_system, create_classified_system_with_owner_history, ClassifiedSystemDraft,
69 SystemKind,
70};
71
72use error::invalid;
73
74fn guid(entity: &'static str, global_id: &str) -> SystemAuthoringResult<()> {
75 if Guid::parse(global_id).is_none() {
76 return Err(invalid(entity, "GlobalId", global_id));
77 }
78 Ok(())
79}
80
81fn check_related(
83 entity: &'static str,
84 attribute: &'static str,
85 global_id: &str,
86 relating: EntityId,
87 related: &[EntityId],
88 relating_word: &str,
89) -> SystemAuthoringResult<()> {
90 guid(entity, global_id)?;
91 if related.is_empty() {
92 return Err(invalid(entity, attribute, "empty"));
93 }
94 if related.contains(&relating) {
95 return Err(invalid(
96 entity,
97 attribute,
98 format!("contains the {relating_word}"),
99 ));
100 }
101 Ok(())
102}
103
104fn check_ports(
106 global_id: &str,
107 relating: EntityId,
108 related: EntityId,
109) -> SystemAuthoringResult<()> {
110 guid("IFCRELCONNECTSPORTS", global_id)?;
111 if relating == related {
112 return Err(invalid(
113 "IFCRELCONNECTSPORTS",
114 "RelatedPort",
115 "same as RelatingPort",
116 ));
117 }
118 Ok(())
119}
120
121fn refs(ids: &[EntityId]) -> Value {
122 Value::List(ids.iter().copied().map(Value::Ref).collect())
123}
124
125pub fn create_system(
135 tx: &mut Transaction,
136 global_id: &str,
137 name: Option<&str>,
138) -> SystemAuthoringResult<EntityId> {
139 guid("IFCSYSTEM", global_id)?;
140 let mut attributes = vec![Value::Null; 5];
141 attributes[0] = Value::Text(global_id.into());
142 attributes[2] = name.map_or(Value::Null, |t| Value::Text(t.into()));
143 Ok(tx.create(Entity::new("IFCSYSTEM", attributes)))
144}
145
146pub fn create_port(
160 tx: &mut Transaction,
161 global_id: &str,
162 name: Option<&str>,
163 flow_direction: Option<&str>,
164) -> SystemAuthoringResult<EntityId> {
165 guid("IFCDISTRIBUTIONPORT", global_id)?;
166 let mut attributes = vec![Value::Null; 10];
167 attributes[0] = Value::Text(global_id.into());
168 attributes[2] = name.map_or(Value::Null, |t| Value::Text(t.into()));
169 attributes[7] = flow_direction.map_or(Value::Null, |t| Value::Enum(t.into()));
170 Ok(tx.create(Entity::new("IFCDISTRIBUTIONPORT", attributes)))
171}
172
173pub fn assign_to_group(
183 tx: &mut Transaction,
184 global_id: &str,
185 group: EntityId,
186 members: &[EntityId],
187) -> SystemAuthoringResult<EntityId> {
188 check_related(
189 "IFCRELASSIGNSTOGROUP",
190 "RelatedObjects",
191 global_id,
192 group,
193 members,
194 "group",
195 )?;
196 let width = group_slot::ASSIGNS_GROUP.max(group_slot::ASSIGNS_RELATED) + 1;
197 let mut attributes = vec![Value::Null; width];
198 attributes[0] = Value::Text(global_id.into());
199 attributes[group_slot::ASSIGNS_RELATED] = refs(members);
200 attributes[group_slot::ASSIGNS_GROUP] = Value::Ref(group);
201 Ok(tx.create(Entity::new("IFCRELASSIGNSTOGROUP", attributes)))
202}
203
204pub fn nest_ports(
214 tx: &mut Transaction,
215 global_id: &str,
216 parent: EntityId,
217 children: &[EntityId],
218) -> SystemAuthoringResult<EntityId> {
219 check_related(
220 "IFCRELNESTS",
221 "RelatedObjects",
222 global_id,
223 parent,
224 children,
225 "parent",
226 )?;
227 let width = port_slot::NESTS_PARENT.max(port_slot::NESTS_CHILDREN) + 1;
228 let mut attributes = vec![Value::Null; width];
229 attributes[0] = Value::Text(global_id.into());
230 attributes[port_slot::NESTS_PARENT] = Value::Ref(parent);
231 attributes[port_slot::NESTS_CHILDREN] = refs(children);
232 Ok(tx.create(Entity::new("IFCRELNESTS", attributes)))
233}
234
235pub fn connect_port_to_element(
244 tx: &mut Transaction,
245 global_id: &str,
246 port: EntityId,
247 element: EntityId,
248) -> SystemAuthoringResult<EntityId> {
249 guid("IFCRELCONNECTSPORTTOELEMENT", global_id)?;
250 let width = port_slot::PORT_TO_ELEMENT_PORT.max(port_slot::PORT_TO_ELEMENT_ELEMENT) + 1;
251 let mut attributes = vec![Value::Null; width];
252 attributes[0] = Value::Text(global_id.into());
253 attributes[port_slot::PORT_TO_ELEMENT_PORT] = Value::Ref(port);
254 attributes[port_slot::PORT_TO_ELEMENT_ELEMENT] = Value::Ref(element);
255 Ok(tx.create(Entity::new("IFCRELCONNECTSPORTTOELEMENT", attributes)))
256}
257
258pub fn connect_ports(
271 tx: &mut Transaction,
272 global_id: &str,
273 relating: EntityId,
274 related: EntityId,
275 realizing: Option<EntityId>,
276) -> SystemAuthoringResult<EntityId> {
277 check_ports(global_id, relating, related)?;
278 let width = connects_slot::REALIZING + 1;
279 let mut attributes = vec![Value::Null; width];
280 attributes[0] = Value::Text(global_id.into());
281 attributes[connects_slot::RELATING] = Value::Ref(relating);
282 attributes[connects_slot::RELATED] = Value::Ref(related);
283 attributes[connects_slot::REALIZING] = realizing.map_or(Value::Null, Value::Ref);
284 Ok(tx.create(Entity::new("IFCRELCONNECTSPORTS", attributes)))
285}
286
287#[deprecated(
305 since = "0.5.0",
306 note = "use ifc_spatial::contain, the canonical IfcRelContainedInSpatialStructure writer (#492)"
307)]
308pub fn contain_in_spatial_structure(
309 tx: &mut Transaction,
310 global_id: &str,
311 structure: EntityId,
312 elements: &[EntityId],
313) -> SystemAuthoringResult<EntityId> {
314 place(
315 tx,
316 "IFCRELCONTAINEDINSPATIALSTRUCTURE",
317 global_id,
318 structure,
319 elements,
320 )
321}
322
323pub fn reference_in_spatial_structure(
335 tx: &mut Transaction,
336 global_id: &str,
337 structure: EntityId,
338 elements: &[EntityId],
339) -> SystemAuthoringResult<EntityId> {
340 place(
341 tx,
342 "IFCRELREFERENCEDINSPATIALSTRUCTURE",
343 global_id,
344 structure,
345 elements,
346 )
347}
348
349fn place(
352 tx: &mut Transaction,
353 entity: &'static str,
354 global_id: &str,
355 structure: EntityId,
356 elements: &[EntityId],
357) -> SystemAuthoringResult<EntityId> {
358 check_related(
359 entity,
360 "RelatedElements",
361 global_id,
362 structure,
363 elements,
364 "structure",
365 )?;
366 let width = placement_slot::RELATING_STRUCTURE.max(placement_slot::RELATED_ELEMENTS) + 1;
367 let mut attributes = vec![Value::Null; width];
368 attributes[0] = Value::Text(global_id.into());
369 attributes[placement_slot::RELATED_ELEMENTS] = refs(elements);
370 attributes[placement_slot::RELATING_STRUCTURE] = Value::Ref(structure);
371 Ok(tx.create(Entity::new(entity, attributes)))
372}
373
374pub fn create_group(
388 tx: &mut Transaction,
389 global_id: &str,
390 name: Option<&str>,
391 description: Option<&str>,
392) -> SystemAuthoringResult<EntityId> {
393 guid("IFCGROUP", global_id)?;
394 let mut attributes = vec![Value::Null; 5];
395 attributes[0] = Value::Text(global_id.into());
396 attributes[2] = name.map_or(Value::Null, |t| Value::Text(t.into()));
397 attributes[3] = description.map_or(Value::Null, |t| Value::Text(t.into()));
398 Ok(tx.create(Entity::new("IFCGROUP", attributes)))
399}