Crate lib3mf_wasm

Crate lib3mf_wasm 

Source
Expand description

§lib3mf-wasm

WebAssembly bindings for lib3mf-rs, enabling browser-based 3MF file processing.

§Overview

This crate provides JavaScript-friendly bindings for the lib3mf-core library, compiled to WebAssembly. It enables client-side 3MF file parsing, validation, and analysis in web browsers without server-side processing.

§When to Use This Crate

  • Browser-based 3MF viewers: Display 3D models directly in the browser
  • Online validation tools: Client-side 3MF file validation without uploading to a server
  • Web-based model inspection: Extract metadata, statistics, and geometry information
  • Privacy-focused applications: Process 3MF files entirely on the client side

§JavaScript Usage

First, build the WASM module using wasm-pack:

wasm-pack build crates/lib3mf-wasm --target web

Then use it in JavaScript:

import init, { WasmModel, set_panic_hook } from './lib3mf_wasm.js';

// Initialize the WASM module
await init();

// Optional: Set up better error messages for debugging
set_panic_hook();

// Load a 3MF file from a file input
const fileInput = document.getElementById('file-input');
fileInput.addEventListener('change', async (e) => {
    const file = e.target.files[0];
    const buffer = await file.arrayBuffer();

    try {
        // Parse the 3MF file
        const model = WasmModel.from_bytes(new Uint8Array(buffer));

        // Access model properties
        console.log(`Unit: ${model.unit()}`);
        console.log(`Objects: ${model.object_count()}`);
    } catch (error) {
        console.error(`Failed to parse 3MF: ${error}`);
    }
});

§Module Structure

This crate exposes a single primary API surface:

  • WasmModel: The main wrapper around lib3mf_core::Model, providing JavaScript-accessible methods for parsing 3MF files and accessing model data.
  • set_panic_hook(): Optional panic handler for better error messages in browser console.

§Current Limitations

This is an early-stage binding layer with limited API surface. Currently supported:

  • Parsing 3MF files from byte arrays
  • Accessing basic model metadata (unit, object count)

Not yet exposed:

  • Validation
  • Geometry access (vertices, triangles)
  • Materials and textures
  • Writing/serialization

For the full Rust API, see lib3mf_core.

Structs§

WasmModel
WebAssembly wrapper around the core 3MF Model.

Functions§

set_panic_hook
Set up better panic messages for debugging in browser console.