ifc_geometry/input/product.rs
1//! Product shape and placement links.
2//!
3//! # Why a view and not a walk
4//!
5//! IfcProduct is abstract: walls, proxies, sites and 200 other
6//! subtypes carry ObjectPlacement and Representation at the same
7//! two absolute slots, inherited from IfcProduct. Reading them by
8//! slot here means the caller never enumerates subtypes, and a
9//! schema query decides what counts as a product.
10
11use ifc_model::{Entity, EntityId, Model};
12
13use crate::slots::Slots;
14
15/// Absolute slots on IfcProduct, inherited by every subtype.
16pub mod slot {
17 /// IfcRoot.GlobalId .. IfcObject.ObjectType occupy slots 0..4.
18 /// IfcProduct adds its own two after them.
19 #[cfg_attr(not(feature = "lowering"), allow(dead_code))]
20 pub const OBJECT_PLACEMENT: usize = 5;
21 /// The IfcProductRepresentation for this product.
22 pub const REPRESENTATION: usize = 6;
23}
24
25/// One IfcProduct occurrence: where it sits and what it looks like.
26#[derive(Debug, Clone, Copy)]
27pub struct Product<'m> {
28 slots: Slots<'m>,
29}
30
31impl<'m> Product<'m> {
32 /// Wrap an entity assumed to be an IfcProduct subtype.
33 pub fn new(id: EntityId, entity: &'m Entity) -> Self {
34 Self {
35 slots: Slots::new(id, entity),
36 }
37 }
38
39 /// The placement chain root, absent for model-space products.
40 ///
41 /// Only `lower` consumes this today. `input` is private, so a kernel-free
42 /// build sees no caller; that is a visibility artifact, not a boundary --
43 /// reading slot 5 is a plain IfcProduct slot read either way.
44 #[cfg_attr(not(feature = "lowering"), allow(dead_code))]
45 pub fn object_placement(&self) -> Option<EntityId> {
46 self.slots.opt_ref(slot::OBJECT_PLACEMENT)
47 }
48
49 /// The shape definition, absent for products with no geometry.
50 pub fn representation(&self) -> Option<EntityId> {
51 self.slots.opt_ref(slot::REPRESENTATION)
52 }
53}
54
55/// Products in the model that carry geometry, in stable id order.
56///
57/// Kernel-free on purpose: asking which entities have a shape is a slot read,
58/// so this answers the same under `--no-default-features` as it does with the
59/// full kernel linked. `tests/kernel_free_build.rs` holds that line.
60///
61/// IfcProduct is abstract with hundreds of subtypes, so enumerating names
62/// would rot. A product is recognised structurally instead: it has a
63/// Representation in slot 6 pointing at an IfcProductRepresentation.
64pub fn geometric_products(model: &Model) -> Vec<EntityId> {
65 let mut found: Vec<EntityId> = model
66 .iter()
67 .filter(|(id, entity)| {
68 let product = Product::new(*id, entity);
69 product.representation().is_some_and(|shape| {
70 model.get(shape).is_some_and(|e| {
71 e.type_name.contains("PRODUCTREPRESENTATION")
72 || e.type_name.contains("PRODUCTDEFINITIONSHAPE")
73 })
74 })
75 })
76 .map(|(id, _)| id)
77 .collect();
78 found.sort();
79 found
80}