ifc_rs/objects/wall/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
mod deserialize;
mod serialize;

use std::ops::{Deref, DerefMut};

use ifc_rs_verify_derive::IfcVerify;

use crate::{
    id::{IdOr, TypedId},
    parser::{optional::OptionalParameter, string::StringPrimitive},
    prelude::*,
};

use super::StructureType;

/// The wall represents a vertical construction that may bound or
/// subdivide spaces. Wall are usually vertical, or nearly vertical,
/// planar elements, often designed to bear structural loads.
/// A wall is however not required to be load bearing.
///
/// https://standards.buildingsmart.org/IFC/DEV/IFC4_2/FINAL/HTML/link/ifcwall.htm
#[derive(IfcVerify)]
pub struct Wall {
    #[inherited]
    element: Element,

    /// Predefined generic type for a wall that is specified in an
    /// enumeration. There may be a property set given specifically
    /// for the predefined types.
    pub predefined_type: OptionalParameter<IdOr<WallType>>,
}

impl Wall {
    pub fn new(name: impl Into<StringPrimitive>) -> Self {
        Self {
            element: Element::new(Product::new(Object::new(Root::new(name.into())))),
            predefined_type: OptionalParameter::omitted(),
        }
    }

    pub fn predefined_type(
        mut self,
        predefined_type: impl Into<IdOr<WallType>>,
        ifc: &mut IFC,
    ) -> Self {
        let id_or: IdOr<_> = predefined_type.into();
        let id_or: IdOr<_> = id_or.or_insert(ifc).into();
        self.predefined_type = id_or.into();
        self
    }
}

impl RootBuilder for Wall {
    fn root_mut(&mut self) -> &mut Root {
        &mut self.element
    }
}

impl ObjectBuilder for Wall {
    fn object_mut(&mut self) -> &mut Object {
        &mut self.element
    }
}

impl ProductBuilder for Wall {
    fn product_mut(&mut self) -> &mut Product {
        &mut self.element
    }
}

impl ElementBuilder for Wall {
    fn element_mut(&mut self) -> &mut Element {
        &mut self.element
    }
}

impl Deref for Wall {
    type Target = Element;

    fn deref(&self) -> &Self::Target {
        &self.element
    }
}

impl DerefMut for Wall {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.element
    }
}

impl IfcType for Wall {
    fn to_structure(&self) -> Option<&dyn Structure> {
        Some(self)
    }
}
impl Structure for Wall {
    fn structure_type(&self) -> Option<StructureType<'_>> {
        Some(StructureType::Wall(self))
    }
}
impl MaterialRelatable for Wall {}

impl TransformableType for Wall {
    fn shape(&self) -> Option<TypedId<ProductDefinitionShape>> {
        self.representation.custom().cloned()
    }
}

#[cfg(test)]
pub mod test {
    use bevy_math::DVec3;
    use winnow::Parser;

    use crate::geometry::axis::Axis3D;
    use crate::geometry::local_placement::LocalPlacement;
    use crate::geometry::point::Point3D;
    use crate::geometry::product_definition_shape::test::new_product_definition_shape;
    use crate::geometry::product_definition_shape::ProductDefinitionShape;
    use crate::geometry::shape_representation::ShapeRepresentation;
    use crate::id::IdOr;
    use crate::objects::application::Application;
    use crate::objects::change_action::ChangeAction;
    use crate::objects::organization::Organization;
    use crate::objects::owner_history::OwnerHistory;
    use crate::objects::person::Person;
    use crate::objects::person_and_org::PersonAndOrganization;
    use crate::objects::shared::{product::ProductBuilder, root::RootBuilder};
    use crate::objects::wall::Wall;
    use crate::parser::timestamp::IfcTimestamp;
    use crate::parser::IFCParse;
    use crate::IFC;

    #[test]
    fn wall_round_trip() {
        let example = "IFCWALL('0DWgwt6o1FOx7466fPk$jl',#2,$,$,$,#33,#25,$,$);";

        let wall: Wall = Wall::parse().parse(example).unwrap();
        let str_wall = wall.to_string();

        assert_eq!(example, str_wall);
    }

    pub fn print_wall_hierarchy(ifc: &IFC) {
        use crate::objects::wall::Wall;

        for (_, wall) in ifc.data.find_all_of_type::<Wall>() {
            println!("wall: {wall}");

            if let Some(owner_history) = wall
                .owner_history
                .custom()
                .map(|&id| ifc.data.get_untyped(id))
            {
                println!("\towner_history: {owner_history}");
            }

            if let Some(id) = wall.object_placement.custom() {
                println!("\tpoint3d: {}", ifc.data.get_untyped(*id));
            }

            if let Some(representation) = wall
                .representation
                .custom()
                .map(|&id| ifc.data.get_untyped(id))
            {
                println!("\trepresentation: {representation}");

                for repr in representation
                    .downcast_ref::<ProductDefinitionShape>()
                    .unwrap()
                    .representations
                    .iter()
                {
                    let shape = ifc.data.get::<ShapeRepresentation>(*repr);
                    println!("\t\tshape_representation: {shape}");

                    let parent_context = shape.parent_context(ifc);

                    println!("\t\t\t\tcontext: {parent_context}");
                    println!(
                        "\t\t\t\t\tcoord_dims: {}",
                        parent_context.coord_space_dimension
                    );

                    let world_coord_system = ifc
                        .data
                        .get::<Axis3D>(parent_context.world_coord_system.into());

                    println!("\t\t\t\t\tworld_coord_system: {world_coord_system}");
                    println!(
                        "\t\t\t\t\t\tcoord_system_point: {}",
                        ifc.data.get_untyped(world_coord_system.location)
                    );

                    for (index, item) in shape.items(ifc).enumerate() {
                        println!("\t\t\titem {index}: {item}");
                    }
                }
            }

            if let Some(id_or) = wall.predefined_type.custom() {
                match id_or {
                    IdOr::Id(id) => println!("\twall_type: {}", ifc.data.get_untyped(*id)),
                    IdOr::Custom(wall_type) => println!("\twall_type: {}", wall_type),
                }
            }
        }
    }

    #[test]
    fn create_wall() {
        let mut ifc = IFC::default();

        let person = ifc.data.insert_new(Person::empty());
        let organization = ifc
            .data
            .insert_new(Organization::new(None, "organization_name", None));
        let person_and_org = ifc
            .data
            .insert_new(PersonAndOrganization::new(person, organization));
        let application = Application::new(organization, "0.0.1", "create_wall_test", "IFC4");
        let application_id = ifc.data.insert_new(application);

        let owner_history = OwnerHistory::new(ChangeAction::Added, IfcTimestamp::now())
            .owning_user(person_and_org, &mut ifc)
            .owning_application(application_id, &mut ifc);

        let axis = Axis3D::new(Point3D::from(DVec3::new(0.0, 0.0, 0.0)), &mut ifc);
        let axis_id = ifc.data.insert_new(axis);
        let local_placement = LocalPlacement::new(axis_id, &mut ifc);

        let representation = new_product_definition_shape(&mut ifc, axis_id.into());

        let wall = Wall::new("global_id_example")
            .owner_history(owner_history, &mut ifc)
            .object_placement(local_placement, &mut ifc)
            .representation(representation, &mut ifc);

        ifc.data.insert_new(wall);

        println!("{}", ifc.data);
        println!();
        print_wall_hierarchy(&ifc);
    }
}