From: lempinen Date: Wed, 27 Apr 2011 09:46:19 +0000 (+0000) Subject: Support (only) 1.7.0 OpenModelica X-Git-Tag: simantics-1.4RC1~38 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ab9bc0841da79a1c9f1333ea9b6d1eb031e57600;p=simantics%2Fsysdyn.git Support (only) 1.7.0 OpenModelica git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20585 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 2137e9e5..5295b6fd 100644 --- a/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java +++ b/org.simantics.modelica/src/org/simantics/modelica/ModelicaManager.java @@ -11,9 +11,11 @@ *******************************************************************************/ package org.simantics.modelica; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; @@ -45,9 +47,8 @@ public class ModelicaManager { return OSType.SUN; return OSType.UNKNOWN; } - - public static File getModelicaExecutable() { - + + public static File getModelicaHome() { String dir = System.getenv("OPENMODELICAHOME"); @@ -67,7 +68,7 @@ public class ModelicaManager { if(entry != null) { URL fileURL = FileLocator.toFileURL(entry); File root = new File( URLDecoder.decode(fileURL.getPath(), "UTF-8") ); - File f = new File(new File(root, "OpenModelica1.5.0"), "om.bat"); + File f = new File(root, "OpenModelica1.7.0"); return f; } } @@ -86,7 +87,7 @@ public class ModelicaManager { case SUN: return new File("/usr/bin/omc"); case WINDOWS: - return new File("c:/OpenModelica1.5.0/bin/omc.exe"); + return new File("c:/OpenModelica1.7.0"); default: throw new UnsatisfiedLinkError("Unsupported operating system: " + os); } @@ -95,15 +96,80 @@ public class ModelicaManager { case APPLE: case LINUX: case SUN: - return new File(dir+"/bin/omc"); + return new File(dir); case WINDOWS: - return new File(dir+"/bin/omc.exe"); + return new File(dir); default: throw new UnsatisfiedLinkError("Unsupported operating system: " + os); } } } + 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())); if(!(temp.delete())) @@ -210,7 +276,7 @@ public class ModelicaManager { public static void buildModel(SimulationLocation simulationLocation, IModelicaMonitor monitor) throws ModelicaException { try { - File omc = getModelicaExecutable(); + File omc = getModelicaExecutable(simulationLocation.simulationDir); if(omc == null) { monitor.message("OpenModelica not found! Install it from www.openmodelica.org"); @@ -219,8 +285,6 @@ public class ModelicaManager { Process process = new ProcessBuilder( omc.getAbsolutePath(), - "+showErrorMessages", - "+d=usedep", simulationLocation.inputFile.getAbsolutePath() ) .directory(simulationLocation.simulationDir.getAbsoluteFile()) @@ -244,7 +308,7 @@ public class ModelicaManager { writeInits(simulationLocation, inits); Process process = new ProcessBuilder( - simulationLocation.exeFile.getAbsolutePath() + getSimulationExecutableBat(simulationLocation.exeFile).getAbsolutePath() ) .directory(simulationLocation.simulationDir.getAbsoluteFile()) .redirectErrorStream(true)