use ifc_model::guid::Guid;
use ifc_model::{Entity, EntityId, Model, Transaction, Value};
use super::authoring::{optional_text, require_name};
use crate::error::{PropertyError, PropertyResult};
pub mod defines_by_type_slot {
pub const GLOBAL_ID: usize = 0;
pub const RELATED_OBJECTS: usize = 4;
pub const RELATING_TYPE: usize = 5;
}
pub fn attach_type(
tx: &mut Transaction,
model: &Model,
global_id: &str,
objects: &[EntityId],
relating_type: EntityId,
) -> PropertyResult<EntityId> {
super::root_authoring::attach_type_record(tx, model, global_id, objects, relating_type, None)
}
pub mod pset_template_slot {
pub const GLOBAL_ID: usize = 0;
pub const NAME: usize = 2;
pub const DESCRIPTION: usize = 3;
pub const TEMPLATE_TYPE: usize = 4;
pub const APPLICABLE_ENTITY: usize = 5;
pub const HAS_PROPERTY_TEMPLATES: usize = 6;
}
pub mod defines_by_template_slot {
pub const GLOBAL_ID: usize = 0;
pub const RELATED_PROPERTY_SETS: usize = 4;
pub const RELATING_TEMPLATE: usize = 5;
}
pub fn add_property_set_template(
tx: &mut Transaction,
global_id: &str,
name: &str,
applicable_entity: Option<&str>,
templates: &[EntityId],
) -> PropertyResult<EntityId> {
if Guid::parse(global_id).is_none() {
return Err(PropertyError::AuthoringInvalid {
entity: "IFCPROPERTYSETTEMPLATE",
attribute: "GlobalId",
value: global_id.to_owned(),
});
}
require_name("IFCPROPERTYSETTEMPLATE", name)?;
if templates.is_empty() {
return Err(PropertyError::AuthoringInvalid {
entity: "IFCPROPERTYSETTEMPLATE",
attribute: "HasPropertyTemplates",
value: "empty".to_owned(),
});
}
let mut attributes = vec![Value::Null; pset_template_slot::HAS_PROPERTY_TEMPLATES + 1];
attributes[pset_template_slot::GLOBAL_ID] = Value::Text(global_id.into());
attributes[pset_template_slot::NAME] = Value::Text(name.into());
attributes[pset_template_slot::APPLICABLE_ENTITY] = optional_text(applicable_entity);
attributes[pset_template_slot::HAS_PROPERTY_TEMPLATES] =
Value::List(templates.iter().copied().map(Value::Ref).collect());
Ok(tx.create(Entity::new("IFCPROPERTYSETTEMPLATE", attributes)))
}
pub fn attach_template(
tx: &mut Transaction,
global_id: &str,
property_sets: &[EntityId],
template: EntityId,
) -> PropertyResult<EntityId> {
if Guid::parse(global_id).is_none() {
return Err(PropertyError::AuthoringInvalid {
entity: "IFCRELDEFINESBYTEMPLATE",
attribute: "GlobalId",
value: global_id.to_owned(),
});
}
if property_sets.is_empty() {
return Err(PropertyError::AuthoringInvalid {
entity: "IFCRELDEFINESBYTEMPLATE",
attribute: "RelatedPropertySets",
value: "empty".to_owned(),
});
}
let mut attributes = vec![Value::Null; defines_by_template_slot::RELATING_TEMPLATE + 1];
attributes[defines_by_template_slot::GLOBAL_ID] = Value::Text(global_id.into());
attributes[defines_by_template_slot::RELATED_PROPERTY_SETS] =
Value::List(property_sets.iter().copied().map(Value::Ref).collect());
attributes[defines_by_template_slot::RELATING_TEMPLATE] = Value::Ref(template);
Ok(tx.create(Entity::new("IFCRELDEFINESBYTEMPLATE", attributes)))
}