Jmonkey STL Writer

Hey, so I wanted to print some procedurally generated stuff I was making in jmonkey and found the resources for producing STL files in java to be limited.

I found one binary STL writer that seemed to be what I was looking for, but none of the files I produced were able to be read by any of the programs I tried.

I don’t know if there’s anyone else really procedurally generating models in jmonkey that they would like to print with a 3d printer, but I whipped up a quick ASCII version that works just fine with my models.

Directions for use:
new ASCIISTLWriter(file name)
writer.beginSave()

Then you iterate over all your triangles and do
writer.face(the three triangle points)
You want the points entered in counter clockwise order or the normals will be reversed.

Lastly you do
writer.endSave();

Then in your project folder should be a file with the filename you entered, which can be opened in any program that uses STL files, and is even human readable because it is ASCII. The file size can get quite large though, once again because it is ASCII.

Code:
[java]
import com.jme3.math.Plane;
import com.jme3.math.Vector3f;
import java.io.FileWriter;
import java.io.IOException;

/**

*/
public class ASCIISTLWriter {

protected FileWriter fw;

protected Vector3f scale = new Vector3f(1, 1, 1);

    protected String fileName;
    
    public ASCIISTLWriter (String FileName){
        this.fileName=FileName;
    }
    
public void beginSave() {
	try {
		fw = new FileWriter(fileName,true);
		writeHeader();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public void endSave() {
	try {
                    fw.write("endsolid "+fileName);
		fw.flush();
		fw.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public void face(Vector3f a, Vector3f b, Vector3f c) {
	try {
		
                    Vector3f normal;
                    //normal from plane
		Plane p = new Plane();
                    p.setPlanePoints(a, b, c);
                    normal = p.getNormal();
                    
                    //Write the normal for the facet (triangle)
		fw.write("facet normal "+normal.x+" "+normal.y+" "+normal.z+System.getProperty( "line.separator" ));
                    //start list of points
                    fw.write("outer loop"+System.getProperty( "line.separator" ));
                    //write the three vertexes
                    fw.write("vertex "+a.x+" "+a.y+" "+a.z+System.getProperty( "line.separator" ));
                    fw.write("vertex "+b.x+" "+b.y+" "+b.z+System.getProperty( "line.separator" ));
                    fw.write("vertex "+c.x+" "+c.y+" "+c.z+System.getProperty( "line.separator" ));
                    //end the end list of points
                    fw.write("endloop"+System.getProperty( "line.separator" ));
                    //end facet (triangle)
                    fw.write("endfacet"+System.getProperty( "line.separator" ));
	} catch (IOException e) {
		e.printStackTrace();
	}
}

protected void writeHeader() throws IOException {
	fw.write("solid "+fileName+System.getProperty( "line.separator" ));
}

}

[/java]