lib3mf 0.1.6

Pure Rust implementation for 3MF (3D Manufacturing Format) parsing and writing
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
//! Slice extension parsing
//!
//! This module handles loading and parsing of 3MF Slice extension elements
//! including slice references and external slice files.

use crate::error::{Error, Result};
use crate::model::*;
use crate::opc::Package;
use quick_xml::Reader;
use std::io::Read;

use super::{load_file_with_decryption, parse_attributes, parse_model_xml_with_config};

/// Load external slice files referenced by SliceRef elements
///
/// This function processes all SliceRef elements in the model, loading the
/// external slice files and merging their content into the main model.
pub(super) fn load_slice_references<R: Read + std::io::Seek>(
    package: &mut Package<R>,
    model: &mut Model,
    config: &ParserConfig,
) -> Result<()> {
    // Type alias for complex nested vector to improve readability
    type SliceRefInfo = (String, String, usize);
    type StackSliceRefs = (usize, Vec<SliceRefInfo>);

    // Collect information needed for loading before we start mutating
    let mut slices_to_load: Vec<StackSliceRefs> = Vec::new();

    for (stack_idx, slice_stack) in model.resources.slice_stacks.iter().enumerate() {
        let mut refs_for_stack = Vec::new();

        // Rule: SliceStack must contain either slices OR slicerefs, not both
        // Per 3MF Slice Extension spec, a slicestack MUST contain either <slice> elements
        // or <sliceref> elements, but MUST NOT contain both element types concurrently.
        if !slice_stack.slices.is_empty() && !slice_stack.slice_refs.is_empty() {
            return Err(Error::InvalidModel(format!(
                "SliceStack {}: Contains both <slice> and <sliceref> elements.\n\
                 Per 3MF Slice Extension spec, a slicestack MUST contain either \
                 <slice> elements or <sliceref> elements, but MUST NOT contain both element types concurrently.",
                slice_stack.id
            )));
        }

        for slice_ref in &slice_stack.slice_refs {
            // Validate slicepath starts with /2D/
            // Per 3MF Slice Extension spec: "For package readability and organization,
            // slice models SHOULD be stored in the 2D folder UNLESS they are part of
            // the root model part."
            // We enforce this as a MUST for SliceRef elements (external slice references)
            // to catch packaging errors. SliceRef elements by definition reference external
            // files and must use the /2D/ folder per spec conventions.
            if !slice_ref.slicepath.starts_with("/2D/") {
                return Err(Error::InvalidModel(format!(
                    "SliceStack {}: SliceRef references invalid path '{}'.\n\
                     Per 3MF Slice Extension spec, external slice models must be stored in the /2D/ folder. \
                     Slicepath must start with '/2D/'.",
                    slice_stack.id, slice_ref.slicepath
                )));
            }

            let normalized_path = if slice_ref.slicepath.starts_with('/') {
                slice_ref.slicepath[1..].to_string()
            } else {
                slice_ref.slicepath.clone()
            };
            refs_for_stack.push((
                normalized_path,
                slice_ref.slicepath.clone(),
                slice_ref.slicestackid,
            ));
        }
        if !refs_for_stack.is_empty() {
            slices_to_load.push((stack_idx, refs_for_stack));
        }
    }

    // Now load and process each slice reference
    for (stack_idx, refs) in slices_to_load {
        for (normalized_path, display_path, expected_stack_id) in refs {
            // Load the slice file from the package (decrypt if encrypted)
            let slice_xml =
                load_file_with_decryption(package, &normalized_path, &display_path, model, config)?;

            // Parse the slice file to extract slices and objects
            let (slices, objects) = parse_slice_file_with_objects(&slice_xml, expected_stack_id)?;

            // Add the slices to this slice stack
            model.resources.slice_stacks[stack_idx]
                .slices
                .extend(slices);

            // Merge objects from the external file into the main model
            model.resources.objects.extend(objects);
        }

        // Clear the slice_refs for this stack
        model.resources.slice_stacks[stack_idx].slice_refs.clear();
    }

    Ok(())
}

/// Parse a slice model file and extract both slices and objects
///
/// This parses a referenced slice file (typically in the 2D/ directory) and
/// extracts all slice data including vertices, polygons, and segments, as well as
/// any object definitions that may be present in the file.
///
/// Note: External slice files may have empty or incomplete structures (e.g., empty
/// build sections), so we parse them and skip validation.
fn parse_slice_file_with_objects(
    xml: &str,
    expected_stack_id: usize,
) -> Result<(Vec<Slice>, Vec<Object>)> {
    // Parse the entire model XML
    // Note: We use all extensions here because external slice files are part of the same
    // 3MF package and should be parsed with the same extension support as the main model.
    // The 3MF spec requires that all files in a package share the same extension context.
    let mut external_model = parse_model_xml_with_config(xml, ParserConfig::with_all_extensions())?;

    // Collect available IDs first (before mutable borrow)
    let available_ids: Vec<usize> = external_model
        .resources
        .slice_stacks
        .iter()
        .map(|s| s.id)
        .collect();

    // Find the slice stack with the expected ID
    let stack_option = external_model
        .resources
        .slice_stacks
        .iter_mut()
        .find(|stack| stack.id == expected_stack_id);

    let stack = match stack_option {
        Some(s) => s,
        None => {
            return Err(Error::InvalidModel(format!(
                "SliceRef references non-existent slicestackid {}.\n\
                 Per 3MF Slice Extension spec, the slicestackid attribute in a <sliceref> element \
                 must reference a valid <slicestack> defined in the external slice file.\n\
                 Available slicestack IDs in external file: {:?}",
                expected_stack_id, available_ids
            )));
        }
    };

    // N_SPX_1606_01: Validate slices against the external slicestack's zbottom
    // before extracting them
    let zbottom = stack.zbottom;
    for (slice_idx, slice) in stack.slices.iter().enumerate() {
        if slice.ztop < zbottom {
            return Err(Error::InvalidModel(format!(
                "External SliceStack {}: Slice {} has ztop={} which is less than zbottom={}.\n\
                 Per 3MF Slice Extension spec, each slice's ztop must be >= the slicestack's zbottom.",
                expected_stack_id, slice_idx, slice.ztop, zbottom
            )));
        }
    }

    // Extract slices
    let slices = std::mem::take(&mut stack.slices);

    // Extract all objects from the external model
    let objects = std::mem::take(&mut external_model.resources.objects);

    Ok((slices, objects))
}

/// Parse slicestack start and return initialized stack
pub(super) fn parse_slicestack_start<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<SliceStack> {
    let attrs = parse_attributes(reader, e)?;
    let id = attrs
        .get("id")
        .ok_or_else(|| Error::InvalidXml("SliceStack missing id attribute".to_string()))?
        .parse::<usize>()?;
    let zbottom = attrs
        .get("zbottom")
        .ok_or_else(|| Error::InvalidXml("SliceStack missing zbottom attribute".to_string()))?
        .parse::<f64>()?;
    Ok(SliceStack::new(id, zbottom))
}

/// Parse slice start and return initialized slice
pub(super) fn parse_slice_start<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<Slice> {
    let attrs = parse_attributes(reader, e)?;
    let ztop = attrs
        .get("ztop")
        .ok_or_else(|| Error::InvalidXml("Slice missing ztop attribute".to_string()))?
        .parse::<f64>()?;
    Ok(Slice::new(ztop))
}

/// Parse sliceref element
pub(super) fn parse_sliceref<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<SliceRef> {
    let attrs = parse_attributes(reader, e)?;
    let slicestackid = attrs
        .get("slicestackid")
        .ok_or_else(|| Error::InvalidXml("SliceRef missing slicestackid attribute".to_string()))?
        .parse::<usize>()?;
    let slicepath = attrs
        .get("slicepath")
        .ok_or_else(|| Error::InvalidXml("SliceRef missing slicepath attribute".to_string()))?
        .to_string();
    Ok(SliceRef::new(slicestackid, slicepath))
}

/// Parse slice vertex (2D vertex)
pub(super) fn parse_slice_vertex<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<Vertex2D> {
    let attrs = parse_attributes(reader, e)?;
    let x = attrs
        .get("x")
        .ok_or_else(|| Error::InvalidXml("Slice vertex missing x attribute".to_string()))?
        .parse::<f64>()?;
    let y = attrs
        .get("y")
        .ok_or_else(|| Error::InvalidXml("Slice vertex missing y attribute".to_string()))?
        .parse::<f64>()?;
    Ok(Vertex2D::new(x, y))
}

/// Parse polygon start and return initialized polygon
pub(super) fn parse_slice_polygon_start<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<SlicePolygon> {
    let attrs = parse_attributes(reader, e)?;
    let startv = attrs
        .get("startv")
        .ok_or_else(|| Error::InvalidXml("Slice polygon missing startv attribute".to_string()))?
        .parse::<usize>()?;
    Ok(SlicePolygon::new(startv))
}

/// Parse segment element
pub(super) fn parse_slice_segment<R: std::io::BufRead>(
    reader: &Reader<R>,
    e: &quick_xml::events::BytesStart,
) -> Result<SliceSegment> {
    let attrs = parse_attributes(reader, e)?;
    let v2 = attrs
        .get("v2")
        .ok_or_else(|| Error::InvalidXml("Slice segment missing v2 attribute".to_string()))?
        .parse::<usize>()?;
    Ok(SliceSegment::new(v2))
}

