thdmaker 0.0.4

A comprehensive 3D file format library supporting AMF, STL, 3MF and other 3D manufacturing formats
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
//! Core 3MF data model structures.

use std::fmt;
use std::str::FromStr;
use super::error::{Error, Result};
use super::primitive::*;
use super::booleanoperations::BooleanShape;
use super::displacement::{DisplacementResources, DisplacementMesh};
use super::material::MaterialResources;
use super::production::{ProductionItem, ProductionComponent, ProductionObject, ProductionBuild, Alternatives};
use super::slice::{SliceResources, SliceObject};
use super::volumetric::{VolumetricResources, LevelSet};
use super::implicit::ImplicitResources;

/// The root 3MF model containing all resources and build instructions.
#[derive(Debug, Clone, Default)]
pub struct Model {
    /// Unit of measurement.
    pub unit: Unit,
    /// Language code (e.g., "en-US").
    pub language: Option<String>,
    /// Required extensions.
    pub required_extensions: Vec<String>,
    /// Recommended extensions.
    pub recommended_extensions: Vec<String>,
    /// Model metadata.
    pub metadatas: Vec<Metadata>,
    /// Resources (materials, objects).
    pub resources: Resources,
    /// Build instructions.
    pub build: Build,
}

impl Model {
    /// Create a new empty model.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a new model with the specified unit.
    pub fn with_unit(unit: Unit) -> Self {
        Self {
            unit,
            ..Default::default()
        }
    }

    /// Add metadata to the model.
    pub fn add_metadata(&mut self, name: impl Into<String>, value: impl Into<String>) -> &mut Self {
        self.metadatas.push(Metadata {
            name: name.into(),
            value: value.into(),
            preserve: false,
            r#type: None,
        });
        self
    }

    /// Set the title metadata.
    pub fn set_title(&mut self, title: impl Into<String>) -> &mut Self {
        self.add_metadata("Title", title)
    }

    /// Set the designer metadata.
    pub fn set_designer(&mut self, designer: impl Into<String>) -> &mut Self {
        self.add_metadata("Designer", designer)
    }

    /// Set the description metadata.
    pub fn set_description(&mut self, description: impl Into<String>) -> &mut Self {
        self.add_metadata("Description", description)
    }

    /// Set the application metadata.
    pub fn set_application(&mut self, application: impl Into<String>) -> &mut Self {
        self.add_metadata("Application", application)
    }

    /// Add a base material group and return its ID.
    pub fn add_base_materials(&mut self, materials: Vec<BaseMaterial>) -> u32 {
        let id = self.resources.next_id();
        self.resources.base_materials.push(BaseMaterials {
            id,
            materials,
            display_properties_id: None,
        });
        id
    }

    /// Add an object and return its ID.
    pub fn add_object(&mut self, object: Object) -> u32 {
        let id = object.id;
        self.resources.objects.push(object);
        self.resources.set_next_id(id + 1);
        id
    }

    /// Create a new object with auto-assigned ID.
    pub fn gen_object(&mut self) -> Object {
        Object::new(self.resources.next_id())
    }

    /// Add an object to the build by ID with optional transform.
    pub fn add_build_item(&mut self, object_id: u32, transform: Option<Matrix3D>) -> &mut Self {
        self.build.items.push(Item::with_transform(object_id, transform));
        self
    }

    /// Get an object by ID.
    pub fn get_object(&self, id: u32) -> Option<&Object> {
        self.resources.objects.iter().find(|o| o.id == id)
    }

    /// Get a mutable object by ID.
    pub fn get_object_mut(&mut self, id: u32) -> Option<&mut Object> {
        self.resources.objects.iter_mut().find(|o| o.id == id)
    }

