Exemplo n.º 1
0
        public void OnGenerateApsimXFiles(object sender, EventArgs e)
        {
            IModel node = Apsim.Get(explorerPresenter.ApsimXFile, explorerPresenter.CurrentNodePath) as IModel;

            List <IModel> children;

            if (node is Experiment)
            {
                children = new List <IModel> {
                    node
                };
            }
            else
            {
                children = Apsim.ChildrenRecursively(node, typeof(Experiment));
            }

            string outDir = ViewBase.AskUserForDirectory("Select a directory to save model files to.");

            if (outDir == null)
            {
                return;
            }
            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }
            string err = "";
            int    i   = 0;

            children.ForEach(expt =>
            {
                explorerPresenter.MainPresenter.ShowMessage("Generating simulation files: ", Simulation.ErrorLevel.Information);
                explorerPresenter.MainPresenter.ShowProgress(100 * i / children.Count, false);
                while (GLib.MainContext.Iteration())
                {
                    ;
                }
                err += (expt as Experiment).GenerateApsimXFile(outDir);
                i++;
            });
            if (err.Length < 1)
            {
                explorerPresenter.MainPresenter.ShowMessage("Successfully generated .apsimx files under " + outDir + ".", Simulation.ErrorLevel.Information);
            }
            else
            {
                explorerPresenter.MainPresenter.ShowMessage(err, Simulation.ErrorLevel.Error);
            }
        }
Exemplo n.º 2
0
        /// <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))
            {
                path = ViewBase.AskUserForDirectory("Select a directory to save model files to.");
                if (path == null)
                {
                    return(false);
                }
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            List <Exception> errors = new List <Exception>();
            int i = 0;

            children.ForEach(sim =>
            {
                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);
            }
        }