Skip to main content

ifc_geometry/authoring/
solid.rs

1//! Swept solids and booleans.
2//!
3//! A boolean is written *unevaluated*: this module records the operation and
4//! its operands, it does not execute it. That preserves exact intent and
5//! keeps the bridge free of computation in the write direction too (ADR
6//! 0004, as amended by ADR 0011).
7
8use ifc_model::{Entity, EntityId, Transaction, Value};
9
10use crate::error::GeometryError;
11use crate::solid::boolean::{slot as boolean_slot, IfcBooleanOperator};
12use crate::solid::swept::{extruded_slot, revolved_slot, swept_area_slot};
13
14use super::invalid;
15
16/// Stage an `IfcExtrudedAreaSolid`.
17///
18/// # Errors
19///
20/// Refuses a non-positive or non-finite depth: EXPRESS declares `Depth` as
21/// `IfcPositiveLengthMeasure`, and a zero-depth extrusion is a face claiming
22/// to be a solid.
23pub fn extruded_area_solid(
24    tx: &mut Transaction,
25    swept_area: EntityId,
26    position: Option<EntityId>,
27    extruded_direction: EntityId,
28    depth: f64,
29) -> Result<EntityId, GeometryError> {
30    if !depth.is_finite() || depth <= 0.0 {
31        return Err(invalid(
32            "IFCEXTRUDEDAREASOLID",
33            "Depth",
34            format!("expected a positive length, got {depth}"),
35        ));
36    }
37    let mut attrs = vec![Value::Null; 4];
38    attrs[swept_area_slot::SWEPT_AREA] = Value::Ref(swept_area);
39    attrs[swept_area_slot::POSITION] = position.map_or(Value::Null, Value::Ref);
40    attrs[extruded_slot::EXTRUDED_DIRECTION] = Value::Ref(extruded_direction);
41    attrs[extruded_slot::DEPTH] = Value::Real(depth);
42    Ok(tx.create(Entity::new("IFCEXTRUDEDAREASOLID", attrs)))
43}
44
45/// Stage an `IfcRevolvedAreaSolid`.
46///
47/// `angle` is in radians, matching the unit-normalised form the reader
48/// produces. A caller working in degrees converts before this point.
49///
50/// # Errors
51///
52/// Refuses a non-finite angle, and one outside `(0, 2*pi]`. Zero sweeps
53/// nothing; beyond a full turn the solid would self-overlap, which IFC does
54/// not define.
55pub fn revolved_area_solid(
56    tx: &mut Transaction,
57    swept_area: EntityId,
58    position: Option<EntityId>,
59    axis: EntityId,
60    angle: f64,
61) -> Result<EntityId, GeometryError> {
62    if !angle.is_finite() || angle <= 0.0 || angle > std::f64::consts::TAU {
63        return Err(invalid(
64            "IFCREVOLVEDAREASOLID",
65            "Angle",
66            format!("expected an angle in (0, 2pi] radians, got {angle}"),
67        ));
68    }
69    let mut attrs = vec![Value::Null; 4];
70    attrs[swept_area_slot::SWEPT_AREA] = Value::Ref(swept_area);
71    attrs[swept_area_slot::POSITION] = position.map_or(Value::Null, Value::Ref);
72    attrs[revolved_slot::AXIS] = Value::Ref(axis);
73    attrs[revolved_slot::ANGLE] = Value::Real(angle);
74    Ok(tx.create(Entity::new("IFCREVOLVEDAREASOLID", attrs)))
75}
76
77/// Stage an `IfcBooleanResult`.
78///
79/// The operation is **recorded, not executed**. The file states the operands
80/// and the operator; evaluating them is a kernel concern belonging to a
81/// consumer, so any kernel -- CGAL, OCCT, Axiolid -- can resolve the same
82/// authored record later.
83///
84/// Operand order matters for `DIFFERENCE`: `first` minus `second`. The
85/// operator type carries that distinction, so it is not restated here.
86pub fn boolean_result(
87    tx: &mut Transaction,
88    operator: IfcBooleanOperator,
89    first: EntityId,
90    second: EntityId,
91) -> Result<EntityId, GeometryError> {
92    if first == second {
93        return Err(invalid(
94            "IFCBOOLEANRESULT",
95            "SecondOperand",
96            "both operands are the same entity",
97        ));
98    }
99    let mut attrs = vec![Value::Null; 3];
100    attrs[boolean_slot::OPERATOR] = Value::Enum(operator.as_token().into());
101    attrs[boolean_slot::FIRST_OPERAND] = Value::Ref(first);
102    attrs[boolean_slot::SECOND_OPERAND] = Value::Ref(second);
103    Ok(tx.create(Entity::new("IFCBOOLEANRESULT", attrs)))
104}