#[cfg(test)]
mod tests {
    use crate::parser::parse_model_xml;

    const CORE_NS: &str = r#"xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02""#;

    fn model_with_slicestack(slicestack_body: &str, objects: &str, build: &str) -> String {
        format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<model unit="millimeter" {ns}>
  <resources>
    {slicestack}
    {objects}
  </resources>
  <build>
    {build}
  </build>
</model>"#,
            ns = CORE_NS,
            slicestack = slicestack_body,
            objects = objects,
            build = build,
        )
    }

    #[test]
    fn test_parse_slicestack_basic() {
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.5">
      <slice ztop="1.0"/>
    </slicestack>"#,
            r#"<object id="1">
      <mesh>
        <vertices>
          <vertex x="0" y="0" z="0"/>
          <vertex x="1" y="0" z="0"/>
          <vertex x="0" y="1" z="0"/>
        </vertices>
        <triangles>
          <triangle v1="0" v2="1" v3="2"/>
        </triangles>
      </mesh>
    </object>"#,
            r#"<item objectid="1"/>"#,
        );
        let model = parse_model_xml(&xml).unwrap();
        assert_eq!(model.resources.slice_stacks.len(), 1);
        assert_eq!(model.resources.slice_stacks[0].id, 10);
        assert_eq!(model.resources.slice_stacks[0].zbottom, 0.5);
        assert_eq!(model.resources.slice_stacks[0].slices.len(), 1);
    }

    #[test]
    fn test_parse_sliceref_missing_slicestackid_rejected() {
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <sliceref slicepath="/2D/ext.model"/>
    </slicestack>"#,
            r#"<object id="1">
      <mesh>
        <vertices>
          <vertex x="0" y="0" z="0"/>
          <vertex x="1" y="0" z="0"/>
          <vertex x="0" y="1" z="0"/>
        </vertices>
        <triangles>
          <triangle v1="0" v2="1" v3="2"/>
        </triangles>
      </mesh>
    </object>"#,
            r#"<item objectid="1"/>"#,
        );
        let result = parse_model_xml(&xml);
        // parsing sliceref with missing slicestackid fails
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("slicestackid"));
    }

    #[test]
    fn test_parse_sliceref_missing_slicepath_rejected() {
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <sliceref slicestackid="5"/>
    </slicestack>"#,
            r#"<object id="1">
      <mesh>
        <vertices>
          <vertex x="0" y="0" z="0"/>
          <vertex x="1" y="0" z="0"/>
          <vertex x="0" y="1" z="0"/>
        </vertices>
        <triangles>
          <triangle v1="0" v2="1" v3="2"/>
        </triangles>
      </mesh>
    </object>"#,
            r#"<item objectid="1"/>"#,
        );
        let result = parse_model_xml(&xml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("slicepath"));
    }

    #[test]
    fn test_parse_slice_vertex_missing_x_rejected() {
        // Empty objects and build sections: these tests focus on slicestack parsing errors
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <slice ztop="1.0">
        <vertices>
          <vertex y="1"/>
        </vertices>
      </slice>
    </slicestack>"#,
            "",
            "",
        );
        let result = parse_model_xml(&xml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("x"));
    }

    #[test]
    fn test_parse_slice_vertex_missing_y_rejected() {
        // Empty objects and build sections: these tests focus on slicestack parsing errors
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <slice ztop="1.0">
        <vertices>
          <vertex x="1"/>
        </vertices>
      </slice>
    </slicestack>"#,
            "",
            "",
        );
        let result = parse_model_xml(&xml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("y"));
    }

    #[test]
    fn test_parse_slice_polygon_missing_startv_rejected() {
        // Empty objects and build sections: these tests focus on slicestack parsing errors
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <slice ztop="1.0">
        <vertices>
          <vertex x="0" y="0"/>
          <vertex x="1" y="0"/>
        </vertices>
        <polygon>
          <segment v2="1"/>
        </polygon>
      </slice>
    </slicestack>"#,
            "",
            "",
        );
        let result = parse_model_xml(&xml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("startv"));
    }

    #[test]
    fn test_parse_slice_segment_missing_v2_rejected() {
        // Empty objects and build sections: these tests focus on slicestack parsing errors
        let xml = model_with_slicestack(
            r#"<slicestack id="10" zbottom="0.0">
      <slice ztop="1.0">
        <vertices>
          <vertex x="0" y="0"/>
          <vertex x="1" y="0"/>
        </vertices>
        <polygon startv="0">
          <segment/>
        </polygon>
      </slice>
    </slicestack>"#,
            "",
            "",
        );
        let result = parse_model_xml(&xml);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("v2"));
    }
}