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
463
464
465
466
467
468
469
470
471
472
473
474
//! `Phase` + `DesignOption` + `Workset` — the project-level
//! organization classes that slice the element set along non-spatial
//! axes. None of them map to a specific IFC4 entity; they surface as
//! property-set tags on the elements they group.
//!
//! - **Phase**: temporal classification. Elements are tagged with
//!   `phase_created` and `phase_demolished` so contractors can answer
//!   "what walls exist at phase 2?" without carrying two models.
//! - **DesignOption**: alternative-design classification. An element
//!   may exist in "Option A" but not "Option B" — when the user picks
//!   Option A as primary, B's elements are hidden.
//! - **Workset**: collaboration unit in workshared models — which
//!   team member "owns" a given element.
//!
//! # Typical field shape
//!
//! Phase:
//!
//! | Field | Type | Semantics |
//! |---|---|---|
//! | `m_name` | String | "Existing", "Phase 1", "New Construction" |
//! | `m_description` | String | Optional descriptive text |
//! | `m_sequence_number` | Primitive u32 | Ordering index (0 = earliest) |
//!
//! DesignOption:
//!
//! | Field | Type | Semantics |
//! |---|---|---|
//! | `m_name` | String | "Option 1", "Facade Study B" |
//! | `m_is_primary` | Primitive bool | True for the option shown by default |
//! | `m_option_set_id` | ElementId | Grouping set containing related options |
//!
//! Workset:
//!
//! | Field | Type | Semantics |
//! |---|---|---|
//! | `m_name` | String | "Architecture", "Mech/Plumb", "Shared Levels and Grids" |
//! | `m_unique_id` | String | Stable GUID for round-trip |
//! | `m_is_open` | Primitive bool | Whether the workset is loaded in the current view |
//! | `m_is_editable` | Primitive bool | Read-only vs writable |

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

macro_rules! simple_decoder {
    ($Struct:ident, $name:literal) => {
        pub struct $Struct;

        impl ElementDecoder for $Struct {
            fn class_name(&self) -> &'static str {
                $name
            }

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

simple_decoder!(PhaseDecoder, "Phase");
simple_decoder!(DesignOptionDecoder, "DesignOption");
simple_decoder!(WorksetDecoder, "Workset");
simple_decoder!(RevisionDecoder, "Revision");

/// Typed view for Phase.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Phase {
    pub name: Option<String>,
    pub description: Option<String>,
    pub sequence_number: Option<u32>,
}

impl Phase {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let mut out = Self::default();
        for (field_name, value) in &decoded.fields {
            match (normalise_field_name(field_name).as_str(), value) {
                ("name", InstanceField::String(s)) => out.name = Some(s.clone()),
                ("description", InstanceField::String(s)) => {
                    out.description = Some(s.clone());
                }
                ("sequencenumber" | "sequence", InstanceField::Integer { value, .. }) => {
                    out.sequence_number = Some(*value as u32);
                }
                _ => {}
            }
        }
        out
    }
}

/// Typed view for DesignOption.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct DesignOption {
    pub name: Option<String>,
    pub is_primary: Option<bool>,
    pub option_set_id: Option<u32>,
}

impl DesignOption {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let mut out = Self::default();
        for (field_name, value) in &decoded.fields {
            match (normalise_field_name(field_name).as_str(), value) {
                ("name", InstanceField::String(s)) => out.name = Some(s.clone()),
                ("isprimary" | "primary", InstanceField::Bool(b)) => {
                    out.is_primary = Some(*b);
                }
                ("optionsetid" | "setid", InstanceField::ElementId { id, .. }) => {
                    out.option_set_id = Some(*id);
                }
                _ => {}
            }
        }
        out
    }
}

/// Typed view for Workset.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Workset {
    pub name: Option<String>,
    pub unique_id: Option<String>,
    pub is_open: Option<bool>,
    pub is_editable: Option<bool>,
}

