ifc_alignment/authoring/
layout.rs1use ifc_model::{Entity, EntityId, Transaction, Value};
15
16use super::{finite, guid, invalid};
17use crate::error::AlignmentError;
18use crate::slot;
19
20fn product_attrs(
22 type_name: &'static str,
23 arity: usize,
24 global_id: &str,
25 name: Option<&str>,
26) -> Result<Vec<Value>, AlignmentError> {
27 let mut attrs = vec![Value::Null; arity];
28 attrs[slot::product::GLOBAL_ID] = guid(type_name, global_id)?;
29 if let Some(name) = name {
30 attrs[slot::product::NAME] = Value::Text(name.into());
31 }
32 Ok(attrs)
33}
34
35pub fn alignment(
41 tx: &mut Transaction,
42 global_id: &str,
43 name: Option<&str>,
44 predefined_type: Option<&'static str>,
45) -> Result<EntityId, AlignmentError> {
46 let mut attrs = product_attrs("IFCALIGNMENT", slot::alignment::ARITY, global_id, name)?;
47 if let Some(token) = predefined_type {
48 attrs[slot::alignment::PREDEFINED_TYPE] = Value::Enum(token.into());
49 }
50 Ok(tx.create(Entity::new("IFCALIGNMENT", attrs)))
51}
52
53pub fn horizontal_layout(
59 tx: &mut Transaction,
60 global_id: &str,
61 name: Option<&str>,
62) -> Result<EntityId, AlignmentError> {
63 let attrs = product_attrs(
64 "IFCALIGNMENTHORIZONTAL",
65 slot::product::ARITY,
66 global_id,
67 name,
68 )?;
69 Ok(tx.create(Entity::new("IFCALIGNMENTHORIZONTAL", attrs)))
70}
71
72pub fn vertical_layout(
78 tx: &mut Transaction,
79 global_id: &str,
80 name: Option<&str>,
81) -> Result<EntityId, AlignmentError> {
82 let attrs = product_attrs(
83 "IFCALIGNMENTVERTICAL",
84 slot::product::ARITY,
85 global_id,
86 name,
87 )?;
88 Ok(tx.create(Entity::new("IFCALIGNMENTVERTICAL", attrs)))
89}
90
91pub fn cant_layout(
102 tx: &mut Transaction,
103 global_id: &str,
104 name: Option<&str>,
105 rail_head_distance: f64,
106) -> Result<EntityId, AlignmentError> {
107 finite("IFCALIGNMENTCANT", "RailHeadDistance", rail_head_distance)?;
108 if rail_head_distance <= 0.0 {
109 return Err(invalid(
110 "IFCALIGNMENTCANT",
111 "RailHeadDistance",
112 "the distance between rail heads must be positive",
113 ));
114 }
115 let mut attrs = product_attrs(
116 "IFCALIGNMENTCANT",
117 slot::cant_layout::ARITY,
118 global_id,
119 name,
120 )?;
121 attrs[slot::cant_layout::RAIL_HEAD_DISTANCE] = Value::Real(rail_head_distance);
122 Ok(tx.create(Entity::new("IFCALIGNMENTCANT", attrs)))
123}
124
125pub fn alignment_segment(
136 tx: &mut Transaction,
137 global_id: &str,
138 design_parameters: EntityId,
139) -> Result<EntityId, AlignmentError> {
140 let mut attrs = product_attrs("IFCALIGNMENTSEGMENT", slot::segment::ARITY, global_id, None)?;
141 attrs[slot::segment::DESIGN_PARAMETERS] = Value::Ref(design_parameters);
142 Ok(tx.create(Entity::new("IFCALIGNMENTSEGMENT", attrs)))
143}