/// <summary> /// Generates .apsimx files for each child model under a given model. /// Returns false if errors were encountered, or true otherwise. /// </summary> /// <param name="model">Model to generate .apsimx files for.</param> /// <param name="path"> /// Path which the files will be saved to. /// If null, the user will be prompted to choose a directory. /// </param> public bool GenerateApsimXFiles(IModel model, string path = null) { if (string.IsNullOrEmpty(path)) { IFileDialog fileChooser = new FileDialog() { Prompt = "Select a directory to save model files to.", Action = FileDialog.FileActionType.SelectFolder }; path = fileChooser.GetFile(); if (!string.IsNullOrEmpty(path)) { MainPresenter.ShowMessage("Generating simulation files: ", Simulation.MessageType.Information); var runner = new Runner(model); var errors = Models.Core.Run.GenerateApsimXFiles.Generate(runner, path, (int percent) => { MainPresenter.ShowProgress(percent, false); }); if (errors == null || errors.Count == 0) { MainPresenter.ShowMessage("Successfully generated .apsimx files under " + path + ".", Simulation.MessageType.Information); return(true); } else { MainPresenter.ShowError(errors); return(false); } } } return(true); }
/// <summary> /// Generates .apsimx files for each child model under a given model. /// Returns false if errors were encountered, or true otherwise. /// </summary> /// <param name="model">Model to generate .apsimx files for.</param> /// <param name="path"> /// Path which the files will be saved to. /// If null, the user will be prompted to choose a directory. /// </param> public bool GenerateApsimXFiles(IModel model, string path = null) { List <IModel> children; if (model is ISimulationGenerator) { children = new List <IModel> { model }; } else { children = Apsim.ChildrenRecursively(model, typeof(ISimulationGenerator)); } if (string.IsNullOrEmpty(path)) { IFileDialog fileChooser = new FileDialog() { Prompt = "Select a directory to save model files to.", Action = FileDialog.FileActionType.SelectFolder }; path = fileChooser.GetFile(); if (string.IsNullOrEmpty(path)) { return(false); } } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } List <Exception> errors = new List <Exception>(); int i = 0; foreach (IModel sim in children) { MainPresenter.ShowMessage("Generating simulation files: ", Simulation.MessageType.Information); MainPresenter.ShowProgress(100 * i / children.Count, false); while (GLib.MainContext.Iteration()) { ; } try { (sim as ISimulationGenerator).GenerateApsimXFile(path); } catch (Exception err) { errors.Add(err); } i++; } if (errors.Count < 1) { MainPresenter.ShowMessage("Successfully generated .apsimx files under " + path + ".", Simulation.MessageType.Information); return(true); } else { MainPresenter.ShowError(errors); return(false); } }
/// <summary> /// Generates .apsimx files for each child model under a given model. /// Returns false if errors were encountered, or true otherwise. /// </summary> /// <param name="model">Model to generate .apsimx files for.</param> /// <param name="path"> /// Path which the files will be saved to. /// If null, the user will be prompted to choose a directory. /// </param> public async Task <bool> GenerateApsimXFiles(IModel model, string path = null) { if (string.IsNullOrEmpty(path)) { IFileDialog fileChooser = new FileDialog() { Prompt = "Select a directory to save model files to.", Action = FileDialog.FileActionType.SelectFolder }; path = fileChooser.GetFile(); } if (!string.IsNullOrEmpty(path)) { MainPresenter.ShowMessage("Generating simulation files: ", Simulation.MessageType.Information); try { var runner = new Runner(model); await Task.Run(() => Models.Core.Run.GenerateApsimXFiles.Generate(runner, 1, path, p => MainPresenter.ShowProgress(p, false), true)); MainPresenter.ShowMessage("Successfully generated .apsimx files under " + path + ".", Simulation.MessageType.Information); return(true); } catch (Exception err) { MainPresenter.ShowError(err); return(false); } } return(true); }