using Autodesk.Revit.DB; using Autodesk.Revit.DB.Electrical; using Autodesk.Revit.DB.Mechanical; using Autodesk.Revit.DB.Plumbing; using Glodon.Revit.Utility; using HStation.RevitDev.RevitDataExport.Utils; using System; using System.Collections.Generic; using System.Linq; namespace HStation.RevitDev.RevitDataExport.Service { public class RevitMepElementService { public static IEnumerable GetSubElements(MEPSystem system) { var elements = new List(); if (system is ElectricalSystem elecSystem) { } else if (system is MechanicalSystem mechSystem) { } else if (system is PipingSystem pipeSystem) { var network = pipeSystem.PipingNetwork; foreach (Element subElem in network) { elements.Add(subElem); } } else { } return elements; } public static List GetSubElementIds(MEPSystem system) { var elements = GetSubElements(system); var elemIds = elements.Select(x => x.Id).ToList(); return elemIds; } public static List GetSubElements(MEPSystemType systemType) { var systemIds = systemType.GetDependentElements(new ElementClassFilter(typeof(PipingSystem),false)); var elements = new List(); foreach (var systemId in systemIds) { var system = systemType.Document.GetElement(systemId) as PipingSystem; if (system == null) { continue; } elements.AddRange(GetSubElements(system)); } return elements; } public static List GetSubElements(Document doc, string sysTypeName) { var pipingSystems = new FilteredElementCollector(doc) .OfClass(typeof(MEPSystem)) .Where(x => x != null && x is PipingSystem && x.Id != ElementId.InvalidElementId && sysTypeName.Any(y => x.Name.Contains(y + " "))) .OfType() .ToList(); var elems = new List(); foreach (var system in pipingSystems) { if (system.PipingNetwork.Size > 0) { foreach (Element elem in system.PipingNetwork) { elems.Add(elem); } } try { if (system.Elements.Size > 0) { foreach (Element elem in system.Elements) { if (elems.Any(x => x.Id.IntegerValue == elem.Id.IntegerValue)) { continue; } elems.Add(elem); } } } catch (Exception) { continue; } } return elems; } public static List GetElements(Document doc, string sysTypeName) { var elems = new List(); if (doc == null || string.IsNullOrEmpty(sysTypeName)) { return elems; } elems = GetElementsByPipeSystemTypeName(doc, sysTypeName); return elems; } private static List GetElementsByPipeSystemTypeName(Document doc, string sysTypeName) { var result = new List(); List instances = GetInstanceAndPipes(doc); foreach (var instance in instances) { var id = instance.Id.IntegerValue; if (id == 3396216) { } var systemType = ParameterOperator.GetElementPipingSystemType(instance); if (systemType != null && systemType.Name == sysTypeName) { result.Add(instance); } } return result; } private static List FamilyInstanceAndPipesCache = null; private static List GetInstanceAndPipes(Document doc) { if (FamilyInstanceAndPipesCache != null) { return FamilyInstanceAndPipesCache; } var cates = doc.Settings.Categories; var itor = cates.GetEnumerator(); var categories = new List(); while (itor.MoveNext()) { var cate = itor.Current as Category; categories.Add(cate); } var types = new List { typeof(FamilyInstance), typeof(Pipe) }; var instances = ElementUtil.GetElements(types, isModel: true, categories: categories); FamilyInstanceAndPipesCache = instances; return instances; } } }