// dat_to_stl.java read .dat file 3D, write .stl file // .dat needs all surface as triangles, tri_split import java.io.*; import java.util.*; import java.text.*; // needs datread.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 dat_to_stl { boolean debug = true; 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"; int pts, pt, k; int dpts[] = new int[20]; // needs poly_split for more than 3 datread DR; // = new datread(filename); // defines data structures int num_points = 0; // set from datread int num_polys = 0; // set from datread double nx, ny, nz; // normal double x[] = new double[20]; double y[] = new double[20]; double z[] = new double[20]; dat_to_stl(String filein, String fileout) // callable { System.out.println("datread.java reading "+filein); DR = new datread(filein); // defines data structures num_points = DR.num_points; num_polys = DR.num_polys; System.out.println("num_points="+num_points+ ", num_polys="+num_polys); try { BufferedWriter out = new BufferedWriter(new FileWriter(fileout)); PrintWriter fout = new PrintWriter(out); fout.println("solid "+fileout); System.out.println("print polygons"); k = 0; // polygons for(int i=0; i3 && j==pts-3) { System.out.println("vert "+(dpts[j+0]+1)+ " "+(dpts[j+1]+1)+ " "+(dpts[0]+1)); } else { System.out.println("vert "+(dpts[j+0]+1)+ " "+(dpts[j+1]+1)+ " "+(dpts[j+2]+1)); } if(pts>3 && j==pts-3) { System.out.println("xyz "+x[j+0]+" "+y[j+0]+" "+z[j+0]); System.out.println("xyz "+x[j+1]+" "+y[j+1]+" "+z[j+1]); System.out.println("xyz "+x[0] +" "+y[0]+ " "+z[0]); } else { System.out.println("xyz "+x[j+0]+" "+y[j+0]+" "+z[j+0]); System.out.println("xyz "+x[j+1]+" "+y[j+1]+" "+z[j+1]); System.out.println("xyz "+x[j+2]+" "+y[j+2]+" "+z[j+2]); } if(pts>3 && j==pts-3) { System.out.println("normal x="+nx+", y="+ny+", z="+nz); fout.println(" facet normal "+nx+" "+ny+" "+nz+" "); fout.println(" outer loop"); fout.println(" vertex "+x[j+0]+" "+y[j+0]+" "+z[j+0]+" "); fout.println(" vertex "+x[j+1]+" "+y[j+1]+" "+z[j+1]+" "); fout.println(" vertex "+x[0]+" " +y[0]+" " +z[0]+" "); fout.println(" endloop "); fout.println(" endfacet "); } else { System.out.println("normal x="+nx+", y="+ny+", z="+nz); fout.println(" facet normal "+nx+" "+ny+" "+nz+" "); fout.println(" outer loop"); fout.println(" vertex "+x[j+0]+" "+y[j+0]+" "+z[j+0]+" "); fout.println(" vertex "+x[j+1]+" "+y[j+1]+" "+z[j+1]+" "); fout.println(" vertex "+x[j+2]+" "+y[j+2]+" "+z[j+2]+" "); fout.println(" endloop "); fout.println(" endfacet "); } } // end j pts } // end i num_polys fout.println("endsolid"); out.close(); } // end try catch(IOException exception) { System.out.println("dat_to_stl.java "+exception); } System.out.println("dat_to_stl.java wrote "+fileout); } // end dat_to_stl void normal(double x[], double y[], double z[]) // of triangle { double ax, ay, az, bx, by, bz, ss; // nx, ny, nz global ax = x[2] - x[0]; ay = y[2] - y[0]; az = z[2] - z[0]; bx = x[1] - x[0]; by = y[1] - y[0]; bz = z[1] - z[0]; nx = ay*bz-az*by; ny = az*bx-ax*bz; nz = ax*by-ay*bx; ss = Math.sqrt(nx*nx+ny*ny+nz*nz); nx = nx / ss; ny = ny / ss; nz = nz / ss; } // end normal public static void main (String[] args) { new dat_to_stl(args[0],args[1]); // .dat .stl } } // end class dat_to_stl