/// <summary>
        /// Main entry point for the application.
        /// </summary>
        /// <param name="dbp">The Diagram Buildin g Properties.</param>
        /// <param name="worker">The worker.</param>
        /// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
        /// <returns></returns>
        //public string GenerateDiagram(string connectionName, List<string> entities, RetrieveAllEntitiesResponse response, int selectedDiagramEntityLabelIndex, BackgroundWorker worker, DoWorkEventArgs e)
        public string GenerateDiagram(DiagramBuildingProperties dbp, BackgroundWorker worker, DoWorkEventArgs e)
        {
            String filename = String.Empty;

            VisioApi.Application application;
            VisioApi.Document    document;
            DiagramBuilder       builder = new DiagramBuilder();

            try
            {
                // Load Visio and create a new document.
                application         = new VisioApi.Application();
                application.Visible = false; // Not showing the UI increases rendering speed
                builder.VersionName = application.Version;
                document            = application.Documents.Add(String.Empty);

                builder._application = application;
                builder._document    = document;

                builder._metadataResponse     = dbp.environmentStructure;
                builder.selectedEntitiesNames = dbp.entities;
                builder.dbp = dbp;

                // Diagram all entities if given no command-line parameters, otherwise diagram
                // those entered as command-line parameters.
                builder.BuildDiagram(dbp.entities, worker, e);
                filename = "EntitesStructure\\Diagrams.vsd";

                // Save the diagram in the current directory using the name of the first
                // entity argument or "AllEntities" if none were given. Close the Visio application.
                document.SaveAs(Directory.GetCurrentDirectory() + "\\" + filename);
                application.Quit();
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                throw;
            }
            catch (System.Exception)
            {
                throw;
            }

            return(filename);
        }
        private void bwGenerateGiagrams_DoWork(object sender, DoWorkEventArgs e)
        {
            //transportStopped = false;
            BackgroundWorker worker = sender as BackgroundWorker;

            //Generate Diagrams
            DiagramBuilder db = new DiagramBuilder();
            try
            {
                string filename = db.GenerateDiagram(DGconnectionName, DGentities, environmentStructure, worker, e);
                DiagramGeneratedSuccesfully = true;
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                DiagramGeneratedSuccesfully = false;
                toolStripStatusLabel1.Text = "Error.";
                MessageBox.Show("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                DiagramGeneratedSuccesfully = false;
                toolStripStatusLabel1.Text = "Error.";
                if (ex.InnerException != null)
                    MessageBox.Show("Error:" + ex.Message + "\n" + ex.InnerException.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    //Personalise error message if Visio not present on the machine
                    if (ex.Message != null && ex.Message.Contains("REGDB_E_CLASSNOTREG"))
                    {
                        string errorMessage = "Error:" + ex.Message + "\n\n" + "You're probably seen this message because Visio 2010 is not found on this machine. \nYou have to install Visio 2010 before generating diagrams!";
                        MessageBox.Show(errorMessage, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                        MessageBox.Show("Error:" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// Main entry point for the application.
        /// </summary>
        /// <param name="connectionName">Name of the connection.</param>
        /// <param name="entities">The entities.</param>
        /// <param name="response">The response.</param>
        /// <param name="worker">The worker.</param>
        /// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
        /// <returns></returns>
        public string GenerateDiagram(string connectionName, List<string> entities, RetrieveAllEntitiesResponse response, BackgroundWorker worker, DoWorkEventArgs e)
        {
            String filename = String.Empty;
            VisioApi.Application application;
            VisioApi.Document document;
            DiagramBuilder builder = new DiagramBuilder();

            try
            {
                // Load Visio and create a new document.
                application = new VisioApi.Application();
                application.Visible = false; // Not showing the UI increases rendering speed
                document = application.Documents.Add(String.Empty);

                builder._application = application;
                builder._document = document;

                builder._metadataResponse = response;
                builder.selectedEntitiesNames = entities;

                // Diagram all entities if given no command-line parameters, otherwise diagram
                // those entered as command-line parameters.
                builder.BuildDiagram(entities, String.Join(", ", entities), worker, e);
                filename = "EntitesStructure\\Diagrams.vsd";

                // Save the diagram in the current directory using the name of the first
                // entity argument or "AllEntities" if none were given. Close the Visio application.
                document.SaveAs(Directory.GetCurrentDirectory() + "\\" + filename);
                application.Quit();
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                throw;
            }
            catch (System.Exception)
            {
                throw;
            }

            return filename;
        }