Expand description
Model-to-XML-to-ZIP serialization pipeline for writing 3MF files.
This module provides the inverse of the parser: it converts an in-memory Model
structure back into a valid 3MF archive (ZIP container with XML and attachments).
§Architecture
The writer mirrors the parser module structure but in reverse:
Model → XML Writer → OPC Writer → Package Writer → ZIP Archive- Model writer (
model_writer): Serializes theModelto XML - Mesh writer (
mesh_writer): Writes<mesh>elements with vertices and triangles - OPC writer (
opc_writer): Generates_rels/.relsand[Content_Types].xml - Package writer (
package_writer): Orchestrates full 3MF package creation - ZIP archive: Compresses and writes the final
.3mffile
§Usage
The primary entry point is the write method on Model:
use lib3mf_core::Model;
use std::fs::File;
// Create or load a model
let model = Model::default();
// Write to a 3MF file
let output = File::create("output.3mf")?;
model.write(output)?;
println!("Model written to output.3mf");§Writer Modules
§Core Writers
model_writer: Writes the<model>root element and resourcesmesh_writer: Writes<mesh>geometry (vertices, triangles, properties)opc_writer: Writes OPC metadata (_rels/.rels,[Content_Types].xml)package_writer: Orchestrates writing of complete 3MF packagexml_writer: Low-level XML writing utilities
§Extension Writers
displacement_writer: Writes Displacement Extension data- Beam Lattice: Not yet implemented (partial support)
- Slice: Partial support (slicestackid attribute only)
- Boolean Operations: Fully supported in
model_writer - Volumetric: Fully supported in
model_writer
§Known Limitations
The writer has some gaps compared to the parser:
- Beam lattice writer: Not implemented. Models with beam lattices can be read but not written back.
- Slice writer: Only writes
slicestackidattribute, doesn’t serialize full slice geometry. - Namespace optimization: Always emits all namespace declarations rather than only needed ones.
These limitations don’t affect core 3MF functionality but prevent full roundtrip testing for these extensions.
§Roundtrip Testing
For supported features, the writer produces output that can be parsed back to an equivalent model:
use lib3mf_core::Model;
use std::io::Cursor;
let original_model = Model::default();
// Write to memory
let mut buffer = Cursor::new(Vec::new());
original_model.write(&mut buffer)?;
// Read back (would need to extract XML from ZIP in practice)
// let parsed_model = parse_model(buffer)?;
// assert_eq!(original_model, parsed_model);§Error Handling
All writing functions return Result<(), Lib3mfError>:
- Io: File system errors, ZIP writing errors, out of disk space
- InvalidStructure: Model contains invalid data that can’t be serialized
The writer never panics on invalid input, though it may produce a 3MF file that fails validation.