    /// Get all objects of a specific type.
    pub fn get_objects(&self, objt: ObjectType) -> Vec<&Object> {
        self.resources.objects.iter().filter(|o| o.r#type == objt).collect()
    }

    /// Get all mutable objects of a specific type.
    pub fn get_objects_mut(&mut self, objt: ObjectType) -> Vec<&mut Object> {
        self.resources.objects.iter_mut().filter(|o| o.r#type == objt).collect()
    }

    /// Get base materials by ID.
    pub fn get_base_material(&self, id: u32) -> Option<&BaseMaterials> {
        self.resources.base_materials.iter().find(|m| m.id == id)
    }

    /// Get mutable base materials by ID.
    pub fn get_base_material_mut(&mut self, id: u32) -> Option<&mut BaseMaterials> {
        self.resources.base_materials.iter_mut().find(|m| m.id == id)
    }

    /// Get displacement resources.
    pub fn get_displacement(&self) -> Option<&DisplacementResources> {
        self.resources.displacements.as_ref()
    }

    /// Get mutable displacement resources.
    pub fn get_displacements_mut(&mut self) -> Option<&mut DisplacementResources> {
        self.resources.displacements.as_mut()
    }

    /// Get materials resources.
    pub fn get_materials(&self) -> Option<&MaterialResources> {
        self.resources.materials.as_ref()
    }

    /// Get mutable materials resources.
    pub fn get_materials_mut(&mut self) -> Option<&mut MaterialResources> {
        self.resources.materials.as_mut()
    }

    /// Get volumetric resources.
    pub fn get_volumetric(&self) -> Option<&VolumetricResources> {
        self.resources.volumetric.as_ref()
    }

    /// Get mutable volumetric resources.
    pub fn get_volumetric_mut(&mut self) -> Option<&mut VolumetricResources> {
        self.resources.volumetric.as_mut()
    }

    /// Get implicit resources.
    pub fn get_implicit(&self) -> Option<&ImplicitResources> {
        self.resources.implicit.as_ref()
    }

    /// Get mutable implicit resources.
    pub fn get_implicit_mut(&mut self) -> Option<&mut ImplicitResources> {
        self.resources.implicit.as_mut()
    }

    /// Validate the model structure.
    pub fn validate(&self) -> Result<()> {
        // Check for duplicate resource IDs
        let mut ids = std::collections::HashSet::new();
        for obj in &self.resources.objects {
            if !ids.insert(obj.id) {
                return Err(Error::DuplicateResourceId(obj.id));
            }
        }
        for mat in &self.resources.base_materials {
            if !ids.insert(mat.id) {
                return Err(Error::DuplicateResourceId(mat.id));
            }
        }

        // Validate build items reference valid objects
        for item in &self.build.items {
            if self.get_object(item.object_id).is_none() {
                return Err(Error::InvalidReference(format!(
                    "build item references non-existent object: {}",
                    item.object_id
                )));
            }
        }

        // Validate object types in build
        for item in &self.build.items {
            if let Some(obj) = self.get_object(item.object_id) {
                if obj.r#type == ObjectType::Other {
                    return Err(Error::InvalidReference(format!(
                        "build item cannot reference 'other' type object: {}",
                        item.object_id
                    )));
                }
            }
        }

        // Validate boolean shape objects
        for obj in &self.resources.objects {
            if obj.is_boolean_shape() {
                // Boolean shape objects cannot have pid or pindex
                if obj.pid.is_some() || obj.pindex.is_some() {
                    return Err(Error::InvalidAttribute {
                        name: "pid/pindex".to_string(),
                        message: format!(
                            "boolean shape object {} cannot have material properties",
                            obj.id
                        ),
                    });
                }

                // Validate boolean shape references
                if let Some(boolean_shape) = obj.get_boolean_shape() {
                    // Validate base object reference
                    if self.get_object(boolean_shape.object_id).is_none() {
                        return Err(Error::InvalidReference(format!(
                            "boolean shape {} references non-existent base object: {}",
                            obj.id, boolean_shape.object_id
                        )));
                    }

                    // Validate boolean operation references
                    for (i, boolean) in boolean_shape.booleans.iter().enumerate() {
                        if self.get_object(boolean.object_id).is_none() {
                            return Err(Error::InvalidReference(format!(
                                "boolean shape {} boolean[{}] references non-existent object: {}",
                                obj.id, i, boolean.object_id
                            )));
                        }
                    }

                    // Validate the boolean shape itself
                    if let Err(e) = boolean_shape.validate() {
                        return Err(Error::InvalidStructure(format!(
                            "boolean shape {} validation failed: {}",
                            obj.id, e
                        )));
                    }
                }
            }
        }

        Ok(())
    }
}

/// A group of metadata.
#[derive(Debug, Clone, Default)]
pub struct MetadataGroup {
    /// Metadata group.
    pub metadatas: Vec<Metadata>,
}

impl MetadataGroup {
    /// Create a new metadata group.
    pub fn new() -> Self {
        Self { metadatas: Vec::new() }
    }
}

/// Model metadata.
#[derive(Debug, Clone, Default)]
pub struct Metadata {
    /// Metadata name (e.g., "Title", "Designer").
    pub name: String,
    /// Metadata value.
    pub value: String,
    /// Whether to preserve this metadata on modification.
    pub preserve: bool,
    /// Optional data type (default: xs:string).
    pub r#type: Option<String>,
}

/// Collection of resources.
#[derive(Debug, Clone, Default)]
pub struct Resources {
    /// Base material groups.
    pub base_materials: Vec<BaseMaterials>,
    /// Optional displacement extension resources.
    pub displacements: Option<DisplacementResources>,
    /// Optional materials and properties extension resources.
    pub materials: Option<MaterialResources>,
    /// Optional slice extension resources.
    pub slices: Option<SliceResources>,
    /// Optional volumetric extension resources.
    pub volumetric: Option<VolumetricResources>,
    /// Optional implicit extension resources.
    pub implicit: Option<ImplicitResources>,
    /// Object definitions.
    pub objects: Vec<Object>,
    /// Internal ID counter.
    next_id: u32,
}

impl Resources {
    /// Get the next available resource ID.
    pub fn next_id(&mut self) -> u32 {
        self.next_id += 1;
        self.next_id
    }

