ifc_lite_geometry/representation/
structural.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Shallow structural entries for callers that must not eagerly decode a shape.
6
7use ifc_lite_core::{slot, DecodedEntity, EntityDecoder, IfcType};
8
9/// Identity only: no type admission, shape decoding or representation selection.
10/// Routing hashes must also retain their existing handling of dangling references.
11#[inline]
12pub fn product_shape_id(element: &DecodedEntity) -> Option<u32> {
13    element.get_ref(slot::IfcProduct::REPRESENTATION.index())
14}
15
16/// Items in list order, skipping non-references and missing/non-list Items.
17/// No item is decoded here: RTC must try its speculative fast point read first,
18/// skip undecodable items itself, and stop without touching later items on a hit.
19#[inline]
20pub fn rep_item_ids(rep: &DecodedEntity) -> impl Iterator<Item = u32> + '_ {
21    rep.get_list(slot::IfcRepresentation::ITEMS.index())
22        .into_iter()
23        .flatten()
24        .filter_map(|value| value.as_entity_ref())
25}
26
27/// Representation IDs in document order, without type admission or decoding.
28/// Like `get_refs`, accepts bare references and typed reference sets, skipping
29/// non-reference members. Missing or invalid values produce no IDs.
30#[inline]
31pub fn pds_representation_ids(pds: &DecodedEntity) -> impl Iterator<Item = u32> {
32    pds.get_refs(slot::IfcProductDefinitionShape::REPRESENTATIONS.index())
33        .unwrap_or_default()
34        .into_iter()
35}
36
37/// Read MappingSource -> MappedRepresentation, decoding only the source.
38/// Callers admit mapped items themselves. No source-type admission is applied:
39/// legacy colour/layer probes accept even a wrongly typed source with slot 1.
40/// Missing/non-reference slots and undecodable sources return None; the target
41/// ID is returned even when dangling, so callers can probe their indexes first.
42#[inline]
43pub fn mapped_item_representation_id(item: &DecodedEntity, decoder: &mut EntityDecoder<'_>) -> Option<u32> {
44    let source_id = item.get_ref(slot::IfcMappedItem::MAPPING_SOURCE.index())?;
45    let source = decoder.decode_by_id(source_id).ok()?;
46    source.get_ref(slot::IfcRepresentationMap::MAPPED_REPRESENTATION.index())
47}
48
49/// Prove an opening is Reference-only (BoundingBox is allowed alongside it).
50/// Unknown/malformed input is inconclusive, including an empty or over-budget
51/// list. Check the 64-entry bound BEFORE decoding any representation, and never
52/// inspect Items: this policy does no geometry work (#4440, #7781 C2-2).
53pub(crate) fn reference_only_opening(id: u32, decoder: &mut EntityDecoder<'_>) -> Option<bool> {
54    let opening = decoder.decode_by_id(id).ok()?;
55    if !opening.ifc_type.is_subtype_of(IfcType::IfcOpeningElement) {
56        return None;
57    }
58    let pds = decoder.decode_by_id(product_shape_id(&opening)?).ok()?;
59    if pds.ifc_type != IfcType::IfcProductDefinitionShape {
60        return None;
61    }
62    let representations = pds.get_list(slot::IfcProductDefinitionShape::REPRESENTATIONS.index())?;
63    if representations.is_empty() || representations.len() > 64 {
64        return None;
65    }
66    let mut reference = false;
67    for value in representations {
68        let representation = decoder.decode_by_id(value.as_entity_ref()?).ok()?;
69        if representation.ifc_type != IfcType::IfcShapeRepresentation {
70            return None;
71        }
72        match super::effective_element_rep_type(&opening, &representation) {
73            Some("Reference") => reference = true,
74            Some("BoundingBox") => {}
75            _ => return None,
76        }
77    }
78    Some(reference)
79}
80
81#[cfg(test)]
82#[path = "structural_tests.rs"]
83mod tests;