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
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
475
476
477
478
479
//! Boolean Operations extension validation
//!
//! This module handles validation of the Boolean Operations extension in 3MF files.
//! The Boolean Operations extension allows defining boolean operations (union, difference,
//! intersection) on mesh objects, including references to objects in external model files
//! within the same 3MF package.

use crate::error::{Error, Result};
use crate::model::{Model, ParserConfig};
use crate::opc::Package;
use quick_xml::Reader;
use quick_xml::events::Event;
use std::collections::{HashMap, HashSet};
use std::io::Read;

use super::secure_content::load_file_with_decryption;
use super::{get_local_name, parse_model_xml_with_config};

/// Maximum number of object IDs to display in error messages
const MAX_DISPLAYED_OBJECT_IDS: usize = 20;

/// Default buffer capacity for XML parsing (4KB)
const XML_BUFFER_CAPACITY: usize = 4096;

/// Validate boolean operations external paths
///
/// This function validates all external references in boolean shapes and their operands.
/// For each external reference (path + objectid), it ensures:
///
/// 1. The referenced file exists in the package
/// 2. The referenced object ID exists in that file (unless encrypted)
///
/// Special handling for encrypted files (Secure Content extension):
/// - Encrypted files cannot be parsed to validate object IDs
/// - We skip validation for encrypted files
///
/// # Arguments
///
/// * `package` - The 3MF package containing all files
/// * `model` - The parsed model with boolean operations
///
/// # Returns
///
/// * `Ok(())` if all external references are valid
/// * `Err` if any reference is invalid or points to a missing file/object
pub(super) fn validate_boolean_external_paths<R: Read + std::io::Seek>(
    package: &mut Package<R>,
    model: &Model,
    config: &ParserConfig,
) -> Result<()> {
    // Cache to avoid re-parsing the same external file multiple times
    let mut external_file_cache: HashMap<String, Vec<usize>> = HashMap::new();

    for object in &model.resources.objects {
        if let Some(ref boolean_shape) = object.boolean_shape {
            // Check if booleanshape references an external file
            if let Some(ref path) = boolean_shape.path {
                // Normalize path: remove leading slash if present
                let normalized_path = path.trim_start_matches('/');

                // Skip validation for encrypted files (Secure Content extension)
                // Encrypted files cannot be parsed, so we can't validate object IDs
                let is_encrypted = model
                    .secure_content
                    .as_ref()
                    .map(|sc| {
                        sc.encrypted_files.iter().any(|encrypted_path| {
                            // Compare normalized paths (both without leading slash)
                            let enc_normalized = encrypted_path.trim_start_matches('/');
                            enc_normalized == normalized_path
                        })
                    })
                    .unwrap_or(false);

                if is_encrypted {
                    // Skip validation for encrypted files - they can't be parsed
                    continue;
                }

                if !package.has_file(normalized_path) {
                    return Err(Error::InvalidModel(format!(
                        "Object {}: Boolean shape references non-existent external file: {}\n\
                         The path attribute in <booleanshape> must reference a valid model file in the 3MF package.\n\
                         Check that:\n\
                         - The file exists in the package\n\
                         - The path is correct (case-sensitive)\n\
                         - The path format follows 3MF conventions (e.g., /3D/filename.model)",
                        object.id, path
                    )));
                }

                // Validate that the referenced object ID exists in the external file
                validate_external_object_id(
                    package,
                    normalized_path,
                    boolean_shape.objectid,
                    object.id,
                    "booleanshape base",
                    &mut external_file_cache,
                    model,
                    config,
                )?;
            }

            // Check if boolean operands reference external files
            for operand in &boolean_shape.operands {
                if let Some(ref path) = operand.path {
                    // Normalize path: remove leading slash if present
                    let normalized_path = path.trim_start_matches('/');

                    // Skip validation for encrypted files (Secure Content extension)
                    // Encrypted files cannot be parsed, so we can't validate object IDs
                    let is_encrypted = model
                        .secure_content
                        .as_ref()
                        .map(|sc| {
                            sc.encrypted_files.iter().any(|encrypted_path| {
                                // Compare normalized paths (both without leading slash)
                                let enc_normalized = encrypted_path.trim_start_matches('/');
                                enc_normalized == normalized_path
                            })
                        })
                        .unwrap_or(false);

                    if is_encrypted {
                        // Skip validation for encrypted files - they can't be parsed
                        continue;
                    }

                    if !package.has_file(normalized_path) {
                        return Err(Error::InvalidModel(format!(
                            "Object {}: Boolean operand references non-existent external file: {}\n\
                             The path attribute in <boolean> must reference a valid model file in the 3MF package.\n\
                             Check that:\n\
                             - The file exists in the package\n\
                             - The path is correct (case-sensitive)\n\
                             - The path format follows 3MF conventions (e.g., /3D/filename.model)",
                            object.id, path
                        )));
                    }

                    // Validate that the referenced object ID exists in the external file
                    validate_external_object_id(
                        package,
                        normalized_path,
                        operand.objectid,
                        object.id,
                        "boolean operand",
                        &mut external_file_cache,
                        model,
                        config,
                    )?;
                }
            }
        }
    }

    Ok(())
}

/// Validate that an object ID exists in an external model file
///
/// Uses a cache to avoid re-parsing the same file multiple times
#[allow(clippy::too_many_arguments)]
fn validate_external_object_id<R: Read + std::io::Seek>(
    package: &mut Package<R>,
    file_path: &str,
    object_id: usize,
    referring_object_id: usize,
    reference_type: &str,
    cache: &mut HashMap<String, Vec<usize>>,
    model: &Model,
    config: &ParserConfig,
) -> Result<()> {
    // Check cache first and load if needed
    if !cache.contains_key(file_path) {
        // Load and parse the external model file (decrypt if encrypted)
        let external_xml = load_file_with_decryption(package, file_path, file_path, model, config)?;

        // Parse just enough to extract object IDs
        let mut reader = Reader::from_str(&external_xml);
        reader.config_mut().trim_text(true);

        let mut buf = Vec::with_capacity(XML_BUFFER_CAPACITY);
        let mut ids = Vec::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                    let name = e.name();
                    let name_str = std::str::from_utf8(name.as_ref())
                        .map_err(|e| Error::InvalidXml(e.to_string()))?;
                    let local_name = get_local_name(name_str);

                    if local_name == "object" {
                        // Extract the id attribute
                        for attr in e.attributes() {
                            let attr = attr.map_err(|e| Error::InvalidXml(e.to_string()))?;
                            let attr_name = std::str::from_utf8(attr.key.as_ref())
                                .map_err(|e| Error::InvalidXml(e.to_string()))?;

                            if attr_name == "id" {
                                let id_str = std::str::from_utf8(&attr.value)
                                    .map_err(|e| Error::InvalidXml(e.to_string()))?;
                                if let Ok(id) = id_str.parse::<usize>() {
                                    ids.push(id);
                                }
                            }
                        }
                    }
                }
                Ok(Event::Eof) => break,
                Err(e) => return Err(Error::Xml(e)),
                _ => {}
            }
            buf.clear();
        }

        // Cache the results for future use
        cache.insert(file_path.to_string(), ids);
    }

    // Get the cached object IDs
    let object_ids = cache.get(file_path).unwrap();

    // Check if the referenced object ID exists
    if !object_ids.contains(&object_id) {
        // Limit displayed IDs to avoid overwhelming error messages
        let display_ids: Vec<usize> = object_ids
            .iter()
            .take(MAX_DISPLAYED_OBJECT_IDS)
            .copied()
            .collect();
        let id_display = if object_ids.len() > MAX_DISPLAYED_OBJECT_IDS {
            format!("{:?} ... ({} total)", display_ids, object_ids.len())
        } else {
            format!("{:?}", display_ids)
        };

        return Err(Error::InvalidModel(format!(
            "Object {}: {} references object ID {} in external file '{}', but that object does not exist.\n\
             Available object IDs in external file: {}\n\
             Check that the referenced object ID is correct.",
            referring_object_id, reference_type, object_id, file_path, id_display
        )));
    }

    Ok(())
}