    /// Set the next ID (used when loading).
    pub fn set_next_id(&mut self, id: u32) {
        if id >= self.next_id {
            self.next_id = id;
        }
    }
}

/// Collection of base materials.
#[derive(Debug, Clone)]
pub struct BaseMaterials {
    /// Unique resource ID.
    pub id: u32,
    /// Individual materials.
    pub materials: Vec<BaseMaterial>,
    /// Optional material extension reference to display properties.
    pub display_properties_id: Option<u32>,
}

impl BaseMaterials {
    /// Create a new base materials group.
    pub fn new(id: u32) -> Self {
        Self {
            id,
            materials: Vec::new(),
            display_properties_id: None,
        }
    }

    /// Add a material.
    pub fn add(&mut self, name: impl Into<String>, display_color: Color) -> usize {
        let index = self.materials.len();
        self.materials.push(BaseMaterial {
            name: name.into(),
            display_color,
        });
        index
    }
}

/// A single base material.
#[derive(Debug, Clone)]
pub struct BaseMaterial {
    /// Material name.
    pub name: String,
    /// Display color for rendering.
    pub display_color: Color,
}

impl BaseMaterial {
    /// Create a new base material.
    pub fn new(name: impl Into<String>, display_color: Color) -> Self {
        Self {
            name: name.into(),
            display_color,
        }
    }
}

/// Build instructions.
#[derive(Debug, Clone, Default)]
pub struct Build {
    /// Optional production extension data.
    pub production: Option<ProductionBuild>,
    /// Items to build.
    pub items: Vec<Item>,
}

impl Build {
    /// Create a new empty build.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an item to build.
    pub fn add_item(&mut self, item: Item) -> &mut Self {
        self.items.push(item);
        self
    }

    /// Add an object by ID.
    pub fn add_object(&mut self, object_id: u32) -> &mut Self {
        self.items.push(Item::new(object_id));
        self
    }

    /// Add an object with transform.
    pub fn add_object_with_transform(&mut self, object_id: u32, transform: Matrix3D) -> &mut Self {
        self.items.push(Item {
            object_id,
            transform: Some(transform),
            part_number: None,
            production: None,
            metadata_group: MetadataGroup::new(),
        });
        self
    }

