UNPKG

geom-parse-stl

Version:

Parse a STL (StereoLithography) ASCII string, ArrayBuffer or ArrayBuffer with ASCII data, and return a simplicial complex.

137 lines (93 loc) 5.57 kB
# geom-parse-stl [![npm version](https://img.shields.io/npm/v/geom-parse-stl)](https://www.npmjs.com/package/geom-parse-stl) [![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://www.npmjs.com/package/geom-parse-stl) [![npm minzipped size](https://img.shields.io/bundlephobia/minzip/geom-parse-stl)](https://bundlephobia.com/package/geom-parse-stl) [![dependencies](https://img.shields.io/librariesio/release/npm/geom-parse-stl)](https://github.com/dmnsgn/geom-parse-stl/blob/main/package.json) [![types](https://img.shields.io/npm/types/geom-parse-stl)](https://github.com/microsoft/TypeScript) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-fa6673.svg)](https://conventionalcommits.org) [![styled with prettier](https://img.shields.io/badge/styled_with-Prettier-f8bc45.svg?logo=prettier)](https://github.com/prettier/prettier) [![linted with eslint](https://img.shields.io/badge/linted_with-ES_Lint-4B32C3.svg?logo=eslint)](https://github.com/eslint/eslint) [![license](https://img.shields.io/github/license/dmnsgn/geom-parse-stl)](https://github.com/dmnsgn/geom-parse-stl/blob/main/LICENSE.md) Parse a STL (StereoLithography) ASCII string, ArrayBuffer or ArrayBuffer with ASCII data, and return a simplicial complex. [![paypal](https://img.shields.io/badge/donate-paypal-informational?logo=paypal)](https://paypal.me/dmnsgn) [![coinbase](https://img.shields.io/badge/donate-coinbase-informational?logo=coinbase)](https://commerce.coinbase.com/checkout/56cbdf28-e323-48d8-9c98-7019e72c97f3) [![twitter](https://img.shields.io/twitter/follow/dmnsgn?style=social)](https://twitter.com/dmnsgn) ## Installation ```bash npm install geom-parse-stl ``` ## Usage ```js import { parseStl } from "geom-parse-stl"; import computeNormals from "geom-normals"; // Fetch the file const url = "./MyModel.stl"; const response = await fetch(url); const buffer = await response.arrayBuffer(); // Parse STL arraybuffer (ASCII or binary) const geometry = parseStl(buffer); // Optionally compute normals geometry.normals = computeNormals(geometry.positions, geometry.cells); console.log(geometry); // { // positions: Float32Array [x, y, z, x, y, z, ...], // faceNormals: Float32Array [x, y, z, x, y, z, ...] // cells: Uint8/16/32/Array [a, b, c, a, b, c, ...], // normals: Float32Array [x, y, z, x, y, z, ...] // } ``` ## API <!-- api-start --> ## Modules <dl> <dt><a href="#module_geom-parse-stl">geom-parse-stl</a></dt> <dd></dd> </dl> ## Typedefs <dl> <dt><a href="#SimplicialComplex">SimplicialComplex</a> : <code>object</code></dt> <dd><p>Geometry definition.</p> </dd> </dl> <a name="module_geom-parse-stl"></a> ## geom-parse-stl - [geom-parse-stl](#module_geom-parse-stl) - [.parseStlAscii(asciiString)](#module_geom-parse-stl.parseStlAscii) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) - [.parseStlBinary(arrayBuffer)](#module_geom-parse-stl.parseStlBinary) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) - [.parseStl(stl)](#module_geom-parse-stl.parseStl) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) <a name="module_geom-parse-stl.parseStlAscii"></a> ### geom-parse-stl.parseStlAscii(asciiString) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) Parse a STL ASCII string and return a simplicial complex. **Kind**: static method of [<code>geom-parse-stl</code>](#module_geom-parse-stl) | Param | Type | | ----------- | ------------------- | | asciiString | <code>string</code> | <a name="module_geom-parse-stl.parseStlBinary"></a> ### geom-parse-stl.parseStlBinary(arrayBuffer) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) Parse a STL ArrayBuffer or ArrayBuffer with ASCII data and return a simplicial complex. **Kind**: static method of [<code>geom-parse-stl</code>](#module_geom-parse-stl) | Param | Type | | ----------- | ------------------------ | | arrayBuffer | <code>ArrayBuffer</code> | <a name="module_geom-parse-stl.parseStl"></a> ### geom-parse-stl.parseStl(stl) ⇒ [<code>SimplicialComplex</code>](#SimplicialComplex) Parse a STL (StereoLithography) ASCII string, ArrayBuffer or ArrayBuffer with ASCII data, and return a simplicial complex. **Kind**: static method of [<code>geom-parse-stl</code>](#module_geom-parse-stl) **See**: https://paulbourke.net/dataformats/stl/ | Param | Type | | ----- | ----------------------------------------------- | | stl | <code>string</code> \| <code>ArrayBuffer</code> | <a name="SimplicialComplex"></a> ## SimplicialComplex : <code>object</code> Geometry definition. **Kind**: global typedef **Properties** | Name | Type | Description | | ----------- | ------------------------------------------------------------------------------- | --------------------- | | positions | <code>Float32Array</code> | | | faceNormals | <code>Float32Array</code> | | | cells | <code>Uint8Array</code> \| <code>Uint16Array</code> \| <code>Uint32Array</code> | | | [name] | <code>string</code> | The STL "solid" name. | <!-- api-end --> ## License MIT. See [license file](https://github.com/dmnsgn/geom-parse-stl/blob/main/LICENSE.md).