コード例 #1
0
ファイル: Main.cs プロジェクト: hut104/ApsimX
        static int Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainView mainForm = new MainView();
            MainPresenter mainPresenter = new MainPresenter();

            try
            {
                mainPresenter.Attach(mainForm, args);
                if (args.Length == 0 || Path.GetExtension(args[0]) != ".cs")
                    Application.Run(mainForm);  
            }
            catch (Exception err)
            {
                File.WriteAllText("errors.txt", err.ToString());
                return 1;
            }

            return 0;
        }
コード例 #2
0
ファイル: ExplorerPresenter.cs プロジェクト: hol353/ApsimX
 /// <summary>Constructor</summary>
 /// <param name="mainPresenter">The presenter for the main window</param>
 public ExplorerPresenter(MainPresenter mainPresenter)
 {
     this.MainPresenter = mainPresenter;
 }
コード例 #3
0
 /// <summary>Initializes a new instance of the <see cref="ExplorerPresenter" /> class</summary>
 /// <param name="mainPresenter">The presenter for the main window</param>
 public ExplorerPresenter(MainPresenter mainPresenter)
 {
     this.MainPresenter = mainPresenter;
 }
コード例 #4
0
 /// <summary>
 /// Attach the view to this presenter.
 /// </summary>
 /// <param name="model"></param>
 /// <param name="view"></param>
 /// <param name="explorerPresenter"></param>
 public void Attach(object model, object view, ExplorerPresenter explorerPresenter)
 {
     this.view           = (CloudJobDisplayView)view;
     this.view.Presenter = this;
     GetCredentials();
 }
コード例 #5
0
        /// <summary>
        /// Pastes the contents of the clipboard.
        /// </summary>
        /// <param name="xml">The XML document text</param>
        /// <param name="parentPath">Path to the parent</param>
        public void Add(string xml, string parentPath)
        {
            try
            {
                XmlDocument document = new XmlDocument();
                try
                {
                    document.LoadXml(xml);
                }
                catch (XmlException)
                {
                    MainPresenter.ShowMessage("Invalid XML. Are you sure you're trying to paste an APSIM model?", Simulation.ErrorLevel.Error);
                }

                object newModel = XmlUtilities.Deserialise(document.DocumentElement, this.ApsimXFile.GetType().Assembly);

                // See if the presenter is happy with this model being added.
                Model         parentModel   = Apsim.Get(this.ApsimXFile, parentPath) as Model;
                AllowDropArgs allowDropArgs = new AllowDropArgs();
                allowDropArgs.NodePath   = parentPath;
                allowDropArgs.DragObject = new DragObject()
                {
                    NodePath  = null,
                    ModelType = newModel.GetType(),
                    Xml       = this.GetClipboardText()
                };

                this.OnAllowDrop(null, allowDropArgs);

                // If it is happy then issue an AddModelCommand.
                if (allowDropArgs.Allow)
                {
                    // If the model xml is a soil object then try and convert from old
                    // APSIM format to new.
                    if (document.DocumentElement.Name == "Soil" && XmlUtilities.Attribute(document.DocumentElement, "Name") != string.Empty)
                    {
                        XmlDocument newDoc = new XmlDocument();
                        newDoc.AppendChild(newDoc.CreateElement("D"));
                        APSIMImporter importer = new APSIMImporter();
                        importer.ImportSoil(document.DocumentElement, newDoc.DocumentElement, newDoc.DocumentElement);
                        XmlNode soilNode = XmlUtilities.FindByType(newDoc.DocumentElement, "Soil");
                        if (soilNode != null &&
                            XmlUtilities.FindByType(soilNode, "Sample") == null &&
                            XmlUtilities.FindByType(soilNode, "InitialWater") == null)
                        {
                            // Add in an initial water and initial conditions models.
                            XmlNode initialWater = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("InitialWater"));
                            XmlUtilities.SetValue(initialWater, "Name", "Initial water");
                            XmlUtilities.SetValue(initialWater, "PercentMethod", "FilledFromTop");
                            XmlUtilities.SetValue(initialWater, "FractionFull", "1");
                            XmlUtilities.SetValue(initialWater, "DepthWetSoil", "NaN");
                            XmlNode initialConditions = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("Sample"));
                            XmlUtilities.SetValue(initialConditions, "Name", "Initial conditions");
                            XmlUtilities.SetValue(initialConditions, "Thickness/double", "1800");
                            XmlUtilities.SetValue(initialConditions, "NO3/double", "10");
                            XmlUtilities.SetValue(initialConditions, "NH4/double", "1");
                            XmlUtilities.SetValue(initialConditions, "NO3Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "NH4Units", "kgha");
                            XmlUtilities.SetValue(initialConditions, "SWUnits", "Volumetric");
                        }

                        document.LoadXml(newDoc.DocumentElement.InnerXml);
                    }

                    IModel child = XmlUtilities.Deserialise(document.DocumentElement, this.ApsimXFile.GetType().Assembly) as IModel;

                    AddModelCommand command = new AddModelCommand(parentModel, document.DocumentElement, this.GetNodeDescription(child), this.view);
                    this.CommandHistory.Add(command, true);
                }
            }
            catch (Exception exception)
            {
                this.MainPresenter.ShowMessage(exception.Message, Simulation.ErrorLevel.Error);
            }
        }
コード例 #6
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))
            {
                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);
            }
        }
コード例 #7
0
ファイル: ExplorerPresenter.cs プロジェクト: lie112/ApsimX
        /// <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);
        }
コード例 #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MainMenu" /> class.
 /// </summary>
 /// <param name="mainPresenter">The presenter to work with</param>
 public MainMenu(MainPresenter mainPresenter)
 {
     presenter = mainPresenter;
 }