// stl_to_dat.java read .stl file 3D into .dat file import java.io.*; import java.util.*; // needs datwrite.java // solid rect sample .stl format // facet normal 0.0 0.0 -1.0 // outer loop // vertex 1.0 1.0 1.0 // vertex 5.0 4.0 1.0 // vertex 1.0 4.0 1.0 // endloop // endfacet // facet normal 0.0 0.0 -1.0 // outer loop // vertex 1.0 1.0 1.0 // vertex 5.0 1.0 1.0 // vertex 5.0 4.0 1.0 // endloop // endfacet //... // endsolid public class stl_to_dat { boolean debug = false; int num_points = 0; int num_polys = 0; // all will be triangles 3,pt1,pt2,pt3 double x[] = new double[10000]; double y[] = new double[10000]; double z[] = new double[10000]; double xyz[][] = new double[10000][3]; double xmin, xmax, ymin, ymax, zmin, zmax; int data_polys[] = new int[10000]; int npt = 0; int pts[] = new int[3]; int ipt; // unique StringTokenizer T; String token; String solid = "solid"; String facet = "facet"; String normal = "normal"; String outer = "outer"; String loop = "loop"; String vertex = "vertex"; String endloop = "endloop"; String endfacet = "endfacet"; String endsolid = "endsolid"; stl_to_dat(String filein, String fileout) // callable { System.out.println("stl_to_dat.java reading "+filein); try { FileReader fin = new FileReader(filein); BufferedReader in = new BufferedReader(fin); String input_line; int len; while(true) // to endsolid { input_line = in.readLine(); // len = input_line.length(); if(debug) System.out.println("input:"+input_line+" len="+len); T = new StringTokenizer(input_line," "); token = T.nextToken(); // ? if(debug) System.out.println("token:"+token); if(token.equals(endsolid)) break; if(token.equals(endloop)) { data_polys[num_polys] = 3; data_polys[num_polys+1] = pts[0]; data_polys[num_polys+2] = pts[1]; data_polys[num_polys+3] = pts[2]; num_polys += 4; npt = 0; continue; } if(token.equals(endfacet)) continue; if(token.equals(vertex)) { token = T.nextToken(); // x if(debug) System.out.println("token:"+token); x[num_points] = Double.parseDouble(token); token = T.nextToken(); // y if(debug) System.out.println("token:"+token); y[num_points] = Double.parseDouble(token); token = T.nextToken(); // z if(debug) System.out.println("token:"+token); z[num_points] = Double.parseDouble(token); num_points++; ipt = unique(); pts[npt] = ipt; // one big per .dat npt++; } } // end while in.close(); } // end try catch(IOException exception) { System.out.println("datread.java "+exception); } System.out.println("unique data points "+num_points); xmin = x[0]; xmax = x[0]; ymin = y[0]; ymax = y[0]; zmin = z[0]; zmax = z[0]; for(int i=0; i