]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Support (only) 1.7.0 OpenModelica
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 Apr 2011 09:46:19 +0000 (09:46 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Wed, 27 Apr 2011 09:46:19 +0000 (09:46 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20585 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java

index 2137e9e51c3f9351da9e30b5ba53f17b607c6ee0..5295b6fdeb2b9a4b692bb14371a52326cae30ce9 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.FileNotFoundException;\r
+import java.io.FileWriter;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
 import java.io.PrintStream;\r
@@ -45,9 +47,8 @@ public class ModelicaManager {
                        return OSType.SUN;\r
                return OSType.UNKNOWN;\r
        }\r
-\r
-       public static File getModelicaExecutable() {\r
-\r
+       \r
+       public static File getModelicaHome() {\r
 \r
                String dir = System.getenv("OPENMODELICAHOME");\r
 \r
@@ -67,7 +68,7 @@ public class ModelicaManager {
                                        if(entry != null) {\r
                                                URL fileURL = FileLocator.toFileURL(entry);\r
                                                File root = new File( URLDecoder.decode(fileURL.getPath(), "UTF-8") );\r
-                                               File f = new File(new File(root, "OpenModelica1.5.0"), "om.bat");\r
+                                               File f = new File(root, "OpenModelica1.7.0");\r
                                                return f;\r
                                        }\r
                                }\r
@@ -86,7 +87,7 @@ public class ModelicaManager {
                        case SUN:\r
                                return new File("/usr/bin/omc");\r
                        case WINDOWS:\r
-                               return new File("c:/OpenModelica1.5.0/bin/omc.exe");\r
+                               return new File("c:/OpenModelica1.7.0");\r
                        default:\r
                                throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
                        }\r
@@ -95,15 +96,80 @@ public class ModelicaManager {
                        case APPLE:\r
                        case LINUX:\r
                        case SUN:\r
-                               return new File(dir+"/bin/omc");\r
+                               return new File(dir);\r
                        case WINDOWS:\r
-                               return new File(dir+"/bin/omc.exe");\r
+                               return new File(dir);\r
                        default:\r
                                throw new UnsatisfiedLinkError("Unsupported operating system: " + os);\r
                        }\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
                if(!(temp.delete()))\r
@@ -210,7 +276,7 @@ public class ModelicaManager {
 \r
        public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException {\r
                try {\r
-                       File omc = getModelicaExecutable();\r
+                       File omc = getModelicaExecutable(simulationLocation.simulationDir);\r
                        \r
                        if(omc == null) {\r
                                monitor.message("OpenModelica not found! Install it from www.openmodelica.org");\r
@@ -219,8 +285,6 @@ public class ModelicaManager {
 \r
                        Process process = new ProcessBuilder(\r
                                        omc.getAbsolutePath(),\r
-                                       "+showErrorMessages",\r
-                                       "+d=usedep",\r
                                        simulationLocation.inputFile.getAbsolutePath()\r
                        )\r
                        .directory(simulationLocation.simulationDir.getAbsoluteFile())\r
@@ -244,7 +308,7 @@ public class ModelicaManager {
                        writeInits(simulationLocation, inits);\r
 \r
                        Process process = new ProcessBuilder(\r
-                                       simulationLocation.exeFile.getAbsolutePath()\r
+                                       getSimulationExecutableBat(simulationLocation.exeFile).getAbsolutePath()\r
                        )\r
                        .directory(simulationLocation.simulationDir.getAbsoluteFile())\r
                        .redirectErrorStream(true)\r