Main.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package ui;
  2. import com.interactivemesh.jfx.importer.stl.StlMeshImporter;
  3. import controller.Controller;
  4. import controller.Export;
  5. import controller.SettingsController;
  6. import javafx.animation.KeyFrame;
  7. import javafx.animation.Timeline;
  8. import javafx.application.Application;
  9. import javafx.application.Platform;
  10. import javafx.collections.FXCollections;
  11. import javafx.collections.ObservableList;
  12. import javafx.concurrent.Task;
  13. import javafx.fxml.FXMLLoader;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.*;
  16. import javafx.scene.control.*;
  17. import javafx.scene.image.Image;
  18. import javafx.scene.image.ImageView;
  19. import javafx.scene.layout.*;
  20. import javafx.scene.paint.Color;
  21. import javafx.scene.paint.PhongMaterial;
  22. import javafx.scene.shape.Mesh;
  23. import javafx.scene.shape.MeshView;
  24. import javafx.scene.transform.Rotate;
  25. import javafx.stage.DirectoryChooser;
  26. import javafx.stage.Stage;
  27. import javafx.stage.StageStyle;
  28. import javafx.util.Duration;
  29. import model.Settings;
  30. import java.io.BufferedReader;
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.io.InputStreamReader;
  34. import java.util.Collections;
  35. public class Main extends Application {
  36. //Save the last x/y value of the mouse, used for rotating the object in the javaFX panel
  37. private int lastX = 0;
  38. private int lastY = 0;
  39. //Which view is currently used for the javaFX panel
  40. private static final int VIEW_ALL = 0;
  41. private static final int VIEW_CONDUCTIVE = 1;
  42. private static final int VIEW_NON_CONDUCTIVE = 2;
  43. private static final int VIEW_CROSS_SECTION = 3;
  44. //If creating the STLs is finished. If yes, change the javaFX view
  45. private static boolean finished = false;
  46. //If the openSCAD installation got detected. If not, show a message.
  47. private static boolean detected = true;
  48. private CheckBox accelerationCheckBox;
  49. private CheckBox weightCheckBox;
  50. private CheckBox tempCheckBox;
  51. private CheckBox tiltingCheckBox;
  52. private CheckBox pressureCheckBox;
  53. private Button createObjectButton;
  54. private ComboBox<String> comboBox;
  55. private Pane fxPanel;
  56. private Group rootGroup;
  57. private StackPane glassPane;
  58. private Parent root;
  59. private Spinner<String> accelerationSpinner;
  60. private Spinner<String> loadSpinner;
  61. @Override
  62. public void start(Stage primaryStage) throws Exception{
  63. root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
  64. primaryStage.setTitle("Off-Line Interactor Printing");
  65. primaryStage.setOnCloseRequest(e -> {
  66. try {
  67. Platform.exit();
  68. }
  69. catch (Exception e1) {
  70. e1.printStackTrace();
  71. }
  72. });
  73. Node nodeAccel = root.lookup("#accelerationPane");
  74. TitledPane pane = (TitledPane) nodeAccel;
  75. BorderPane borderPane = new BorderPane();
  76. accelerationCheckBox = new CheckBox();
  77. borderPane.setLeft(accelerationCheckBox);
  78. BorderPane.setAlignment(accelerationCheckBox, Pos.CENTER_LEFT);
  79. Label accelerationLabel = new Label(" Acceleration");
  80. accelerationLabel.setOnMouseClicked(event -> {
  81. accelerationCheckBox.setSelected(!accelerationCheckBox.isSelected());
  82. accelerationClicked();
  83. });
  84. Image imgAccelSymbol = new Image(getClass().getResource("/pictures/acceleration.png").toString());
  85. ImageView imViewAccelSymbol = new ImageView(imgAccelSymbol);
  86. imViewAccelSymbol.setFitWidth(30);
  87. imViewAccelSymbol.setFitHeight(30);
  88. accelerationLabel.setGraphic(imViewAccelSymbol);
  89. borderPane.setCenter(accelerationLabel);
  90. pane.setGraphic(borderPane);
  91. accelerationCheckBox.setOnAction(event -> {
  92. accelerationClicked();
  93. event.consume();
  94. });
  95. Node nodeWeight = root.lookup("#weightPane");
  96. TitledPane paneWeight = (TitledPane) nodeWeight;
  97. BorderPane borderPaneWeight = new BorderPane();
  98. weightCheckBox = new CheckBox();
  99. borderPaneWeight.setLeft(weightCheckBox);
  100. BorderPane.setAlignment(weightCheckBox, Pos.CENTER_LEFT);
  101. Label weightLabel = new Label(" Load");
  102. weightLabel.setOnMouseClicked(event -> {
  103. weightCheckBox.setSelected(!weightCheckBox.isSelected());
  104. weightClicked();
  105. });
  106. Image imgWeightSymbol = new Image(getClass().getResource("/pictures/weight.png").toString());
  107. ImageView imViewWeightSymbol = new ImageView(imgWeightSymbol);
  108. imViewWeightSymbol.setFitWidth(30);
  109. imViewWeightSymbol.setFitHeight(30);
  110. weightLabel.setGraphic(imViewWeightSymbol);
  111. borderPaneWeight.setCenter(weightLabel);
  112. paneWeight.setGraphic(borderPaneWeight);
  113. weightCheckBox.setOnAction(event -> {
  114. weightClicked();
  115. event.consume();
  116. });
  117. Node nodeTemp = root.lookup("#temperaturePane");
  118. TitledPane paneTemp = (TitledPane) nodeTemp;
  119. BorderPane borderPaneTemp = new BorderPane();
  120. tempCheckBox = new CheckBox();
  121. borderPaneTemp.setLeft(tempCheckBox);
  122. BorderPane.setAlignment(tempCheckBox, Pos.CENTER_LEFT);
  123. Label tempLabel = new Label(" Temperature");
  124. tempLabel.setOnMouseClicked(event -> {
  125. tempCheckBox.setSelected(!tempCheckBox.isSelected());
  126. temperatureClicked();
  127. });
  128. Image imgTempSymbol = new Image(getClass().getResource("/pictures/temperature.png").toString());
  129. ImageView imViewTempSymbol = new ImageView(imgTempSymbol);
  130. imViewTempSymbol.setFitWidth(14);
  131. imViewTempSymbol.setFitHeight(30);
  132. tempLabel.setGraphic(imViewTempSymbol);
  133. borderPaneTemp.setCenter(tempLabel);
  134. paneTemp.setGraphic(borderPaneTemp);
  135. tempCheckBox.setOnAction(event -> {
  136. temperatureClicked();
  137. event.consume();
  138. });
  139. Node nodeRotation = root.lookup("#rotationPane");
  140. TitledPane paneRotation = (TitledPane) nodeRotation;
  141. BorderPane borderPaneRotation = new BorderPane();
  142. tiltingCheckBox = new CheckBox();
  143. borderPaneRotation.setLeft(tiltingCheckBox);
  144. BorderPane.setAlignment(tiltingCheckBox, Pos.CENTER_LEFT);
  145. Label rotationLabel = new Label(" Tilting");
  146. rotationLabel.setOnMouseClicked(event -> {
  147. tiltingCheckBox.setSelected(!tiltingCheckBox.isSelected());
  148. tiltingClicked();
  149. });
  150. Image imgRotationSymbol = new Image(getClass().getResource("/pictures/rotation.png").toString());
  151. ImageView imViewRotationSymbol = new ImageView(imgRotationSymbol);
  152. imViewRotationSymbol.setFitWidth(30);
  153. imViewRotationSymbol.setFitHeight(30);
  154. rotationLabel.setGraphic(imViewRotationSymbol);
  155. borderPaneRotation.setCenter(rotationLabel);
  156. paneRotation.setGraphic(borderPaneRotation);
  157. tiltingCheckBox.setOnAction(event -> {
  158. tiltingClicked();
  159. event.consume();
  160. });
  161. Node nodePressure = root.lookup("#pressurePane");
  162. TitledPane panePressure = (TitledPane) nodePressure;
  163. BorderPane borderPanePressure = new BorderPane();
  164. pressureCheckBox = new CheckBox();
  165. borderPanePressure.setLeft(pressureCheckBox);
  166. BorderPane.setAlignment(pressureCheckBox, Pos.CENTER_LEFT);
  167. Label pressureLabel = new Label(" Pressure");
  168. pressureLabel.setOnMouseClicked(event -> {
  169. pressureCheckBox.setSelected(!pressureCheckBox.isSelected());
  170. pressureClicked();
  171. });
  172. Image imgPressureSymbol = new Image(getClass().getResource("/pictures/pressure.png").toString());
  173. ImageView imViewPressureSymbol = new ImageView(imgPressureSymbol);
  174. imViewPressureSymbol.setFitWidth(30);
  175. imViewPressureSymbol.setFitHeight(30);
  176. pressureLabel.setGraphic(imViewPressureSymbol);
  177. borderPanePressure.setCenter(pressureLabel);
  178. panePressure.setGraphic(borderPanePressure);
  179. pressureCheckBox.setOnAction(event -> {
  180. pressureClicked();
  181. event.consume();
  182. });
  183. primaryStage.setScene(new Scene(root, 1280, 720));
  184. primaryStage.show();
  185. accelerationLabel.setMinWidth(((TitledPane) nodeAccel).getWidth() * 0.83);
  186. weightLabel.setMinWidth(((TitledPane) nodeAccel).getWidth() * 0.83);
  187. tempLabel.setMinWidth(((TitledPane) nodeAccel).getWidth() * 0.83);
  188. rotationLabel.setMinWidth(((TitledPane) nodeAccel).getWidth() * 0.83);
  189. pressureLabel.setMinWidth(((TitledPane) nodeAccel).getWidth() * 0.83);
  190. ObservableList<String> accelerationEntries = FXCollections.observableArrayList("5.0 m/s²", "12.0 m/s²");
  191. accelerationSpinner = new Spinner<>();
  192. SpinnerValueFactory<String> accelerationValueFactory = new SpinnerValueFactory<String>() {
  193. @Override
  194. public void decrement(int steps) {
  195. String current = this.getValue();
  196. int index = accelerationEntries.indexOf(current);
  197. this.setValue(accelerationEntries.get((accelerationEntries.size() + index - steps)%accelerationEntries.size()));
  198. double value = Double.parseDouble(this.getValue().replace(" m/s²", ""));
  199. Controller.getC().setAcceleration(value);
  200. }
  201. @Override
  202. public void increment(int steps) {
  203. String current = this.getValue();
  204. int index = accelerationEntries.indexOf(current);
  205. this.setValue(accelerationEntries.get((index + steps)%accelerationEntries.size()));
  206. double value = Double.parseDouble(this.getValue().replace(" m/s²", ""));
  207. Controller.getC().setAcceleration(value);
  208. }
  209. };
  210. accelerationValueFactory.setValue("5.0 m/s²");
  211. accelerationSpinner.setValueFactory(accelerationValueFactory);
  212. accelerationSpinner.setDisable(true);
  213. accelerationSpinner.setMaxWidth(120);
  214. HBox accelHBox = ((HBox)root.lookup("#accelerationContent"));
  215. accelHBox.getChildren().add(accelerationSpinner);
  216. ObservableList<Node> workingCollection = FXCollections.observableArrayList(accelHBox.getChildren());
  217. Collections.swap(workingCollection, 1, 3);
  218. Collections.swap(workingCollection, 2, 3);
  219. accelHBox.getChildren().setAll(workingCollection);
  220. ObservableList<String> loadEntries = FXCollections.observableArrayList("5.4 Kg", "10.8 Kg", "16.2 Kg");
  221. loadSpinner = new Spinner<>();
  222. SpinnerValueFactory<String> loadValueFactory = new SpinnerValueFactory<String>() {
  223. @Override
  224. public void decrement(int steps) {
  225. String current = this.getValue();
  226. int index = loadEntries.indexOf(current);
  227. this.setValue(loadEntries.get((loadEntries.size() + index - steps)%loadEntries.size()));
  228. double value = Double.parseDouble(this.getValue().replace(" Kg", ""));
  229. Controller.getC().setWeight((int)(value*10));
  230. }
  231. @Override
  232. public void increment(int steps) {
  233. String current = this.getValue();
  234. int index = loadEntries.indexOf(current);
  235. this.setValue(loadEntries.get((index + steps)%loadEntries.size()));
  236. double value = Double.parseDouble(this.getValue().replace(" Kg", ""));
  237. Controller.getC().setWeight((int)(value*10));
  238. }
  239. };
  240. loadValueFactory.setValue("5.4 Kg");
  241. loadSpinner.setValueFactory(loadValueFactory);
  242. loadSpinner.setDisable(true);
  243. loadSpinner.setMaxWidth(120);
  244. HBox loadHBox = ((HBox)root.lookup("#weightContent"));
  245. loadHBox.getChildren().add(loadSpinner);
  246. ObservableList<Node> loadWorkingCollection = FXCollections.observableArrayList(loadHBox.getChildren());
  247. Collections.swap(loadWorkingCollection, 1, 3);
  248. Collections.swap(loadWorkingCollection, 2, 3);
  249. loadHBox.getChildren().setAll(loadWorkingCollection);
  250. ToggleGroup groupTemp = new ToggleGroup();
  251. ((RadioButton) root.lookup("#temperatureRadio1")).setToggleGroup(groupTemp);
  252. ((RadioButton) root.lookup("#temperatureRadio2")).setToggleGroup(groupTemp);
  253. ((RadioButton) root.lookup("#temperatureRadio1")).selectedProperty().addListener((observable, oldValue, newValue) -> {
  254. if (newValue)
  255. Controller.getC().setTemperature(Settings.Temp.OVER0);
  256. });
  257. ((RadioButton) root.lookup("#temperatureRadio2")).selectedProperty().addListener((observable, oldValue, newValue) -> {
  258. if (newValue)
  259. Controller.getC().setTemperature(Settings.Temp.UNDER0);
  260. });
  261. ToggleGroup groupTilt = new ToggleGroup();
  262. ((RadioButton) root.lookup("#rotationRadio1")).setToggleGroup(groupTilt);
  263. ((RadioButton) root.lookup("#rotationRadio2")).setToggleGroup(groupTilt);
  264. ((RadioButton) root.lookup("#rotationRadio1")).selectedProperty().addListener((observable, oldValue, newValue) -> {
  265. if (newValue)
  266. Controller.getC().setTilt(Settings.Tilt.TILT);
  267. });
  268. ((RadioButton) root.lookup("#rotationRadio2")).selectedProperty().addListener((observable, oldValue, newValue) -> {
  269. if (newValue)
  270. Controller.getC().setTilt(Settings.Tilt.FLIP);
  271. });
  272. fxPanel = (Pane) root.lookup("#fxPanel");
  273. SubScene subScene = createScene();
  274. fxPanel.getChildren().add(subScene);
  275. subScene.heightProperty().bind(fxPanel.heightProperty());
  276. subScene.widthProperty().bind(fxPanel.widthProperty());
  277. fxPanel.setOnMousePressed(event -> {
  278. lastX = (int) event.getX();
  279. lastY = (int) event.getY();
  280. });
  281. fxPanel.setOnMouseDragged(event -> {
  282. //Rotation
  283. Group root1 = rootGroup;//(SubScene) fxPanel.getChildren().get(1);
  284. Rotate rY = (Rotate) root1.getTransforms().get(0);
  285. rY.setAngle(rY.getAngle()+ event.getY() - lastY);
  286. lastY = (int) event.getY();
  287. Rotate rX = (Rotate) root1.getTransforms().get(1);
  288. rX.setAngle(rX.getAngle()+ event.getX() - lastX);
  289. lastX = (int) event.getX();
  290. });
  291. fxPanel.setOnScroll(event -> {
  292. //Zooming
  293. Group root12 = rootGroup;//(SubScene) fxPanel.getChildren().get(1);
  294. int direction;
  295. if (root12.getScaleX() > 0) {
  296. if (event.getDeltaY() > 0)
  297. direction = 1;
  298. else
  299. direction = -1;
  300. }
  301. else {
  302. if (event.getDeltaY() > 0)
  303. direction = 1;
  304. else
  305. direction = 0;
  306. }
  307. root12.setScaleX(root12.getScaleX() + direction);
  308. root12.setScaleY(root12.getScaleY() + direction);
  309. root12.setScaleZ(root12.getScaleZ() + direction);
  310. });
  311. comboBox = (ComboBox<String>) root.lookup("#comboBox");
  312. ObservableList<String> options = FXCollections.observableArrayList(
  313. "Show All Parts",
  314. "Show Conductive Only",
  315. "Show Non-Conductive Only",
  316. "Show Cross-Section"
  317. );
  318. comboBox.getItems().addAll(options);
  319. comboBox.setValue(options.get(3));
  320. comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> Platform.runLater(() -> {
  321. clearView((SubScene)fxPanel.getChildren().get(0));
  322. int index = -1;
  323. switch (newValue) {
  324. case "Show All Parts": index = 0; break;
  325. case "Show Conductive Only": index = 1; break;
  326. case "Show Non-Conductive Only": index = 2; break;
  327. case "Show Cross-Section": index = 3; break;
  328. }
  329. changeObject((SubScene)fxPanel.getChildren().get(0), index);
  330. }));
  331. createObjectButton = (Button) root.lookup("#createObjectButton");
  332. Image imageCreate = new Image(getClass().getResource("/pictures/cog.png").toString());
  333. ImageView imageViewCreate = new ImageView(imageCreate);
  334. imageViewCreate.setFitWidth(30);
  335. imageViewCreate.setFitHeight(30);
  336. createObjectButton.setGraphic(imageViewCreate);
  337. createObjectButton.setContentDisplay(ContentDisplay.LEFT);
  338. createObjectButton.setOnAction(event -> {
  339. Task task = new Task() {
  340. @Override
  341. protected Void call() {
  342. Export.createSTL();
  343. return null;
  344. }
  345. };
  346. task.setOnSucceeded( e -> glassPane.setVisible(false));
  347. task.setOnRunning(e -> {
  348. glassPane.setVisible(true);
  349. clearView((SubScene) fxPanel.getChildren().get(0));
  350. this.root.lookup("#saveObjectButton").setDisable(true);
  351. });
  352. new Thread(task).start();
  353. //Same as above
  354. comboBox.setDisable(false);
  355. Task task2 = new Task() {
  356. @Override
  357. protected Void call() {
  358. while (!finished) {
  359. try {
  360. Thread.sleep(100);
  361. } catch (InterruptedException e) {
  362. e.printStackTrace();
  363. }
  364. }
  365. finished = false;
  366. int index = -1;
  367. switch (comboBox.getValue()) {
  368. case "Show All Parts": index = 0; break;
  369. case "Show Conductive Only": index = 1; break;
  370. case "Show Non-Conductive Only": index = 2; break;
  371. case "Show Cross-Section": index = 3; break;
  372. }
  373. int finalIndex = index;
  374. Platform.runLater(() -> changeObject((SubScene) fxPanel.getChildren().get(0), finalIndex));
  375. return null;
  376. }
  377. };
  378. new Thread(task2).start();
  379. });
  380. Button saveObjectButton = ((Button) root.lookup("#saveObjectButton"));
  381. Image imageSave = new Image(getClass().getResource("/pictures/save.png").toString());
  382. ImageView imageViewSave = new ImageView(imageSave);
  383. imageViewSave.setFitWidth(30);
  384. imageViewSave.setFitHeight(30);
  385. saveObjectButton.setGraphic(imageViewSave);
  386. saveObjectButton.setContentDisplay(ContentDisplay.LEFT);
  387. saveObjectButton.setOnAction(event -> {
  388. DirectoryChooser chooser = new DirectoryChooser();
  389. chooser.setTitle("Save to Folder");
  390. Platform.runLater(() -> {
  391. File file = chooser.showDialog(primaryStage);
  392. if (file != null) {
  393. Export.saveFilesTo(file);
  394. }
  395. });
  396. });
  397. Image image = new Image(getClass().getResource("/pictures/information-icon-6086.png").toString());
  398. ImageView imViewAccel = new ImageView(image);
  399. imViewAccel.setFitWidth(15);
  400. imViewAccel.setFitHeight(15);
  401. ((Button) root.lookup("#accelerationInfo")).setGraphic(imViewAccel);
  402. ImageView imViewWeight = new ImageView(image);
  403. imViewWeight.setFitWidth(15);
  404. imViewWeight.setFitHeight(15);
  405. ((Button) root.lookup("#weightInfo")).setGraphic(imViewWeight);
  406. ImageView imViewFreeze = new ImageView(image);
  407. imViewFreeze.setFitWidth(15);
  408. imViewFreeze.setFitHeight(15);
  409. ((Button) root.lookup("#freezeInfo")).setGraphic(imViewFreeze);
  410. ImageView imViewMelt = new ImageView(image);
  411. imViewMelt.setFitWidth(15);
  412. imViewMelt.setFitHeight(15);
  413. ((Button) root.lookup("#meltInfo")).setGraphic(imViewMelt);
  414. ImageView imViewTilt = new ImageView(image);
  415. imViewTilt.setFitWidth(15);
  416. imViewTilt.setFitHeight(15);
  417. ((Button) root.lookup("#tiltInfo")).setGraphic(imViewTilt);
  418. ImageView imViewFlip = new ImageView(image);
  419. imViewFlip.setFitWidth(15);
  420. imViewFlip.setFitHeight(15);
  421. ((Button) root.lookup("#flipInfo")).setGraphic(imViewFlip);
  422. ImageView imViewSqueeze = new ImageView(image);
  423. imViewSqueeze.setFitWidth(15);
  424. imViewSqueeze.setFitHeight(15);
  425. ((Button) root.lookup("#squeezeInfo")).setGraphic(imViewSqueeze);
  426. Image imageSettings = new Image(getClass().getResource("/pictures/cog.png").toString());
  427. ImageView imageViewSettings = new ImageView(imageSettings);
  428. imageViewSettings.setFitWidth(20);
  429. imageViewSettings.setFitHeight(20);
  430. ((Button)root.lookup("#settingsButton")).setGraphic(imageViewSettings);
  431. Image imgSnowflake = new Image(getClass().getResource("/pictures/snowflake.png").toString());
  432. ImageView snowflakeIV = new ImageView(imgSnowflake);
  433. snowflakeIV.setFitWidth(20);
  434. snowflakeIV.setFitHeight(20);
  435. Label snowflakeLabel = (Label) root.lookup("#temperatureFallingLabel");
  436. snowflakeLabel.setGraphic(snowflakeIV);
  437. Image imgFlame = new Image(getClass().getResource("/pictures/flame.png").toString());
  438. ImageView flameIV = new ImageView(imgFlame);
  439. flameIV.setFitWidth(20);
  440. flameIV.setFitHeight(20);
  441. Label flameLabel = (Label) root.lookup("#temperatureRisingLabel");
  442. flameLabel.setGraphic(flameIV);
  443. Image imgTilt = new Image(getClass().getResource("/pictures/tilt.png").toString());
  444. ImageView tiltIV = new ImageView(imgTilt);
  445. tiltIV.setFitWidth(20);
  446. tiltIV.setFitHeight(20);
  447. Label tiltLabel = (Label) root.lookup("#tiltLabel");
  448. tiltLabel.setGraphic(tiltIV);
  449. Image imgFlip = new Image(getClass().getResource("/pictures/flip.png").toString());
  450. ImageView flipIV = new ImageView(imgFlip);
  451. flipIV.setFitWidth(20);
  452. flipIV.setFitHeight(20);
  453. Label flipLabel = (Label) root.lookup("#flipLabel");
  454. flipLabel.setGraphic(flipIV);
  455. glassPane = new StackPane();
  456. glassPane.setStyle("-fx-background-color: rgba(0, 0, 0, 0.2);");
  457. glassPane.setVisible(false);
  458. Label loadingLabel = new Label("Creating Object...");
  459. Image loadingImage = new Image(getClass().getResource("/pictures/loadingCircle.gif").toString());
  460. ImageView imView = new ImageView(loadingImage);
  461. imView.setFitHeight(16);
  462. imView.setFitWidth(128);
  463. // loadingLabel.setGraphic(new ImageView(image));
  464. VBox vb = new VBox();
  465. //ProgressBar pbar = new ProgressBar();
  466. //pbar.setProgress(0.5);
  467. vb.setAlignment(Pos.CENTER);
  468. //vb.getChildren().add(pbar);
  469. vb.getChildren().add(imView);
  470. vb.getChildren().add(loadingLabel);
  471. glassPane.getChildren().add(vb);
  472. StackPane.setAlignment(vb, Pos.CENTER);
  473. ((GridPane)root).add(glassPane, 1, 0);
  474. //If openSCAD could not be found, show a warning. The text depends on the OS.
  475. if (!detected) {
  476. if (System.getProperty("os.name").toLowerCase().contains("windows")) {
  477. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  478. alert.initStyle(StageStyle.UTILITY);
  479. alert.setTitle("Information");
  480. alert.setHeaderText("Warning");
  481. alert.setContentText("Couldn't find openscad.exe.\nMake sure that OpenSCAD is installed and select its folder under Menu -> Settings.");
  482. alert.showAndWait();
  483. }
  484. else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
  485. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  486. alert.initStyle(StageStyle.UTILITY);
  487. alert.setTitle("Information");
  488. alert.setHeaderText("Warning");
  489. alert.setContentText("Couldn't find openscad.\nMake sure that OpenSCAD is installed and is visible gloabaly.");
  490. alert.showAndWait();
  491. }
  492. else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
  493. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  494. alert.initStyle(StageStyle.UTILITY);
  495. alert.setTitle("Information");
  496. alert.setHeaderText("Warning");
  497. alert.setContentText("Couldn't find openscad.app.\nMake sure that OpenSCAD is installed and select its folder under Menu -> Settings.");
  498. alert.showAndWait();
  499. }
  500. }
  501. }
  502. public static String olipPath;
  503. public static void main(String[] args) {
  504. if (System.getProperty("os.name").toLowerCase().contains("windows"))
  505. olipPath = System.getenv("AppData") + File.separator + "Olip" + File.separator;
  506. else if (System.getProperty("os.name").toLowerCase().contains("mac"))
  507. olipPath = System.getProperty("user.home") + "/Library/Application Support/Olip/";
  508. else //Linux
  509. olipPath = System.getProperty("user.home") + "/.olip/";
  510. //Load the settings.ini
  511. File file = new File(olipPath + "settings.ini");
  512. if (file.exists())
  513. SettingsController.loadSettings();
  514. else {
  515. //If there isn't a settings.ini file, create a new one and search if OpenSCAD is installed.
  516. //Windows: exe typically under the given path.
  517. if (System.getProperty("os.name").toLowerCase().contains("windows")) {
  518. File openscadexe = new File("C:\\Program Files\\OpenSCAD\\openscad.exe");
  519. if (openscadexe.exists())
  520. Controller.getC().setOpenSCADPath(openscadexe.toPath().getParent());
  521. else {
  522. notDetected();
  523. }
  524. }
  525. //Linux: OpenSCAD is visible globally, so check via command line
  526. else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
  527. try {
  528. Process p = Runtime.getRuntime().exec("which openscad");
  529. p.waitFor();
  530. BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  531. String s = in.readLine();
  532. if (s != null) {
  533. Controller.getC().setOpenSCADPath(new File(s).toPath());
  534. }
  535. else {
  536. notDetected();
  537. }
  538. } catch (IOException | InterruptedException e1) {
  539. e1.printStackTrace();
  540. }
  541. }
  542. //MacOS: OpenSCAD is installed as an app, so look for in in the application folder
  543. else if (System.getProperty("os.name").toLowerCase().contains("mac")) {
  544. File openscadapp = new File("/Applications/OpenSCAD.app");
  545. if (openscadapp.exists())
  546. Controller.getC().setOpenSCADPath(new File(openscadapp.toPath().toString() + "/Contents/MacOS/").toPath());
  547. else
  548. notDetected();
  549. }
  550. SettingsController.saveSettings();
  551. }
  552. launch(args);
  553. }
  554. private void accelerationClicked() {
  555. boolean disable = accelerationCheckBox.isSelected();
  556. createObjectButton.setDisable(!disable);
  557. disableWeight();
  558. disableTemperature();
  559. disableTilting();
  560. disablePressure();
  561. root.lookup("#accelerationLabel").setDisable(!disable);
  562. accelerationSpinner.setDisable(!disable);
  563. Controller.getC().acceleration(disable);
  564. }
  565. private void disableAcceleration() {
  566. accelerationCheckBox.setSelected(false);
  567. Controller.getC().acceleration(false);
  568. root.lookup("#accelerationLabel").setDisable(true);
  569. accelerationSpinner.setDisable(true);
  570. }
  571. private void weightClicked() {
  572. boolean disable = weightCheckBox.isSelected();
  573. createObjectButton.setDisable(!disable);
  574. disableAcceleration();
  575. disableTemperature();
  576. disableTilting();
  577. disablePressure();
  578. root.lookup("#weightLabel").setDisable(!disable);
  579. loadSpinner.setDisable(!disable);
  580. Controller.getC().weight(disable);
  581. }
  582. private void disableWeight() {
  583. weightCheckBox.setSelected(false);
  584. Controller.getC().weight(false);
  585. root.lookup("#weightLabel").setDisable(true);
  586. loadSpinner.setDisable(true);
  587. }
  588. private void temperatureClicked() {
  589. boolean disable = tempCheckBox.isSelected();
  590. createObjectButton.setDisable(!disable);
  591. disableWeight();
  592. disableAcceleration();
  593. disableTilting();
  594. disablePressure();
  595. root.lookup("#temperatureRisingLabel").setDisable(!disable);
  596. root.lookup("#temperatureFallingLabel").setDisable(!disable);
  597. root.lookup("#temperatureRadio1").setDisable(!disable);
  598. root.lookup("#temperatureRadio2").setDisable(!disable);
  599. Controller.getC().temperature(disable);
  600. }
  601. private void disableTemperature() {
  602. tempCheckBox.setSelected(false);
  603. Controller.getC().temperature(false);
  604. root.lookup("#temperatureRadio1").setDisable(true);
  605. root.lookup("#temperatureRadio2").setDisable(true);
  606. root.lookup("#temperatureRisingLabel").setDisable(true);
  607. root.lookup("#temperatureFallingLabel").setDisable(true);
  608. }
  609. private void tiltingClicked() {
  610. boolean disable = tiltingCheckBox.isSelected();
  611. createObjectButton.setDisable(!disable);
  612. disableWeight();
  613. disableTemperature();
  614. disableAcceleration();
  615. disablePressure();
  616. root.lookup("#rotationRadio1").setDisable(!disable);
  617. root.lookup("#rotationRadio2").setDisable(!disable);
  618. root.lookup("#tiltLabel").setDisable(!disable);
  619. root.lookup("#flipLabel").setDisable(!disable);
  620. Controller.getC().tilt(disable);
  621. }
  622. private void disableTilting() {
  623. tiltingCheckBox.setSelected(false);
  624. Controller.getC().tilt(false);
  625. root.lookup("#rotationRadio1").setDisable(true);
  626. root.lookup("#rotationRadio2").setDisable(true);
  627. root.lookup("#tiltLabel").setDisable(true);
  628. root.lookup("#flipLabel").setDisable(true);
  629. }
  630. private void pressureClicked() {
  631. boolean disable = pressureCheckBox.isSelected();
  632. createObjectButton.setDisable(!disable);
  633. disableWeight();
  634. disableTemperature();
  635. disableTilting();
  636. disableAcceleration();
  637. //root.lookup("#pressureContent").setDisable(!disable);
  638. Controller.getC().squeeze(disable);
  639. }
  640. private void disablePressure() {
  641. pressureCheckBox.setSelected(false);
  642. Controller.getC().squeeze(false);
  643. }
  644. /**
  645. * Initialize the JavaFX panel
  646. * @return initialized panel
  647. */
  648. private SubScene createScene() {
  649. rootGroup = new Group();
  650. SubScene scene = new SubScene(rootGroup, 1000, 1000, true, null);
  651. scene.setFill(new Color(0.9, 0.9, 0.9, 1.0));
  652. Timeline timer = new Timeline(new KeyFrame(Duration.seconds(0.001667), event -> {
  653. rootGroup.setTranslateX(fxPanel.getWidth()/2);
  654. rootGroup.setTranslateY(fxPanel.getHeight()/2);
  655. }));
  656. timer.setCycleCount(Timeline.INDEFINITE);
  657. timer.play();
  658. rootGroup.setScaleX(Math.min(fxPanel.getWidth()/80, fxPanel.getHeight()/80));
  659. rootGroup.setScaleY(Math.min(fxPanel.getWidth()/80, fxPanel.getHeight()/80));
  660. rootGroup.setScaleZ(Math.min(fxPanel.getWidth()/80, fxPanel.getHeight()/80));
  661. //rootGroup.setTranslateX(400);
  662. //rootGroup.setTranslateY(400);
  663. rootGroup.setTranslateZ(0);
  664. Rotate r = new Rotate();
  665. r.setAngle(-60);
  666. r.setAxis(Rotate.Y_AXIS);
  667. rootGroup.getTransforms().add(r);
  668. Rotate r2 = new Rotate();
  669. r.setAxis(Rotate.X_AXIS);
  670. r2.setAngle(-20);
  671. rootGroup.getTransforms().add(r2);
  672. rootGroup.getChildren().add(new AmbientLight(Color.WHITE));
  673. return scene;
  674. }
  675. /**
  676. * Reload the object (changes if another object got created) or just change the view mode
  677. * @param scene area to show the 3d scene
  678. * @param viewMode view mode, dependend on dropdown menu
  679. */
  680. private void changeObject(SubScene scene, int viewMode) {
  681. Label previewlabel = ((Label) this.root.lookup("#previewLabel"));
  682. Label propertiesLabel = ((Label) this.root.lookup("#propertiesLabel"));
  683. if (Settings.getSettings().isAcceleration()) {
  684. previewlabel.setText("Preview for Acceleration");
  685. propertiesLabel.setText("Acceleration Limit: " + Settings.getSettings().getAcceleration() + " m/s²\n" +
  686. "Fill Hole " + (Controller.getC().getFill()?"enabled":"disabled")+ "\n" +
  687. "Touchscreen Dot Size " + Controller.getC().getSizeScreen() + " mm\n" +
  688. "Finger Dot Size " + Controller.getC().getSizeFinger() + " mm");
  689. } else if (Settings.getSettings().isWeight()) {
  690. previewlabel.setText("Preview for Load");
  691. propertiesLabel.setText("Weight Limit: " + Settings.getSettings().getWeight()/10.0 + " Kg\n" +
  692. "Fill Hole " + (Controller.getC().getFill()?"enabled":"disabled")+ "\n" +
  693. "Touchscreen Dot Size " + Controller.getC().getSizeScreen() + " mm\n" +
  694. "Finger Dot Size " + Controller.getC().getSizeFinger() + " mm");
  695. } else if (Settings.getSettings().isTemperature()) {
  696. previewlabel.setText("Preview for Temperature");
  697. propertiesLabel.setText((Settings.getSettings().getTemperature()== Settings.Temp.OVER0?"Rising":"Falling") + " Temperature\n" +
  698. "Fill Hole " + (Controller.getC().getFill()?"enabled":"disabled") + "\n" +
  699. "Touchscreen Dot Size " + Controller.getC().getSizeScreen() + " mm\n" +
  700. "Finger Dot Size " + Controller.getC().getSizeFinger() + " mm");
  701. } else if (Settings.getSettings().isTilt()) {
  702. previewlabel.setText("Preview for Rotation");
  703. propertiesLabel.setText("Rotation by " + (Settings.getSettings().getTilting()== Settings.Tilt.TILT?"90°":"180°") + "\n" +
  704. "Fill Hole " + (Controller.getC().getFill()?"enabled":"disabled")+ "\n" +
  705. "Touchscreen Dot Size " + Controller.getC().getSizeScreen() + " mm\n" +
  706. "Finger Dot Size " + Controller.getC().getSizeFinger() + " mm");
  707. } else if (Settings.getSettings().isSqueeze()) {
  708. previewlabel.setText("Preview for Pressure");
  709. propertiesLabel.setText("Fill Hole " + (Controller.getC().getFill()?"enabled":"disabled")+ "\n" +
  710. "Touchscreen Dot Size " + Controller.getC().getSizeScreen() + " mm\n" +
  711. "Finger Dot Size " + Controller.getC().getSizeFinger() + " mm");
  712. } else {
  713. previewlabel.setText("Preview");
  714. propertiesLabel.setText("");
  715. }
  716. Group root = (Group) scene.getRoot();
  717. StlMeshImporter stlimport = new StlMeshImporter();
  718. String fileName;
  719. switch (viewMode) {
  720. case VIEW_ALL: fileName = Main.olipPath + "output.stl"; break;
  721. case VIEW_CONDUCTIVE: fileName = Main.olipPath + "output_conductive.stl"; break;
  722. case VIEW_NON_CONDUCTIVE: fileName = Main.olipPath + "output.stl"; break;
  723. case VIEW_CROSS_SECTION: fileName = Main.olipPath + "output_crosssection.stl"; break;
  724. default: fileName = Main.olipPath + "output.stl";
  725. }
  726. stlimport.read(new File(fileName));
  727. Mesh mesh = stlimport.getImport();
  728. MeshView meshView = new MeshView(mesh);
  729. PhongMaterial mat = new PhongMaterial();
  730. mat.setSpecularColor(Color.WHITE);
  731. if (viewMode == VIEW_CONDUCTIVE)
  732. mat.setDiffuseColor(Color.RED);
  733. else
  734. mat.setDiffuseColor(Color.LIGHTGRAY);
  735. meshView.setMaterial(mat);
  736. root.getChildren().add(meshView);
  737. if (viewMode == VIEW_ALL || viewMode == VIEW_CROSS_SECTION) {
  738. String fileName2;
  739. if (viewMode == VIEW_ALL)
  740. fileName2 = Main.olipPath + "output_conductive.stl";
  741. else
  742. fileName2 = Main.olipPath + "output_crosssection_conductive.stl";
  743. stlimport.read(new File(fileName2));
  744. Mesh mesh2 = stlimport.getImport();
  745. MeshView meshView2 = new MeshView(mesh2);
  746. PhongMaterial mat2 = new PhongMaterial();
  747. mat2.setSpecularColor(Color.WHITE);
  748. mat2.setDiffuseColor(Color.RED);
  749. meshView2.setMaterial(mat2);
  750. root.getChildren().add(meshView2);
  751. }
  752. this.root.lookup("#saveObjectButton").setDisable(false);
  753. }
  754. private void clearView(SubScene scene) {
  755. Group root = (Group) scene.getRoot();
  756. while (root.getChildren().size() > 0)
  757. root.getChildren().remove(0);
  758. }
  759. /**
  760. * Tell this frame that the creation of the STLs is finished
  761. */
  762. public static void finished() {
  763. finished = true;
  764. }
  765. /**
  766. * Tell this frame that openSCAD could not be found
  767. */
  768. private static void notDetected() {
  769. detected = false;
  770. }
  771. }