From: lempinen Date: Fri, 16 Sep 2011 12:33:51 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22295 ac1ea38d-2e2b... X-Git-Tag: simantics-1.5~31 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e908d6747b23e5d138fe125472c9d5e87bcb2cdd;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22295 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java index e9b448e5..24424f4d 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -11,16 +11,15 @@ *******************************************************************************/ package org.simantics.modelica; -import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.URL; import java.net.URLDecoder; import java.util.HashMap; +import java.util.Map; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Platform; @@ -111,70 +110,6 @@ public class ModelicaManager { } } - public static File getModelicaExecutable(File simulationDir) { - - File modelicaHome = getModelicaHome(); - - if(!modelicaHome.isDirectory()) - throw new UnsatisfiedLinkError("OpenModelica probably not installed. Tried to find it from: " + modelicaHome.getAbsolutePath()); - - try { - File omBat = new File(simulationDir, "om.bat"); - FileWriter fw = new FileWriter(omBat); - BufferedWriter out = new BufferedWriter(fw); - out.write("@echo off"); - out.newLine(); - out.write("set OPENMODELICAHOME=" + modelicaHome.getAbsolutePath()); - out.newLine(); - out.write("set OPENMODELICALIBRARY=%OPENMODELICAHOME%\\lib\\omlibrary\\msl31"); - out.newLine(); - out.write("%OPENMODELICAHOME%\\bin\\omc.exe %*"); - out.newLine(); - out.close(); - fw.close(); - - return omBat; - } catch (IOException e) { - e.printStackTrace(); - throw new UnsatisfiedLinkError("OpenModelica probably not installed. Tried to find it from: " + modelicaHome.getAbsolutePath()); - } - } - - public static File getSimulationExecutableBat(File exeFile) { - - File modelicaHome = getModelicaHome(); - - File folder = new File(exeFile.getParent()); - - try { - File exeBat = new File(folder, exeFile.getName() + ".bat"); - FileWriter fw = new FileWriter(exeBat); - BufferedWriter out = new BufferedWriter(fw); - - out.write("@echo off"); - out.newLine(); - out.write("set OPENMODELICAHOME=" + modelicaHome.getAbsolutePath()); - out.newLine(); - out.write("set OPENMODELICALIBRARY=%OPENMODELICAHOME%\\lib\\omlibrary\\msl31"); - out.newLine(); - out.write("set OMPATH=%OPENMODELICAHOME%\\bin"); - out.newLine(); - out.write("echo %PATH%|findstr /i %OMPATH% >nul:"); - out.newLine(); - out.write("if %errorlevel%==1 set PATH=%PATH%;%OMPATH%"); - out.newLine(); - out.write(exeFile.getAbsolutePath() + " %*"); - out.newLine(); - - out.close(); - fw.close(); - return exeBat; - } - catch (IOException e) { - e.printStackTrace(); - throw new UnsatisfiedLinkError("Creating bat for the exe failed"); - } - } protected static File createTempDirectory() throws IOException { final File temp = File.createTempFile("temp", Long.toString(System.nanoTime())); @@ -229,6 +164,7 @@ public class ModelicaManager { } catch (IOException e) { System.err.println("Not able to read simulation output"); e.printStackTrace(); + break; } } } @@ -282,21 +218,27 @@ public class ModelicaManager { public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException { try { - File omc = getModelicaExecutable(simulationLocation.simulationDir); - if(omc == null) { - monitor.message("OpenModelica not found! Install it from www.openmodelica.org"); - throw new ModelicaException("OpenModelica not found"); - } - - Process process = new ProcessBuilder( - omc.getAbsolutePath(), + File openModelicaHome = getModelicaHome(); + + ProcessBuilder processBuilder = new ProcessBuilder( + openModelicaHome.getAbsolutePath() + "\\bin\\omc.exe", simulationLocation.inputFile.getAbsolutePath() ) .directory(simulationLocation.simulationDir.getAbsoluteFile()) - .redirectErrorStream(true) - .start(); + .redirectErrorStream(true); + + + Map env = processBuilder.environment(); + + env.put("OPENMODELICAHOME", openModelicaHome.getAbsolutePath()); + env.put("OPENMODELICALIBRARY", openModelicaHome.getAbsolutePath() + "\\lib\\omlibrary\\msl31"); + env.put("OMPATH", openModelicaHome.getAbsolutePath() + "\\bin"); + env.put("Path", env.get("Path") + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\MinGW\\lib"); + + Process process = processBuilder.start(); printProcessOutput(process, monitor); + if(!simulationLocation.exeFile.isFile()) throw new ModelicaException(".exe file not created\nSee log at " + simulationLocation.simulationDir.getAbsolutePath()); @@ -309,17 +251,28 @@ public class ModelicaManager { public static Process runModelica(SimulationLocation simulationLocation, IModelicaMonitor monitor, HashMap inits) throws IOException { + try { writeInits(simulationLocation, inits); - - Process process = new ProcessBuilder( - getSimulationExecutableBat(simulationLocation.exeFile).getAbsolutePath() + + ProcessBuilder processBuilder = new ProcessBuilder( + simulationLocation.exeFile.getAbsolutePath() ) - .directory(simulationLocation.simulationDir.getAbsoluteFile()) - .redirectErrorStream(true) - .start(); - + .directory(new File(simulationLocation.simulationDir.getAbsolutePath())) + .redirectErrorStream(true); + + Map env = processBuilder.environment(); + File openModelicaHome = getModelicaHome(); + env.put("OPENMODELICAHOME", openModelicaHome.getAbsolutePath()); + env.put("OPENMODELICALIBRARY", openModelicaHome.getAbsolutePath() + "\\lib\\omlibrary\\msl31"); + env.put("OMPATH", openModelicaHome.getAbsolutePath() + "\\bin"); + env.put("Path", env.get("Path") + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\bin" + + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\MinGW\\lib"); + + + Process process = processBuilder.start(); + return process; } catch(IOException e) { e.printStackTrace();