Пример #1
0
        private void RunSWAT(ArcSWAT.SWATModelType modelType, ArcSWAT.SWATResultIntervalType interval)
        {
            if (_scenario == null)
            {
                return;
            }
            if (modelType == ArcSWAT.SWATModelType.UNKNOWN)
            {
                return;
            }
            if (interval == ArcSWAT.SWATResultIntervalType.UNKNOWN)
            {
                return;
            }
            if (_scenario.getModelResult(modelType, interval).Status == ArcSWAT.ScenarioResultStatus.NORMAL)
            {
                if (MessageBox.Show("There is a pre-generated model result. Do you want to overwrite?", SWAT_SQLite.NAME, MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
                {
                    return;
                }
            }

            //find the corresponding executables
            string swatexe = SWAT_SQLite.InstallationFolder + @"swat_exes\" + ArcSWAT.ScenarioResultStructure.getSWATExecutableName(modelType);

            if (!System.IO.File.Exists(swatexe))
            {
                SWAT_SQLite.showInformationWindow("Can't find " + swatexe);
                return;
            }

            //change output interval
            _scenario.modifyOutputInterval(interval);

            //close the connection with the databse
            //or the file can't be deleted
            //error severe (46): Inconsistent OPEN/CLOSE parameters will be given
            ArcSWAT.ScenarioResult r = _scenario.getModelResult(modelType, interval);
            if (r != null)
            {
                r.close();
            }

            //start to run the model
            Process myProcess = new Process();

            try
            {
                myProcess.EnableRaisingEvents              = true;
                myProcess.StartInfo.UseShellExecute        = false;
                myProcess.StartInfo.FileName               = swatexe;
                myProcess.StartInfo.CreateNoWindow         = true;
                myProcess.StartInfo.RedirectStandardError  = true;
                myProcess.StartInfo.RedirectStandardOutput = true;
                myProcess.StartInfo.WorkingDirectory       = _scenario.ModelFolder;
                myProcess.OutputDataReceived              += (sender, agrs) =>
                {
                    if (agrs.Data != null)
                    {
                        updateMessage(agrs.Data);
                    }
                };
                myProcess.ErrorDataReceived += (sender, agrs) =>
                {
                    if (agrs.Data != null)
                    {
                        updateMessage(agrs.Data);
                    }
                };
                myProcess.Exited += (send, agrs) =>
                {
                    //update the results
                    if (onSimulationFinished != null)
                    {
                        onSimulationFinished(modelType, interval);
                    }

                    //update the date time of the result
                    //must be called after onSimulationFinished as the result status is updated in onSimulationFinished
                    updateSimulationTime();
                };
                updateMessage("Runing " + ModelType.ToString() + " in " + _scenario.ModelFolder);
                myProcess.Start();
                myProcess.BeginOutputReadLine();
                myProcess.BeginErrorReadLine();
                //myProcess.WaitForExit();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }