Skip to main content

ifc_geometry/lower/
collection.rs

1//! Loose geometry collections: sets and surface models.
2//!
3//! # Why these are collections and not solids
4//!
5//! `IfcShellBasedSurfaceModel` and `IfcFaceBasedSurfaceModel` describe
6//! SURFACES, not volumes. Their shells may even all be closed, but the entity
7//! still declares a surface model and is not a legal boolean operand.
8//! Promoting one to a `BRep` would let a quantity takeoff report a volume the
9//! file never claimed, so each shell lowers on its own and the members stay in
10//! a `Collection`.
11//!
12//! `IfcGeometricSet` is looser still: its members are points, curves or
13//! surfaces, mixed freely within one dimensionality. Each element therefore
14//! dispatches individually rather than being assumed homogeneous.
15
16use axiolid_model::{GeometryNode, NodeId};
17use ifc_model::EntityId;
18
19use crate::error::GeometryResult;
20use crate::lower::curve::lower_curve_node;
21use crate::lower::session::LoweringSession;
22use crate::lower::surface::lower_surface_node;
23use crate::select::is_a;
24use crate::solid::surface_model::{FaceBasedSurfaceModel, GeometricSet, ShellBasedSurfaceModel};
25use crate::transform::Transform;
26
27/// Chain kind reported when a collection nests too deeply or cycles.
28const KIND: &str = "geometric collection";
29
30/// Lower a geometric set or surface model into a `Collection` node.
31pub fn lower_collection_node(
32    session: &mut LoweringSession<'_>,
33    id: EntityId,
34    frame: Transform,
35) -> GeometryResult<NodeId> {
36    if let Some(node) = session.memoized(id, KIND, frame) {
37        return Ok(node);
38    }
39    session.enter(id, KIND)?;
40    let result = build(session, id, frame);
41    session.exit(id);
42    let node = result?;
43    session.memoize(id, KIND, frame, node);
44    Ok(node)
45}
46
47fn build(
48    session: &mut LoweringSession<'_>,
49    id: EntityId,
50    frame: Transform,
51) -> GeometryResult<NodeId> {
52    let entity = session.entity(id, id)?;
53    let type_name = session.type_name(id)?.to_ascii_uppercase();
54    let members = match type_name.as_str() {
55        "IFCSHELLBASEDSURFACEMODEL" => ShellBasedSurfaceModel::new(id, entity).shells()?,
56        "IFCFACEBASEDSURFACEMODEL" => FaceBasedSurfaceModel::new(id, entity).face_sets()?,
57        "IFCGEOMETRICSET" | "IFCGEOMETRICCURVESET" => GeometricSet::new(id, entity).elements()?,
58        other => return Err(session.unsupported(id, other, "geometric collection family")),
59    };
60
61    let mut nodes = Vec::with_capacity(members.len());
62    for member in members {
63        nodes.push(member_node(session, id, member, frame)?);
64    }
65    session.node_for(id, GeometryNode::Collection(nodes))
66}
67
68/// Lower one member, routing each family to the path that accepts it.
69///
70/// Three routes, because a collection's members are not all representation
71/// items:
72///
73/// - Shells and connected face sets are topology, not items, so the item
74///   dispatcher rejects them.
75/// - Curves and surfaces ARE items but are deliberately not top-level in
76///   `dispatch`: everywhere else they are reached through the solid that
77///   sweeps or bounds them, and making them dispatchable globally would let a
78///   bare curve stand in for a body representation. Inside an
79///   `IfcGeometricSet` they are exactly the payload, so they route here and
80///   only here.
81/// - Everything else is an ordinary item.
82///
83/// Family tests go through the generated `is_a` supertype table rather than a
84/// literal type list, so a curve subtype added to `lower_curve_node` is picked
85/// up here without a second edit that could drift out of step.
86fn member_node(
87    session: &mut LoweringSession<'_>,
88    referrer: EntityId,
89    id: EntityId,
90    frame: Transform,
91) -> GeometryResult<NodeId> {
92    let member_type = session.type_name(id)?.to_ascii_uppercase();
93    if matches!(
94        member_type.as_str(),
95        "IFCCLOSEDSHELL" | "IFCOPENSHELL" | "IFCCONNECTEDFACESET"
96    ) {
97        return crate::lower::brep::lower_shell_node(session, referrer, id, frame);
98    }
99    if is_a(&member_type, "IFCCURVE") {
100        return lower_curve_node(session, id, frame);
101    }
102    if is_a(&member_type, "IFCSURFACE") {
103        return lower_surface_node(session, id, frame);
104    }
105    crate::lower::dispatch::lower_representation_item(session, id, frame)
106}