// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the Dam Macrostability Kernel. // // The Dam Macrostability Kernel is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Diagnostics; using System.IO; using System.Threading; using Deltares.DamMacroStability.Calculator.Properties; namespace Deltares.DamMacroStability.Calculator { /// /// Writes an ini file and runs the DGeoStability executable /// public class DGeoStabilityExeRunner { private const string DefaultExePath = @".\DGeoStability.exe"; /// /// Initializes a new instance of the class. /// public DGeoStabilityExeRunner() { DGeoStabilityExePath = DefaultExePath; } /// /// DGeoStability executable path. /// public string DGeoStabilityExePath { get; set; } /// /// Calculate DGeoStability project /// /// name of the input file or folder public void RunDGeoStabilityProject(string project) { if (string.IsNullOrEmpty(project) || project.Trim() == "") { throw new ArgumentException(string.Format( Resources.DGeoStabilityExeRunner_RunDGeoStabilityProject_ProjectNameNullOrEmpty, project)); } if (!File.Exists(project) && (!Directory.Exists(project))) { throw new FileNotFoundException(string.Format( Resources.DGeoStabilityExeRunner_RunDGeoStabilityProject_ProjectNotExist, project)); } if (!File.Exists(DGeoStabilityExePath)) { throw new FileNotFoundException(string.Format( Resources.DGeoStabilityExeRunner_RunDGeoStabilityProject_ExecutableNotFound, DGeoStabilityExePath)); } try { ProcessRun(project); } catch (ArgumentNullException argumentNullException) { throw new DGeoStabilityExeRunnerException(argumentNullException.Message, argumentNullException); } catch (FileNotFoundException fileNotFoundException) { throw new DGeoStabilityExeRunnerException(fileNotFoundException.Message, fileNotFoundException); } } /// /// Processes the input file using the working folder /// /// The folder or file name; /// if a filename is specified then the file will be calculated; /// if a foldername is specified, all the stability files in the folder will be calculated. private void ProcessRun(string folderOrFileName) { var iniFilename = CreateDGeoStabilityIniFile(folderOrFileName); var argument = ""; argument = string.Format("/b {0}", iniFilename); var programpath = ""; programpath = DGeoStabilityExePath; var process = new Process { StartInfo = { FileName = programpath, Arguments = argument, UseShellExecute = false } }; process.Start(); try { process.WaitForExit(); } catch (ThreadInterruptedException) { // thread was killed by user action to stop calculation } finally { if (File.Exists(iniFilename)) { File.Delete(iniFilename); } } } internal string CreateDGeoStabilityIniFile(string inputName) { string filename = Path.GetTempFileName(); filename = Path.ChangeExtension(filename, "ini"); string defaultIniFilename = Path.Combine(Path.GetDirectoryName(DGeoStabilityExePath), "DGeoStability.ini"); string newIniContent = "[D-Geo Stability batch processing]"; var isDirectoryBatchCalculation = Directory.Exists(inputName); if (isDirectoryBatchCalculation) { newIniContent += Environment.NewLine + "Path=" + inputName; newIniContent += Environment.NewLine + "Filespec=*.sti"; } else { newIniContent += Environment.NewLine + "InputFileName=" + inputName; } newIniContent += Environment.NewLine + "Plot Critical Circle=1"; newIniContent += Environment.NewLine + "PlotWMF=1"; newIniContent += Environment.NewLine + "PlotJPeg=1"; // When DGeoStability.ini file exist add the content to the ini file that runs DGeoStability // This functionality was added for the so-called PlotHack options, see MWDAM-819 if (File.Exists(defaultIniFilename)) { string iniContent = File.ReadAllText(defaultIniFilename); newIniContent += Environment.NewLine + Environment.NewLine + iniContent; }; File.WriteAllText(filename, newIniContent); return filename; } } }