// //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(); } }
public AdminFamiliesBAPRequest(UIApplication uiApp, String text) { MainUI uiForm = BARevitTools.Application.thisApp.newMainUi; uiForm.adminFamiliesBAPDoneLabel.Visible = false; RVTDocument doc = uiApp.ActiveUIDocument.Document; SaveAsOptions saveAsOptions = new SaveAsOptions(); saveAsOptions.Compact = true; saveAsOptions.MaximumBackups = 3; saveAsOptions.OverwriteExistingFile = true; Dictionary <string, ExternalDefinition> sharedParameterDefinitions = new Dictionary <string, ExternalDefinition>(); //Determine if the shared parameters file is accessible and try to get the shared parameters so their definition names and definitions could be added to a dictionary bool sharedParametersIsAccessible = true; try { DefinitionGroups sharedParameterGroups = uiApp.Application.OpenSharedParameterFile().Groups; foreach (DefinitionGroup group in sharedParameterGroups) { foreach (ExternalDefinition definition in group.Definitions) { if (!sharedParameterDefinitions.Keys.Contains(definition.Name)) { sharedParameterDefinitions.Add(definition.Name, definition); } } } } //If the access fails, then the shared parameters file was not accessible. catch { sharedParametersIsAccessible = false; } //Get the number of families to process from the DataGridView int filesToProcess = 0; foreach (DataGridViewRow rowCount in uiForm.adminFamiliesBAPFamiliesDGV.Rows) { if (rowCount.Cells["Family Select"].Value.ToString() == "True") { filesToProcess++; } } //Prepare the progress bar uiForm.adminFamiliesBAPProgressBar.Value = 0; uiForm.adminFamiliesBAPProgressBar.Minimum = 0; uiForm.adminFamiliesBAPProgressBar.Maximum = filesToProcess; uiForm.adminFamiliesBAPProgressBar.Step = 1; uiForm.adminFamiliesBAPProgressBar.Visible = true; //Stop any edits to the DataGridView of parameters to add uiForm.adminFamiliesBAPParametersDGV.EndEdit(); try { foreach (DataGridViewRow row in uiForm.adminFamiliesBAPFamiliesDGV.Rows) { try { //If the checkbox for including the family in the process is not null, continue if (row.Cells["Family Select"].Value != null) { //Grab the file path for the family string filePath = row.Cells["Family Path"].Value.ToString(); List <string> famParamNames = new List <string>(); //If the checkbox is checked to select the family if (row.Cells["Family Select"].Value.ToString() == "True") { //Open the family document RVTDocument famDoc = RVTOperations.OpenRevitFile(uiApp, filePath); if (famDoc.IsFamilyDocument) { //Grab the family manager and make a list of parameter names already in the family FamilyManager familyManager = famDoc.FamilyManager; foreach (FamilyParameter famParam in familyManager.Parameters) { if (!famParamNames.Contains(famParam.Definition.Name)) { famParamNames.Add(famParam.Definition.Name); } else { continue; } } foreach (DataGridViewRow newParamRow in uiForm.adminFamiliesBAPParametersDGV.Rows) { try { //Get the name of the parameter, group, and type of parameter to add string name = newParamRow.Cells["Parameter Name"].Value.ToString(); BuiltInParameterGroup group = RVTOperations.GetBuiltInParameterGroupFromString(newParamRow.Cells["Parameter Group"].Value.ToString()); ParameterType type = RVTOperations.GetParameterTypeFromString(newParamRow.Cells["Parameter Type"].Value.ToString()); //Determine if the checkbox for making the parameter an instance parameter is checked bool isInstance = false; try { isInstance = Convert.ToBoolean(newParamRow.Cells["Parameter Is Instance"].Value.ToString()); } catch { isInstance = false; } //Determine if the read-only checkbox for the parameter being a shared parameter is checked. bool isShared = false; try { isShared = Convert.ToBoolean(newParamRow.Cells["Parameter Is Shared"].Value.ToString()); } catch { isShared = false; } //If the parameter is shared, and the parameter file is still accessible, and the family does not already contain a parameter with that name, continue if (isShared == true && sharedParametersIsAccessible == true && !famParamNames.Contains(name)) { using (Transaction t = new Transaction(famDoc, "Add Parameter")) { t.Start(); //Get the parameter definition from the dictionary of shared parameters, then add it to the family ExternalDefinition definition = sharedParameterDefinitions[newParamRow.Cells["Parameter Name"].Value.ToString()]; FamilyParameter newParam = familyManager.AddParameter(definition, group, isInstance); try { //Try to assign the parameter value if one is to be assigned if (newParamRow.Cells["Parameter Value"].Value != null) { //If the number of types is greater than 0, cycle through them if (familyManager.Types.Size > 0) { foreach (FamilyType familyType in familyManager.Types) { //For each type, make a subtransaction SubTransaction s1 = new SubTransaction(famDoc); s1.Start(); try { //Then set the family type as current familyManager.CurrentType = familyType; //Attempt to set the parameter value RVTOperations.SetFamilyParameterValue(familyManager, newParam, RVTOperations.SetParameterValueFromString(newParamRow.Cells["Parameter Type"].Value.ToString(), newParamRow.Cells["Parameter Value"].Value)); s1.Commit(); } catch { //If that fails, let the user know and break out of the loop MessageBox.Show(String.Format("Could not assign value {0} to parameter {1} for type {2} in family {3}.", newParamRow.Cells["Parameter Value"], newParamRow.Cells["Parameter Name"], familyType.Name, row.Cells["Family Name"])); break; } } } else { //If there was was no family types in the family, just set it for the default one RVTOperations.SetFamilyParameterValue(familyManager, newParam, RVTOperations.SetParameterValueFromString(newParamRow.Cells["Parameter Type"].Value.ToString(), newParamRow.Cells["Parameter Value"].Value)); } } } catch { //If assignment fails, let the user know the parameter value, parameter name, and the family where the failure occured. MessageBox.Show(String.Format("Could not assign value {0} to parameter {1} for family {2}", newParamRow.Cells["Parameter Value"], newParamRow.Cells["Parameter Name"], row.Cells["Family Name"])); } t.Commit(); } } else if (isShared == true && sharedParametersIsAccessible == false && !famParamNames.Contains(name)) { //If the shared parameter file could not be accessed, let the user know MessageBox.Show(String.Format("Could not set the shared parameter {0} because the shared parameters file for this project could not be found. " + "Verify the shared parameters file is mapped correctly.", name)); } else if (isShared != true && !famParamNames.Contains(name)) { //Otherwise, just make a standard parameter using (Transaction t = new Transaction(famDoc, "Add Parameter")) { t.Start(); FamilyParameter newParam = familyManager.AddParameter(name, group, type, isInstance); try { if (newParamRow.Cells["Parameter Value"].Value != null) { RVTOperations.SetFamilyParameterValue(familyManager, newParam, RVTOperations.SetParameterValueFromString(newParamRow.Cells["Parameter Type"].Value.ToString(), newParamRow.Cells["Parameter Value"].Value)); } } catch { continue; } t.Commit(); } } else { //If all other conditions were not met, then the parameter already exists. MessageBox.Show(String.Format("Could not make parameter '{0}' for {1} because it already exists.", name, famDoc.Title)); } } catch { continue; } finally { //Save the family ModelPath modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath); famDoc.SaveAs(filePath, saveAsOptions); } } } //Close the family famDoc.Close(false); } else { continue; } //Step forward the progress bar uiForm.adminFamiliesBAPProgressBar.PerformStep(); } } catch { MessageBox.Show("The family could not be opened, likely due to being saved in a newer version of Revit"); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } finally { //Clean up the backups when done GeneralOperations.CleanRfaBackups(uiForm.adminFamiliesBAPFamilyDirectory); } uiForm.adminFamiliesBAPProgressBar.Visible = false; uiForm.adminFamiliesBAPDoneLabel.Visible = true; uiForm.Update(); }