/// Validate that an object ID (and optionally UUID) exists in an external model file
///
/// Uses a cache to avoid re-parsing the same file multiple times
/// Cache stores: (object_id, optional_uuid)
#[allow(clippy::too_many_arguments)]
pub(super) fn validate_external_object_reference<R: Read + std::io::Seek>(
    package: &mut Package<R>,
    file_path: &str,
    object_id: usize,
    _expected_uuid: &Option<String>,
    reference_context: &str,
    cache: &mut HashMap<String, Vec<(usize, Option<String>)>>,
    model: &Model,
    config: &ParserConfig,
) -> Result<()> {
    // Check cache first and get object info
    if !cache.contains_key(file_path) {
        // Load and parse the external model file (decrypt if encrypted)
        let external_xml = load_file_with_decryption(package, file_path, file_path, model, config)?;

        // Parse to extract object IDs and UUIDs
        let mut reader = Reader::from_str(&external_xml);
        reader.config_mut().trim_text(true);

        let mut buf = Vec::with_capacity(XML_BUFFER_CAPACITY);
        let mut info = Vec::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                    let name = e.name();
                    let name_str = std::str::from_utf8(name.as_ref())
                        .map_err(|e| Error::InvalidXml(e.to_string()))?;
                    let local_name = get_local_name(name_str);

                    if local_name == "object" {
                        let mut obj_id = None;
                        let mut obj_uuid = None;

                        // Extract id and p:UUID attributes
                        for attr in e.attributes() {
                            let attr = attr.map_err(|e| Error::InvalidXml(e.to_string()))?;
                            let attr_name = std::str::from_utf8(attr.key.as_ref())
                                .map_err(|e| Error::InvalidXml(e.to_string()))?;

                            match attr_name {
                                "id" => {
                                    let id_str = std::str::from_utf8(&attr.value)
                                        .map_err(|e| Error::InvalidXml(e.to_string()))?;
                                    obj_id = id_str.parse::<usize>().ok();
                                }
                                "p:UUID" => {
                                    let uuid_str = std::str::from_utf8(&attr.value)
                                        .map_err(|e| Error::InvalidXml(e.to_string()))?;
                                    obj_uuid = Some(uuid_str.to_string());
                                }
                                _ => {}
                            }
                        }

                        if let Some(id) = obj_id {
                            info.push((id, obj_uuid));
                        }
                    } else if local_name == "component" {
                        // N_XPM_0803_01: Validate that non-root model files don't have components with p:path
                        // Per 3MF Production Extension spec Chapter 2:
                        // "Non-root model file components MUST only reference objects in the same model file"
                        // This prevents component reference chains across multiple files
                        for attr in e.attributes() {
                            let attr = attr.map_err(|e| Error::InvalidXml(e.to_string()))?;
                            let attr_name = std::str::from_utf8(attr.key.as_ref())
                                .map_err(|e| Error::InvalidXml(e.to_string()))?;

                            // Check for p:path attribute (standard production extension namespace)
                            // We check for the exact "p:path" attribute name
                            if attr_name == "p:path" {
                                let path_value = std::str::from_utf8(&attr.value)
                                    .map_err(|e| Error::InvalidXml(e.to_string()))?;
                                return Err(Error::InvalidModel(format!(
                                    "External model file '{}' contains a component with p:path=\"{}\". \
                                     Per 3MF Production Extension specification (Chapter 2), only components \
                                     in the root model file may have p:path attributes. Non-root model files \
                                     must only reference objects within the same file. This restriction \
                                     prevents component reference chains across multiple files.",
                                    file_path, path_value
                                )));
                            }
                        }
                    }
                }
                Ok(Event::Eof) => break,
                Err(e) => return Err(Error::Xml(e)),
                _ => {}
            }
            buf.clear();
        }

        // Cache the results for future use
        cache.insert(file_path.to_string(), info);
    }

    // Get the cached info
    let object_info = cache.get(file_path).unwrap();

    // Check if the referenced object ID exists
    let found_obj = object_info.iter().find(|(id, _)| *id == object_id);

    if found_obj.is_none() {
        // Object ID not found
        let available_ids: Vec<usize> = object_info
            .iter()
            .map(|(id, _)| *id)
            .take(MAX_DISPLAYED_OBJECT_IDS)
            .collect();
        let id_display = if object_info.len() > MAX_DISPLAYED_OBJECT_IDS {
            format!("{:?} ... ({} total)", available_ids, object_info.len())
        } else {
            format!("{:?}", available_ids)
        };

        return Err(Error::InvalidModel(format!(
            "{}: References object ID {} in external file '{}', but that object does not exist.\n\
             Available object IDs in external file: {}\n\
             Check that the referenced object ID is correct.",
            reference_context, object_id, file_path, id_display
        )));
    }

    // If we have an expected UUID, validate it matches
    // NOTE: Per official 3MF test suite (P_XXX_2203_04_Prod_Ext.3mf, P_OPX_3002_03_production.3mf),
    // UUID mismatches between component p:UUID and referenced object p:UUID are allowed.
    // The component's p:UUID is for identifying the component instance, not for matching
    // the referenced object's UUID. UUID validation is therefore commented out.
    /*
    if let Some(ref expected) = expected_uuid {
        if let Some((_, Some(ref actual_uuid))) = found_obj {
            if expected != actual_uuid {
                return Err(Error::InvalidModel(format!(
                    "{}: UUID mismatch for object {} in external file '{}'.\n\
                     Expected p:UUID='{}' but found p:UUID='{}'.\n\
                     UUIDs must match when referencing external objects.",
                    reference_context, object_id, file_path, expected, actual_uuid
                )));
            }
        }
    }
    */

    Ok(())
}

