*******************************************************************************/\r
package org.simantics.modelica;\r
\r
-import java.io.BufferedWriter;\r
import java.io.File;\r
import java.io.FileInputStream;\r
-import java.io.FileWriter;\r
import java.io.IOException;\r
import java.io.InputStream;\r
import java.io.PrintStream;\r
import java.net.URL;\r
import java.net.URLDecoder;\r
import java.util.HashMap;\r
+import java.util.Map;\r
\r
import org.eclipse.core.runtime.FileLocator;\r
import org.eclipse.core.runtime.Platform;\r
}\r
}\r
\r
- public static File getModelicaExecutable(File simulationDir) {\r
-\r
- File modelicaHome = getModelicaHome();\r
-\r
- if(!modelicaHome.isDirectory())\r
- throw new UnsatisfiedLinkError("OpenModelica probably not installed. Tried to find it from: " + modelicaHome.getAbsolutePath());\r
-\r
- try {\r
- File omBat = new File(simulationDir, "om.bat");\r
- FileWriter fw = new FileWriter(omBat);\r
- BufferedWriter out = new BufferedWriter(fw);\r
- out.write("@echo off");\r
- out.newLine();\r
- out.write("set OPENMODELICAHOME=" + modelicaHome.getAbsolutePath());\r
- out.newLine();\r
- out.write("set OPENMODELICALIBRARY=%OPENMODELICAHOME%\\lib\\omlibrary\\msl31");\r
- out.newLine();\r
- out.write("%OPENMODELICAHOME%\\bin\\omc.exe %*");\r
- out.newLine();\r
- out.close();\r
- fw.close();\r
-\r
- return omBat;\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- throw new UnsatisfiedLinkError("OpenModelica probably not installed. Tried to find it from: " + modelicaHome.getAbsolutePath());\r
- }\r
- }\r
-\r
- public static File getSimulationExecutableBat(File exeFile) {\r
-\r
- File modelicaHome = getModelicaHome();\r
-\r
- File folder = new File(exeFile.getParent());\r
-\r
- try {\r
- File exeBat = new File(folder, exeFile.getName() + ".bat");\r
- FileWriter fw = new FileWriter(exeBat);\r
- BufferedWriter out = new BufferedWriter(fw);\r
-\r
- out.write("@echo off");\r
- out.newLine();\r
- out.write("set OPENMODELICAHOME=" + modelicaHome.getAbsolutePath());\r
- out.newLine();\r
- out.write("set OPENMODELICALIBRARY=%OPENMODELICAHOME%\\lib\\omlibrary\\msl31");\r
- out.newLine();\r
- out.write("set OMPATH=%OPENMODELICAHOME%\\bin");\r
- out.newLine();\r
- out.write("echo %PATH%|findstr /i %OMPATH% >nul:");\r
- out.newLine();\r
- out.write("if %errorlevel%==1 set PATH=%PATH%;%OMPATH%");\r
- out.newLine();\r
- out.write(exeFile.getAbsolutePath() + " %*");\r
- out.newLine();\r
-\r
- out.close();\r
- fw.close();\r
- return exeBat;\r
- }\r
- catch (IOException e) {\r
- e.printStackTrace();\r
- throw new UnsatisfiedLinkError("Creating bat for the exe failed");\r
- }\r
- }\r
\r
protected static File createTempDirectory() throws IOException {\r
final File temp = File.createTempFile("temp", Long.toString(System.nanoTime()));\r
} catch (IOException e) {\r
System.err.println("Not able to read simulation output");\r
e.printStackTrace();\r
+ break;\r
}\r
}\r
} \r
\r
public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException {\r
try {\r
- File omc = getModelicaExecutable(simulationLocation.simulationDir);\r
\r
- if(omc == null) {\r
- monitor.message("OpenModelica not found! Install it from www.openmodelica.org");\r
- throw new ModelicaException("OpenModelica not found"); \r
- }\r
-\r
- Process process = new ProcessBuilder(\r
- omc.getAbsolutePath(),\r
+ File openModelicaHome = getModelicaHome();\r
+ \r
+ ProcessBuilder processBuilder = new ProcessBuilder(\r
+ openModelicaHome.getAbsolutePath() + "\\bin\\omc.exe",\r
simulationLocation.inputFile.getAbsolutePath()\r
)\r
.directory(simulationLocation.simulationDir.getAbsoluteFile())\r
- .redirectErrorStream(true)\r
- .start();\r
+ .redirectErrorStream(true);\r
+\r
+ \r
+ Map<String, String> env = processBuilder.environment();\r
+ \r
+ env.put("OPENMODELICAHOME", openModelicaHome.getAbsolutePath());\r
+ env.put("OPENMODELICALIBRARY", openModelicaHome.getAbsolutePath() + "\\lib\\omlibrary\\msl31");\r
+ env.put("OMPATH", openModelicaHome.getAbsolutePath() + "\\bin");\r
+ env.put("Path", env.get("Path") + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\MinGW\\lib");\r
+ \r
+ Process process = processBuilder.start();\r
printProcessOutput(process, monitor);\r
+ \r
\r
if(!simulationLocation.exeFile.isFile())\r
throw new ModelicaException(".exe file not created\nSee log at " + simulationLocation.simulationDir.getAbsolutePath()); \r
\r
public static Process runModelica(SimulationLocation simulationLocation, IModelicaMonitor monitor, HashMap<String, String> inits) throws IOException {\r
\r
+ \r
try {\r
\r
writeInits(simulationLocation, inits);\r
-\r
- Process process = new ProcessBuilder(\r
- getSimulationExecutableBat(simulationLocation.exeFile).getAbsolutePath()\r
+ \r
+ ProcessBuilder processBuilder = new ProcessBuilder( \r
+ simulationLocation.exeFile.getAbsolutePath()\r
)\r
- .directory(simulationLocation.simulationDir.getAbsoluteFile())\r
- .redirectErrorStream(true)\r
- .start();\r
-\r
+ .directory(new File(simulationLocation.simulationDir.getAbsolutePath()))\r
+ .redirectErrorStream(true);\r
+ \r
+ Map<String, String> env = processBuilder.environment();\r
+ File openModelicaHome = getModelicaHome();\r
+ env.put("OPENMODELICAHOME", openModelicaHome.getAbsolutePath());\r
+ env.put("OPENMODELICALIBRARY", openModelicaHome.getAbsolutePath() + "\\lib\\omlibrary\\msl31");\r
+ env.put("OMPATH", openModelicaHome.getAbsolutePath() + "\\bin");\r
+ env.put("Path", env.get("Path") + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\bin"\r
+ + System.getProperty("path.separator") + openModelicaHome.getAbsolutePath() + "\\MinGW\\lib");\r
+ \r
+ \r
+ Process process = processBuilder.start();\r
+ \r
return process;\r
} catch(IOException e) {\r
e.printStackTrace();\r