openbim_ifc_binding_core/
spatial.rs1use crate::record::{Field, Record, ToRecord};
14use crate::{BindingError, IfcModel};
15
16#[derive(Debug, Clone, PartialEq)]
18pub struct SpatialTree {
19 pub release: Option<String>,
22 pub roots: Vec<u64>,
25 pub nodes: Vec<SpatialNode>,
27 pub orphans: Vec<u64>,
30 pub dangling: Vec<(u64, u64)>,
32 pub anomalies: Vec<SpatialAnomaly>,
34}
35
36#[derive(Debug, Clone, PartialEq)]
38pub struct SpatialNode {
39 pub id: u64,
41 pub global_id: Option<String>,
43 pub name: Option<String>,
45 pub type_name: String,
47 pub kind: String,
50 pub parent: Option<u64>,
52 pub children: Vec<u64>,
54 pub elements: Vec<u64>,
56 pub referenced: Vec<u64>,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct SpatialAnomaly {
64 pub kind: String,
67 pub relation: u64,
69 pub subject: u64,
71 pub kept: Option<u64>,
73}
74
75impl IfcModel {
76 pub fn spatial_tree(&self) -> Result<SpatialTree, BindingError> {
81 #[cfg(feature = "spatial")]
82 {
83 read(self)
84 }
85 #[cfg(not(feature = "spatial"))]
86 {
87 Err(BindingError::FeatureDisabled("spatial"))
88 }
89 }
90}
91
92#[cfg(feature = "spatial")]
93fn read(model: &IfcModel) -> Result<SpatialTree, BindingError> {
94 use ifc::spatial::SpatialAnomaly as Anomaly;
95 use ifc::SpatialKind;
96
97 model.declared_schema()?;
98 let tree = ifc::SpatialTree::build(&model.inner);
99 let ids = |ids: &[ifc::EntityId]| ids.iter().map(|id| id.0).collect::<Vec<_>>();
100 let mut nodes = Vec::new();
101 for node in tree.containers() {
102 let identity = model.identity(node.id.0)?;
103 let kind = match node.kind {
104 SpatialKind::Project => "project",
105 SpatialKind::Site => "site",
106 SpatialKind::Building => "building",
107 SpatialKind::Storey => "storey",
108 SpatialKind::Space => "space",
109 _ => "other",
110 };
111 nodes.push(SpatialNode {
112 id: node.id.0,
113 global_id: identity.global_id,
114 name: identity.name,
115 type_name: model.type_of(node.id.0)?.to_owned(),
116 kind: kind.to_owned(),
117 parent: node.parent.map(|id| id.0),
118 children: ids(&node.children),
119 elements: ids(&node.elements),
120 referenced: ids(tree.referenced_elements(node.id)),
121 });
122 }
123 let anomalies = tree
124 .anomalies()
125 .iter()
126 .map(|anomaly| {
127 let (kind, relation, subject, kept) = match *anomaly {
128 Anomaly::ContainedTwice {
129 element,
130 kept,
131 relation,
132 ..
133 } => ("contained-twice", relation, element, Some(kept)),
134 Anomaly::AggregatedTwice {
135 child,
136 kept,
137 relation,
138 ..
139 } => ("aggregated-twice", relation, child, Some(kept)),
140 Anomaly::ContainedInNonContainer {
141 relation,
142 structure,
143 } => ("contained-in-non-container", relation, structure, None),
144 Anomaly::ReferencedInNonContainer {
145 relation,
146 structure,
147 } => ("referenced-in-non-container", relation, structure, None),
148 _ => ("other", ifc::EntityId(0), ifc::EntityId(0), None),
150 };
151 SpatialAnomaly {
152 kind: kind.to_owned(),
153 relation: relation.0,
154 subject: subject.0,
155 kept: kept.map(|id| id.0),
156 }
157 })
158 .collect();
159 Ok(SpatialTree {
160 release: tree
161 .release()
162 .map(|release| release.release_id().to_owned()),
163 roots: ids(tree.roots()),
164 nodes,
165 orphans: ids(tree.orphans()),
166 dangling: tree
167 .dangling()
168 .iter()
169 .map(|(relation, target)| (relation.0, target.0))
170 .collect(),
171 anomalies,
172 })
173}
174
175impl ToRecord for SpatialTree {
176 fn to_record(&self) -> Record {
177 Record::new(
178 "SpatialTree",
179 vec![
180 ("release", Field::text(self.release.clone())),
181 ("roots", Field::ids(self.roots.iter().copied())),
182 ("nodes", Field::records(&self.nodes)),
183 ("orphans", Field::ids(self.orphans.iter().copied())),
184 (
185 "dangling",
186 Field::List(
187 self.dangling
188 .iter()
189 .map(|&(relation, target)| {
190 Field::Record(Record::new(
191 "SpatialDanglingReference",
192 vec![
193 ("relation", Field::Id(relation)),
194 ("target", Field::Id(target)),
195 ],
196 ))
197 })
198 .collect(),
199 ),
200 ),
201 ("anomalies", Field::records(&self.anomalies)),
202 ],
203 )
204 }
205}
206
207impl ToRecord for SpatialNode {
208 fn to_record(&self) -> Record {
209 Record::new(
210 "SpatialNode",
211 vec![
212 ("id", Field::Id(self.id)),
213 ("global_id", Field::text(self.global_id.clone())),
214 ("name", Field::text(self.name.clone())),
215 ("type_name", Field::Text(self.type_name.clone())),
216 ("kind", Field::Text(self.kind.clone())),
217 ("parent", Field::id(self.parent)),
218 ("children", Field::ids(self.children.iter().copied())),
219 ("elements", Field::ids(self.elements.iter().copied())),
220 ("referenced", Field::ids(self.referenced.iter().copied())),
221 ],
222 )
223 }
224}
225
226impl ToRecord for SpatialAnomaly {
227 fn to_record(&self) -> Record {
228 Record::new(
229 "SpatialAnomaly",
230 vec![
231 ("kind", Field::Text(self.kind.clone())),
232 ("relation", Field::Id(self.relation)),
233 ("subject", Field::Id(self.subject)),
234 ("kept", Field::id(self.kept)),
235 ],
236 )
237 }
238}