Skip to main content

ifc_geometry/lower/
context.rs

1//! Product placement and representation selection.
2//!
3//! # Why placement is composed here and not per item
4//!
5//! A representation item is authored in its product local space.
6//! The product hangs off an IfcLocalPlacement chain that walks up
7//! the spatial tree to the site. Lowering an item without that
8//! chain puts every product at the world origin: the geometry is
9//! individually correct and the building is a heap.
10//!
11//! # Units are converted once
12//!
13//! Placement coordinates are raw file units, so the resolver
14//! composes the chain unconverted and this module converts the
15//! composed result. Converting per link would raise the scale to
16//! the power of the chain depth.
17
18use axiolid_model::NodeId;
19use ifc_model::EntityId;
20
21use crate::error::{GeometryError, GeometryResult};
22// The frame is kernel-free, so lowering and body description compose the
23// same model space from one function.
24use crate::input::context::representation_frame_resolved;
25// Moved to `input::product`: it never needed the kernel. Re-exported so
26// the pre-existing `lower::context::geometric_products` path still resolves.
27pub use crate::input::product::geometric_products;
28use crate::input::representation::{
29    select_product_representation, Representation, RepresentationPurpose,
30};
31use crate::lower::dispatch::lower_representation_item;
32use crate::lower::session::LoweringSession;
33use crate::transform::Transform;
34
35pub use crate::constraint::product_world_transform;
36pub use crate::input::representation::select_shape_representation;
37
38/// Lower every item of a product selected representation into one graph.
39///
40/// All items share one session, so a product whose Body holds several
41/// solids yields one graph with one Collection root rather than N
42/// disconnected graphs the caller has to merge.
43pub fn lower_product_items(
44    session: &mut LoweringSession<'_>,
45    product: EntityId,
46) -> GeometryResult<Option<NodeId>> {
47    lower_product_representation(session, product, RepresentationPurpose::Body)
48}
49
50/// Select and lower a product representation for an explicit consumer path.
51pub fn lower_product_representation(
52    session: &mut LoweringSession<'_>,
53    product: EntityId,
54    purpose: RepresentationPurpose,
55) -> GeometryResult<Option<NodeId>> {
56    // The session says how an `IfcLinearPlacement` resolves: from its cache,
57    // or through the caller's evaluator (#353).
58    let Some(world) = representation_frame_resolved(
59        session.model(),
60        session.units(),
61        product,
62        purpose,
63        session.linear_resolution(),
64    )?
65    else {
66        return Ok(None);
67    };
68    lower_product_representation_in(session, product, purpose, world)
69}
70
71/// Select and lower a product representation with every item placed by
72/// `frame` instead of the product's world frame.
73///
74/// Net lowering (#388) lowers a host's Body in the host's own frame and
75/// each opening's relative to it, then places the result once. `frame` is
76/// in metres and already includes whatever context frame applies.
77pub(crate) fn lower_product_representation_in(
78    session: &mut LoweringSession<'_>,
79    product: EntityId,
80    purpose: RepresentationPurpose,
81    frame: Transform,
82) -> GeometryResult<Option<NodeId>> {
83    let Some(representation) = select_product_representation(session.model(), product, purpose)?
84    else {
85        return Ok(None);
86    };
87
88    let entity = session
89        .model()
90        .get(representation)
91        .ok_or(GeometryError::MissingEntity {
92            referrer: product,
93            missing: representation,
94        })?;
95    let items = Representation::new(representation, entity).items()?;
96    let mut roots = Vec::with_capacity(items.len());
97    for item in items {
98        roots.push(lower_representation_item(session, item, frame)?);
99    }
100    collect_roots(session, product, roots)
101}
102
103/// Select and lower a product representation, leaving out each item whose
104/// STEP type name `leave_out` accepts.
105///
106/// `Ok(None)` when the product has no representation for `purpose`;
107/// otherwise the root of the items lowered (`None` when every item was
108/// left out) and the items left out, in authored order. Only the
109/// representation's own items are tested: an item nested in a mapped
110/// representation or a set is lowered, or refused, as usual. Every item
111/// not left out lowers as [`lower_product_representation`] lowers it, and
112/// its refusal still refuses the product. The curves reader is its only
113/// caller (#512).
114#[cfg(feature = "compile")]
115pub(crate) fn lower_product_representation_leaving_out(
116    session: &mut LoweringSession<'_>,
117    product: EntityId,
118    purpose: RepresentationPurpose,
119    leave_out: impl Fn(&str) -> bool,
120) -> GeometryResult<Option<(Option<NodeId>, Vec<EntityId>)>> {
121    let Some(frame) = representation_frame_resolved(
122        session.model(),
123        session.units(),
124        product,
125        purpose,
126        session.linear_resolution(),
127    )?
128    else {
129        return Ok(None);
130    };
131    let Some(representation) = select_product_representation(session.model(), product, purpose)?
132    else {
133        return Ok(None);
134    };
135    let entity = session
136        .model()
137        .get(representation)
138        .ok_or(GeometryError::MissingEntity {
139            referrer: product,
140            missing: representation,
141        })?;
142    let items = Representation::new(representation, entity).items()?;
143    let (mut roots, mut left_out) = (Vec::with_capacity(items.len()), Vec::new());
144    for item in items {
145        // A dangling item is lowered, so it is refused as usual.
146        match session.model().get(item) {
147            Some(entity) if leave_out(&entity.type_name) => left_out.push(item),
148            _ => roots.push(lower_representation_item(session, item, frame)?),
149        }
150    }
151    Ok(Some((collect_roots(session, product, roots)?, left_out)))
152}
153
154/// One root for the lowered items: none, the only one, or a collection.
155fn collect_roots(
156    session: &mut LoweringSession<'_>,
157    product: EntityId,
158    roots: Vec<NodeId>,
159) -> GeometryResult<Option<NodeId>> {
160    match roots.len() {
161        0 => Ok(None),
162        1 => Ok(Some(roots[0])),
163        _ => Ok(Some(session.node_for(
164            product,
165            axiolid_model::GeometryNode::Collection(roots),
166        )?)),
167    }
168}