1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use ifc_lite_core::{DecodedEntity, EntityDecoder, IfcType};
use std::collections::HashMap;
use super::fill::extract_annotation_fill_area;
use super::primitives::{SymbolicCircle, SymbolicData, SymbolicPolyline};
use super::text::extract_text_literal;
use super::transform::{
circle_center, compose_transforms, parse_axis2_placement_2d,
parse_cartesian_transformation_operator, Transform2D,
};
use super::trimmed_curve::extract_trimmed_curve;
// ────────────────────────────────────────────────────────────────────────────
// Item dispatch. One function per IFC representation-item type; recursive
// for set + mapped-item containers.
// ────────────────────────────────────────────────────────────────────────────
#[allow(clippy::too_many_arguments)]
pub(super) fn extract_symbolic_item(
item: &DecodedEntity,
decoder: &mut EntityDecoder,
express_id: u32,
ifc_type: &str,
rep_identifier: &str,
unit_scale: f32,
transform: &Transform2D,
rtc_x: f32,
rtc_z: f32,
styled_items: &HashMap<u32, Vec<u32>>,
out: &mut SymbolicData,
) {
match item.ifc_type {
IfcType::IfcGeometricSet | IfcType::IfcGeometricCurveSet => {
if let Some(elements_attr) = item.get(0) {
if let Ok(elements) = decoder.resolve_ref_list(elements_attr) {
for element in elements {
extract_symbolic_item(
&element,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
transform,
rtc_x,
rtc_z,
styled_items,
out,
);
}
}
}
}
IfcType::IfcMappedItem => {
let Some(source_id) = item.get_ref(0) else { return };
let Ok(rep_map) = decoder.decode_by_id(source_id) else { return };
// MappingOrigin (rep_map attr 0) is MANDATORY on IfcRepresentationMap
// (unlike ObjectPlacement's optional PlacementRelTo): a dangling ref
// or an absent/null attribute is malformed data, not a legitimate
// zero, so both become `unresolved()` (#2256).
let mapping_origin_transform = match rep_map.get_ref(0) {
Some(origin_id) => match decoder.decode_by_id(origin_id) {
Ok(origin)
if origin.ifc_type == IfcType::IfcAxis2Placement2D
|| origin.ifc_type == IfcType::IfcAxis2Placement3D =>
{
parse_axis2_placement_2d(&origin, decoder, unit_scale)
}
Ok(_) => Transform2D::unresolved(), // wrong type (#2355)
Err(_) => Transform2D::unresolved(), // dangling ref (#2256)
},
None => Transform2D::unresolved(), // mandatory attr absent (#2256)
};
// MappingTarget (item attr 1) is likewise MANDATORY on
// IfcMappedItem — same failure/absence treatment as above.
let mapping_target_transform = match item.get_ref(1) {
Some(target_ref) => match decoder.decode_by_id(target_ref) {
Ok(target)
if matches!(
target.ifc_type,
IfcType::IfcCartesianTransformationOperator
| IfcType::IfcCartesianTransformationOperator2D
| IfcType::IfcCartesianTransformationOperator2DnonUniform
| IfcType::IfcCartesianTransformationOperator3D
| IfcType::IfcCartesianTransformationOperator3DnonUniform
) =>
{
parse_cartesian_transformation_operator(&target, decoder, unit_scale)
}
Ok(_) => Transform2D::unresolved(), // wrong type (#2355)
Err(_) => Transform2D::unresolved(), // dangling ref (#2256)
},
None => Transform2D::unresolved(), // mandatory attr absent (#2256)
};
let origin_with_target =
compose_transforms(&mapping_target_transform, &mapping_origin_transform);
let composed_transform = compose_transforms(transform, &origin_with_target);
if let Some(mapped_rep_id) = rep_map.get_ref(1) {
if let Ok(mapped_rep) = decoder.decode_by_id(mapped_rep_id) {
if let Some(items_attr) = mapped_rep.get(3) {
if let Ok(items) = decoder.resolve_ref_list(items_attr) {
for sub_item in items {
extract_symbolic_item(
&sub_item,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
&composed_transform,
rtc_x,
rtc_z,
styled_items,
out,
);
}
}
}
}
}
}
IfcType::IfcPolyline => {
if let Some(points_attr) = item.get(0) {
if let Ok(point_entities) = decoder.resolve_ref_list(points_attr) {
let mut points: Vec<f32> = Vec::with_capacity(point_entities.len() * 2);
let mut first_z: Option<f32> = None;
for pe in point_entities.iter() {
if pe.ifc_type != IfcType::IfcCartesianPoint {
continue;
}
let coords = match pe.get(0).and_then(|a| a.as_list()) {
Some(c) => c,
None => continue,
};
let local_x = coords.first().and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let local_y = coords.get(1).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let local_z = coords.get(2).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
if first_z.is_none() {
first_z = Some(local_z);
}
let (wx, wy) = transform.transform_point(local_x, local_y);
let x = wx - rtc_x;
let y = -wy + rtc_z; // Y-flip to match section-cut coord system
if x.is_finite() && y.is_finite() {
points.push(x);
points.push(y);
}
}
if points.len() >= 4 {
let n = points.len();
let is_closed = n >= 4
&& (points[0] - points[n - 2]).abs() < 0.001
&& (points[1] - points[n - 1]).abs() < 0.001;
let world_y = first_z.unwrap_or(0.0) + transform.tz;
out.polylines.push(SymbolicPolyline {
express_id,
ifc_type: ifc_type.to_string(),
points,
closed: is_closed,
world_y,
representation: rep_identifier.to_string(),
});
}
}
}
}
IfcType::IfcIndexedPolyCurve => {
let Some(points_ref) = item.get_ref(0) else { return };
let Ok(points_list) = decoder.decode_by_id(points_ref) else { return };
let Some(coord_list_attr) = points_list.get(0) else { return };
let Some(coord_list) = coord_list_attr.as_list() else { return };
let mut points: Vec<f32> = Vec::with_capacity(coord_list.len() * 2);
let mut first_z: Option<f32> = None;
for coord in coord_list {
let Some(coords) = coord.as_list() else { continue };
let local_x = coords.first().and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let local_y = coords.get(1).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let local_z = coords.get(2).and_then(|v| v.as_float()).unwrap_or(0.0) as f32 * unit_scale;
if first_z.is_none() {
first_z = Some(local_z);
}
let (wx, wy) = transform.transform_point(local_x, local_y);
let x = wx - rtc_x;
let y = -wy + rtc_z;
if x.is_finite() && y.is_finite() {
points.push(x);
points.push(y);
}
}
if points.len() >= 4 {
let n = points.len();
let is_closed = n >= 4
&& (points[0] - points[n - 2]).abs() < 0.001
&& (points[1] - points[n - 1]).abs() < 0.001;
let world_y = first_z.unwrap_or(0.0) + transform.tz;
out.polylines.push(SymbolicPolyline {
express_id,
ifc_type: ifc_type.to_string(),
points,
closed: is_closed,
world_y,
representation: rep_identifier.to_string(),
});
}
}
IfcType::IfcCircle => {
// × scale(): a scalar radius never passes through transform_point (#1985).
let r = item.get(1).and_then(|a| a.as_float()).unwrap_or(0.0) as f32;
let radius = r * unit_scale * transform.scale();
let (center_x, center_y, center_z) = circle_center(item, decoder, unit_scale);
if !(radius.is_finite() && radius > 0.0 && center_x.is_finite() && center_y.is_finite()) {
return;
}
let (wx, wy) = transform.transform_point(center_x, center_y);
out.circles.push(SymbolicCircle::full(
express_id,
ifc_type.to_string(),
wx - rtc_x,
-wy + rtc_z,
radius,
center_z + transform.tz,
rep_identifier.to_string(),
));
}
IfcType::IfcEllipse => {
// NOT × scale() (unlike IfcCircle): sampled points go through transform_point.
let semi_a = item.get(1).and_then(|a| a.as_float()).unwrap_or(0.0) as f32 * unit_scale;
let semi_b = item.get(2).and_then(|a| a.as_float()).unwrap_or(0.0) as f32 * unit_scale;
if semi_a <= 0.0 || semi_b <= 0.0 || !semi_a.is_finite() || !semi_b.is_finite() {
return;
}
let (cx_local, cy_local, cz_local) = circle_center(item, decoder, unit_scale);
const SEGMENTS: usize = 64;
let mut points: Vec<f32> = Vec::with_capacity((SEGMENTS + 1) * 2);
for i in 0..=SEGMENTS {
let t = (i as f32) * std::f32::consts::TAU / (SEGMENTS as f32);
let lx = cx_local + semi_a * t.cos();
let ly = cy_local + semi_b * t.sin();
let (wx, wy) = transform.transform_point(lx, ly);
let x = wx - rtc_x;
let y = -wy + rtc_z;
if x.is_finite() && y.is_finite() {
points.push(x);
points.push(y);
}
}
if points.len() >= 4 {
out.polylines.push(SymbolicPolyline {
express_id,
ifc_type: ifc_type.to_string(),
points,
closed: true,
world_y: cz_local + transform.tz,
representation: rep_identifier.to_string(),
});
}
}
IfcType::IfcTrimmedCurve => {
extract_trimmed_curve(
item,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
transform,
rtc_x,
rtc_z,
out,
);
}
IfcType::IfcCompositeCurve => {
if let Some(segments_attr) = item.get(0) {
if let Ok(segments) = decoder.resolve_ref_list(segments_attr) {
for segment in segments {
if let Some(curve_ref) = segment.get_ref(2) {
if let Ok(parent_curve) = decoder.decode_by_id(curve_ref) {
extract_symbolic_item(
&parent_curve,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
transform,
rtc_x,
rtc_z,
styled_items,
out,
);
}
}
}
}
}
}
IfcType::IfcLine => {
// Infinite — no sensible 2D segment to emit.
}
IfcType::IfcTextLiteral | IfcType::IfcTextLiteralWithExtent => {
extract_text_literal(
item,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
transform,
rtc_x,
rtc_z,
styled_items,
out,
);
}
IfcType::IfcAnnotationFillArea => {
extract_annotation_fill_area(
item,
decoder,
express_id,
ifc_type,
rep_identifier,
unit_scale,
transform,
rtc_x,
rtc_z,
styled_items,
out,
);
}
_ => {
// Unknown / unsupported curve type — skip silently.
}
}
}