npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@amandaghassaei/stl-parser

v1.7.0

Published

Standalone module for parsing binary and ASCII STL files, written in TypeScript.

Downloads

149

Readme

@amandaghassaei/stl-parser

stl-parser main image

NPM Package Build Size NPM Downloads License

Standalone module for loading and parsing binary/ASCII STL files – unit tested and written in TypeScript. Parsing code based on Threejs STLLoader.

Live demo: apps.amandaghassaei.com/stl-parser/demo/

STL File Format

The .stl file format stores geometry (and sometimes color) information for 3D triangulated meshes. stl-parser does not generate or render .stl files, but it will allow you to parse them in a browser or Nodejs environment. Example .stl files can be found in test/stl/.

Installation

Install via npm

npm install @amandaghassaei/stl-parser

and import into your project:

import {
  parseSTL,
  loadSTL,
  loadSTLAsync,
} from '@amandaghassaei/stl-parser';

Import into HTML

Import stl-parser.min.js directly into your html:

<html>
  <head>
    <script src="stl-parser.min.js"></script>
  </head>
  <body>
  </body>
</html>

STLParserLib will be accessible globally:

const { parseSTL, loadSTL, loadSTLAsync } = STLParserLib;

Use

Full API documentation in docs.

// Load and parse the .stl file using url or File object.
loadSTL('./teapot.stl', (mesh) => {
  const {
    vertices,
    facesNormals,
    facesColors,
    edgesIndices,
    boundingBox,
  } = mesh;
});
// Also try:
// const mesh = await loadSTLAsync('./teapot.stl');

// Or parse file buffer or string synchronously.
const meshFromBuffer = parseSTL(fs.readFileSync('./teapot.stl'));
const meshFromString = parseSTL(`solid ASCII
  facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
    outer loop
      vertex   1.000000e+01 0.000000e+00 -1.000000e+01
      vertex   0.000000e+00 0.000000e+00 -1.000000e+01
      vertex   1.000000e+01 1.000000e+01 -1.000000e+01
    endloop
  endfacet
  ....
  facet normal 0.000000e+00 -1.000000e+00 0.000000e+00
    outer loop
      vertex   0.000000e+00 0.000000e+00 0.000000e+00
      vertex   1.000000e+01 0.000000e+00 -1.000000e+01
      vertex   1.000000e+01 0.000000e+00 0.000000e+00
    endloop
  endfacet
endsolid`);
  • vertices is a Float32Array of length 3 * numVertices containing a flat list of vertex positions in the following order [x0, y0, z0, x1, y1, z1, ...]. Each group of three vertices make up a triangle in the .stl mesh – by default, vertices are not shared between triangles in the .stl format, see mergeVertices() below.
  • facesNormals is a Float32Array of length 3 * numFaces containing a flat list of face normals in the following order [nx0, ny0, nz0, nx1, ny1, nz1, ...]
  • If available, facesColors is a Float32Array of length 3 * numFaces containing a flat list of face colors in the following order [r0, g0, b0, r1, g1, b1, ...].
  • edgesIndices is a Uint32Array containing all unique edgesIndices (expressed as pairs of vertex indices) in the mesh in the following order: [e01, e02, e11, e12, ...]. edgesIndices is calculated when queried and then cached.
  • boundingBox returns the min and max of the mesh's bounding box in the form: { min: [x, y, z], max: [x, y, z] }. boundingBox is calculated when queried and then cached.

The mesh object returned by parseSTL, loadSTL, and loadSTLAsync also exposes methods for modifying its geometry:

mesh.mergeVertices().scaleVerticesToUnitBoundingBox();
const { facesIndices } = mesh;
  • STLMesh.mergeVertices() merges coincident vertices and adds a facesIndices array to the mesh object. facesIndices has length 3 * numFaces and contains a flat list of triangle face vertex indices in the following order: [v01, v02, v03, v11, v12, v13, ...].
  • STLMesh.scaleVerticesToUnitBoundingBox() scales the vertices values (in place) to fit inside a unit box centered around the origin.

Build Notes

This library imports fs in Nodejs environments to load files from a url string. This code is never hit in the browser environment (the browser uses a XMLHttpRequest instead), but you may see build warnings due to the import('fs') statement in the code. (I'm wondering if there is a better way to handle this that is compatible for both Nodejs and browser environments, please let me know if you have ideas.)

To fix this warning, you need to set fs as an external dependency in your build settings:

// rollup.config.js
export default {
  ...
  external: ['fs'],
};
// vite.config.js
export default {
  build: {
    rollupOptions: {
      external: ['fs'],
    },
  }
  ...
}

Limitations

  • See limitations listed in Threejs STLLoader. If you have a file that is not being parsed correctly, please upload it in an Issue so it can be added as a test case. Pull requests welcome.

Acknowledgements

  • Most of the parsing code in this library is a TypeScript port of the STLLoader class from Threejs.

License

This work is licensed under an MIT License. It depends on the following:

Related Libraries

Development

I don't have any plans to continue developing this package, but I'm happy to review pull requests if you would like to add a new feature / fix a bug.

To install dev dependencies:

npm install

To compile src to dist:

npm run build

Testing

To run tests:

npm run test