using System; using System.Diagnostics; using System.Linq; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.UI; namespace SZBIM.StandardTools { /// /// Revit执行命令 /// [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class SZComponentIdentificationCmd : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { //程序UI界面 UIApplication uiapp = commandData.Application; //获取元素(选择) 显示元素 视图(活动视图)管理(对象) UIDocument uidoc = uiapp.ActiveUIDocument; //程序 Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; //获取位置和场地 视图(多个视图)管理 获取元素(Revit 项目里的全部元素) Document doc = uidoc.Document; //获取所有打开文档 DocumentSet docset = uiapp.Application.Documents; //当前视图 View view = doc.ActiveView; #region FilterExecute var instances = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).ToElements(); var hosts = new FilteredElementCollector(doc).OfClass(typeof(HostObject)).ToElements(); var railings = new FilteredElementCollector(doc).OfClass(typeof(Railing)).ToElements(); var stairs = new FilteredElementCollector(doc).OfClass(typeof(Stairs)).ToElements(); using (Transaction trans = new Transaction(doc, "赋予深圳构件标识")) { try { trans.Start(); foreach (var instance in instances) { var param = instance.GetParameters("深圳构件标识").FirstOrDefault(); if (instance.Category.Id == Category.GetCategory(doc, BuiltInCategory.OST_StructuralFraming).Id) { param?.Set("结构梁"); } else if (instance.Category.Id == Category.GetCategory(doc, BuiltInCategory.OST_StructuralColumns).Id) { param?.Set("结构柱"); } else { param?.Set(instance.Category.Name); } } foreach (var railing in railings) { var param = railing.GetParameters("深圳构件标识").FirstOrDefault(); param?.Set("栏杆"); } foreach (var stair in stairs) { var param = stair.GetParameters("深圳构件标识").FirstOrDefault(); param?.Set("楼梯"); } foreach (var host in hosts) { var param = host.GetParameters("深圳构件标识").FirstOrDefault(); if (param == null) { continue; } if (host is Wall wall) { switch (wall.WallType.Kind) { case WallKind.Basic: var structural = wall.get_Parameter(BuiltInParameter.WALL_STRUCTURAL_SIGNIFICANT).AsInteger() == 1; var usage = wall.get_Parameter(BuiltInParameter.WALL_STRUCTURAL_USAGE_PARAM).AsInteger(); if (structural) { if (usage == 2) { param.Set("剪力墙"); } } else { var function = wall.WallType.get_Parameter(BuiltInParameter.FUNCTION_PARAM).AsInteger(); switch (function) { case 0: param.Set("建筑内墙"); break; case 1: param.Set("建筑外墙"); break; } } break; case WallKind.Curtain: param.Set("幕墙系统"); break; case WallKind.Stacked: break; } } if (host is Floor floor) { var structural = floor.get_Parameter(BuiltInParameter.FLOOR_PARAM_IS_STRUCTURAL).AsInteger() == 1; param.Set(structural ? "楼面" : "混凝土结构楼板"); } //DebugInfo(host); } trans.Commit(); } catch (Exception ex) { if (trans.GetStatus() == TransactionStatus.Started) { trans.Commit(); } } } #endregion FilterExecute return Result.Succeeded; static void DebugInfo(Element element) { DateTime currentTime = DateTime.Now; string formattedTime = currentTime.ToString("yyyy-MM-dd HH:mm:ss"); Debug.WriteLine($"{formattedTime}{element.Name}-{element.Id}"); } } } }