Exemplo n.º 1
0
        public void CreateFileDocumentation(object sender, EventArgs e)
        {
            try
            {
                if (this.explorerPresenter.Save())
                {
                    explorerPresenter.MainPresenter.ShowMessage("Creating documentation...", Simulation.MessageType.Information);
                    explorerPresenter.MainPresenter.ShowWaitCursor(true);

                    var modelToDocument = explorerPresenter.ApsimXFile.FindByPath(explorerPresenter.CurrentNodePath)?.Value as IModel;

                    var destinationFolder = Path.GetDirectoryName(explorerPresenter.ApsimXFile.FileName);
                    CreateFileDocumentationCommand command = new CreateFileDocumentationCommand(explorerPresenter, destinationFolder);
                    string fileNameWritten = command.FileNameWritten;

                    command.Do();
                    explorerPresenter.MainPresenter.ShowMessage("Written " + fileNameWritten, Simulation.MessageType.Information);

                    // Open the document.
                    if (ProcessUtilities.CurrentOS.IsWindows)
                    {
                        Process.Start(fileNameWritten);
                    }
                }
            }
            catch (Exception err)
            {
                explorerPresenter.MainPresenter.ShowError(err);
            }
            finally
            {
                explorerPresenter.MainPresenter.ShowWaitCursor(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create documentation based on the specified file.
        /// </summary>
        /// <param name="documentObject">The documentObject node that describes what to document.</param>
        /// <param name="apsimDirectory">The APSIM root directory.</param>
        /// <param name="destinationFolder">The folder where the PDF should be created.</param>
        /// <param name="destinationUrl">The server destination URL where all files will end up.</param>
        /// <returns>HTML snippet for a single model document.</returns>
        private static string CreateModelDocumentation(JObject documentObject, string apsimDirectory, string destinationFolder, string destinationUrl)
        {
            string href;
            string hrefName = documentObject["Name"].ToString();

            if (documentObject["URL"] != null)
            {
                href = documentObject["URL"].ToString();
                return(string.Format("<p><a href=\"{0}\" target=\"_blank\">{1}</a></p>", href, hrefName));
            }
            else
            {
                var fileName = Path.Combine(apsimDirectory, documentObject["FileName"].ToString());
                if (File.Exists(fileName))
                {
                    // Open the file.
                    var simulations = FileFormat.ReadFromFile <Simulations>(fileName, out List <Exception> creationExceptions);
                    if (creationExceptions?.Count > 0)
                    {
                        throw creationExceptions[0];
                    }

                    // Create some necessary presenters and views.
                    var mainPresenter     = new MainPresenter();
                    var explorerPresenter = new ExplorerPresenter(mainPresenter);

                    var explorerView = new ExplorerView(null);
                    explorerPresenter.Attach(simulations, explorerView, explorerPresenter);

                    // Document model.
                    if (documentObject["ModelNameToDocument"] == null)
                    {
                        Console.WriteLine("Creating documentation from " + fileName);

                        // Whole of simulation document.
                        var createDoc = new CreateFileDocumentationCommand(explorerPresenter, destinationFolder);
                        createDoc.Do();
                        href = Path.GetFileName(createDoc.FileNameWritten);
                    }
                    else
                    {
                        Console.WriteLine("Creating model description documentation from " + fileName);

                        // Specific model description documentation.
                        var modelNameToDocument = documentObject["ModelNameToDocument"].ToString();
                        var model = simulations.FindInScope(modelNameToDocument) as IModel;
                        if (model == null)
                        {
                            return(null);
                        }
                        var outputFileName = documentObject["OutputFileName"]?.ToString();
                        var createDoc      = new CreateParamsInputsOutputsDocCommand(explorerPresenter, model, destinationFolder, outputFileName);
                        createDoc.Do();
                        href = Path.GetFileName(createDoc.FileNameWritten);
                    }

                    return(string.Format("<p><a href=\"{0}/{1}\" target=\"_blank\">{2}</a></p>", destinationUrl, href, hrefName));
                }
                else
                {
                    return(null);
                }
            }
        }