rvt 0.4.0

Apache-2.0 clean-room reader for Autodesk Revit files (.rvt, .rfa, .rte, .rft) without Revit: metadata, previews and the embedded schema of 2016-2026 files, and typed elements with measured geometry from Revit 2024 and 2025 projects, exported as IFC4, glTF and plan SVG.
Documentation
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! `Door` + `Window` — wall-hosted opening elements. Both are
//! `FamilyInstance` subtypes at the class hierarchy level but ship
//! with enough stable scalar fields to decode directly without first
//! resolving the full family graph (which is task L5B-21).
//!
//! Each element references:
//! - A **host** (the wall it cuts through), via `m_host_id`
//! - A **symbol/type** (the family type definition), via `m_symbol_id`
//! - A **level**, inherited from the host wall's level
//! - A **location point** with a rotation angle
//!
//! Plus element-level overrides: `m_sill_height` for windows,
//! `m_flip_hand` and `m_flip_facing` for doors (left-swing vs
//! right-swing, interior vs exterior).
//!
//! # Typical Revit field shape (stable 2016–2026)
//!
//! Door:
//!
//! | Field | Type | Semantics |
//! |---|---|---|
//! | `m_level_id` | ElementId | Host level |
//! | `m_host_id` | ElementId | Wall (or other host) the door is cut into |
//! | `m_symbol_id` | ElementId | FamilySymbol (door type: "Single-Flush 36\" x 84\"") |
//! | `m_location_x`, `m_location_y`, `m_location_z` | f64 | Door location in project coords |
//! | `m_rotation` | f64 | Rotation angle in radians (usually aligned to host wall) |
//! | `m_flip_hand` | bool | Hinge on opposite side |
//! | `m_flip_facing` | bool | Swings into opposite space |
//!
//! Window:
//!
//! | Field | Type | Semantics |
//! |---|---|---|
//! | `m_level_id` | ElementId | Host level |
//! | `m_host_id` | ElementId | Wall the window sits in |
//! | `m_symbol_id` | ElementId | FamilySymbol |
//! | `m_location_x`, `m_location_y`, `m_location_z` | f64 | Centre of the window in project coords |
//! | `m_rotation` | f64 | Rotation radians |
//! | `m_sill_height` | f64 | Height of windowsill above host level (feet) |

use super::level::normalise_field_name;
use crate::formats;
use crate::geometry::Point3;
use crate::walker::{DecodedElement, ElementDecoder, HandleIndex, InstanceField};
use crate::{Error, Result};

/// Registered decoder for the `Door` class.
pub struct DoorDecoder;

impl ElementDecoder for DoorDecoder {
    fn class_name(&self) -> &'static str {
        "Door"
    }

    fn decode(
        &self,
        bytes: &[u8],
        schema: &formats::ClassEntry,
        _index: &HandleIndex,
    ) -> Result<DecodedElement> {
        if schema.name != "Door" {
            return Err(Error::BasicFileInfo(format!(
                "DoorDecoder received wrong schema: {}",
                schema.name
            )));
        }
        Ok(crate::walker::decode_instance(bytes, 0, schema))
    }
}

/// Registered decoder for the `Window` class.
pub struct WindowDecoder;

impl ElementDecoder for WindowDecoder {
    fn class_name(&self) -> &'static str {
        "Window"
    }

    fn decode(
        &self,
        bytes: &[u8],
        schema: &formats::ClassEntry,
        _index: &HandleIndex,
    ) -> Result<DecodedElement> {
        if schema.name != "Window" {
            return Err(Error::BasicFileInfo(format!(
                "WindowDecoder received wrong schema: {}",
                schema.name
            )));
        }
        Ok(crate::walker::decode_instance(bytes, 0, schema))
    }
}

/// Fields shared by every wall-hosted opening element (Door,
/// Window, and future Opening). Gathered in one pass so neither
/// `Door::from_decoded` nor `Window::from_decoded` has to re-scan.
#[derive(Debug, Clone, Copy, Default)]
struct OpeningCommon {
    location: Option<Point3>,
    rotation_radians: Option<f64>,
    level_id: Option<u32>,
    host_id: Option<u32>,
    symbol_id: Option<u32>,
}

fn collect_common(decoded: &DecodedElement) -> OpeningCommon {
    let mut lx = None;
    let mut ly = None;
    let mut lz = None;
    let mut out = OpeningCommon::default();
    for (field_name, value) in &decoded.fields {
        match (normalise_field_name(field_name).as_str(), value) {
            ("locationx", InstanceField::Float { value, .. }) => lx = Some(*value),
            ("locationy", InstanceField::Float { value, .. }) => ly = Some(*value),
            ("locationz", InstanceField::Float { value, .. }) => lz = Some(*value),
            ("rotation", InstanceField::Float { value, .. }) => {
                out.rotation_radians = Some(*value);
            }
            ("levelid" | "hostlevelid", InstanceField::ElementId { id, .. }) => {
                out.level_id = Some(*id);
            }
            ("hostid", InstanceField::ElementId { id, .. }) => out.host_id = Some(*id),
            ("symbolid" | "typeid" | "familysymbolid", InstanceField::ElementId { id, .. }) => {
                out.symbol_id = Some(*id);
            }
            _ => {}
        }
    }
    if let (Some(x), Some(y), Some(z)) = (lx, ly, lz) {
        out.location = Some(Point3::new(x, y, z));
    }
    out
}

