using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Nice3point.Revit.Toolkit.External; using System; using System.Linq; using System.Windows; namespace Szmedi.RvKits.ModelManager; [Transaction(TransactionMode.Manual)] public class AddViewFiltersCmd : ExternalCommand { public override void Execute() { var result=MessageBox.Show("该操作会向当前视图添加多个土建的过滤器,是否继续?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result != MessageBoxResult.Yes) { return; } using (TransactionGroup tg = new(Document, "区分土建构件")) { tg.Start(); //建筑模型与结构模型相关构件颜色区分。例如剪力墙和内隔墙颜色区分 using (Transaction trans = new(Document, "建筑结构构件颜色区分")) { trans.Start(); //类别 ISet categoryIds = new HashSet { new(BuiltInCategory.OST_Walls), new(BuiltInCategory.OST_Floors), new(BuiltInCategory.OST_StructuralFraming), new(BuiltInCategory.OST_StructuralColumns), new(BuiltInCategory.OST_StructuralFoundation) }; //结构墙过滤器 ElementId wallParamId = new(BuiltInParameter.WALL_STRUCTURAL_USAGE_PARAM); FilterRule wallFilterRule = ParameterFilterRuleFactory.CreateEqualsRule(wallParamId, 1); //结构楼板过滤器 ElementId floorParamId = new(BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL); FilterRule floorFilterRule = ParameterFilterRuleFactory.CreateEqualsRule(floorParamId, 1); #if REVIT2018 try { var wallFilter = CreateViewFilter(Document, "结构墙", BuiltInCategory.OST_Walls, wallFilterRule); var floorFilter = CreateViewFilter(Document, "结构板", BuiltInCategory.OST_Floors, floorFilterRule); ActiveView.AddFilter(wallFilter.Id); ActiveView.AddFilter(floorFilter.Id); List ids = new() { new ElementId(BuiltInCategory.OST_StructuralFraming) }; if (FilterCategoryRule.AllCategoriesFilterable(ids)) { FilterCategoryRule framingCategoryRule = new(ids); var filter = CreateViewFilter(Document, "结构梁", BuiltInCategory.OST_StructuralFraming); ActiveView.AddFilter(filter.Id); } ids = new List { new ElementId(BuiltInCategory.OST_StructuralColumns) }; if (FilterCategoryRule.AllCategoriesFilterable(ids)) { FilterCategoryRule columnCategoryRule = new(ids); var filter = CreateViewFilter(Document, "结构柱", BuiltInCategory.OST_StructuralColumns); ActiveView.AddFilter(filter.Id); } ids = new List { new ElementId(BuiltInCategory.OST_StructuralFoundation) }; if (FilterCategoryRule.AllCategoriesFilterable(ids)) { FilterCategoryRule foundationCategoryRule = new(ids); var filter = CreateViewFilter(Document, "结构基础", BuiltInCategory.OST_StructuralFoundation); ActiveView.AddFilter(filter.Id); } } catch (Exception e) { LogAssists.WriteLog(e.Message); } #elif REVIT2019 || REVIT2020 try { //墙 ElementParameterFilter structuralWallFilter = CreateParameterFilter(BuiltInCategory.OST_Walls, wallFilterRule); //板 ElementParameterFilter structuralFloorFilter = CreateParameterFilter(BuiltInCategory.OST_Floors, floorFilterRule); //梁 ElementParameterFilter framingFilter = CreateCategoryFilter(BuiltInCategory.OST_StructuralFraming); //结构柱 ElementParameterFilter columnFilter = CreateCategoryFilter(BuiltInCategory.OST_StructuralColumns); //结构基础 ElementParameterFilter foundationFilter = CreateCategoryFilter(BuiltInCategory.OST_StructuralFoundation); List filters = new() { framingFilter, columnFilter, structuralWallFilter, structuralFloorFilter, foundationFilter }; LogicalOrFilter orFilter = new(filters); if (!ParameterFilterElement.ElementFilterIsAcceptableForParameterFilterElement(Document, categoryIds, orFilter)) { MessageBox.Show("结构元素过滤器不能用于视图过滤器", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } else { ParameterFilterElement structuralFilter = ParameterFilterElement.Create(Document, "结构构件", categoryIds); if (structuralFilter.AllRuleParametersApplicable(orFilter)) { structuralFilter.SetElementFilter(orFilter); ActiveView.AddFilter(structuralFilter.Id); } else { MessageBox.Show("结构过滤器错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } catch (Exception e) { LogAssists.WriteLog(e.Message); } #endif trans.Commit(); } //不同功能区域例如人防板与非人防区域板用颜色加以区分。 using (Transaction trans = new(Document, "是否人防板模型区分")) { trans.Start(); ISet categoryIds = new HashSet { new(BuiltInCategory.OST_Floors) }; //注释 ElementId floorParamId = new(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS); FilterRule floorFilterRule = ParameterFilterRuleFactory.CreateContainsRule(floorParamId, "人防", true); //类型名称 ElementId floorTypeParamId = new(BuiltInParameter.SYMBOL_NAME_PARAM); FilterRule floorTypeFilterRule = ParameterFilterRuleFactory.CreateContainsRule(floorTypeParamId, "人防", true); #if REVIT2018 try { var floorCommentFilter = CreateViewFilter(Document, "人防楼板-注释", BuiltInCategory.OST_Floors, floorFilterRule); ActiveView.AddFilter(floorCommentFilter.Id); OverrideGraphicSettings settings = ActiveView.GetFilterOverrides(floorCommentFilter.Id); //settings.SetCutLineColor(new Color(255, 0, 0)); settings.SetProjectionLineColor(new Color(255, 0, 0)); ActiveView.SetFilterOverrides(floorCommentFilter.Id, settings); var floorTypeFilter = CreateViewFilter(Document, "人防楼板-类型名称", BuiltInCategory.OST_Floors, floorTypeFilterRule); ActiveView.AddFilter(floorTypeFilter.Id); settings = ActiveView.GetFilterOverrides(floorTypeFilter.Id); //settings.SetCutLineColor(new Color(255, 0, 0)); settings.SetProjectionLineColor(new Color(255, 0, 0)); ActiveView.SetFilterOverrides(floorTypeFilter.Id, settings); } catch (Exception e) { LogAssists.WriteLog(e.Message); } #elif REVIT2020 || REVIT2019 ElementParameterFilter floorFilter = CreateParameterFilter(BuiltInCategory.OST_Floors, floorFilterRule); ElementParameterFilter floorTypeFilter = CreateParameterFilter(BuiltInCategory.OST_Floors, floorTypeFilterRule); List filters = new() { floorFilter, floorTypeFilter }; LogicalOrFilter orFilter = new(filters); try { if (ParameterFilterElement.ElementFilterIsAcceptableForParameterFilterElement(Document, categoryIds, orFilter)) { ParameterFilterElement filter = ParameterFilterElement.Create(Document, "人防板", categoryIds); if (filter.AllRuleParametersApplicable(orFilter)) { filter.SetElementFilter(orFilter); } ActiveView.AddFilter(filter.Id); OverrideGraphicSettings settings = ActiveView.GetFilterOverrides(filter.Id); //settings.SetCutLineColor(new Color(255, 0, 0)); settings.SetProjectionLineColor(new Color(255, 0, 0)); ActiveView.SetFilterOverrides(filter.Id, settings); } } catch (Exception e) { LogAssists.WriteLog(e.Message); } #endif trans.Commit(); } //为了后期分析管线等便捷直观,结构建模时,梁要根据高度进行颜色标注区分。不仅要标注梁构件外观颜色,内部材质颜色也最好同时标注,以利于切面观察颜色。 using (Transaction trans = new(Document, "梁高度颜色区分")) { trans.Start(); ISet categoryIds = new HashSet { new(BuiltInCategory.OST_StructuralFraming) }; //得到所有梁底标高 IList framings = new FilteredElementCollector(Document) .OfCategory(BuiltInCategory.OST_StructuralFraming) .OfClass(typeof(FamilyInstance)) .ToElements(); ISet heightSet = new HashSet(); foreach (Element framing in framings) { double bottomElev = framing.get_Parameter(BuiltInParameter.STRUCTURAL_ELEVATION_AT_BOTTOM).AsDouble(); heightSet.Add(bottomElev); } List heightsOrdered = heightSet.OrderBy(d => d).ToList(); int count = heightsOrdered.Count; if (count > 0) { int step = 255 / count; //创建梁过滤器 for (int i = 0; i < count; i++) { double height = heightsOrdered[i]; ElementId framingBottomParamId = new(BuiltInParameter.STRUCTURAL_ELEVATION_AT_BOTTOM); FilterRule framingFilterRule = ParameterFilterRuleFactory.CreateEqualsRule(framingBottomParamId, height, 0.001); #if REVIT2018 var filter = CreateViewFilter( Document, $"梁底高度-({Convert.ToInt16(height * 304.8)})", BuiltInCategory.OST_StructuralFraming, framingFilterRule ); ActiveView.AddFilter(filter.Id); OverrideGraphicSettings settings = ActiveView.GetFilterOverrides(filter.Id); //settings.SetCutLineColor(new Color(255, 0, 0)); settings.SetProjectionLineColor(new Color((byte)(step * (count - i)), (byte)(step * i), (byte)(step * i))); ActiveView.SetFilterOverrides(filter.Id, settings); #elif REVIT2020 || REVIT2019 ElementParameterFilter floorFilter = CreateParameterFilter( BuiltInCategory.OST_StructuralFraming, framingFilterRule ); List filters = new() { floorFilter }; LogicalOrFilter orFilter = new(filters); try { if (ParameterFilterElement.ElementFilterIsAcceptableForParameterFilterElement(Document, categoryIds, orFilter)) { ParameterFilterElement filter = ParameterFilterElement.Create( Document, $"梁底高度-({Convert.ToInt16(height * 304.8)})", categoryIds ); if (filter.AllRuleParametersApplicable(orFilter)) { filter.SetElementFilter(orFilter); } else { MessageBox.Show("梁底高过滤器错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } ActiveView.AddFilter(filter.Id); OverrideGraphicSettings settings = ActiveView.GetFilterOverrides(filter.Id); //settings.SetCutLineColor(new Color(255, 0, 0)); settings.SetProjectionLineColor(new Color((byte)(step * (count - i)), (byte)(step * i), (byte)(step * i))); ActiveView.SetFilterOverrides(filter.Id, settings); } } catch (Exception e) { LogAssists.WriteLog(e.Message); } #endif } } trans.Commit(); } tg.Commit(); } MessageBox.Show("过滤器已添加", "提示"); } /// /// 创建类别的参数过滤器 /// /// /// private static ElementParameterFilter CreateCategoryFilter(BuiltInCategory builtInCategory) { ElementParameterFilter filter = null; List commonFilterRules = new(); List categoryIds = new() { new(builtInCategory) }; //创建参数过滤器 if (FilterCategoryRule.AllCategoriesFilterable(categoryIds)) { FilterCategoryRule categoryRule = new(categoryIds); commonFilterRules.Add(categoryRule); filter = new ElementParameterFilter(commonFilterRules); } return filter; } /// /// 创建参数的参数过滤器 /// /// /// /// private static ElementParameterFilter CreateParameterFilter(BuiltInCategory builtInCategory, params FilterRule[] rules) { ElementParameterFilter commonFilter = null; List commonFilterRules = new(); List commonCategoryForRules = new() { new(builtInCategory) }; //判断类别是否可用于过滤器 if (FilterCategoryRule.AllCategoriesFilterable(commonCategoryForRules)) { FilterCategoryRule commonCategoryRule = new(commonCategoryForRules); commonFilterRules.Add(commonCategoryRule); commonFilterRules.AddRange(rules); commonFilter = new ElementParameterFilter(commonFilterRules); } return commonFilter; } #if REVIT2018 /// /// 创建视图过滤器 /// /// /// /// /// /// private static ParameterFilterElement CreateViewFilter( Document Document, string filterName, BuiltInCategory category, params FilterRule[] filterRules ) { ParameterFilterElement viewFilter = null; ISet categoryIds = new HashSet { new ElementId(category) }; List rules = new(); foreach (var filterRule in filterRules) { rules.Add(filterRule); } if (ParameterFilterElement.AllRuleParametersApplicable(Document, categoryIds, rules)) { viewFilter = ParameterFilterElement.Create(Document, filterName, categoryIds, rules); } return viewFilter; } #endif }