impl Workset {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let mut out = Self::default();
        for (field_name, value) in &decoded.fields {
            match (normalise_field_name(field_name).as_str(), value) {
                ("name", InstanceField::String(s)) => out.name = Some(s.clone()),
                ("uniqueid" | "guid", InstanceField::String(s)) => {
                    out.unique_id = Some(s.clone());
                }
                ("isopen", InstanceField::Bool(b)) => out.is_open = Some(*b),
                ("iseditable" | "editable", InstanceField::Bool(b)) => {
                    out.is_editable = Some(*b);
                }
                _ => {}
            }
        }
        out
    }

    /// True when the workset is both open and editable — the typical
    /// "I can modify this" combination in workshared Revit models.
    pub fn is_modifiable(&self) -> Option<bool> {
        Some(self.is_open? && self.is_editable?)
    }
}

/// Issued-status enum for a Revit drawing revision.
///
/// Revit's `RevisionVisibility` + `RevisionIssued` split into a single
/// simplified enum. `Draft` means the revision is still being edited;
/// `Issued` means it has been signed off and the revision table on the
/// sheet should lock it in place.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RevisionStatus {
    #[default]
    Draft,
    Issued,
    Other,
}

impl RevisionStatus {
    pub fn from_code(code: u32) -> Self {
        match code {
            0 => Self::Draft,
            1 => Self::Issued,
            _ => Self::Other,
        }
    }
}

/// Typed view of a decoded Revision.
///
/// Revisions are Revit's mechanism for tracking drawing-set updates
/// over a project's lifecycle. Each Revision gets a sequence number,
/// a date, a description, and an issued-to / issued-by pair. Sheets
/// display the active revisions in a revision schedule, and the
/// revision cloud tool annotates which regions of a drawing changed.
///
/// Typical fields (observed 2016–2026):
///
/// | Field | Type | Semantics |
/// |---|---|---|
/// | `m_sequence_number` | Primitive u32 | 1-indexed order; drives the default label. |
/// | `m_revision_date` | String | Free-form date, typically "YYYY-MM-DD" or "MM/DD/YY". |
/// | `m_description` | String | Human-readable summary, shown on the revision schedule. |
/// | `m_issued_to` | String | "Client", "Architect of Record", etc. |
/// | `m_issued_by` | String | Author / firm. |
/// | `m_status` | Primitive u32 | 0 = Draft, 1 = Issued. |
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Revision {
    pub sequence_number: Option<u32>,
    pub revision_date: Option<String>,
    pub description: Option<String>,
    pub issued_to: Option<String>,
    pub issued_by: Option<String>,
    pub status: Option<RevisionStatus>,
}

impl Revision {
    pub fn from_decoded(decoded: &DecodedElement) -> Self {
        let mut out = Self::default();
        for (field_name, value) in &decoded.fields {
            match (normalise_field_name(field_name).as_str(), value) {
                (
                    "sequencenumber" | "sequence" | "number",
                    InstanceField::Integer { value, .. },
                ) => {
                    out.sequence_number = Some(*value as u32);
                }
                ("revisiondate" | "date", InstanceField::String(s)) => {
                    out.revision_date = Some(s.clone());
                }
                ("description", InstanceField::String(s)) => {
                    out.description = Some(s.clone());
                }
                ("issuedto", InstanceField::String(s)) => {
                    out.issued_to = Some(s.clone());
                }
                ("issuedby", InstanceField::String(s)) => {
                    out.issued_by = Some(s.clone());
                }
                ("status" | "revisionstatus", InstanceField::Integer { value, .. }) => {
                    out.status = Some(RevisionStatus::from_code(*value as u32));
                }
                _ => {}
            }
        }
        out
    }

