/// <summary> /// Filter Element By Categories /// </summary> /// <param name="doc"></param> /// <param name="categoryNames"></param> /// <returns></returns> public static ElementFilter FiltersElementByCategory(this Document doc, IEnumerable <string> categoryNames) { ElementFilter categoriesFilter = null; if (categoryNames != null && categoryNames.Any()) { var categoryIds = new List <ElementId>(); foreach (var categoryName in categoryNames) { var category = doc.Settings.Categories.get_Item(categoryName); if (category != null) { categoryIds.Add(category.Id); } } var categoryFilters = new List <ElementFilter>(); if (categoryIds.Count > 0) { var categoryRule = new FilterCategoryRule(categoryIds); var categoryFilter = new ElementParameterFilter(categoryRule); categoryFilters.Add(categoryFilter); } if (categoryFilters.Count > 0) { categoriesFilter = new LogicalOrFilter(categoryFilters); } } return(categoriesFilter); }
public static FilteredElementCollector ExportableElements(this Document doc) { var catsToExport = ConfigurationManager.ActiveConfiguration.ExportedCategories; List <ElementId> ids = new List <BuiltInCategory>(catsToExport) .ConvertAll <ElementId>(c => new ElementId((int)c)); FilterCategoryRule r = new FilterCategoryRule(ids); ElementParameterFilter f = new ElementParameterFilter(r, true); // Use a logical OR of category filters IList <ElementFilter> a = new List <ElementFilter>(catsToExport.Count); foreach (BuiltInCategory bic in catsToExport) { a.Add(new ElementCategoryFilter(bic)); } LogicalOrFilter categoryFilter = new LogicalOrFilter(a); // Run the collector FilteredElementCollector els = new FilteredElementCollector(doc) .WhereElementIsNotElementType() .WhereElementIsViewIndependent() .WherePasses(categoryFilter); return(els); }
public static ElementFilter FiltersToElementFilter(Document document, IEnumerable <Filter> filters, IEnumerable <string> categoryNames) { var parameters = new List <Parameter>(); var orElementFilters = new List <ElementFilter>(); var andElementFilters = new List <ElementFilter>(); var filtersWithParameterNames = filters.Where(x => !string.IsNullOrEmpty(x.ParameterName)); ElementFilter combinedFilter = null; if (filtersWithParameterNames.Count() > 0) { foreach (var filter in filters) { if (string.IsNullOrEmpty(filter.Value)) { continue; } ElementId parameterId; if (GetParameterIdByName(document, filter.ParameterName) is ElementId id) { parameterId = id; } else { continue; } var pvp = new ParameterValueProvider(parameterId); var filterRule = FilterRuleProvider.GetFilterRule(filter.Value, pvp, (StorageType)Enum.Parse(typeof(StorageType), filter.StorageType.ToString()), filter.CompareMethod); var elementFilter = new ElementParameterFilter(filterRule, filter.Operator == OperatorType.Nand || filter.Operator == OperatorType.Nor); if (filter.Operator == OperatorType.And || filter.Operator == OperatorType.Nand) { andElementFilters.Add(elementFilter); } if (filter.Operator == OperatorType.Or || filter.Operator == OperatorType.Nor) { orElementFilters.Add(elementFilter); } } var filterList = new List <ElementFilter>(); ElementFilter orFilter = null; if (orElementFilters.Count > 1) { orFilter = new LogicalOrFilter(orElementFilters); } else if (orElementFilters.Count == 1) { orFilter = orElementFilters.First(); } if (andElementFilters.Count > 1) { var excludeFilter = new LogicalAndFilter(andElementFilters); filterList.Add(excludeFilter); } else if (andElementFilters.Count == 1) { filterList.Add(andElementFilters.First()); } if (filterList.Count > 1) { combinedFilter = new LogicalAndFilter(filterList); } if (filterList.Count == 1) { combinedFilter = filterList.First(); } if (combinedFilter != null && orFilter != null) { combinedFilter = new LogicalAndFilter(combinedFilter, orFilter); } if (filterList.Count == 0 && orFilter != null) { combinedFilter = orFilter; } } ElementFilter categoriesFilter = null; if (categoryNames != null && categoryNames.Count() > 0) { var categoryIds = new List <ElementId>(); foreach (var categoryName in categoryNames) { var category = document.Settings.Categories.get_Item(categoryName); if (category != null) { categoryIds.Add(category.Id); } } var categoryFilters = new List <ElementFilter>(); if (categoryIds.Count > 0) { var categoryRule = new FilterCategoryRule(categoryIds); var categoryFilter = new ElementParameterFilter(categoryRule); categoryFilters.Add(categoryFilter); } if (categoryFilters.Count > 0) { categoriesFilter = new LogicalOrFilter(categoryFilters); } } if (combinedFilter != null && categoriesFilter != null) { combinedFilter = new LogicalAndFilter(combinedFilter, categoriesFilter); } else if (combinedFilter == null && categoriesFilter != null) { combinedFilter = categoriesFilter; } return(combinedFilter); }
/// <summary> /// Return parameter data for all /// elements of all the given categories /// </summary> static void GetParamValuesForCats( Dictionary <string, Dictionary <string, List <string> > > map_cat_to_uid_to_param_values, Document doc, BuiltInCategory[] cats) { // One top level dictionary per category foreach (BuiltInCategory cat in cats) { map_cat_to_uid_to_param_values.Add( cat.Description(), new Dictionary <string, List <string> >()); } // Collect all required elements // The FilterCategoryRule as used here seems to // have no filtering effect at all! // It passes every single element, afaict. List <ElementId> ids = new List <BuiltInCategory>(cats) .ConvertAll <ElementId>(c => new ElementId((int)c)); FilterCategoryRule r = new FilterCategoryRule(ids); ElementParameterFilter f = new ElementParameterFilter(r, true); // Use a logical OR of category filters IList <ElementFilter> a = new List <ElementFilter>(cats.Length); foreach (BuiltInCategory bic in cats) { a.Add(new ElementCategoryFilter(bic)); } LogicalOrFilter categoryFilter = new LogicalOrFilter(a); // Run the collector FilteredElementCollector els = new FilteredElementCollector(doc) .WhereElementIsNotElementType() .WhereElementIsViewIndependent() .WherePasses(categoryFilter); // Retrieve parameter data for each element foreach (Element e in els) { Category cat = e.Category; if (null == cat) { Debug.Print( "element {0} {1} has null category", e.Id, e.Name); continue; } List <string> param_values = GetParamValues(e); BuiltInCategory bic = (BuiltInCategory) (e.Category.Id.IntegerValue); string catkey = bic.Description(); string uid = e.UniqueId; map_cat_to_uid_to_param_values[catkey].Add( uid, param_values); } }
public IEnumerable <CW_Category> Get(IEnumerable <string> documentTitles, string idOrName, bool mustContainElements, IEnumerable <CW_CategoryType> categoryTypes) { var result = new List <CW_Category>(); foreach (var documentTitle in documentTitles) { var document = GetDocument(documentTitle); if (int.TryParse(idOrName, out int elementIdValue)) { var iterator = document.Settings.Categories.ForwardIterator(); while (iterator.MoveNext()) { var category = iterator.Current as Category; if (category.Id.IntegerValue == elementIdValue) { var cwCategory = RvtToCwElementConverter.SetCategoryData(new CW_Category(), category); result.Add(cwCategory); break; } } break; } else if (idOrName != string.Empty) { var category = document.Settings.Categories.get_Item(idOrName); if (category != null) { var cwCategory = RvtToCwElementConverter.SetCategoryData(new CW_Category(), category); result.Add(cwCategory); break; } } else { foreach (Category category in document.Settings.Categories) { if (categoryTypes == null || categoryTypes.Any(x => (int)x == (int)category.CategoryType) || result.Any(x => x.Name == category.Name)) { if (mustContainElements) { var categoryEvaluator = new FilterCategoryRule(new List <ElementId> { category.Id }); var categoryFilter = new ElementParameterFilter(categoryEvaluator); var collector = new FilteredElementCollector(document).WherePasses(categoryFilter).WhereElementIsNotElementType(); if (collector.Any()) { var cwCategory = RvtToCwElementConverter.SetCategoryData(new CW_Category(), category); result.Add(cwCategory); } } else { var cwCategory = RvtToCwElementConverter.SetCategoryData(new CW_Category(), category); result.Add(cwCategory); } } } } } return(result); }