stlfiletools
v2.3.1
Published
Basic tools to create a 3d model in a STL file for 3d printing.Maps a function which takes a position and returns a height onto a rectangle or sphere.eg model generated from perlin noise.
Readme
stlFileTools
This is a simple set of tools to create a 3d model in a STL file for 3d printing including the following functionalities.
*The STL files are currently only in ASCII
- Major redo of structure
Functions to manipulate vectors
Function to write a single triangle onto a stl file
Function which creates a sphere and maps values generated from a custom function as varying radii.
Function which maps height values from a custom function onto a flat plane. eg perlin Noise
Function which maps height values from a custom function onto a rectangular prism.
The manipulation of the plain and rectangular prism by 3 by 3 transformation matrices.
example/Demonstration
The following is an example and demonstration of the library where a cos graph is used as the custom function.The parameters will be explained later.

const stl = require("stlfiletools");
var test = new stl.STLfile("test.stl","test");
var soMatix = [[2,1,0],[-1,3,0],[0,0,4]];
//User defined function which returns value based on position
function cosGrath(x,y){
return Math.cos(Math.sqrt(((x-50)/10)*((x-50)/10) + ((y-50)/10) * ((y-50)/10)));
}
var offset = new stl.cord(0,0,15);
test.RectMeshFromFunction(0.1,100,100,1,cosGrath,offset,5);
test.MatrixMult(soMatix);
test.CreateSTLfile();Cords
cord
This class will be used repeatedly for coordinates in 3 dimensions.
class cord {
constructor(x,y,z){
this.x = x;
this.y = y;
this.z = z;
}
};
STLfile class
class STLfile{
constructor(fileName,Name){
this.Name = Name;
this.FileName = fileName;
this.triangles = [];
this.txt = ...
this.CreateSTLfile = ...
this.Sphere = ...
this.FlatMeshFromFunction = ...
this.RectMeshFromFunction = ...
this.BlockyMesh = ...
this.CreateWallBlock =...
this.MatrixMult =...
}- fileName : Name of stl file
- Name : Name of solid
- this.triangles : array of array of 3 points for each triangle, the order is following the right hand rule, points are stored using cord class. Object for STL files new one should be created for each stl file.
const stl = require('stlfiletools');
var test = new stl.STLfile("test.stl","testSolid");STLfile.triangles
Array of triangles where every element is a array of vertices for the triangle in order of the right hand rule. Triangles can be directly added.
example

const stl = require('stlfiletools');
var PolyGon = new stl.STLfile("solid.stl","solid");
var v1 = new stl.cord(3,0,5);
var v2 = new stl.cord(5,2,0);
var v3 = new stl.cord(4,5,4);
var v4 = new stl.cord(0,6,5);
var v5 = new stl.cord(4,3,6);
var v6 = new stl.cord(0,3,3);
//triangles
PolyGon.triangles.push([v1,v2,v5]);
PolyGon.triangles.push([v5,v2,v3]);
PolyGon.triangles.push([v5,v3,v4]);
PolyGon.triangles.push([v5,v6,v1]);
PolyGon.triangles.push([v1,v6,v2]);
PolyGon.triangles.push([v2,v6,v3]);
PolyGon.triangles.push([v3,v6,v4]);
PolyGon.triangles.push([v4,v6,v5]);
PolyGon.CreateSTLfile(); STLfile.txt()
Returns ASCII txt for stlfile format using the current triangles.
STLfile.CreateSTLfile()
Creates stl file from triangle array. Async function.
Mesh Generation Functions
*All parameters can be set to undefined if not needed.
STLfile.Sphere()
function Sphere(r,pointsPerSlice,bottomStart, RadiusAtPoint)
r : base radius
pointsPerSlice : how many points per revolution,determine how spherical the shape seems(all odd numbers will be changed to even)
bottomStart : cord class,States offset from original position.
RadiusAtPoint : User defined function which returns radius at given point with the parameters theta : anglein radian from positive z axis,epsilon : angle in radian from horizontal axis,r : base radius.
function example(theta,epsilon,r){
return r ;
}
example

const stl = require('stlfiletools');
var TstSph = new stl.STLfile("test.stl","test");
//starts stl file
function example( theta, epsilon, r) {
return r;
}
var offset = new stl.cord(0,0,15);
//Creates sphere with radius 5
TstSph.Sphere(5,30,offset,example);
TstSph.CreateSTLfile();STLfile.RectMeshFromFunction()
*the RectMeshFromFunction has one extra variable elevation then FlatMeshFromFunction
function
RectMeshFromFunction(cellSize,length,width,hightParm,CordAt,offSet,elevation)
- cellSize : distance between one data point and the other
- length : number of points in the y direction actual length is from cellSize * length
- width : number of points in the x direction actual width from cellSize * width
- hightParm : z values from user function is multiplied by this.Not so useful.
- CordAt : function which returns z value from x and y positions.x is between 0 and width,y is between 0 and height(EgFunction(x,y))
function example(x,y){
return 1;
}
- offset : cord class.states offset from original position.
- elevation : value added onto z value.
example
The following example creates a natural looking mesh from Perlin noise where values are mapped to height values.

const stl = require('stlfiletools');
function perlinnoise(x,y){
//~
//perlin code
//~
}
var perlinMesh = new stl.STLfile("test.stl","test");
perlinMesh.RectMeshFromFunction(0.5,140,80,1,perlinnoise,undefined,1);
perlinMesh.CreateSTLfile();STLfile.FlatMeshFromFunction()
function FlatMeshFromFunction(cellSize,length,width,hightParm,CordAt,offSet)- cellSize : distance between one data point and the other
- length : number of points in the y direction actual length is from cellSize * length
- width : number of points in the x direction actual width from cellSize * width
- hightParm : z values from user function is multiplied by this.Not so useful.
- CordAt : user defined function which returns z value from x and y positions.x is between 0 and width,y is between 0 and height.
function example(x,y){
return 1;
}- offset : cord class.states offset from original position.-optional
example
The following example creates a plane from a user defined function cosGrath and then rotates it by multiplying a rotation matrix. 
const stl = require('stlfiletools');
//rotation matrix
var rotation = [[Math.cos(1),0,Math.sin(1)],[0,1,0],[-1*Math.sin(1),0,Math.cos(1)]];
//user defined function
function cosGrath(x,y){
return Math.cos(Math.sqrt(((x*2-50)/10)*((x*2-50)/10) + ((y*2-50)/10) * ((y*2-50)/10)));
}
//sets offset
var offset = new stl.cord(0,0,15);
var FlatMesh = new stl.STLfile("flatMesh.stl","Fmesh");
//creates mesh
FlatMesh.FlatMeshFromFunction(0.1,100,100,1,cosGrath,offset);
FlatMesh.MatrixMult(rotation);//transforms each point by matrix
FlatMesh.CreateSTLfile();STLfile.BlockyMesh()
function BlockyMesh(cellSize,length,width,hightParm,CordAt,offSet,elevation)- cellSize:size of one cell
- length:number of cells in y direction.Physical length is cellSize * length
- Width:number of cells in x direction.Physical length is cellsize * width
- hightParm:z value is times by this
- CordAt:function wich returns a hight value from x and y cordinates,0<=x<=width,0<=y<=height eg:
function At(x,y){
return GrathInfo2dMatrix[x][y];
}- offSet:offset of mesh,cord class with x,y,z
- elevation:hight values are added on to this
Creates blocky mesh.For Graphs and discrete changes.Async function wich returns 0 when finished. Adds necessary triangles to triangles.
example
the following example uses a 2d matrix to map out hight values;

const stl = require('stlfiletools');
var BlockMesh = new stl.STLfile("BlockMesh.stl","BMesh");
var CordMatrix = [
[3,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,4,0,0,0,0,1,0,0,0,5,0,0,0,1],
[0,0,4,5,4,0,0,0,2,0,0,0,0,0,0,0,1],
[0,0,0,4,0,0,0,0,3,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,4,5,5,5,5,5,5,5,5,5],
[2,0,0,0,0,0,0,4,5,5,4,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,4,4,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,2,0,0,0,4,0,0,0,2,0,0,0,5,0,1],
[0,1,5,3,0,5,1,3,0,5,3,4,0,5,3,5,1],
[0,0,4,0,0,0,2,0,0,0,1,0,0,0,5,0,1],
[2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1]
];
function ret(x,y){
return CordMatrix[y][x];
}
var offf = new stl.cord(1,2,1);
BlockMesh.BlockyMesh(5,CordMatrix.length,CordMatrix[0].length,1,ret,offf,5);
BlockMesh.CreateSTLfile();Precise functions
These are functions which are the bases of the mesh Generation functions.
Tool.STLNormal()
function STLNormal(vect1,vect2,vect3)A normal vector in the class cord is returned.The length of the normal vector is not set to 1.The vectors should be listed in order of the right hand rule.
vect1 : cord class
vect2 : cord class
vect3 : cord class
Tool.LogTriangle()
function LogTriangle(vect1,vect2, vect3, normal)Returns a string about the triangle which can be added to a stl file. The vectors should be in order of right hand rule where the normal is pointing out.If the normal is not specified it is evaluated from the input vectors in order.
- vect1 : cord class
- vect2 : cord class
- vect3 : cord class
- normal : cord class
example
const stl = require('./main.js');
var vect1 = {x:10,y:2,z:2};
var vect2 = {x:12,y:12,z:5};
var vect3 = {x:1,y:0,z:3};
console.log(stl.Tool.LogTriangle(vect1,vect2,vect3));output:
facet normal 1.736159e-1 -3.146788e-1 9.331853e-1
outer loop
vertex 1.000000e+1 2.000000e+0 2.000000e+0
vertex 1.200000e+1 1.200000e+1 5.000000e+0
vertex 1.000000e+0 0.000000e+0 3.000000e+0
endloop
endfacetHelper functions
doc.sinGrath()
Built in example for the rect and flat meshes,returns a value based on x and y.
function sinGrath(x,y)x : int as index(width)
y : int as index(len)
doc.RadiusAtPointSphere()
Built in example for sphere mesh,always returns r.
function RadiusAtPointSphere( theta, epsilon, r)- theta : radian angle from pos z axis
- epsilon : radian angle horizontal
- r : number base radius