    /// Is this revision finalised (`Issued` status)?
    pub fn is_issued(&self) -> bool {
        matches!(self.status, Some(RevisionStatus::Issued))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

    #[test]
    fn phase_from_fields() {
        let fields = vec![
            (
                "m_name".into(),
                InstanceField::String("New Construction".into()),
            ),
            (
                "m_sequence_number".into(),
                InstanceField::Integer {
                    value: 1,
                    signed: false,
                    size: 4,
                },
            ),
        ];
        let decoded = DecodedElement {
            id: None,
            class: "Phase".into(),
            fields,
            byte_range: 0..0,
            provenance: Default::default(),
        };
        let p = Phase::from_decoded(&decoded);
        assert_eq!(p.name.as_deref(), Some("New Construction"));
        assert_eq!(p.sequence_number, Some(1));
    }

    #[test]
    fn design_option_primary_flag() {
        let primary = DesignOption {
            is_primary: Some(true),
            ..Default::default()
        };
        let alternative = DesignOption {
            is_primary: Some(false),
            ..Default::default()
        };
        assert_eq!(primary.is_primary, Some(true));
        assert_eq!(alternative.is_primary, Some(false));
    }

    #[test]
    fn workset_modifiability() {
        let modifiable = Workset {
            is_open: Some(true),
            is_editable: Some(true),
            ..Default::default()
        };
        let read_only = Workset {
            is_open: Some(true),
            is_editable: Some(false),
            ..Default::default()
        };
        let closed = Workset {
            is_open: Some(false),
            is_editable: Some(true),
            ..Default::default()
        };
        let unknown = Workset::default();
        assert_eq!(modifiable.is_modifiable(), Some(true));
        assert_eq!(read_only.is_modifiable(), Some(false));
        assert_eq!(closed.is_modifiable(), Some(false));
        assert_eq!(unknown.is_modifiable(), None);
    }

    #[test]
    fn workset_guid_preserves() {
        let fields = vec![
            (
                "m_unique_id".into(),
                InstanceField::String("abc-123-def-456".into()),
            ),
            ("m_is_open".into(), InstanceField::Bool(true)),
        ];
        let decoded = DecodedElement {
            id: None,
            class: "Workset".into(),
            fields,
            byte_range: 0..0,
            provenance: Default::default(),
        };
        let w = Workset::from_decoded(&decoded);
        assert_eq!(w.unique_id.as_deref(), Some("abc-123-def-456"));
        assert_eq!(w.is_open, Some(true));
    }

    #[test]
    fn empty_tolerance() {
        let empty = DecodedElement {
            id: None,
            class: "Phase".into(),
            fields: vec![],
            byte_range: 0..0,
            provenance: Default::default(),
        };
        assert!(Phase::from_decoded(&empty).name.is_none());
        assert!(DesignOption::from_decoded(&empty).name.is_none());
        assert!(Workset::from_decoded(&empty).name.is_none());
    }

    #[test]
    fn class_names() {
        assert_eq!(PhaseDecoder.class_name(), "Phase");
        assert_eq!(DesignOptionDecoder.class_name(), "DesignOption");
        assert_eq!(WorksetDecoder.class_name(), "Workset");
        assert_eq!(RevisionDecoder.class_name(), "Revision");
    }

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

    #[test]
    fn revision_status_mapping() {
        assert_eq!(RevisionStatus::from_code(0), RevisionStatus::Draft);
        assert_eq!(RevisionStatus::from_code(1), RevisionStatus::Issued);
        assert_eq!(RevisionStatus::from_code(42), RevisionStatus::Other);
    }

    #[test]
    fn revision_from_decoded() {
        let fields = vec![
            (
                "m_sequence_number".into(),
                InstanceField::Integer {
                    value: 3,
                    signed: false,
                    size: 4,
                },
            ),
            (
                "m_revision_date".into(),
                InstanceField::String("2026-04-20".into()),
            ),
            (
                "m_description".into(),
                InstanceField::String("Permit set".into()),
            ),
            ("m_issued_to".into(), InstanceField::String("Client".into())),
            (
                "m_issued_by".into(),
                InstanceField::String("Architect of Record".into()),
            ),
            (
                "m_status".into(),
                InstanceField::Integer {
                    value: 1,
                    signed: false,
                    size: 4,
                },
            ),
        ];
        let decoded = DecodedElement {
            id: None,
            class: "Revision".into(),
            fields,
            byte_range: 0..0,
            provenance: Default::default(),
        };
        let r = Revision::from_decoded(&decoded);
        assert_eq!(r.sequence_number, Some(3));
        assert_eq!(r.revision_date.as_deref(), Some("2026-04-20"));
        assert_eq!(r.description.as_deref(), Some("Permit set"));
        assert_eq!(r.issued_to.as_deref(), Some("Client"));
        assert_eq!(r.issued_by.as_deref(), Some("Architect of Record"));
        assert_eq!(r.status, Some(RevisionStatus::Issued));
        assert!(r.is_issued());
    }

    #[test]
    fn revision_is_issued_when_unset_is_false() {
        let r = Revision::default();
        assert!(!r.is_issued());
    }
}