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
beamlattice_writer: Writes Beam Lattice Extension datadisplacement_writer: Writes Displacement Extension dataslice_writer: Writes Slice Extension datavolumetric_writer: Writes Volumetric Extension data- Boolean Operations: Fully supported in
model_writer
§Known Limitations
The writer has some gaps compared to the parser:
- Namespace optimization: Always emits all namespace declarations rather than only needed ones.
These limitations don’t affect core 3MF functionality.
§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.
Modules§
- beamlattice_
writer - Beam lattice extension writer.
- displacement_
writer - Displacement extension writer.
- mesh_
writer - Mesh geometry writer.
- model_
write_ zip Model::write()convenience method implementation.- model_
writer - Core model XML serializer.
- opc_
writer - OPC content types and relationship writers.
- package_
writer - Package-level ZIP archive writer.
- slice_
writer - Slice extension writer.
- volumetric_
writer - Volumetric extension writer.
- xml_
writer - Low-level XML writer primitives.