Skip to main content

ifc_alignment/authoring/
layout.rs

1//! Alignment roots and their layout children.
2//!
3//! `IfcAlignment`, `IfcAlignmentHorizontal`, `IfcAlignmentVertical` and
4//! `IfcAlignmentCant` are all `IfcProduct` subtypes, so they carry the same
5//! seven inherited attributes before adding their own.
6//!
7//! Nesting is not written here: these writers stage the products only, and
8//! the caller relates them with an `IfcRelNests` (alignment to layouts,
9//! layout to `IfcAlignmentSegment`s). No crate has a dedicated generic
10//! `IfcRelNests` writer (`ifc-systems::nest_ports` nests ports only); the
11//! relationship is staged as a schema-checked record built with
12//! `ifc_author::EntityBuilder`, or as a plain `Entity` on the transaction.
13
14use ifc_model::{Entity, EntityId, Transaction, Value};
15
16use super::{finite, guid, invalid};
17use crate::error::AlignmentError;
18use crate::slot;
19
20/// Fill the seven inherited `IfcProduct` slots.
21fn 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
35/// Stage an `IfcAlignment`.
36///
37/// # Errors
38///
39/// Refuses a `GlobalId` that is not 22 characters.
40pub 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
53/// Stage an `IfcAlignmentHorizontal`.
54///
55/// # Errors
56///
57/// Refuses a `GlobalId` that is not 22 characters.
58pub 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
72/// Stage an `IfcAlignmentVertical`.
73///
74/// # Errors
75///
76/// Refuses a `GlobalId` that is not 22 characters.
77pub 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
91/// Stage an `IfcAlignmentCant`.
92///
93/// `rail_head_distance` is the gauge the cant values are measured against.
94/// It is mandatory on this entity, and a non-positive value would make the
95/// cant angle meaningless, so it is checked rather than written blindly.
96///
97/// # Errors
98///
99/// Refuses a `GlobalId` that is not 22 characters, and a rail head distance
100/// that is not finite and positive.
101pub 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
125/// Stage an `IfcAlignmentSegment` wrapping one parameter segment.
126///
127/// A layout does not nest parameter segments directly: it nests these
128/// wrappers, each pointing at its `DesignParameters`. `AlignmentView`'s
129/// traversal resolves that reference, so a layout built without wrappers
130/// reads back as a malformed segment chain.
131///
132/// # Errors
133///
134/// Refuses a `GlobalId` that is not 22 characters.
135pub 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}