Export.java 18 KB

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