    pub fn get_transform(&self, object_id: u32) -> Option<&Matrix3D> {
        let mut trans = None;
        for item in &self.items {
            if item.object_id == object_id {
                trans = item.transform.as_ref();
                break;
            }
        }
        trans
    }
}

/// A single build item.
#[derive(Debug, Clone)]
pub struct Item {
    /// Reference to object ID.
    pub object_id: u32,
    /// Optional transformation matrix.
    pub transform: Option<Matrix3D>,
    /// Optional part number.
    pub part_number: Option<String>,
    /// Optional production extension data.
    pub production: Option<ProductionItem>,
    /// Item metadata group.
    pub metadata_group: MetadataGroup,
}

impl Item {
    /// Create a new item.
    pub fn new(object_id: u32) -> Self {
        Self {
            object_id,
            transform: None,
            part_number: None,
            production: None,
            metadata_group: MetadataGroup::new(),
        }
    }

    /// Create an item with transform.
    pub fn with_transform(object_id: u32, transform: Option<Matrix3D>) -> Self {
        Self {
            object_id,
            transform,
            part_number: None,
            production: None,
            metadata_group: MetadataGroup::new(),
        }
    }

    /// Set the transform.
    pub fn set_transform(&mut self, transform: Matrix3D) -> &mut Self {
        self.transform = Some(transform);
        self
    }

    /// Set the part number.
    pub fn set_part_number(&mut self, part_number: impl Into<String>) -> &mut Self {
        self.part_number = Some(part_number.into());
        self
    }
}

/// A 3D object resource.
#[derive(Debug, Clone)]
pub struct Object {
    /// Unique resource ID.
    pub id: u32,
    /// Object type.
    pub r#type: ObjectType,
    /// Optional thumbnail path.
    pub thumbnail: Option<String>,
    /// Optional part number.
    pub part_number: Option<String>,
    /// Optional object name.
    pub name: Option<String>,
    /// Default property group ID.
    pub pid: Option<u32>,
    /// Default property index.
    pub pindex: Option<u32>,
    /// Optional displacement resource ID.
    pub did: Option<u32>,
    /// Optional production extension data.
    pub production: Option<ProductionObject>,
    /// Optional slice extension data.
    pub slice: Option<SliceObject>,
    /// Object metadata.
    pub metadata_group: MetadataGroup,
    /// Object content (mesh or components or extension structure).
    pub content: ObjectContent,
}

impl Object {
    /// Create a new object.
    pub fn new(id: u32) -> Self {
        Self {
            id,
            r#type: ObjectType::Model,
            thumbnail: None,
            part_number: None,
            name: None,
            pid: None,
            pindex: None,
            did: None,
            production: None,
            slice: None,
            metadata_group: MetadataGroup::new(),
            content: ObjectContent::Mesh(Mesh::new()),
        }
    }

    /// Create a new object with a mesh.
    pub fn with_mesh(id: u32, mesh: Mesh) -> Self {
        Self {
            id,
            r#type: ObjectType::Model,
            thumbnail: None,
            part_number: None,
            name: None,
            pid: None,
            pindex: None,
            did: None,
            production: None,
            slice: None,
            metadata_group: MetadataGroup::new(),
            content: ObjectContent::Mesh(mesh),
        }
    }

    /// Create a new object with components.
    pub fn with_components(id: u32, components: Components) -> Self {
        Self {
            id,
            r#type: ObjectType::Model,
            thumbnail: None,
            part_number: None,
            name: None,
            pid: None,
            pindex: None,
            did: None,
            production: None,
            slice: None,
            metadata_group: MetadataGroup::new(),
            content: ObjectContent::Components(components),
        }
    }

    /// Create a new object with a boolean shape.
    pub fn with_boolean_shape(id: u32, boolean_shape: BooleanShape) -> Self {
        Self {
            id,
            r#type: ObjectType::Model,
            thumbnail: None,
            part_number: None,
            name: None,
            pid: None,
            pindex: None,
            did: None,
            production: None,
            slice: None,
            metadata_group: MetadataGroup::new(),
            content: ObjectContent::BooleanShape(boolean_shape),
        }
    }

