Skip to main content

Crate lib3mf

Crate lib3mf 

Source
Expand description

§lib3mf

A pure Rust implementation for reading and writing 3MF (3D Manufacturing Format) files.

This library provides functionality to read, parse, create, and write 3MF files, which are ZIP-based containers following the Open Packaging Conventions (OPC) standard and containing XML-based 3D model data.

§Features

  • Pure Rust implementation with no unsafe code
  • Parse 3MF file structure (ZIP/OPC container)
  • Read 3D model data including meshes, vertices, and triangles
  • Write and serialize 3MF files
  • Support for materials and colors
  • Metadata extraction and writing
  • Round-trip support (read-write-read)
  • Mesh-plane slicing with contour extraction

§Reading Example

use lib3mf::Model;
use std::fs::File;

let file = File::open("model.3mf")?;
let model = Model::from_reader(file)?;

println!("Model contains {} objects", model.resources.objects.len());

§Writing Example

use lib3mf::{Model, Object, Mesh, Vertex, Triangle, BuildItem};

// Create a new model
let mut model = Model::new();

// Create a mesh with a simple triangle
let mut mesh = Mesh::new();
mesh.vertices.push(Vertex::new(0.0, 0.0, 0.0));
mesh.vertices.push(Vertex::new(10.0, 0.0, 0.0));
mesh.vertices.push(Vertex::new(5.0, 10.0, 0.0));
mesh.triangles.push(Triangle::new(0, 1, 2));

// Create an object with the mesh
let mut object = Object::new(1);
object.mesh = Some(mesh);

// Add to resources and build
model.resources.objects.push(object);
model.build.items.push(BuildItem::new(1));

// Write to file
model.write_to_file("output.3mf")?;

§Mesh Slicing Example

Requires the mesh-ops feature (enabled by default).

use lib3mf::{collect_intersection_segments, assemble_contours, Mesh, Vertex, Triangle};

// Create or load a mesh
let mut mesh = Mesh::new();
// ... add vertices and triangles ...

// Slice the mesh at Z=5.0
let segments = collect_intersection_segments(&mesh, 5.0);

// Assemble segments into closed contours
let contours = assemble_contours(segments, 1e-6);

println!("Found {} contours at Z=5.0", contours.len());
for (i, contour) in contours.iter().enumerate() {
    println!("Contour {} has {} vertices", i, contour.len());
}

Re-exports§

pub use error::Error;
pub use error::Result;
pub use extension::ExtensionHandler;
pub use extension::ExtensionRegistry;
pub use extensions::BeamLatticeExtensionHandler;
pub use extensions::BooleanOperationsExtensionHandler;
pub use extensions::DisplacementExtensionHandler;
pub use extensions::MaterialExtensionHandler;
pub use extensions::ProductionExtensionHandler;
pub use extensions::SecureContentExtensionHandler;
pub use extensions::SliceExtensionHandler;
pub use extensions::create_default_registry;
pub use extensions::register_all_handlers;
pub use key_provider::KeyProvider;
pub use mesh_ops::Point2D;
pub use mesh_ops::SubdivisionMethod;
pub use mesh_ops::SubdivisionOptions;
pub use mesh_ops::assemble_contours;
pub use mesh_ops::collect_intersection_segments;
pub use mesh_ops::subdivide;
pub use mesh_ops::subdivide_simple;
pub use mesh_ops::triangle_plane_intersection;
pub use model::AccessRight;
pub use model::BaseMaterial;
pub use model::BaseMaterialGroup;
pub use model::Beam;
pub use model::BeamCapMode;
pub use model::BeamSet;
pub use model::BlendMethod;
pub use model::BooleanOpType;
pub use model::BooleanRef;
pub use model::BooleanShape;
pub use model::Build;
pub use model::BuildItem;
pub use model::CEKParams;
pub use model::Channel;
pub use model::ColorGroup;
pub use model::Component;
pub use model::Composite;
pub use model::CompositeMaterials;
pub use model::Consumer;
pub use model::CustomElementHandler;
pub use model::CustomElementResult;
pub use model::CustomExtensionContext;
pub use model::CustomExtensionInfo;
pub use model::CustomValidationHandler;
pub use model::Disp2DCoords;
pub use model::Disp2DGroup;
pub use model::Displacement2D;
pub use model::DisplacementMesh;
pub use model::DisplacementTriangle;
pub use model::Extension;
pub use model::FilterMode;
pub use model::ImplicitVolume;
pub use model::KEKParams;
pub use model::Material;
pub use model::Mesh;
pub use model::MetadataEntry;
pub use model::Model;
pub use model::Multi;
pub use model::MultiProperties;
pub use model::NormVector;
pub use model::NormVectorGroup;
pub use model::Object;
pub use model::ObjectType;
pub use model::ParserConfig;
pub use model::ProductionInfo;
pub use model::ResourceData;
pub use model::ResourceDataGroup;
pub use model::Resources;
pub use model::SecureContentInfo;
pub use model::Slice;
pub use model::SlicePolygon;
pub use model::SliceRef;
pub use model::SliceSegment;
pub use model::SliceStack;
pub use model::Tex2Coord;
pub use model::Texture2D;
pub use model::Texture2DGroup;
pub use model::Thumbnail;
pub use model::TileStyle;
pub use model::Triangle;
pub use model::Vertex;
pub use model::Vertex2D;
pub use model::VolumetricBoundary;
pub use model::VolumetricData;
pub use model::VolumetricProperty;
pub use model::VolumetricPropertyGroup;
pub use model::Voxel;
pub use model::VoxelGrid;

Modules§

error
Error types for 3MF parsing
extension
Extension trait system for pluggable 3MF extension architecture
extensions
Concrete implementations of ExtensionHandler trait
key_provider
Key provider trait for SecureContent encryption/decryption
mesh_ops
Triangle mesh operations using parry3d
model
Data structures representing 3MF models
opc
OPC (Open Packaging Conventions) handling for 3MF files
parser
XML parsing for 3MF model files
polygon_clipping
Polygon clipping and self-intersection resolution for slices
polygon_triangulation
Polygon triangulation for filled 2D rendering
streaming
Streaming parser for large 3MF files
validator
Validation logic for 3MF models