]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22295 ac1ea38d-2e2b...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 16 Sep 2011 12:33:51 +0000 (12:33 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 16 Sep 2011 12:33:51 +0000 (12:33 +0000)
org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java

index e9b448e546698c30f8c8d98fc0f5cd9e8ae97ef0..24424f4d36a2c82538de0df41d786c5ce5ce3a61 100644 (file)
  *******************************************************************************/\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
@@ -111,70 +110,6 @@ public class ModelicaManager {
         }\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
@@ -229,6 +164,7 @@ public class ModelicaManager {
                     } catch (IOException e) {\r
                         System.err.println("Not able to read simulation output");\r
                         e.printStackTrace();\r
+                        break;\r
                     }\r
                 }\r
             } \r
@@ -282,21 +218,27 @@ public class ModelicaManager {
 \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
@@ -309,17 +251,28 @@ public class ModelicaManager {
 \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