Skip to content
Shapemetry

Mesh Formats (STL, OBJ, PLY)

Faceted formats trade exact B-Rep for triangles. All doors live in @huukhanhnguyen/io and follow one pattern: write* takes a B-Rep document and tessellates it; read* rebuilds a faceted B-Rep document you can feed back to Brep.*.

STL

import { writeStl, writeStlAscii, readStl } from '@huukhanhnguyen/io'
import { Brep } from '@huukhanhnguyen/geometry'

const box = Brep.box(10, 10, 10)

const binary: Uint8Array = writeStl(box)   // binary STL
const ascii: string = writeStlAscii(box)   // ASCII STL

const back = readStl(binary)               // string | Uint8Array in → faceted B-Rep document
console.log(Brep.isClosed(back))          // watertight input → closed solid

OBJ

import { writeObj, readObj } from '@huukhanhnguyen/io'

const objText = writeObj(box)   // geometry only, one object
const back = readObj(objText)   // faceted B-Rep document

PLY

import { writePly, readPly } from '@huukhanhnguyen/io'

const bin = writePly(box, 'binary') as Uint8Array
const asc = writePly(box, 'ascii') as string
const back = readPly(bin)

Round-trip reality

All three readers rebuild through solidFromTriangleMesh(positions, indices): for a watertight closed input the result is a closed two-manifold document. A box survives the round trip in every format — the package tests assert exactly that. What does not survive: analytic surfaces (cylinders become facets), colors, and names. Keep STEP/IGES for exact interchange.

Whole-scene export

When you have entities (from @huukhanhnguyen/model evaluation or scene work) rather than one document, the tessellationTo* doors bake and write the whole set:

import { tessellationToStl, tessellationToObj, type Entity } from '@huukhanhnguyen/io'

const stl: Uint8Array = tessellationToStl(entities, options)
const obj = tessellationToObj(entities, options) // ObjResult — OBJ + MTL, one `o` per sourceKey

entitiesToObj(entities, { tessellate: true }) additionally handles entities whose B-Rep needs baking first.

  • 3MFreadThreemf / writeThreemf, same shape as STL.
  • glTF/GLB — richer (materials, hierarchy); see glTF, USD and drawing export.
  • Parametric export re-exports these doors from @huukhanhnguyen/model too (tessellationToStl, tessellationToObj, …).

Where next

Last updated: 📖 1 min readEdit on GitHub