public AdminFamiliesSRCPRequest(UIApplication uiApp, String text) { MainUI uiForm = BARevitTools.Application.thisApp.newMainUi; RVTDocument doc = uiApp.ActiveUIDocument.Document; //Clear out the backup families from the directory List <string> backupFamilies = GeneralOperations.GetAllRvtBackupFamilies(uiForm.adminFamiliesSRCPDirectoryTextBox.Text, true); GeneralOperations.CleanRfaBackups(backupFamilies); //Get the family files from the directory. If the option to use the date since last modified was checked, use the first method, else use the second method List <string> familyFiles = new List <string>(); if (uiForm.adminFamiliesSRCPUseDateCheckBox.Checked) { familyFiles = GeneralOperations.GetAllRvtFamilies(uiForm.adminFamiliesSRCPDirectoryTextBox.Text, uiForm.adminFamiliesSRCPDatePicker.Value, true); } else { familyFiles = GeneralOperations.GetAllRvtFamilies(uiForm.adminFamiliesSRCPDirectoryTextBox.Text, false); } //Verify the number of families collected is more than 0 if (familyFiles.Count > 0) { foreach (string familyFile in familyFiles) { RVTDocument famDoc = RVTOperations.OpenRevitFile(uiApp, familyFile); Family family = famDoc.OwnerFamily; Parameter rcp = family.get_Parameter(BuiltInParameter.ROOM_CALCULATION_POINT); if (rcp.AsInteger() == 0) { try { using (Transaction t1 = new Transaction(famDoc, "SetRoomCalculationPoint")) { t1.Start(); rcp.Set(1); t1.Commit(); } uiForm.adminFamiliesSRCPListBox.Items.Add(famDoc.Title.Replace(".rfa", "") + ": Updated"); } catch (Exception e) { uiForm.adminFamiliesSRCPListBox.Items.Add(famDoc.Title.Replace(".rfa", "") + ": FAILED"); } } else { uiForm.adminFamiliesSRCPListBox.Items.Add(famDoc.Title.Replace(".rfa", "") + ": Ignored"); } RVTOperations.SaveRevitFile(uiApp, famDoc, true); } } GeneralOperations.CleanRfaBackups(uiForm.adminFamiliesSRCPDirectoryTextBox.Text); }
// //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(); } }
private void SetParameters(UIApplication uiApp, string familyFile, ExternalDefinition externalDefinition) { try { //Get the FileInfo of the family file and then get the LastWriteTime value as a date string in MM/DD/YYYY format FileInfo fileInfo = new FileInfo(familyFile); string lastModified = fileInfo.LastWriteTime.ToShortDateString(); //Open the family file RVTDocument famDoc = RVTOperations.OpenRevitFile(uiApp, familyFile); if (famDoc != null) { //Get the family manager for the family file FamilyManager famMan = famDoc.FamilyManager; //Get the family parameters and add them to a dictionary indexed by parameter name FamilyParameter famParameter = null; Dictionary <string, FamilyParameter> famParamDict = new Dictionary <string, FamilyParameter>(); foreach (FamilyParameter famParam in famMan.Parameters) { famParamDict.Add(famParam.Definition.Name, famParam); } //Get the number of family types because there must be at least one family type to make this work FamilyTypeSet types = famMan.Types; int numberOfTypes = types.Size; Transaction t1 = new Transaction(famDoc, "SetParameters"); t1.Start(); if (numberOfTypes == 0) { //If the number of family types was 0, then one must be made. Thus Default is a family type created try { famMan.NewType("Default"); } catch { MessageBox.Show(String.Format("Could not make a default type or find any type for {0}", familyFile)); } } //Once the existence of a family type is confirmed, move on with determining if the version parameter already exists. If it doesn't add the parameter to the family and regenerate the document if (!famParamDict.Keys.Contains(BARevitTools.Properties.Settings.Default.RevitUFVPParameter)) { famParameter = famMan.AddParameter(externalDefinition, BuiltInParameterGroup.PG_IDENTITY_DATA, false); famDoc.Regenerate(); } else { famParameter = famParamDict[BARevitTools.Properties.Settings.Default.RevitUFVPParameter]; } //Check to see if the value for the parameter is equal to the date last modified if (famMan.CurrentType.AsString(famParameter) == lastModified) { //If so, roll back the transaction and just close the file. t1.RollBack(); famDoc.Close(false); } else { //Otherwise set the formula of the version parameter to the quote encapsulated date, just like it would appear in Revit, commit it, then save. famMan.SetFormula(famParameter, "\"" + lastModified + "\""); t1.Commit(); RVTOperations.SaveRevitFile(uiApp, famDoc, true); } } else { //If the family could not be opened for any reason, let the user know MessageBox.Show(String.Format("{0} could not be opened.", familyFile)); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }
public QaqcRFSPRequest(UIApplication uiApp, String text) { MainUI uiForm = BARevitTools.Application.thisApp.newMainUi; List <string> familyFiles = uiForm.qaqcRFSPFamilyFiles; foreach (string familyFile in familyFiles) { //Open the family file and get its Family Manager. Then, get the list of family parameters RVTDocument familyDoc = uiApp.Application.OpenDocumentFile(familyFile); FamilyManager famMan = familyDoc.FamilyManager; IList <FamilyParameter> famParams = famMan.GetParameters(); string famName = familyDoc.Title.Replace(".rfa", ""); //Start a transaction using (Transaction t1 = new Transaction(familyDoc, "ChangeSharedParameters")) { t1.Start(); try { //Cycle through the family parameters foreach (FamilyParameter param in famParams) { string paramName = param.Definition.Name; try { //If the parameter is shared, and the name does not contain BA or BAS, continue if (param.IsShared && !paramName.ToUpper().Contains("BA ") && !paramName.ToUpper().Contains("BAS ")) { //A temporary parameter needs to be made in the place of the one to be removed, so get the group of the parameter to be replaced BuiltInParameterGroup paramGroup = param.Definition.ParameterGroup; string paramTempName = "tempName"; //Determine if the parameter to replaced is an instance parameter bool paramInstance = param.IsInstance; //Make replace the parameter with the temporary one, giving it the same group and instance settings FamilyParameter newParam = famMan.ReplaceParameter(param, paramTempName, paramGroup, paramInstance); //Then, rename the new parameter famMan.RenameParameter(newParam, paramName); //Add to the ListBox the outcome of the shared parameter replacement uiForm.qaqcRFSPParametersListBox.Items.Add(famName + " : " + paramName + ": SUCCESS"); } } catch { //If the replacement fails, report that too uiForm.qaqcRFSPParametersListBox.Items.Add(famName + " : " + paramName + ": FAILED"); } } t1.Commit(); //Update the MainUI uiForm.qaqcRFSPParametersListBox.Update(); uiForm.qaqcRFSPParametersListBox.Refresh(); } catch { t1.RollBack(); } } //Save the family and remove the backups RVTOperations.SaveRevitFile(uiApp, familyDoc, true); GeneralOperations.CleanRfaBackups(Path.GetDirectoryName(familyFile)); } }
public MaterialsCMSRequest(UIApplication uiApp, String text) { MainUI uiForm = BARevitTools.Application.thisApp.newMainUi; ProgressBar progressBar = uiForm.materialsCMSExcelCreateSymbolsProgressBar; DataGridView dgv = uiForm.materialsCMSExcelDataGridView; int rowsCount = dgv.Rows.Count; int columnsCount = dgv.Columns.Count; List <string> familyTypesMade = new List <string>(); //Prepare the progress bar. The column count is one less because the first column is the column for family type names progressBar.Minimum = 0; progressBar.Maximum = (rowsCount) * (columnsCount - 1); progressBar.Step = 1; progressBar.Visible = true; RVTDocument doc = uiApp.ActiveUIDocument.Document; //Reset the progress bar uiForm.materialsCMSExcelCreateSymbolsProgressBar.Value = 0; //First, try to use the family from the project. If that fails, use the family file Family familyToUse = RVTGetElementsByCollection.FamilyByFamilyName(uiApp, Path.GetFileNameWithoutExtension(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule)); //Assuming nothing went to hell in the process of loading one famiy... if (familyToUse != null) { //Save out the family to use RVTDocument tempFamDoc = doc.EditFamily(familyToUse); RVTOperations.SaveRevitFile(uiApp, tempFamDoc, @"C:\Temp\" + tempFamDoc.Title, true); //Open the family to use and get its FamilyManager RVTDocument famDoc = RVTOperations.OpenRevitFile(uiApp, @"C:\Temp\" + Path.GetFileName(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule)); FamilyManager famMan = famDoc.FamilyManager; //Get the parameters from the Family Manager and add them to a dictionary FamilyParameterSet parameters = famMan.Parameters; Dictionary <string, FamilyParameter> famParamDict = new Dictionary <string, FamilyParameter>(); foreach (FamilyParameter param in parameters) { famParamDict.Add(param.Definition.Name, param); } //Start a new transaction to make the new family types in the family to use using (Transaction t1 = new Transaction(famDoc, "MakeNewTypes")) { t1.Start(); //Cycle through the rows in the dgv for (int i = 0; i < rowsCount; i++) { //The first column cell will be the type name string newTypeName = dgv.Rows[i].Cells[0].Value.ToString(); //Get the family type names in the family Dictionary <string, FamilyType> existingTypeNames = RVTOperations.GetFamilyTypeNames(famMan); //If the family to make from the DGV does not exist in the dictionary keys... if (!existingTypeNames.Keys.Contains(newTypeName)) { //Make the family type and add it to the list of types made FamilyType newType = famMan.NewType(newTypeName); famMan.CurrentType = newType; familyTypesMade.Add(newType.Name); } else { //If the type exists, set the current type it from the dictionary and add it to the list of types made famMan.CurrentType = existingTypeNames[newTypeName]; familyTypesMade.Add(famMan.CurrentType.Name); } //Next, evaluate the columns that contain parameters for (int j = 1; j < columnsCount; j++) { //The parameter names will be retrieved from the column HeaderText property string paramName = dgv.Columns[j].HeaderText; //Meanwhile the parameter value will come from the DGV cells var paramValue = dgv.Rows[i].Cells[j].Value; try { //If the parameter dictionary contains the parameter and the value to assign it is not empty, continue. if (paramValue.ToString() != "" && famParamDict.Keys.Contains(paramName)) { //Get the family parameter and check if it is locked by a formula FamilyParameter param = famParamDict[paramName]; if (!param.IsDeterminedByFormula) { //If it is not locked by a formula, set the parameter ParameterType paramType = param.Definition.ParameterType; RVTOperations.SetFamilyParameterValue(famMan, param, paramValue); } } } catch {; } finally { //Always perform the step to indicate the progress. progressBar.PerformStep(); } } } t1.Commit(); } //Use another transaction to delete the types that were not needed using (Transaction t2 = new Transaction(famDoc, "DeleteOldTypes")) { t2.Start(); //Cycle through the family types and determine if it is in the list of types made foreach (FamilyType type in famMan.Types) { if (!familyTypesMade.Contains(type.Name)) { //If the type is not in the list of types made, delete it from the family file famMan.CurrentType = type; famMan.DeleteCurrentType(); } } t2.Commit(); } //Save the family document at this point as all of the types and their parameters have been set famDoc.Close(true); using (Transaction t3 = new Transaction(doc, "LoadFamily")) { t3.Start(); doc.LoadFamily(@"C:\Temp\" + Path.GetFileName(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule), new RVTFamilyLoadOptions(), out Family loadedFamily); t3.Commit(); } //Get the drafting view type in the project for BA Drafting View, else just get the first drafting view type ViewDrafting placementView = null; var draftingViews = new FilteredElementCollector(doc).OfClass(typeof(ViewDrafting)).WhereElementIsNotElementType().ToElements(); ViewFamilyType draftingViewType = null; try { //From the view family types collection, get the first one where its name is BA Drafting View draftingViewType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).WhereElementIsElementType().ToElements().Where(elem => elem.Name == "BA Drafting View").First() as ViewFamilyType; } catch { //Well, crap. It doesn't exist. Just get the first type then and call it good. draftingViewType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).WhereElementIsElementType().ToElements().First() as ViewFamilyType; } //Start a transaction for making the ID Material View and placing the family symbol types using (Transaction t4 = new Transaction(doc, "MakeIDMaterialView")) { t4.Start(); foreach (ViewDrafting view in draftingViews) { //Find the view named ID Material View, or whatever jibberish someone typed in the CMS text box for the name to use. in the drafting views if (view.Name == "ID Material View" || view.Name == uiForm.materialsCMSSetViewNameTextBox.Text) { //Delete the drafting view doc.Delete(view.Id); doc.Regenerate(); break; } else { continue; } } //Make a new view placementView = ViewDrafting.Create(doc, draftingViewType.Id); placementView.Scale = 1; if (uiForm.materialsCMSSetViewNameTextBox.Text != "") { //If someone defined a custom view name, use that and strip out brackets if they exist placementView.Name = uiForm.materialsCMSSetViewNameTextBox.Text.Replace("{", "").Replace("}", ""); } else { //Otherwise, this will be the new ID Material View placementView.Name = "ID Material View"; } try { //Set the view sort parameters if they exist placementView.GetParameters(Properties.Settings.Default.BAViewSort1).First().Set("2 Plans"); placementView.GetParameters(Properties.Settings.Default.BAViewSort2).First().Set("230 Finish Plans"); } catch (Exception e) { MessageBox.Show(e.ToString()); } doc.Regenerate(); t4.Commit(); } //Do magic to place each symbol in its view by calling the method below in this class PlaceSymbolsInView(uiApp, "ID Use", "Mark", placementView); //Clean up the files from the operations GeneralOperations.CleanRfaBackups(GeneralOperations.GetAllRvtBackupFamilies(@"C:\Temp\", false)); File.Delete(@"C:\Temp\" + Path.GetFileName(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule)); Application.thisApp.newMainUi.materialsCMSFamilyToUse = RVTGetElementsByCollection.FamilyByFamilyName(uiApp, Path.GetFileNameWithoutExtension(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule)); } else { //If the family could not be found, well, let the user know of this. MessageBox.Show(String.Format("The {0} family could not be found in the project.", Path.GetFileNameWithoutExtension(Properties.Settings.Default.RevitFamilyMaterialsCMSSymbIdMaterialSchedule))); } }