public static void AddFilterToView( Document doc, Autodesk.Revit.DB.View view, SelectionFilterElement filterElement, Color cutFillColor, string[] cutFillPatterns, Color projFillColor, string[] projFillPatterns) { using (Transaction t = new Transaction(doc, "Add filter to view and set overrides")) { t.Start(); ElementId filterId = filterElement.Id; view.AddFilter(filterId); doc.Regenerate(); OverrideGraphicSettings overrideSettings = view.GetFilterOverrides(filterId); Element cutFill = (from element in new FilteredElementCollector(doc) .OfClass(typeof(FillPatternElement)).Cast <Element>() where cutFillPatterns.Any(pattern => pattern.Equals(element.Name)) select element) .FirstOrDefault(); Element projectFill = (from element in new FilteredElementCollector(doc) .OfClass(typeof(FillPatternElement)).Cast <Element>() where projFillPatterns.Any(pattern => pattern.Equals(element.Name)) select element) .FirstOrDefault(); overrideSettings.SetCutFillColor(cutFillColor); overrideSettings.SetCutFillPatternId(cutFill.Id); overrideSettings.SetProjectionFillColor(projFillColor); overrideSettings.SetProjectionFillPatternId(projectFill.Id); view.SetFilterOverrides(filterId, overrideSettings); t.Commit(); } }
private void BtnTransfer_Click(object sender, EventArgs e) { List <string> infoFiltersTransferred = new List <string>(); List <string> infoFiltersGraphicOverriden = new List <string>(); #region items selected? if (LsvFilters.CheckedItems.Count == 0) { UI.Info.Form_Info1.infoMsgMain = "Select"; UI.Info.Form_Info1.infoMsgBody = "Select one or more View Filters from the list to continue."; using (UI.Info.Form_Info1 thisForm = new Info.Form_Info1()) { thisForm.ShowDialog(); } return; } if (LsvTemplates.CheckedItems.Count == 0) { UI.Info.Form_Info1.infoMsgMain = "Select"; UI.Info.Form_Info1.infoMsgBody = "Select one or more View Templates from the list to continue."; using (UI.Info.Form_Info1 thisForm = new Info.Form_Info1()) { thisForm.ShowDialog(); } return; } #endregion #region CHECK IF FILTER IS ALREADY APPLIED List <string> infoIsFilterApplied = new List <string>(); bool triggerIsFilterApplied = false; foreach (ListViewItem tItem in LsvTemplates.CheckedItems) // LOOP THROUGH TEMPLATES { ElementId templateId = new ElementId(tItem.ImageIndex); Autodesk.Revit.DB.View template = m_doc.GetElement(templateId) as Autodesk.Revit.DB.View; foreach (ListViewItem vItem in LsvFilters.CheckedItems) // LOOP THROUGH FILTERS { ElementId filterId = new ElementId(vItem.ImageIndex); if (template.IsFilterApplied(filterId)) { infoIsFilterApplied.Add(string.Format("{0} -> {1}", vItem.Text, template.Name)); triggerIsFilterApplied = true; } } } if (triggerIsFilterApplied) { // show warning form UI.Info.Form_Warning.warningHead = "You should know..."; UI.Info.Form_Warning.warningMain = "The following Filters are already applied to one or more selected View Templates:"; UI.Info.Form_Warning.warningFoot = "Graphic settings will be overriden."; UI.Info.Form_Warning.warningList = infoIsFilterApplied; using (UI.Info.Form_Warning thisForm = new Info.Form_Warning()) { thisForm.ShowDialog(); } } #endregion #region PROCEED WITH TRANSACTION executionTime.Start(); using (Transaction t = new Transaction(m_doc, "Transfer View Filters")) { Ana_NoOfTemplates = LsvTemplates.CheckedItems.Count; // collect data for analytics t.Start(); // FOR EACH FILTER SELECTED FROM THE LIST, GET HOST VIEW TEMPLATE AND GRAPHIC OVERRIDES foreach (ListViewItem filterItem in LsvFilters.CheckedItems) { ElementId filterId = new ElementId(filterItem.ImageIndex); // GET SOURCE VIEW TEMPLATE TO GET GRAPHIC OVERRIDES Autodesk.Revit.DB.View sourceTemplate = Data.Helpers.GetTemplateByName(m_doc, filterItem.Group.ToString()); OverrideGraphicSettings graphSetting = sourceTemplate.GetFilterOverrides(filterId); //FOR EACH TARGET TEMPLATE SELECTED, APPLY FILTERS foreach (ListViewItem templateItem in LsvTemplates.CheckedItems) { ElementId templateId = new ElementId(templateItem.ImageIndex); Autodesk.Revit.DB.View targetTemplate = m_doc.GetElement(templateId) as Autodesk.Revit.DB.View; if (!targetTemplate.IsFilterApplied(filterId)) // IF FILTER IS NOT APPLIED, ADD FILTER AND OVERRIDE GRAPHICS { targetTemplate.AddFilter(filterId); targetTemplate.SetFilterOverrides(filterId, graphSetting); infoFiltersTransferred.Add(m_doc.GetElement(filterId).Name + " -> " + targetTemplate.Name); Ana_NoOfFilters_Applied += 1; // collect data for analytics } else // IF FILTER ALREADY APPLIED, JUST OVERRIDE GRAPHICS { targetTemplate.SetFilterOverrides(filterId, graphSetting); infoFiltersGraphicOverriden.Add(m_doc.GetElement(filterId).Name + " -> " + targetTemplate.Name); Ana_NoOfFilters_Overriden += 1; // collect data for analytics } Ana_NoOfFilters_Total += 1; // collect data for analytics } } t.Commit(); } executionTime.Stop(); ExecTimeElapseS = executionTime.Elapsed.Seconds.ToString(); // collect data for analytics #endregion #region show Results Form UI.Info.Form_Results.resultHead = "Results"; UI.Info.Form_Results.resultMain1 = "Filters Transferred:"; UI.Info.Form_Results.resultMain2 = "Filters with Graphic Settings Overriden"; UI.Info.Form_Results.resultList1 = infoFiltersTransferred; UI.Info.Form_Results.resultList2 = infoFiltersGraphicOverriden; using (UI.Info.Form_Results thisForm = new Info.Form_Results()) { thisForm.ShowDialog(); } #endregion// show Results Form DialogResult = DialogResult.OK; useTime.Stop(); UseTimeElapseS = useTime.Elapsed.Seconds.ToString(); // collect data for analytics Utilities.GetAnalyticsCSV(m_doc, m_app); Helpers.TransferViewFiltersAnalytics(m_doc, m_app, UseTimeElapseS, ExecTimeElapseS, Ana_NoOfFilters_Total, Ana_NoOfFilters_Applied, Ana_NoOfFilters_Overriden, Ana_NoOfTemplates); }