1use ifc_model::{Entity, EntityId, Model, Transaction, Value};
18
19use ifc_model::guid::Guid;
20
21use crate::{PropertyError, PropertyResult};
22
23use super::value_form::{require_ifc_value, require_ifc_values};
24
25pub mod pset_slot {
27 pub const GLOBAL_ID: usize = 0;
29 pub const NAME: usize = 2;
31 pub const DESCRIPTION: usize = 3;
33 pub const HAS_PROPERTIES: usize = 4;
35}
36
37pub mod single_value_slot {
39 pub const NAME: usize = 0;
41 pub const SPECIFICATION: usize = 1;
43 pub const NOMINAL_VALUE: usize = 2;
45 pub const UNIT: usize = 3;
47}
48
49pub mod defines_slot {
51 pub const GLOBAL_ID: usize = 0;
53 pub const RELATED_OBJECTS: usize = 4;
55 pub const RELATING_DEFINITION: usize = 5;
57}
58
59pub fn add_property_single_value(
77 tx: &mut Transaction,
78 name: &str,
79 specification: Option<&str>,
80 value: Option<Value>,
81 unit: Option<EntityId>,
82) -> PropertyResult<EntityId> {
83 if name.trim().is_empty() {
84 return Err(PropertyError::AuthoringInvalid {
85 entity: "IFCPROPERTYSINGLEVALUE",
86 attribute: "Name",
87 value: name.to_owned(),
88 });
89 }
90 if let Some(value) = value.as_ref() {
91 require_ifc_value("IFCPROPERTYSINGLEVALUE", "NominalValue", value)?;
92 }
93 let mut attributes = vec![Value::Null; single_value_slot::UNIT + 1];
94 attributes[single_value_slot::NAME] = Value::Text(name.into());
95 attributes[single_value_slot::SPECIFICATION] = optional_text(specification);
96 attributes[single_value_slot::NOMINAL_VALUE] = value.unwrap_or(Value::Null);
97 attributes[single_value_slot::UNIT] = unit.map_or(Value::Null, Value::Ref);
98 Ok(tx.create(Entity::new("IFCPROPERTYSINGLEVALUE", attributes)))
99}
100
101pub fn add_property_set(
122 tx: &mut Transaction,
123 global_id: &str,
124 name: &str,
125 description: Option<&str>,
126 properties: &[(&str, EntityId)],
127) -> PropertyResult<EntityId> {
128 check_property_set(global_id, name, properties)?;
129 let refs = properties.iter().map(|(_, id)| Value::Ref(*id)).collect();
130 let mut attributes = vec![Value::Null; pset_slot::HAS_PROPERTIES + 1];
131 attributes[pset_slot::GLOBAL_ID] = Value::Text(global_id.into());
132 attributes[pset_slot::NAME] = Value::Text(name.into());
133 attributes[pset_slot::DESCRIPTION] = optional_text(description);
134 attributes[pset_slot::HAS_PROPERTIES] = Value::List(refs);
135 Ok(tx.create(Entity::new("IFCPROPERTYSET", attributes)))
136}
137
138pub(super) fn check_property_set(
141 global_id: &str,
142 name: &str,
143 properties: &[(&str, EntityId)],
144) -> PropertyResult<()> {
145 if Guid::parse(global_id).is_none() {
146 return Err(PropertyError::AuthoringInvalid {
147 entity: "IFCPROPERTYSET",
148 attribute: "GlobalId",
149 value: global_id.to_owned(),
150 });
151 }
152 if name.trim().is_empty() {
153 return Err(PropertyError::AuthoringInvalid {
154 entity: "IFCPROPERTYSET",
155 attribute: "Name",
156 value: name.to_owned(),
157 });
158 }
159 if properties.is_empty() {
160 return Err(PropertyError::AuthoringInvalid {
161 entity: "IFCPROPERTYSET",
162 attribute: "HasProperties",
163 value: String::from("an empty set"),
164 });
165 }
166 let mut seen = std::collections::BTreeSet::new();
167 for (property_name, _) in properties {
168 if !seen.insert(*property_name) {
169 return Err(PropertyError::AuthoringInvalid {
170 entity: "IFCPROPERTYSET",
171 attribute: "HasProperties",
172 value: (*property_name).to_owned(),
173 });
174 }
175 }
176 Ok(())
177}
178
179pub(super) fn optional_text(value: Option<&str>) -> Value {
180 value.map_or(Value::Null, |text| Value::Text(text.into()))
181}
182
183pub fn attach_property_set(
209 tx: &mut Transaction,
210 model: &Model,
211 global_id: &str,
212 objects: &[EntityId],
213 property_set: EntityId,
214) -> PropertyResult<EntityId> {
215 super::root_authoring::attach_definition(tx, model, global_id, objects, property_set, None)
216}
217
218pub mod enumerated_slot {
220 pub const NAME: usize = 0;
222 pub const SPECIFICATION: usize = 1;
224 pub const VALUES: usize = 2;
226 pub const REFERENCE: usize = 3;
228}
229
230pub mod bounded_slot {
232 pub const NAME: usize = 0;
234 pub const SPECIFICATION: usize = 1;
236 pub const UPPER: usize = 2;
238 pub const LOWER: usize = 3;
240 pub const UNIT: usize = 4;
242 pub const SET_POINT: usize = 5;
244}
245
246pub mod list_slot {
248 pub const NAME: usize = 0;
250 pub const SPECIFICATION: usize = 1;
252 pub const VALUES: usize = 2;
254 pub const UNIT: usize = 3;
256}
257
258pub mod table_slot {
260 pub const NAME: usize = 0;
262 pub const SPECIFICATION: usize = 1;
264 pub const DEFINING: usize = 2;
266 pub const DEFINED: usize = 3;
268 pub const EXPRESSION: usize = 4;
270 pub const DEFINING_UNIT: usize = 5;
272 pub const DEFINED_UNIT: usize = 6;
274 pub const INTERPOLATION: usize = 7;
276}
277
278pub mod reference_slot {
280 pub const NAME: usize = 0;
282 pub const SPECIFICATION: usize = 1;
284 pub const USAGE_NAME: usize = 2;
286 pub const REFERENCE: usize = 3;
288}
289
290pub mod complex_slot {
292 pub const NAME: usize = 0;
294 pub const SPECIFICATION: usize = 1;
296 pub const USAGE_NAME: usize = 2;
298 pub const HAS_PROPERTIES: usize = 3;
300}
301
302pub(super) fn require_name(entity: &'static str, name: &str) -> PropertyResult<()> {
308 if name.trim().is_empty() {
309 return Err(PropertyError::AuthoringInvalid {
310 entity,
311 attribute: "Name",
312 value: name.to_owned(),
313 });
314 }
315 Ok(())
316}
317
318pub fn add_property_enumerated_value(
327 tx: &mut Transaction,
328 name: &str,
329 specification: Option<&str>,
330 values: Option<Vec<Value>>,
331 enumeration: Option<EntityId>,
332) -> PropertyResult<EntityId> {
333 require_name("IFCPROPERTYENUMERATEDVALUE", name)?;
334 if let Some(values) = values.as_ref() {
335 if values.is_empty() {
336 return Err(PropertyError::AuthoringInvalid {
337 entity: "IFCPROPERTYENUMERATEDVALUE",
338 attribute: "EnumerationValues",
339 value: "empty".to_owned(),
340 });
341 }
342 require_ifc_values("IFCPROPERTYENUMERATEDVALUE", "EnumerationValues", values)?;
343 }
344 let mut attributes = vec![Value::Null; enumerated_slot::REFERENCE + 1];
345 attributes[enumerated_slot::NAME] = Value::Text(name.into());
346 attributes[enumerated_slot::SPECIFICATION] = optional_text(specification);
347 attributes[enumerated_slot::VALUES] = values.map_or(Value::Null, Value::List);
348 attributes[enumerated_slot::REFERENCE] = enumeration.map_or(Value::Null, Value::Ref);
349 Ok(tx.create(Entity::new("IFCPROPERTYENUMERATEDVALUE", attributes)))
350}
351
352fn measure_of(value: &Value) -> Option<&str> {
358 match value {
359 Value::Typed { type_name, .. } => Some(type_name.as_ref()),
360 _ => None,
361 }
362}
363
364pub fn add_property_bounded_value(
375 tx: &mut Transaction,
376 name: &str,
377 specification: Option<&str>,
378 upper: Option<Value>,
379 lower: Option<Value>,
380 set_point: Option<Value>,
381 unit: Option<EntityId>,
382) -> PropertyResult<EntityId> {
383 require_name("IFCPROPERTYBOUNDEDVALUE", name)?;
384 for (attribute, value) in [
385 ("UpperBoundValue", upper.as_ref()),
386 ("LowerBoundValue", lower.as_ref()),
387 ("SetPointValue", set_point.as_ref()),
388 ] {
389 if let Some(value) = value {
390 require_ifc_value("IFCPROPERTYBOUNDEDVALUE", attribute, value)?;
391 }
392 }
393 let stated: Vec<(&str, &Value)> = [
394 ("UpperBoundValue", upper.as_ref()),
395 ("LowerBoundValue", lower.as_ref()),
396 ("SetPointValue", set_point.as_ref()),
397 ]
398 .into_iter()
399 .filter_map(|(label, value)| value.map(|value| (label, value)))
400 .collect();
401 if let Some(((_, first), rest)) = stated.split_first() {
402 let want = measure_of(first);
403 for (label, value) in rest {
404 if measure_of(value) != want {
405 return Err(PropertyError::AuthoringInvalid {
406 entity: "IFCPROPERTYBOUNDEDVALUE",
407 attribute: "SameUnit",
408 value: (*label).to_owned(),
409 });
410 }
411 }
412 }
413 let mut attributes = vec![Value::Null; bounded_slot::SET_POINT + 1];
414 attributes[bounded_slot::NAME] = Value::Text(name.into());
415 attributes[bounded_slot::SPECIFICATION] = optional_text(specification);
416 attributes[bounded_slot::UPPER] = upper.unwrap_or(Value::Null);
417 attributes[bounded_slot::LOWER] = lower.unwrap_or(Value::Null);
418 attributes[bounded_slot::UNIT] = unit.map_or(Value::Null, Value::Ref);
419 attributes[bounded_slot::SET_POINT] = set_point.unwrap_or(Value::Null);
420 Ok(tx.create(Entity::new("IFCPROPERTYBOUNDEDVALUE", attributes)))
421}
422
423pub fn add_property_list_value(
431 tx: &mut Transaction,
432 name: &str,
433 specification: Option<&str>,
434 values: Option<Vec<Value>>,
435 unit: Option<EntityId>,
436) -> PropertyResult<EntityId> {
437 require_name("IFCPROPERTYLISTVALUE", name)?;
438 if let Some(values) = values.as_ref() {
439 if values.is_empty() {
440 return Err(PropertyError::AuthoringInvalid {
441 entity: "IFCPROPERTYLISTVALUE",
442 attribute: "ListValues",
443 value: "empty".to_owned(),
444 });
445 }
446 require_ifc_values("IFCPROPERTYLISTVALUE", "ListValues", values)?;
447 }
448 let mut attributes = vec![Value::Null; list_slot::UNIT + 1];
449 attributes[list_slot::NAME] = Value::Text(name.into());
450 attributes[list_slot::SPECIFICATION] = optional_text(specification);
451 attributes[list_slot::VALUES] = values.map_or(Value::Null, Value::List);
452 attributes[list_slot::UNIT] = unit.map_or(Value::Null, Value::Ref);
453 Ok(tx.create(Entity::new("IFCPROPERTYLISTVALUE", attributes)))
454}
455
456#[derive(Debug, Default)]
462#[non_exhaustive]
463pub struct TableValueDraft<'a> {
464 pub name: &'a str,
466 pub specification: Option<&'a str>,
468 pub defining: Option<Vec<Value>>,
470 pub defined: Option<Vec<Value>>,
472 pub defining_unit: Option<EntityId>,
474 pub defined_unit: Option<EntityId>,
476 pub expression: Option<&'a str>,
478 pub interpolation: Option<&'a str>,
480}
481
482pub fn add_property_table_value(
497 tx: &mut Transaction,
498 draft: TableValueDraft<'_>,
499) -> PropertyResult<EntityId> {
500 let TableValueDraft {
501 name,
502 specification,
503 defining,
504 defined,
505 defining_unit,
506 defined_unit,
507 expression,
508 interpolation,
509 } = draft;
510 require_name("IFCPROPERTYTABLEVALUE", name)?;
511 let sizes = (
512 defining.as_ref().map(Vec::len),
513 defined.as_ref().map(Vec::len),
514 );
515 if let (Some(left), Some(right)) = sizes {
516 if left != right {
517 return Err(PropertyError::AuthoringInvalid {
518 entity: "IFCPROPERTYTABLEVALUE",
519 attribute: "DefiningValues",
520 value: format!("{left} defining against {right} defined"),
521 });
522 }
523 }
524 for (label, column) in [
525 ("DefiningValues", defining.as_ref()),
526 ("DefinedValues", defined.as_ref()),
527 ] {
528 let Some(column) = column else { continue };
529 if column.is_empty() {
530 return Err(PropertyError::AuthoringInvalid {
531 entity: "IFCPROPERTYTABLEVALUE",
532 attribute: label,
533 value: "empty".to_owned(),
534 });
535 }
536 require_ifc_values("IFCPROPERTYTABLEVALUE", label, column)?;
537 let want = measure_of(&column[0]);
538 if column.iter().any(|value| measure_of(value) != want) {
539 return Err(PropertyError::AuthoringInvalid {
540 entity: "IFCPROPERTYTABLEVALUE",
541 attribute: label,
542 value: "mixed measures".to_owned(),
543 });
544 }
545 }
546 let mut attributes = vec![Value::Null; table_slot::INTERPOLATION + 1];
547 attributes[table_slot::NAME] = Value::Text(name.into());
548 attributes[table_slot::SPECIFICATION] = optional_text(specification);
549 attributes[table_slot::DEFINING] = defining.map_or(Value::Null, Value::List);
550 attributes[table_slot::DEFINED] = defined.map_or(Value::Null, Value::List);
551 attributes[table_slot::EXPRESSION] = optional_text(expression);
552 attributes[table_slot::DEFINING_UNIT] = defining_unit.map_or(Value::Null, Value::Ref);
553 attributes[table_slot::DEFINED_UNIT] = defined_unit.map_or(Value::Null, Value::Ref);
554 attributes[table_slot::INTERPOLATION] =
555 interpolation.map_or(Value::Null, |token| Value::Enum(token.into()));
556 Ok(tx.create(Entity::new("IFCPROPERTYTABLEVALUE", attributes)))
557}
558
559pub fn add_property_reference_value(
568 tx: &mut Transaction,
569 name: &str,
570 specification: Option<&str>,
571 usage_name: Option<&str>,
572 reference: Option<EntityId>,
573) -> PropertyResult<EntityId> {
574 require_name("IFCPROPERTYREFERENCEVALUE", name)?;
575 let mut attributes = vec![Value::Null; reference_slot::REFERENCE + 1];
576 attributes[reference_slot::NAME] = Value::Text(name.into());
577 attributes[reference_slot::SPECIFICATION] = optional_text(specification);
578 attributes[reference_slot::USAGE_NAME] = optional_text(usage_name);
579 attributes[reference_slot::REFERENCE] = reference.map_or(Value::Null, Value::Ref);
580 Ok(tx.create(Entity::new("IFCPROPERTYREFERENCEVALUE", attributes)))
581}
582
583pub fn add_complex_property(
597 tx: &mut Transaction,
598 name: &str,
599 specification: Option<&str>,
600 usage_name: &str,
601 properties: &[(&str, EntityId)],
602) -> PropertyResult<EntityId> {
603 require_name("IFCCOMPLEXPROPERTY", name)?;
604 if usage_name.trim().is_empty() {
605 return Err(PropertyError::AuthoringInvalid {
606 entity: "IFCCOMPLEXPROPERTY",
607 attribute: "UsageName",
608 value: usage_name.to_owned(),
609 });
610 }
611 if properties.is_empty() {
612 return Err(PropertyError::AuthoringInvalid {
613 entity: "IFCCOMPLEXPROPERTY",
614 attribute: "HasProperties",
615 value: "empty".to_owned(),
616 });
617 }
618 let mut seen = std::collections::BTreeSet::new();
619 for (property_name, _) in properties {
620 if !seen.insert(*property_name) {
621 return Err(PropertyError::AuthoringInvalid {
622 entity: "IFCCOMPLEXPROPERTY",
623 attribute: "HasProperties",
624 value: (*property_name).to_owned(),
625 });
626 }
627 }
628 let refs = properties.iter().map(|(_, id)| Value::Ref(*id)).collect();
629 let mut attributes = vec![Value::Null; complex_slot::HAS_PROPERTIES + 1];
630 attributes[complex_slot::NAME] = Value::Text(name.into());
631 attributes[complex_slot::SPECIFICATION] = optional_text(specification);
632 attributes[complex_slot::USAGE_NAME] = Value::Text(usage_name.into());
633 attributes[complex_slot::HAS_PROPERTIES] = Value::List(refs);
634 let own = tx.create(Entity::new("IFCCOMPLEXPROPERTY", attributes));
638 if properties.iter().any(|(_, id)| *id == own) {
639 return Err(PropertyError::AuthoringInvalid {
640 entity: "IFCCOMPLEXPROPERTY",
641 attribute: "HasProperties",
642 value: "self reference".to_owned(),
643 });
644 }
645 Ok(own)
646}
647
648pub mod element_quantity_slot {
650 pub const GLOBAL_ID: usize = 0;
652 pub const NAME: usize = 2;
654 pub const DESCRIPTION: usize = 3;
656 pub const METHOD: usize = 4;
658 pub const QUANTITIES: usize = 5;
660}
661
662pub mod complex_quantity_slot {
664 pub const NAME: usize = 0;
666 pub const DESCRIPTION: usize = 1;
668 pub const HAS_QUANTITIES: usize = 2;
670 pub const DISCRIMINATION: usize = 3;
672 pub const QUALITY: usize = 4;
674 pub const USAGE: usize = 5;
676}
677
678pub fn add_element_quantity(
697 tx: &mut Transaction,
698 global_id: &str,
699 name: &str,
700 method_of_measurement: Option<&str>,
701 quantities: &[EntityId],
702) -> PropertyResult<EntityId> {
703 check_element_quantity(global_id, name, quantities)?;
704 let mut attributes = vec![Value::Null; element_quantity_slot::QUANTITIES + 1];
705 attributes[element_quantity_slot::GLOBAL_ID] = Value::Text(global_id.into());
706 attributes[element_quantity_slot::NAME] = Value::Text(name.into());
707 attributes[element_quantity_slot::METHOD] = optional_text(method_of_measurement);
708 attributes[element_quantity_slot::QUANTITIES] =
709 Value::List(quantities.iter().copied().map(Value::Ref).collect());
710 Ok(tx.create(Entity::new("IFCELEMENTQUANTITY", attributes)))
711}
712
713pub(super) fn check_element_quantity(
716 global_id: &str,
717 name: &str,
718 quantities: &[EntityId],
719) -> PropertyResult<()> {
720 if Guid::parse(global_id).is_none() {
721 return Err(PropertyError::AuthoringInvalid {
722 entity: "IFCELEMENTQUANTITY",
723 attribute: "GlobalId",
724 value: global_id.to_owned(),
725 });
726 }
727 require_name("IFCELEMENTQUANTITY", name)?;
728 if quantities.is_empty() {
729 return Err(PropertyError::AuthoringInvalid {
730 entity: "IFCELEMENTQUANTITY",
731 attribute: "Quantities",
732 value: "empty".to_owned(),
733 });
734 }
735 Ok(())
736}
737
738pub fn add_physical_complex_quantity(
748 tx: &mut Transaction,
749 name: &str,
750 description: Option<&str>,
751 quantities: &[EntityId],
752 discrimination: &str,
753) -> PropertyResult<EntityId> {
754 require_name("IFCPHYSICALCOMPLEXQUANTITY", name)?;
755 if discrimination.trim().is_empty() {
756 return Err(PropertyError::AuthoringInvalid {
757 entity: "IFCPHYSICALCOMPLEXQUANTITY",
758 attribute: "Discrimination",
759 value: discrimination.to_owned(),
760 });
761 }
762 if quantities.is_empty() {
763 return Err(PropertyError::AuthoringInvalid {
764 entity: "IFCPHYSICALCOMPLEXQUANTITY",
765 attribute: "HasQuantities",
766 value: "empty".to_owned(),
767 });
768 }
769 let mut attributes = vec![Value::Null; complex_quantity_slot::USAGE + 1];
770 attributes[complex_quantity_slot::NAME] = Value::Text(name.into());
771 attributes[complex_quantity_slot::DESCRIPTION] = optional_text(description);
772 attributes[complex_quantity_slot::HAS_QUANTITIES] =
773 Value::List(quantities.iter().copied().map(Value::Ref).collect());
774 attributes[complex_quantity_slot::DISCRIMINATION] = Value::Text(discrimination.into());
775 Ok(tx.create(Entity::new("IFCPHYSICALCOMPLEXQUANTITY", attributes)))
776}
777
778mod builders;