/// Typed view of a decoded Door.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Door {
    pub level_id: Option<u32>,
    pub host_id: Option<u32>,
    pub symbol_id: Option<u32>,
    pub location: Option<Point3>,
    pub rotation_radians: Option<f64>,
    pub flip_hand: Option<bool>,
    pub flip_facing: Option<bool>,
}

impl Door {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let c = collect_common(decoded);
        let mut out = Self {
            level_id: c.level_id,
            host_id: c.host_id,
            symbol_id: c.symbol_id,
            location: c.location,
            rotation_radians: c.rotation_radians,
            flip_hand: None,
            flip_facing: None,
        };
        for (field_name, value) in &decoded.fields {
            match (normalise_field_name(field_name).as_str(), value) {
                ("fliphand", InstanceField::Bool(b)) => out.flip_hand = Some(*b),
                ("flipfacing", InstanceField::Bool(b)) => out.flip_facing = Some(*b),
                _ => {}
            }
        }
        out
    }

    /// True when the door has been flipped from its family-default
    /// orientation (either hand or facing).
    pub fn is_flipped(&self) -> Option<bool> {
        match (self.flip_hand, self.flip_facing) {
            (Some(h), Some(f)) => Some(h ^ f),
            (Some(h), None) => Some(h),
            (None, Some(f)) => Some(f),
            (None, None) => None,
        }
    }
}

/// Typed view of a decoded Window.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Window {
    pub level_id: Option<u32>,
    pub host_id: Option<u32>,
    pub symbol_id: Option<u32>,
    pub location: Option<Point3>,
    pub rotation_radians: Option<f64>,
    /// Distance from host level's elevation up to the windowsill.
    pub sill_height_feet: Option<f64>,
}