    /// Create a new object with a displacement mesh.
    pub fn with_displacement_mesh(id: u32, displacement_mesh: DisplacementMesh) -> Self {
        Self {
            id,
            r#type: ObjectType::Model,
            thumbnail: None,
            part_number: None,
            name: None,
            pid: None,
            pindex: None,
            did: None,
            production: None,
            slice: None,
            metadata_group: MetadataGroup::new(),
            content: ObjectContent::DisplacementMesh(displacement_mesh),
        }
    }

    /// Check if this object is a mesh.
    pub fn is_mesh(&self) -> bool {
        matches!(self.content, ObjectContent::Mesh(_))
    }

    /// Check if this object has components.
    pub fn is_components(&self) -> bool {
        matches!(self.content, ObjectContent::Components(_))
    }

    /// Check if this object is a boolean shape.
    pub fn is_boolean_shape(&self) -> bool {
        matches!(self.content, ObjectContent::BooleanShape(_))
    }

    /// Check if this object is a displacement mesh.
    pub fn is_displacement_mesh(&self) -> bool {
        matches!(self.content, ObjectContent::DisplacementMesh(_))
    }

    /// Check if this object is alternatives.
    pub fn is_alternatives(&self) -> bool {
        matches!(self.content, ObjectContent::Alternatives(_))
    }

    /// Get the mesh if this object contains one.
    pub fn get_mesh(&self) -> Option<&Mesh> {
        match &self.content {
            ObjectContent::Mesh(mesh) => Some(mesh),
            _ => None,
        }
    }

    /// Get a mutable mesh if this object contains one.
    pub fn get_mesh_mut(&mut self) -> Option<&mut Mesh> {
        match &mut self.content {
            ObjectContent::Mesh(mesh) => Some(mesh),
            _ => None,
        }
    }

    /// Get the components if this object contains them.
    pub fn get_components(&self) -> Option<&Components> {
        match &self.content {
            ObjectContent::Components(components) => Some(components),
            _ => None,
        }
    }

    /// Get the boolean shape if this object contains one.
    pub fn get_boolean_shape(&self) -> Option<&BooleanShape> {
        match &self.content {
            ObjectContent::BooleanShape(boolean_shape) => Some(boolean_shape),
            _ => None,
        }
    }

    /// Get a mutable boolean shape if this object contains one.
    /// Get a mutable boolean shape if this object contains one.
    pub fn get_boolean_shape_mut(&mut self) -> Option<&mut BooleanShape> {
        match &mut self.content {
            ObjectContent::BooleanShape(boolean_shape) => Some(boolean_shape),
            _ => None,
        }
    }

    /// Get the displacement mesh if this object contains one.
    pub fn get_displacement_mesh(&self) -> Option<&DisplacementMesh> {
        match &self.content {
            ObjectContent::DisplacementMesh(displacement_mesh) => Some(displacement_mesh),
            _ => None,
        }
    }

    /// Get a mutable displacement mesh if this object contains one.
    pub fn get_displacement_mesh_mut(&mut self) -> Option<&mut DisplacementMesh> {
        match &mut self.content {
            ObjectContent::DisplacementMesh(displacement_mesh) => Some(displacement_mesh),
            _ => None,
        }
    }

    /// Get the alternatives if this object contains them.
    pub fn get_alternatives(&self) -> Option<&Alternatives> {
        match &self.content {
            ObjectContent::Alternatives(alternatives) => Some(alternatives),
            _ => None,
        }
    }

    /// Get a mutable alternatives if this object contains them.
    pub fn get_alternatives_mut(&mut self) -> Option<&mut Alternatives> {
        match &mut self.content {
            ObjectContent::Alternatives(alternatives) => Some(alternatives),
            _ => None,
        }
    }

