Skip to main content

ifc_style/
assignment.rs

1//! Style assignment, item binding, and deterministic cascade resolution.
2//!
3//! IFC2x3 `IfcPresentationStyleAssignment` wrappers are projected explicitly
4//! and flattened only by the resolver. IFC4/IFC4X3 direct style selections keep
5//! their original IDs.
6//!
7//! A unique direct item assignment wins over layer styles. Multiple direct
8//! assignments are an ambiguity error; layer candidates remain inspectable.
9//! [`StyleIndex`] answers the surface colour of an item and, as the last
10//! fallback, of a material (#449), and the cut fill of a material (#597).
11
12use ifc_model::{EntityId, Value};
13
14use crate::error::{StyleError, StyleResult};
15use crate::view::Record;
16
17mod colour;
18mod fill;
19mod layer;
20mod resolution;
21mod styled_item;
22
23pub use colour::{ColourSource, StyleIndex, SurfaceColour};
24pub use layer::PresentationStyleAssignment;
25pub use resolution::{ResolvedStyle, StyleSource};
26pub use styled_item::StyledItem;
27
28/// One member of IFC2x3/IFC4's legacy presentation-style select.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum PresentationStyleMember {
31    /// A reference to an `IfcPresentationStyle`.
32    Style(EntityId),
33    /// The `IfcNullStyle.NULL` sentinel: this slot deliberately carries no style.
34    Null,
35}
36
37pub(crate) fn presentation_style_members(
38    record: Record<'_, '_>,
39    attribute: &'static str,
40    minimum: usize,
41) -> StyleResult<Vec<PresentationStyleMember>> {
42    let value = record.value(attribute)?;
43    let Value::List(items) = value.unwrap_typed() else {
44        return Err(record.invalid(attribute, value));
45    };
46    if items.len() < minimum {
47        return Err(StyleError::InvalidValue {
48            entity: record.entity.type_name.to_string(),
49            id: record.id,
50            attribute,
51            value: format!("{} member(s), expected at least {minimum}", items.len()),
52        });
53    }
54
55    items
56        .iter()
57        .map(|item| match item {
58            Value::Ref(target) => {
59                record.check_reference(*target, "IfcPresentationStyle")?;
60                Ok(PresentationStyleMember::Style(*target))
61            }
62            Value::Typed { type_name, value }
63                if type_name.eq_ignore_ascii_case("IFCNULLSTYLE")
64                    && matches!(value.as_ref(), Value::Enum(member) if member.eq_ignore_ascii_case("NULL")) =>
65            {
66                Ok(PresentationStyleMember::Null)
67            }
68            _ => Err(record.invalid(attribute, item)),
69        })
70        .collect()
71}