impl Window {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let c = collect_common(decoded);
        let mut out = Self {
            level_id: c.level_id,
            host_id: c.host_id,
            symbol_id: c.symbol_id,
            location: c.location,
            rotation_radians: c.rotation_radians,
            sill_height_feet: None,
        };
        for (field_name, value) in &decoded.fields {
            if let ("sillheight", InstanceField::Float { value, .. }) =
                (normalise_field_name(field_name).as_str(), value)
            {
                out.sill_height_feet = Some(*value);
            }
        }
        out
    }

    /// Sill height in inches — convenience for US-customary callers.
    pub fn sill_height_inches(&self) -> Option<f64> {
        self.sill_height_feet.map(|ft| ft * 12.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::formats::{ClassEntry, FieldEntry, FieldType};

    fn synth_door_schema() -> ClassEntry {
        let f64_prim = FieldType::Primitive {
            kind: 0x07,
            size: 8,
        };
        ClassEntry {
            name: "Door".into(),
            offset: 0,
            fields: vec![
                FieldEntry {
                    name: "m_level_id".into(),
                    cpp_type: None,
                    field_type: Some(FieldType::ElementId),
                },
                FieldEntry {
                    name: "m_host_id".into(),
                    cpp_type: None,
                    field_type: Some(FieldType::ElementId),
                },
                FieldEntry {
                    name: "m_symbol_id".into(),
                    cpp_type: None,
                    field_type: Some(FieldType::ElementId),
                },
                FieldEntry {
                    name: "m_location_x".into(),
                    cpp_type: None,
                    field_type: Some(f64_prim.clone()),
                },
                FieldEntry {
                    name: "m_location_y".into(),
                    cpp_type: None,
                    field_type: Some(f64_prim.clone()),
                },
                FieldEntry {
                    name: "m_location_z".into(),
                    cpp_type: None,
                    field_type: Some(f64_prim.clone()),
                },
                FieldEntry {
                    name: "m_rotation".into(),
                    cpp_type: None,
                    field_type: Some(f64_prim),
                },
                FieldEntry {
                    name: "m_flip_hand".into(),
                    cpp_type: None,
                    field_type: Some(FieldType::Primitive {
                        kind: 0x01,
                        size: 1,
                    }),
                },
                FieldEntry {
                    name: "m_flip_facing".into(),
                    cpp_type: None,
                    field_type: Some(FieldType::Primitive {
                        kind: 0x01,
                        size: 1,
                    }),
                },
            ],
            tag: Some(1),
            parent: None,
            declared_field_count: Some(9),
            was_parent_only: false,
            ancestor_tag: None,
        }
    }

    fn synth_door_bytes() -> Vec<u8> {
        let mut b = Vec::new();
        // m_level_id, m_host_id, m_symbol_id
        for id in [1_u32, 42_u32, 101_u32] {
            b.extend_from_slice(&0u32.to_le_bytes());
            b.extend_from_slice(&id.to_le_bytes());
        }
        // m_location = (5.0, 10.0, 0.0)
        for v in [5.0_f64, 10.0, 0.0] {
            b.extend_from_slice(&v.to_le_bytes());
        }
        // m_rotation = 0.0
        b.extend_from_slice(&0.0_f64.to_le_bytes());
        // m_flip_hand = true, m_flip_facing = false
        b.push(1);
        b.push(0);
        b
    }

    #[test]
    fn door_decoder_rejects_wrong_schema() {
        let wrong = ClassEntry {
            name: "Window".into(),
            ..synth_door_schema()
        };
        assert!(
            DoorDecoder
                .decode(&[], &wrong, &HandleIndex::new())
                .is_err()
        );
    }

    #[test]
    fn door_decodes_and_tracks_flips() {
        let decoded = DoorDecoder
            .decode(
                &synth_door_bytes(),
                &synth_door_schema(),
                &HandleIndex::new(),
            )
            .unwrap();
        let d = Door::from_decoded(&decoded);
        assert_eq!(d.level_id, Some(1));
        assert_eq!(d.host_id, Some(42));
        assert_eq!(d.symbol_id, Some(101));
        assert_eq!(d.location, Some(Point3::new(5.0, 10.0, 0.0)));
        assert_eq!(d.rotation_radians, Some(0.0));
        assert_eq!(d.flip_hand, Some(true));
        assert_eq!(d.flip_facing, Some(false));
        assert_eq!(d.is_flipped(), Some(true));
    }

    #[test]
    fn door_is_flipped_combinations() {
        // Both flipped: double negative → not flipped.
        let both = Door {
            flip_hand: Some(true),
            flip_facing: Some(true),
            ..Default::default()
        };
        assert_eq!(both.is_flipped(), Some(false));
        // Neither flag set.
        assert_eq!(Door::default().is_flipped(), None);
    }

    #[test]
    fn window_decoder_rejects_wrong_schema() {
        let wrong = ClassEntry {
            name: "Door".into(),
            offset: 0,
            fields: vec![],
            tag: None,
            parent: None,
            declared_field_count: None,
            was_parent_only: false,
            ancestor_tag: None,
        };
        assert!(
            WindowDecoder
                .decode(&[], &wrong, &HandleIndex::new())
                .is_err()
        );
    }

    #[test]
    fn door_and_window_tolerate_empty_bytes() {
        // Short / empty input must not panic — decode_instance yields
        // truncated/missing fields rather than inventing values.
        let door_schema = synth_door_schema();
        let door = DoorDecoder
            .decode(&[], &door_schema, &HandleIndex::new())
            .expect("empty bytes still return DecodedElement");
        assert_eq!(door.class, "Door");
        let window_schema = ClassEntry {
            name: "Window".into(),
            ..door_schema
        };
        let window = WindowDecoder
            .decode(&[], &window_schema, &HandleIndex::new())
            .expect("empty bytes still return DecodedElement");
        assert_eq!(window.class, "Window");
    }

    #[test]
    fn window_collects_location_and_sill() {
        let empty = DecodedElement {
            id: None,
            class: "Window".into(),
            fields: vec![
                (
                    "m_location_x".into(),
                    InstanceField::Float {
                        value: 1.0,
                        size: 8,
                    },
                ),
                (
                    "m_location_y".into(),
                    InstanceField::Float {
                        value: 2.0,
                        size: 8,
                    },
                ),
                (
                    "m_location_z".into(),
                    InstanceField::Float {
                        value: 0.0,
                        size: 8,
                    },
                ),
                (
                    "m_sill_height".into(),
                    InstanceField::Float {
                        value: 2.5,
                        size: 8,
                    },
                ),
            ],
            byte_range: 0..0,
            provenance: Default::default(),
        };
        let w = Window::from_decoded(&empty);
        assert_eq!(w.location, Some(Point3::new(1.0, 2.0, 0.0)));
        assert_eq!(w.sill_height_feet, Some(2.5));
        assert_eq!(w.sill_height_inches(), Some(30.0));
    }

    #[test]
    fn window_tolerates_missing_fields() {
        let empty = DecodedElement {
            id: None,
            class: "Window".into(),
            fields: vec![],
            byte_range: 0..0,
            provenance: Default::default(),
        };
        let w = Window::from_decoded(&empty);
        assert!(w.location.is_none());
        assert!(w.sill_height_feet.is_none());
        assert!(w.rotation_radians.is_none());
    }

    #[test]
    fn class_names() {
        assert_eq!(DoorDecoder.class_name(), "Door");
        assert_eq!(WindowDecoder.class_name(), "Window");
    }
}