pub struct WasmModel { /* private fields */ }Expand description
WebAssembly wrapper around the core 3MF Model.
This struct provides JavaScript-accessible methods for working with 3MF files
in the browser. It wraps lib3mf_core::Model and exposes a subset of its
functionality through WASM bindings.
§Primary API Surface
The main way to use this from JavaScript:
- Parse a 3MF file from bytes using
WasmModel::from_bytes() - Query model properties:
unit(),object_count()
§JavaScript Usage
const model = WasmModel.from_bytes(fileBytes);
console.log(`Unit: ${model.unit()}`);
console.log(`Objects: ${model.object_count()}`);§Full Workflow Example
import init, { WasmModel, set_panic_hook } from './lib3mf_wasm.js';
await init();
set_panic_hook();
// Load from file input
const file = document.getElementById('file-input').files[0];
const buffer = await file.arrayBuffer();
const model = WasmModel.from_bytes(new Uint8Array(buffer));
// Display basic info
document.getElementById('unit').textContent = model.unit();
document.getElementById('count').textContent = model.object_count();Implementations§
Source§impl WasmModel
impl WasmModel
Sourcepub fn new() -> WasmModel
pub fn new() -> WasmModel
Create a new empty 3MF Model.
This creates a model with default values (millimeters unit, no objects, empty build).
In most cases, you should use WasmModel::from_bytes() instead to parse an existing
3MF file.
§JavaScript Usage
const model = new WasmModel();
// Model is empty - unit is Millimeter by default§Note
This constructor has limited utility since the WASM bindings don’t currently expose model-building APIs. It’s primarily for internal use and testing.
Source§impl WasmModel
impl WasmModel
Sourcepub fn from_bytes(data: &[u8]) -> Result<WasmModel, JsError>
pub fn from_bytes(data: &[u8]) -> Result<WasmModel, JsError>
Parse a 3MF file from a byte array (e.g. from a file upload).
This is the primary way to load 3MF files in the browser. It handles the full parsing pipeline:
- Interprets bytes as a ZIP archive
- Parses OPC relationships to locate the model XML
- Parses the XML into the in-memory model structure
- Returns a
WasmModelready for inspection
§Arguments
data- The bytes of the .3mf file (ZIP archive). Typically obtained from a browser file input viaFileReaderorFile.arrayBuffer().
§Returns
Returns a WasmModel on success, or throws a JavaScript error on failure.
§Errors
This function can fail in several ways:
- Invalid ZIP: The bytes don’t represent a valid ZIP archive
- Missing model part: The archive lacks a valid OPC relationship to a 3D model
- Malformed XML: The model XML is invalid or doesn’t conform to the 3MF schema
- Parser errors: Semantic errors in the 3MF structure (invalid references, etc.)
Errors are returned as JavaScript exceptions with descriptive messages.
§Example (Rust)
use lib3mf_wasm::WasmModel;
let file_bytes = std::fs::read("model.3mf")?;
let model = WasmModel::from_bytes(&file_bytes)?;§JavaScript Usage
try {
const buffer = await file.arrayBuffer();
const model = WasmModel.from_bytes(new Uint8Array(buffer));
console.log("Parsed successfully");
} catch (error) {
console.error(`Parse failed: ${error}`);
}Sourcepub fn object_count(&self) -> usize
pub fn object_count(&self) -> usize
Get the total number of objects in the model resources.
This counts all objects in the model’s resource collection, not just objects referenced by build items.
§JavaScript Usage
const count = model.object_count();
console.log(`Model contains ${count} objects`);§Note
Build items may reference the same object multiple times (instances), so the number of build items may differ from the object count.
Trait Implementations§
Source§impl FromWasmAbi for WasmModel
impl FromWasmAbi for WasmModel
Source§impl IntoWasmAbi for WasmModel
impl IntoWasmAbi for WasmModel
Source§impl LongRefFromWasmAbi for WasmModel
impl LongRefFromWasmAbi for WasmModel
Source§impl OptionFromWasmAbi for WasmModel
impl OptionFromWasmAbi for WasmModel
Source§impl OptionIntoWasmAbi for WasmModel
impl OptionIntoWasmAbi for WasmModel
Source§impl RefFromWasmAbi for WasmModel
impl RefFromWasmAbi for WasmModel
Source§impl RefMutFromWasmAbi for WasmModel
impl RefMutFromWasmAbi for WasmModel
Source§impl TryFromJsValue for WasmModel
impl TryFromJsValue for WasmModel
Source§impl VectorFromWasmAbi for WasmModel
impl VectorFromWasmAbi for WasmModel
Source§impl VectorIntoWasmAbi for WasmModel
impl VectorIntoWasmAbi for WasmModel
impl SupportsConstructor for WasmModel
impl SupportsInstanceProperty for WasmModel
impl SupportsStaticProperty for WasmModel
Auto Trait Implementations§
impl Freeze for WasmModel
impl RefUnwindSafe for WasmModel
impl Send for WasmModel
impl Sync for WasmModel
impl Unpin for WasmModel
impl UnwindSafe for WasmModel
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.