/// Validate an external model file's triangles for material property consistency
///
/// N_XXM_0601_02: External model files (non-root) must have proper material properties
/// When an object has some triangles with material properties and some without,
/// the object must have a default pid to provide material for unmaterialized triangles
pub(super) fn validate_external_model_triangles<R: Read + std::io::Seek>(
    package: &mut Package<R>,
    file_path: &str,
    model: &Model,
    validated_files: &mut HashSet<String>,
    config: &ParserConfig,
) -> Result<()> {
    // Skip if already validated or is encrypted
    if validated_files.contains(file_path) {
        return Ok(());
    }

    let is_encrypted = model
        .secure_content
        .as_ref()
        .map(|sc| {
            sc.encrypted_files.iter().any(|encrypted_path| {
                let enc_normalized = encrypted_path.trim_start_matches('/');
                enc_normalized == file_path
            })
        })
        .unwrap_or(false);

    if is_encrypted {
        // Skip validation for encrypted files
        validated_files.insert(file_path.to_string());
        return Ok(());
    }

    // Load and fully parse the external model file
    let external_xml = load_file_with_decryption(package, file_path, file_path, model, config)?;

    // Parse the external model file with all extensions enabled plus common custom extensions
    // We use a comprehensive config instead of the main model's config because:
    // 1. External files may declare different required extensions than the main model
    // 2. We're only validating triangle material properties, not enforcing extension requirements
    // 3. This prevents failures when external files use extensions not in the main model's config
    let external_config = ParserConfig::with_all_extensions()
        .with_custom_extension(
            "http://schemas.3mf.io/3dmanufacturing/displacement/2023/10",
            "Displacement 2023/10",
        )
        .with_custom_extension(
            "http://schemas.microsoft.com/3dmanufacturing/trianglesets/2021/07",
            "TriangleSets",
        );

    let external_model = match parse_model_xml_with_config(&external_xml, external_config) {
        Ok(model) => model,
        Err(e) => {
            return Err(Error::InvalidModel(format!(
                "External model file '{}' failed to parse: {}",
                file_path, e
            )));
        }
    };

    // Validate triangle properties in the external model using the shared helper function
    for object in &external_model.resources.objects {
        if let Some(ref mesh) = object.mesh {
            // Use the shared validation function from validator module
            crate::validator::validate_object_triangle_materials(
                object.id,
                object.pid,
                mesh,
                &format!("External model file '{}': Object {}", file_path, object.id),
            )?;
        }
    }

    validated_files.insert(file_path.to_string());
    Ok(())
}