Export.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package controller;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.nio.charset.Charset;
  11. import java.nio.file.Files;
  12. import java.nio.file.Path;
  13. import java.nio.file.Paths;
  14. import java.nio.file.StandardCopyOption;
  15. import java.util.List;
  16. import model.Instructions;
  17. import model.Settings;
  18. import model.Settings.Temp;
  19. import model.Settings.Tilt;
  20. import model.Specifications;
  21. import ui.Main;
  22. /**
  23. * The Export-Class that creates the STLs, instructions and the Android-file.
  24. * @author Martin Herbers
  25. *
  26. */
  27. public class Export {
  28. private static Settings settings = Settings.getSettings();
  29. //Depending on the OS, a path with spaces needs different symbols around it. E.g. Windows: "C:\program files\" or MacOS: '/Applications/'
  30. private static String pathComma;
  31. public static void createSTL() {
  32. //Set the console's chaining/parallel starting syntax and the "PathComma" depending on the OS
  33. String start = "";
  34. if (System.getProperty("os.name").contains("Windows")) {
  35. start = " && start ";
  36. pathComma = "\"";
  37. }
  38. else if (System.getProperty("os.name").contains("Linux")) {
  39. start = " & ";
  40. pathComma = "\"";
  41. }
  42. else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
  43. start = " && open -a ";
  44. pathComma = "'";
  45. }
  46. //Part the command in 5 parts that get additions depending on which object got chosen
  47. //comFolder changes the directory to the .exe on Windows or the binary file in MacOs. Not needed in Linux
  48. String comFolder = "cd " + pathComma + settings.getOpenSCADPath() + pathComma;
  49. //Line for the full non-conductive object
  50. String comLine = start + "openscad "+ (System.getProperty("os.name").toLowerCase().contains("mac")?"-W -n --args ":"") +"-o " + pathComma + System.getProperty("user.dir") + File.separator + "output.stl"+ pathComma + " ";
  51. //Line for all conductive parts
  52. String comLine2 = start + "openscad "+ (System.getProperty("os.name").toLowerCase().contains("mac")?"-W -n --args ":"") +"-o " + pathComma + System.getProperty("user.dir") + File.separator + "output_conductive.stl" + pathComma + " ";
  53. //Line for the non-conductive part for cross-section view
  54. String comLine3 = start + "openscad "+ (System.getProperty("os.name").toLowerCase().contains("mac")?"-W -n --args ":"") +"-o " + pathComma + System.getProperty("user.dir") + File.separator + "output_crossSection.stl" + pathComma + " ";
  55. //Line for the conducive parts for cross-section view
  56. String comLine4 = start + "openscad "+ (System.getProperty("os.name").toLowerCase().contains("mac")?"-W -n --args ":"") +"-o " + pathComma + System.getProperty("user.dir") + File.separator + "output_crossSection_conductive.stl" + pathComma + " ";
  57. //Instructions that will be added to the .txt file
  58. List<String> instructions = null;
  59. //Specification of the selected object, used for the android app to show the correct shape
  60. List<String> android = null;
  61. //Copy the OpenSCAD-file temporarily from the .jar, otherwise it can't be used by OpenSCAD.
  62. File tempFile = null;
  63. //If weight is selected...
  64. if (settings.isWeight()) {
  65. tempFile = new File("weight.scad");
  66. //Copy the .scad file
  67. try {
  68. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/weight.scad");
  69. OutputStream out = new FileOutputStream(tempFile);
  70. int read;
  71. byte[] bytes = new byte[1024];
  72. while ((read = in.read(bytes)) != -1) {
  73. out.write(bytes, 0, read);
  74. }
  75. in.close();
  76. out.close();
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. //Add the necessary variables to the command line arguments
  81. int walls = Math.round(settings.getWeight()/27);
  82. comLine += "-D numberSupports=" + walls + " -D conductive=false -D crossSection=false -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "weight.scad" + pathComma;
  83. comLine2+= "-D numberSupports=" + walls + " -D conductive=true -D crossSection=false -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "weight.scad" + pathComma;
  84. comLine3+= "-D numberSupports=" + walls + " -D conductive=false -D crossSection=true -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "weight.scad" + pathComma;
  85. comLine4+= "-D numberSupports=" + walls + " -D conductive=true -D crossSection=true -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "weight.scad" + pathComma;
  86. instructions = Instructions.getWeightInstruction();
  87. android = Specifications.getWeightSpecs();
  88. }
  89. else if (settings.isAcceleration()) {
  90. tempFile = new File("acceleration.scad");
  91. try {
  92. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/acceleration.scad");
  93. OutputStream out = new FileOutputStream(tempFile);
  94. int read;
  95. byte[] bytes = new byte[1024];
  96. while ((read = in.read(bytes)) != -1) {
  97. out.write(bytes, 0, read);
  98. }
  99. in.close();
  100. out.close();
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. double height = (settings.getAcceleration()<8?10:18);//TODO function from acceleration to wall height? linear? exponential? something else?
  105. comLine += "-D wallHeight=" + height + " -D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "acceleration.scad" + pathComma;
  106. comLine2+= "-D wallHeight=" + height + " -D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "acceleration.scad" + pathComma;
  107. comLine3+= "-D wallHeight=" + height + " -D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "acceleration.scad" + pathComma;
  108. comLine4+= "-D wallHeight=" + height + " -D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "acceleration.scad" + pathComma;
  109. instructions = Instructions.getAccelerationInstruction();
  110. android = Specifications.getAccelerationSpecs();
  111. }
  112. else if (settings.isSqueeze()) {
  113. tempFile = new File("squeeze.scad");
  114. try {
  115. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/squeeze.scad");
  116. OutputStream out = new FileOutputStream(tempFile);
  117. int read;
  118. byte[] bytes = new byte[1024];
  119. while ((read = in.read(bytes)) != -1) {
  120. out.write(bytes, 0, read);
  121. }
  122. in.close();
  123. out.close();
  124. } catch (IOException e) {
  125. e.printStackTrace();
  126. }
  127. comLine += "-D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "squeeze.scad" + pathComma;
  128. comLine2+= "-D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "squeeze.scad" + pathComma;
  129. comLine3+= "-D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "squeeze.scad" + pathComma;
  130. comLine4+= "-D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "squeeze.scad" + pathComma;
  131. instructions = Instructions.getSqueezeInstruction();
  132. android = Specifications.getSqueezeSpecs();
  133. }
  134. else if (settings.isTemperature()) {
  135. if (settings.getTemperature() == Temp.OVER0) {
  136. tempFile = new File("iceMelting.scad");
  137. try {
  138. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/iceMelting.scad");
  139. OutputStream out = new FileOutputStream(tempFile);
  140. int read;
  141. byte[] bytes = new byte[1024];
  142. while ((read = in.read(bytes)) != -1) {
  143. out.write(bytes, 0, read);
  144. }
  145. in.close();
  146. out.close();
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. comLine += "-D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceMelting.scad" + pathComma;
  151. comLine2+= "-D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceMelting.scad" + pathComma;
  152. comLine3+= "-D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceMelting.scad" + pathComma;
  153. comLine4+= "-D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceMelting.scad" + pathComma;
  154. instructions = Instructions.getTemperatureMeltInstruction();
  155. android = Specifications.getTemperatureMeltSpecs();
  156. }
  157. else {
  158. tempFile = new File("iceFreezing.scad");
  159. try {
  160. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/iceFreezing.scad");
  161. OutputStream out = new FileOutputStream(tempFile);
  162. int read;
  163. byte[] bytes = new byte[1024];
  164. while ((read = in.read(bytes)) != -1) {
  165. out.write(bytes, 0, read);
  166. }
  167. in.close();
  168. out.close();
  169. } catch (IOException e) {
  170. e.printStackTrace();
  171. }
  172. comLine += "-D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceFreezing.scad" + pathComma;
  173. comLine2+= "-D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceFreezing.scad" + pathComma;
  174. comLine3+= "-D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceFreezing.scad" + pathComma;
  175. comLine4+= "-D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "iceFreezing.scad" + pathComma;
  176. instructions = Instructions.getTemperatureFreezeInstruction();
  177. android = Specifications.getTemperatureFreezeSpecs();
  178. }
  179. }
  180. else if (settings.isTilt()) {
  181. if (settings.getTilting().equals(Tilt.FLIP)) {
  182. tempFile = new File("flip.scad");
  183. try {
  184. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/flip.scad");
  185. OutputStream out = new FileOutputStream(tempFile);
  186. int read;
  187. byte[] bytes = new byte[1024];
  188. while ((read = in.read(bytes)) != -1) {
  189. out.write(bytes, 0, read);
  190. }
  191. in.close();
  192. out.close();
  193. } catch (IOException e) {
  194. e.printStackTrace();
  195. }
  196. comLine += "-D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "flip.scad" + pathComma;
  197. comLine2+= "-D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "flip.scad" + pathComma;
  198. comLine3+= "-D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "flip.scad" + pathComma;
  199. comLine4+= "-D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "flip.scad" + pathComma;
  200. instructions = Instructions.getFlipInstruction();
  201. android = Specifications.getFlipSpecs();
  202. }
  203. else {
  204. tempFile = new File("tilt.scad");
  205. try {
  206. InputStream in = Export.class.getResourceAsStream("/OpenSCADFiles/tilt.scad");
  207. OutputStream out = new FileOutputStream(tempFile);
  208. int read;
  209. byte[] bytes = new byte[1024];
  210. while ((read = in.read(bytes)) != -1) {
  211. out.write(bytes, 0, read);
  212. }
  213. in.close();
  214. out.close();
  215. } catch (IOException e) {
  216. e.printStackTrace();
  217. }
  218. comLine += "-D conductive=false -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "tilt.scad" + pathComma;
  219. comLine2+= "-D conductive=true -D crossSection=false -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "tilt.scad" + pathComma;
  220. comLine3+= "-D conductive=false -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "tilt.scad" + pathComma;
  221. comLine4+= "-D conductive=true -D crossSection=true -D fillLater="+ settings.getFill() +" -D sizeScreen=" + settings.getSizeScreen() + " -D sizeFinger=" + settings.getSizeFinger() + " " + pathComma + System.getProperty("user.dir") + File.separator + "tilt.scad" + pathComma;
  222. instructions = Instructions.getTiltInstruction();
  223. android = Specifications.getTiltSpecs();
  224. }
  225. }
  226. //ADD CODE HERE FOR ADDITIONAL INTERACTION
  227. //(else if()...)
  228. //Needs:
  229. //Copying openscad-file to current folder
  230. //Commandline addition for each view-part
  231. //Instructions-text and object-specification
  232. if (instructions == null || android == null)
  233. return;
  234. String[] arguments = new String[3];
  235. //Depending on the os more arguments are needed to interpret the strings as commands
  236. if (System.getProperty("os.name").contains("Windows")) {
  237. arguments[0] = "cmd.exe";
  238. arguments[1] = "/c";
  239. }
  240. else if (System.getProperty("os.name").contains("Linux")) {
  241. arguments[0] = "bash";
  242. arguments[1] = "-c";
  243. }
  244. else {
  245. arguments[0] = "/bin/bash";
  246. arguments[1] = "-c";
  247. }
  248. arguments[2] = (System.getProperty("os.name").contains("Linux")?"":comFolder) +
  249. (System.getProperty("os.name").contains("Linux")?comLine.substring(3):comLine) +
  250. comLine2 + comLine4 + comLine3;
  251. //Run the command line
  252. try {
  253. Process p = Runtime.getRuntime().exec(arguments);
  254. BufferedReader stdOut=new BufferedReader(new InputStreamReader(p.getInputStream()));
  255. @SuppressWarnings("unused")
  256. String s;
  257. while((s=stdOut.readLine())!=null){
  258. System.out.println(s);
  259. }
  260. Main.finished();
  261. } catch (IOException e1) {
  262. e1.printStackTrace();
  263. }
  264. //Create the instructions .txt file
  265. Path file = Paths.get("instructions.txt");
  266. try {
  267. Files.write(file, instructions, Charset.forName("UTF-8"));
  268. } catch (IOException e) {
  269. e.printStackTrace();
  270. }
  271. //Create the objectSpecs.txt file
  272. Path output = Paths.get("objectSpecs.txt");
  273. try {
  274. Files.write(output, android, Charset.forName("UTF-8"));
  275. } catch (IOException e) {
  276. e.printStackTrace();
  277. }
  278. //Delete all created files after closing the programm. If you want permanent files, either use the "save object" button or copy the files before closing the programm.
  279. File f1 = new File(System.getProperty("user.dir") + File.separator + "output.stl");
  280. f1.deleteOnExit();
  281. File f2 = new File(System.getProperty("user.dir") + File.separator + "output_conductive.stl");
  282. f2.deleteOnExit();
  283. File f3 = new File(System.getProperty("user.dir") + File.separator + "output_crossSection.stl");
  284. f3.deleteOnExit();
  285. File f4 = new File(System.getProperty("user.dir") + File.separator + "output_crossSection_conductive.stl");
  286. f4.deleteOnExit();
  287. File f5 = new File(System.getProperty("user.dir") + File.separator + "instructions.txt");
  288. f5.deleteOnExit();
  289. File f6 = new File(System.getProperty("user.dir") + File.separator + "objectSpecs.txt");
  290. f6.deleteOnExit();
  291. //Delete the OpenSCAD file
  292. tempFile.delete();
  293. }
  294. /**
  295. * Copy all files that are necessary for printing and using the object to a location of your choice.
  296. * @param folder directory where the files will be saved to
  297. */
  298. public static void saveFilesTo(File folder) {
  299. try {
  300. File output = new File(new java.io.File( "." ).getCanonicalPath() + File.separator + "output.stl");
  301. Files.copy(output.toPath(), new File(folder.getAbsolutePath() + File.separator + "output.stl").toPath(), StandardCopyOption.REPLACE_EXISTING);
  302. File outputCond = new File(new java.io.File( "." ).getCanonicalPath() + File.separator + "output_conductive.stl");
  303. Files.copy(outputCond.toPath(), new File(folder.getAbsolutePath() + File.separator + "output_conductive.stl").toPath(), StandardCopyOption.REPLACE_EXISTING);
  304. File instructions = new File(new java.io.File( "." ).getCanonicalPath() + File.separator + "instructions.txt");
  305. Files.copy(instructions.toPath(), new File(folder.getAbsolutePath() + File.separator + "instructions.txt").toPath(), StandardCopyOption.REPLACE_EXISTING);
  306. File android = new File(new java.io.File( "." ).getCanonicalPath() + File.separator + "objectSpecs.txt");
  307. Files.copy(android.toPath(), new File(folder.getAbsolutePath() + File.separator + "objectSpecs.txt").toPath(), StandardCopyOption.REPLACE_EXISTING);
  308. } catch (IOException e) {
  309. e.printStackTrace();
  310. }
  311. }
  312. }