Commit 0c8eb61d authored by Ivan Dages's avatar Ivan Dages
Browse files

insert components

parent c836392e
......@@ -1219,6 +1219,167 @@ public class EditorMenuBar extends MenuBar {
subdivStage.show();
});
MenuItem insertComponentItem = new MenuItem("Insert component");
insertComponentItem.setOnAction(event -> {
if (!modelLoaded()) {
return;
}
Component selectedComponent = selectComponent(insertComponentItem.getText());
if (selectedComponent == null) {
return;
}
// TODO refactor : a lot of the following code is a copy/paste from subdivideComponentItem
GridPane scenePane = new GridPane();
scenePane.setAlignment(Pos.TOP_CENTER);
Scene insertScene = new Scene(scenePane);
Stage insertStage = new Stage(StageStyle.UTILITY);
insertStage.setTitle(insertComponentItem.getText());
insertStage.setScene(insertScene);
insertStage.initOwner(Nomad3DEditor.getInstance().getStage());
final double lineHeight = 30;
GridPane childPane = new GridPane();
childPane.setVgap(5);
childPane.setHgap(10);
childPane.setAlignment(Pos.CENTER);
scenePane.addRow(0, childPane);
childPane.setPrefHeight(2*lineHeight);
GridPane.setMargin(childPane, new Insets(10));
Label nameLabel = new Label("Name : ");
nameLabel.setPrefWidth(50);
Label meshLabel = new Label("STL file : ");
meshLabel.setPrefWidth(nameLabel.getPrefWidth());
int colCount = 20;
TextField nameField = new TextField();
nameField.setPrefColumnCount(colCount);
nameField.setPrefHeight(lineHeight);
TextField meshField = new TextField();
meshField.setPrefColumnCount(colCount);
meshField.setPrefHeight(lineHeight);
Button browseButton = new Button("Browse files");
browseButton.setPrefSize(80, lineHeight);
browseButton.setOnAction(browseEvent -> {
FileChooser chooser = new FileChooser();
chooser.setTitle("Open model");
if (!meshField.getText().isEmpty()) {
File meshFile = new File(meshField.getText());
if (meshFile.exists() && meshFile.getParentFile() != null && meshFile.getParentFile().exists()) {
chooser.setInitialDirectory(meshFile.getParentFile());
}
}
chooser.getExtensionFilters().addAll(new ExtensionFilter("STL files", "*.STL"));
File selectedFile = chooser.showOpenDialog(null);
if (selectedFile != null) {
meshField.setText(selectedFile.getAbsolutePath());
if (nameField.getText().isEmpty()) {
nameField.setText(FilenameUtils.removeExtension(selectedFile.getName()));
}
}
});
childPane.addRow(0, nameLabel, nameField);
childPane.addRow(1, meshLabel, meshField, browseButton);
childPane.setPrefWidth(450);
GridPane buttonsPane = new GridPane();
buttonsPane.setVgap(5);
buttonsPane.setHgap(10);
buttonsPane.setAlignment(Pos.CENTER);
scenePane.addRow(1, buttonsPane);
GridPane.setMargin(buttonsPane, new Insets(10));
Button okButton = new Button("OK");
okButton.setDefaultButton(true);
okButton.setOnAction(okEvent -> {
try {
Path tmpDirPath = Files.createTempDirectory("nomad-3d-editor-insertion-");
File tmpDir = tmpDirPath.toFile();
tmpDir.deleteOnExit();
Logger.getLogger("nomad-3d").info("Creating temporary directory " + tmpDirPath + " for STL files");
try {
File inFile = new File(meshField.getText());
File tmpFile = new File(tmpDir, inFile.getName());
Files.copy(inFile.toPath(), tmpFile.toPath());
tmpFile.deleteOnExit();
Logger.getLogger("nomad-3d").info("Copy : " + inFile + " --> " + tmpFile);
} catch (Exception e) {
// Empty fields are ignored
if (!nameField.getText().isEmpty() && !meshField.getText().isEmpty()) {
Logger.getLogger("nomad-3d").severe(e.getMessage());
e.printStackTrace();
}
}
// Convert geometries
File modelDir = new File(Nomad3DEditor.getInstance().getModel().getDirectoryPath());
String blenderExe = "blender";
if (System.getProperty("os.name").toUpperCase().startsWith("WIN")) {
blenderExe = "C:\\Program Files\\Blender Foundation\\Blender\\blender.exe";
}
// TODO get this from somewhere
double decimateRatio = 0.5;
String converterPath = ".." + File.separator + "nomad-3d-converter";
// Call the blender script
ProcessBuilder builder = new ProcessBuilder(blenderExe, "--background", "--python",
converterPath + File.separator + "scripts" + File.separator + "simplify.py",
tmpDir.toString(), modelDir.toString(), Double.toString(decimateRatio));
builder.inheritIO();
// builder.redirectOutput(ProcessBuilder.Redirect.appendTo(BLENDER_LOG_FILE));
// builder.redirectError(ProcessBuilder.Redirect.appendTo(BLENDER_LOG_FILE));
Logger.getLogger("nomad-3d").info("Executing command : \n" + builder.command());
Process blenderProcess = builder.start();
int exitValue = blenderProcess.waitFor();
Logger.getLogger("nomad-3d").info("Blender exit value : " + exitValue);
// Add new components to the model
Logger.getLogger("nomad-3d").info("Inserting the new component in the model...");
Component child = new Component();
child.setName(nameField.getText());
child.setFileName(FilenameUtils.removeExtension(new File(meshField.getText()).getName()));
boolean fixed = false;
for (ConfigParams parentConfig : selectedComponent.getConfigurations()) {
ConfigParams config = new ConfigParams(child);
config.setConfiguration(parentConfig.getConfiguration());
config.setTranslation(parentConfig.getTranslation().clone());
config.setRotation(parentConfig.getRotation().clone());
config.setScale(parentConfig.getScale().clone());
config.setVisible(parentConfig.isVisible());
config.setFixed(parentConfig.isFixed());
fixed |= config.isFixed();
child.addConfiguration(config);
}
child.setMaterial(selectedComponent.getMaterial());
child.setAxis(((fixed) ? Axis.FIXED_AXIS : Axis.FREE_AXIS).clone());
selectedComponent.addChild(child);
Logger.getLogger("nomad-3d").info("New component inserted.");
updateScene();
} catch (Exception e) {
e.printStackTrace();
} finally {
insertStage.close();
}
});
Button cancelButton = new Button("Cancel");
cancelButton.setCancelButton(true);
cancelButton.setOnAction(cancelEvent -> {
insertStage.close();
});
buttonsPane.addRow(0, okButton, cancelButton);
insertStage.show();
});
MenuItem removeComponentsItem = new MenuItem("Remove components");
removeComponentsItem.setOnAction(event -> {
if (Nomad3DEditor.getInstance().getModel() == null || Nomad3DEditor.getInstance().getModel().getRoot() == null) {
......@@ -1328,7 +1489,7 @@ public class EditorMenuBar extends MenuBar {
this.removeConfig = new Menu("Remove configuration");
Menu componentsMenu = new Menu("Components");
componentsMenu.getItems().addAll(regroupComponentsItem, subdivideComponentItem, removeComponentsItem);
componentsMenu.getItems().addAll(regroupComponentsItem, subdivideComponentItem, insertComponentItem, removeComponentsItem);
Menu editConfigMenu = new Menu("Configurations");
editConfigMenu.getItems().addAll(analyzeConfigItem, saveMedianConfigItem, saveMinConfigItem, saveMaxConfigItem,
saveConfigItem, this.removeConfig);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment