Files
SzmediTools/Szmedi.RvKits/ModelManager/AddViewFiltersCmd.cs

367 lines
17 KiB
C#
Raw Normal View History

2025-09-16 16:06:41 +08:00
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<ElementId> categoryIds = new HashSet<ElementId>
{
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<ElementId> 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<ElementId> { 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<ElementId> { 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<ElementFilter> 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<ElementId> categoryIds = new HashSet<ElementId> { 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<ElementFilter> 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<ElementId> categoryIds = new HashSet<ElementId> { new(BuiltInCategory.OST_StructuralFraming) };
//得到所有梁底标高
IList<Element> framings = new FilteredElementCollector(Document)
.OfCategory(BuiltInCategory.OST_StructuralFraming)
.OfClass(typeof(FamilyInstance))
.ToElements();
ISet<double> heightSet = new HashSet<double>();
foreach (Element framing in framings)
{
double bottomElev = framing.get_Parameter(BuiltInParameter.STRUCTURAL_ELEVATION_AT_BOTTOM).AsDouble();
heightSet.Add(bottomElev);
}
List<double> 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<ElementFilter> 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("过滤器已添加", "提示");
}
/// <summary>
/// 创建类别的参数过滤器
/// </summary>
/// <param name="builtInCategory"></param>
/// <returns></returns>
private static ElementParameterFilter CreateCategoryFilter(BuiltInCategory builtInCategory)
{
ElementParameterFilter filter = null;
List<FilterRule> commonFilterRules = new();
List<ElementId> categoryIds = new() { new(builtInCategory) };
//创建参数过滤器
if (FilterCategoryRule.AllCategoriesFilterable(categoryIds))
{
FilterCategoryRule categoryRule = new(categoryIds);
commonFilterRules.Add(categoryRule);
filter = new ElementParameterFilter(commonFilterRules);
}
return filter;
}
/// <summary>
/// 创建参数的参数过滤器
/// </summary>
/// <param name="builtInCategory"></param>
/// <param name="rules"></param>
/// <returns></returns>
private static ElementParameterFilter CreateParameterFilter(BuiltInCategory builtInCategory, params FilterRule[] rules)
{
ElementParameterFilter commonFilter = null;
List<FilterRule> commonFilterRules = new();
List<ElementId> 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
/// <summary>
/// 创建视图过滤器
/// </summary>
/// <param name="Document"></param>
/// <param name="filterName"></param>
/// <param name="category"></param>
/// <param name="filterRules"></param>
/// <returns></returns>
private static ParameterFilterElement CreateViewFilter(
Document Document,
string filterName,
BuiltInCategory category,
params FilterRule[] filterRules
)
{
ParameterFilterElement viewFilter = null;
ISet<ElementId> categoryIds = new HashSet<ElementId> { new ElementId(category) };
List<FilterRule> 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
}