    /// Set the mesh.
    pub fn set_mesh(&mut self, mesh: Mesh) -> &mut Self {
        self.content = ObjectContent::Mesh(mesh);
        self
    }

    /// Set the components.
    pub fn set_components(&mut self, components: Components) -> &mut Self {
        self.content = ObjectContent::Components(components);
        self
    }

    /// Set the boolean shape.
    pub fn set_boolean_shape(&mut self, boolean_shape: BooleanShape) -> &mut Self {
        self.content = ObjectContent::BooleanShape(boolean_shape);
        self
    }

    /// Set the displacement mesh.
    pub fn set_displacement_mesh(&mut self, displacement_mesh: DisplacementMesh) -> &mut Self {
        self.content = ObjectContent::DisplacementMesh(displacement_mesh);
        self
    }

    /// Set the a object.
    pub fn set_alternatives(&mut self, alternatives: Alternatives) -> &mut Self {
        self.content = ObjectContent::Alternatives(alternatives);
        self
    }

    /// Set the object name.
    pub fn set_name(&mut self, name: impl Into<String>) -> &mut Self {
        self.name = Some(name.into());
        self
    }

    /// Set the object type.
    pub fn set_type(&mut self, object_type: ObjectType) -> &mut Self {
        self.r#type = object_type;
        self
    }

    /// Set the default material.
    pub fn set_material(&mut self, pid: u32, pindex: u32) -> &mut Self {
        self.pid = Some(pid);
        self.pindex = Some(pindex);
        self
    }
}

/// Object type in the 3MF model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ObjectType {
    /// Standard model object to be manufactured.
    #[default]
    Model,
    /// Solid support structure (filled).
    Solidsupport,
    /// Support structure (surface only, not filled).
    Support,
    /// Surface object (not a solid).
    Surface,
    /// Other object type (cannot be built directly).
    Other,
}

impl fmt::Display for ObjectType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ObjectType::Model => write!(f, "model"),
            ObjectType::Solidsupport => write!(f, "solidsupport"),
            ObjectType::Support => write!(f, "support"),
            ObjectType::Surface => write!(f, "surface"),
            ObjectType::Other => write!(f, "other"),
        }
    }
}

impl FromStr for ObjectType {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "model" => Ok(ObjectType::Model),
            "solidsupport" => Ok(ObjectType::Solidsupport),
            "support" => Ok(ObjectType::Support),
            "surface" => Ok(ObjectType::Surface),
            "other" => Ok(ObjectType::Other),
            _ => Err(Error::InvalidAttribute {
                name: "type".to_string(),
                message: format!("unknown object type: {}", s),
            }),
        }
    }
}

/// Object content: either a mesh, components, boolean shape, or displacement mesh.
#[derive(Debug, Clone)]
pub enum ObjectContent {
    /// Triangle mesh.
    Mesh(Mesh),
    /// Component assembly.
    Components(Components),
    /// Boolean shape extension.
    BooleanShape(BooleanShape),
    /// Displacement mesh extension.
    DisplacementMesh(DisplacementMesh),
    /// Production alternative extension.
    Alternatives(Alternatives),
    /// Volumetric levelset extension.
    LevelSet(LevelSet),
}

impl Default for ObjectContent {
    fn default() -> Self {
        ObjectContent::Mesh(Mesh::default())
    }
}

/// A group of components.
#[derive(Debug, Clone)]
pub struct  Components {
    /// Components.
    pub components: Vec<Component>,
}

/// A component reference.
#[derive(Debug, Clone)]
pub struct Component {
    /// Referenced object ID.
    pub object_id: u32,
    /// Optional transformation.
    pub transform: Option<Matrix3D>,
    /// Optional production extension data.
    pub production: Option<ProductionComponent>,
}

impl Component {
    /// Create a new component.
    pub fn new(object_id: u32) -> Self {
        Self {
            object_id,
            transform: None,
            production: None,
        }
    }

    /// Create a component with transform.
    pub fn with_transform(object_id: u32, transform: Matrix3D) -> Self {
        Self {
            object_id,
            transform: Some(transform),
            production: None,
        }
    }
}