WasmModel

Struct WasmModel 

Source
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:

  1. Parse a 3MF file from bytes using WasmModel::from_bytes()
  2. 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

Source

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

Source

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:

  1. Interprets bytes as a ZIP archive
  2. Parses OPC relationships to locate the model XML
  3. Parses the XML into the in-memory model structure
  4. Returns a WasmModel ready for inspection
§Arguments
  • data - The bytes of the .3mf file (ZIP archive). Typically obtained from a browser file input via FileReader or File.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}`);
}
Source

pub fn unit(&self) -> String

Get the unit of measurement used in the model.

Returns the unit as a string for display in JavaScript.

§Possible Return Values
  • "Millimeter" (default, most common)
  • "Centimeter"
  • "Inch"
  • "Foot"
  • "Meter"
  • "MicroMeter"
§JavaScript Usage
const unit = model.unit();
console.log(`Model uses ${unit} units`);
Source

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 Default for WasmModel

Default implementation delegates to new().

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<WasmModel> for JsValue

Source§

fn from(value: WasmModel) -> Self

Converts to this type from the input type.
Source§

impl FromWasmAbi for WasmModel

Source§

type Abi = u32

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl IntoWasmAbi for WasmModel

Source§

type Abi = u32

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl LongRefFromWasmAbi for WasmModel

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRef<WasmModel>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl OptionFromWasmAbi for WasmModel

Source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl OptionIntoWasmAbi for WasmModel

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl RefFromWasmAbi for WasmModel

Source§

type Abi = u32

The Wasm ABI type references to Self are recovered from.
Source§

type Anchor = RcRef<WasmModel>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
Source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
Source§

impl RefMutFromWasmAbi for WasmModel

Source§

type Abi = u32

Same as RefFromWasmAbi::Abi
Source§

type Anchor = RcRefMut<WasmModel>

Same as RefFromWasmAbi::Anchor
Source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
Source§

impl TryFromJsValue for WasmModel

Source§

fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>

Performs the conversion.
Source§

fn try_from_js_value_ref(value: &JsValue) -> Option<Self>

Performs the conversion.
Source§

impl VectorFromWasmAbi for WasmModel

Source§

type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi

Source§

unsafe fn vector_from_abi(js: Self::Abi) -> Box<[WasmModel]>

Source§

impl VectorIntoWasmAbi for WasmModel

Source§

impl WasmDescribe for WasmModel

Source§

impl WasmDescribeVector for WasmModel

Source§

impl SupportsConstructor for WasmModel

Source§

impl SupportsInstanceProperty for WasmModel

Source§

impl SupportsStaticProperty for WasmModel

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ReturnWasmAbi for T
where T: IntoWasmAbi,

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V