Main.java 34 KB

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