コード例 #1
0
        public AdminFamiliesUFRequest(UIApplication uiApp, String text)
        {
            try
            {
                MainUI      uiForm = BARevitTools.Application.thisApp.newMainUi;
                RVTDocument doc    = uiApp.ActiveUIDocument.Document;

                bool   fullSync        = uiForm.adminFamiliesUFFullSyncCheckbox.Checked;
                string currentVersion  = Properties.Settings.Default.BARTRevitFamilyCurrentYear;
                string upgradedVersion = uiApp.Application.VersionNumber;
                //The current library path is needed to make the new library path by swapping the year of the current development library with the year of the Revit version running.
                string currentLibraryPath  = Properties.Settings.Default.RevitBAFamilyLibraryPath;
                string upgradedLibraryPath = currentLibraryPath.Replace(currentVersion, upgradedVersion);
                //If the new library does not yet exist, make it. This will likely be used only once when a new version of Revit is released.
                if (!Directory.Exists(upgradedLibraryPath))
                {
                    Directory.CreateDirectory(upgradedLibraryPath);
                }

                //Do some cleanup in the current library and the new library if backups exist
                GeneralOperations.CleanRfaBackups(currentLibraryPath);
                GeneralOperations.CleanRfaBackups(upgradedLibraryPath);

                //Create a list of family paths in the current library, and a list of family paths in the new library.
                List <string> familiesInCurrentLibrary = GeneralOperations.GetAllRvtFamilies(currentLibraryPath, false);
                //Also, make a dictionary because while the family paths will be evaluated, the paths themselves will need to be retrieved later by family name.
                Dictionary <string, string> currentLibraryDict  = new Dictionary <string, string>();
                List <string> familiesInUpgradedLibrary         = GeneralOperations.GetAllRvtFamilies(upgradedLibraryPath, false);
                Dictionary <string, string> upgradedLibraryDict = new Dictionary <string, string>();
                List <string> familiesToUpgrade = new List <string>();
                List <string> familiesToDelete  = new List <string>();
                List <string> upgradedFamilies  = new List <string>();
                List <string> deletedFamilies   = new List <string>();

                //Just a variable for storing which family name is currently being evaluated
                string currentEvaluation = "";
                //If there are families in the new library...
                if (familiesInUpgradedLibrary.Count > 0)
                {
                    foreach (string upgradedFamilyPath in familiesInUpgradedLibrary)
                    {
                        try
                        {
                            //Get the name of the family and add it as a key in the dictionary, along with the family path
                            currentEvaluation = Path.GetFileNameWithoutExtension(upgradedFamilyPath);
                            upgradedLibraryDict.Add(Path.GetFileNameWithoutExtension(upgradedFamilyPath), upgradedFamilyPath);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }

                //Next, evaluate what families are in the current library
                foreach (string currentFamilyPath in familiesInCurrentLibrary)
                {
                    //Skip any path that contains "Archive" or "Backup"
                    if (!currentFamilyPath.Contains("Archive") && !currentFamilyPath.Contains("Backup"))
                    {
                        try
                        {
                            //Add the name off the family to dictionary as a key assigned the path
                            currentEvaluation = Path.GetFileNameWithoutExtension(currentFamilyPath);
                            currentLibraryDict.Add(Path.GetFileNameWithoutExtension(currentFamilyPath), currentFamilyPath);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }

                //Going back to the families in the new library
                if (familiesInUpgradedLibrary.Count > 0)
                {
                    //Evaluate each family name serving as a key in the dictionary for the new library
                    foreach (string upgradedFamily in upgradedLibraryDict.Keys)
                    {
                        //If the current library dictionary has a key with the same family name...
                        if (currentLibraryDict.Keys.Contains(upgradedFamily))
                        {
                            //Compare the dates when the families were last modified to determine if the one in the new library is older than the one in the current library
                            DateTime currentFamilyWriteTime  = File.GetLastWriteTime(currentLibraryDict[upgradedFamily]);
                            DateTime upgradedFamilyWriteTime = File.GetLastWriteTime(upgradedLibraryDict[upgradedFamily]);
                            //If the familly in the current library is more recently modified, add it to the list of families that will need upgraded and saved to the new library, thus replacing the one in the new library with the updated one
                            if (currentFamilyWriteTime > upgradedFamilyWriteTime)
                            {
                                familiesToUpgrade.Add(currentLibraryDict[upgradedFamily]);
                            }
                        }
                        else
                        {
                            //Otherwise, if the family in the new library does not exist in the current library, add it to a list of families to delete if a full sync is performed
                            familiesToDelete.Add(upgradedLibraryDict[upgradedFamily]);
                        }
                    }

                    //If the new library does not contain the family in the current library, add it to the list of families to upgrade and save to the new library.
                    foreach (string originalFamily in currentLibraryDict.Keys)
                    {
                        if (!upgradedLibraryDict.Keys.Contains(originalFamily))
                        {
                            familiesToUpgrade.Add(currentLibraryDict[originalFamily]);
                        }
                    }
                }
                else
                {
                    //If there are no families in the new library, such as one that was just created for a new version of Revit, then all families from the current library will need upgraded and saved in the new library
                    familiesToUpgrade = familiesInCurrentLibrary;
                }

                //Cycle through all of the families that will need to be upgraded and saved to the new library
                foreach (string familyToUpgrade in familiesToUpgrade)
                {
                    try
                    {
                        //First, determine if the family can be opened and is not a newer version of Revit
                        if (RVTOperations.RevitVersionUpgradeCheck(uiApp, familyToUpgrade, true))
                        {
                            //If it can be opened, upgrade it and add its name to the list of families that were upgraded
                            bool result = RVTOperations.UpgradeRevitFile(uiApp, familyToUpgrade, familyToUpgrade.Replace(currentVersion, upgradedVersion), true);
                            if (result == true)
                            {
                                upgradedFamilies.Add(Path.GetFileNameWithoutExtension(familyToUpgrade));
                            }
                        }
                        else
                        {
                            //If the family could not be opened because it was saved in a newer version of Revit, let the user know and skip over the family
                            MessageBox.Show(String.Format("{0} was saved in a new version of Revit", Path.GetFileNameWithoutExtension(familyToUpgrade)));
                        }
                    }
                    catch (Exception f)
                    { MessageBox.Show(f.ToString()); }
                }

                //If the user checked the box to do a full sync, delete the families that were added to the list of families to delete. This will ensure the new library and current library match regarding what families they contain
                if (fullSync)
                {
                    foreach (string familyToDelete in familiesToDelete)
                    {
                        try
                        {
                            File.Delete(familyToDelete);
                            deletedFamilies.Add(Path.GetFileNameWithoutExtension(familyToDelete));
                        }
                        catch (Exception e)
                        { MessageBox.Show(e.ToString()); }
                    }
                }

                //Update the listboxes of the UI showing what families were upgraded and what were deleted
                uiForm.adminFamiliesUFUpgradedFamiliesListBox.DataSource = upgradedFamilies;
                uiForm.adminFamiliesUFDeletedFamiliesListBox.DataSource  = deletedFamilies;
                //Cleanup the family backups in the new library if they exist
                GeneralOperations.CleanRfaBackups(upgradedLibraryPath);
            }
            catch (Exception g)
            {
                MessageBox.Show(g.ToString());
            }
        }
コード例 #2
0
        //
        //This class is associated with the Upgrade Projects tool
        public SetupUPRequest(UIApplication uiApp, String text)
        {
            MainUI       uiForm = BARevitTools.Application.thisApp.newMainUi;
            DataGridView dgv    = uiForm.setupUPDataGridView;

            dgv.EndEdit();

            RVTDocument doc = uiApp.ActiveUIDocument.Document;
            string      hostFilePathToUpgrade  = uiForm.setupUPOriginalFilePathTextBox.Text;
            string      hostFilePathForUpgrade = uiForm.setupUPUpgradedFilePathSetTextBox.Text + "\\" + uiForm.setupUPUpgradedFilePathUserTextBox.Text + ".rvt";

            //Reset the progress bar
            uiForm.setupUPProgressBar.Value   = 0;
            uiForm.setupUPProgressBar.Minimum = 0;
            uiForm.setupUPProgressBar.Step    = 1;

            //Determine the number of steps for the progress bar based on the number of links checked for upgrading plus 1 for the host
            int progressBarSteps = 1;

            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (row.Cells["Upgrade"].Value != null)
                {
                    if (row.Cells["Upgrade"].Value.ToString() == "True")
                    {
                        progressBarSteps++;
                    }
                }
            }
            uiForm.setupUPProgressBar.Maximum = progressBarSteps;

            //Let the user know if they are trying to save over a file that already exists
            if (File.Exists(hostFilePathForUpgrade))
            {
                MessageBox.Show("A file already exists with the name and location specified for the upgrade of the host Revit project file");
            }
            //If they didn't set a save location for the file, let them know
            else if (uiForm.setupUPUpgradedFilePathUserTextBox.Text == "")
            {
                MessageBox.Show("No location for the upgrade of the host Revit project file is set");
            }
            else
            {
                //Otherwise, show the progress bar and step through the rows of the DataGridView links
                uiForm.setupUPProgressBar.Visible = true;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    try
                    {
                        if (dgv.Rows[i].Cells["Upgrade"].Value != null)
                        {
                            //If the link is allowed to be upgraded, and its checkbox is checked, continue
                            if (dgv.Rows[i].Cells["Allow Upgrade"].Value.ToString() == "True" && dgv.Rows[i].Cells["Upgrade"].Value.ToString() == "True")
                            {
                                //Grab the orignial path for the link and the path for where it will be saved
                                string linkFilePathToUpgrade  = dgv.Rows[i].Cells["Original Path"].Value.ToString();
                                string linkFilePathForUpgrade = dgv.Rows[i].Cells["New Path"].Value.ToString();
                                //Perform the upgrade operations on the file and report if it was successful
                                bool linkResult = RVTOperations.UpgradeRevitFile(uiApp, linkFilePathToUpgrade, linkFilePathForUpgrade, false);

                                if (linkResult == true)
                                {
                                    //If the upgrade was successful, set the Upgrade Result column to true and set the row's background color to GreenYellow
                                    dgv.Rows[i].Cells["Upgrade Result"].Value = true;
                                    dgv.Rows[i].DefaultCellStyle.BackColor    = System.Drawing.Color.GreenYellow;
                                }
                                else
                                {
                                    //If the upgrade failed, set the Upgrade Result column to false and set the background color to red
                                    dgv.Rows[i].Cells["Upgrade Result"].Value = false;
                                    dgv.Rows[i].DefaultCellStyle.BackColor    = System.Drawing.Color.Red;
                                }
                                //Step forward the progress bar
                                uiForm.setupUPProgressBar.PerformStep();
                                uiForm.Update();
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }

                //Once the links are done upgrading, upgrade the host
                bool hostResult = RVTOperations.UpgradeRevitFile(uiApp, hostFilePathToUpgrade, hostFilePathForUpgrade, false);
                //Determine how many links were able to be upgraded
                int countOfUpgradeLinks = 0;
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    try
                    {
                        if (row.Cells["Upgrade"].Value != null)
                        {
                            if (row.Cells["Upgrade"].Value.ToString() == "True")
                            {
                                countOfUpgradeLinks++;
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                    finally
                    {
                        uiForm.setupUPProgressBar.PerformStep();
                        uiForm.Update();
                    }
                }

                //If the host was able to be upgraded, continue
                if (hostResult == true)
                {
                    //Set the background color of the text box to GreenYellow
                    uiForm.setupUPOriginalFilePathTextBox.BackColor = System.Drawing.Color.GreenYellow;
                    if (countOfUpgradeLinks > 0)
                    {
                        try
                        {
                            //Open the upgraded host and get the links
                            RVTDocument hostDoc = RVTOperations.OpenRevitFile(uiApp, hostFilePathForUpgrade);
                            Dictionary <string, RevitLinkType> linkNames = new Dictionary <string, RevitLinkType>();
                            var linkTypes = new FilteredElementCollector(hostDoc).OfClass(typeof(RevitLinkType)).ToElements();
                            foreach (RevitLinkType linkType in linkTypes)
                            {
                                //Add each link to a dictionary with their file name and indexed link type
                                linkNames.Add(linkType.Name.Replace(".rvt", ""), linkType);
                            }

                            //Cycle through the links
                            foreach (DataGridViewRow row in dgv.Rows)
                            {
                                try
                                {
                                    //If the link's row was checked for upgrading, it was able to be upgraded, the upgraded file exists at the New Path location, and the dictionary of links contains the original name of the link, continue
                                    if (row.Cells["Upgrade"].Value.ToString() == "True" &&
                                        row.Cells["Upgrade Result"].Value.ToString() == "True" &&
                                        File.Exists(row.Cells["New Path"].Value.ToString()) &&
                                        linkNames.Keys.Contains(row.Cells["Original Name"].Value.ToString()))
                                    {
                                        try
                                        {
                                            //Get the link to reload via the name from the dictionary
                                            RevitLinkType linkToReload = linkNames[row.Cells["Original Name"].Value.ToString().Replace(".rvt", "")];
                                            //Convert the link's user path to a model path, then reload it
                                            ModelPath modelPathToLoadFrom = ModelPathUtils.ConvertUserVisiblePathToModelPath(row.Cells["New Path"].Value.ToString());
                                            linkToReload.LoadFrom(modelPathToLoadFrom, new WorksetConfiguration());
                                        }
                                        catch (Exception e)
                                        {
                                            //If the link was upgraded but could not be reloaded, set the background color to orange and let the user know it could not be reloaded
                                            row.DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
                                            MessageBox.Show(String.Format("Could not remap the link named {0} in the host file", row.Cells["Original Name"].Value.ToString()));
                                            MessageBox.Show(e.ToString());
                                        }
                                    }
                                }
                                catch { continue; }
                            }
                            //Save the upgraded host file
                            RVTOperations.SaveRevitFile(uiApp, hostDoc, true);
                        }
                        catch (Exception e) { MessageBox.Show(e.ToString()); }
                    }
                }
                else
                {
                    //If the host file failed to upgrade, set its text box background color to red
                    uiForm.setupUPOriginalFilePathTextBox.BackColor = System.Drawing.Color.Red;
                }
                uiForm.Update();
                uiForm.Refresh();
            }
        }