using Autodesk.Revit.DB; using Autodesk.Revit.DB.Mechanical; using Autodesk.Revit.DB.Plumbing; using FWindSoft.SystemExtensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FWindSoft.Revit { public static class PipeExtension { /// /// 获取较较大直径的管道 /// /// /// /// public static Pipe GetLargerDiameter(this Pipe first, Pipe second) { return first.Diameter.MoreEqual(second.Diameter) ? first : second; } /// /// 获取较较小直径的管道 /// /// /// /// public static Pipe GetSmallerDiameter(this Pipe first, Pipe second) { return first.Diameter.LessEqual(second.Diameter) ? first : second; } /// /// 获取管道系统分类 /// /// /// public static PipeSystemType GetPipeSystemType(this Pipe pipe) { var pipingSystem = pipe.MEPSystem as PipingSystem; if (pipingSystem != null) return pipingSystem.SystemType; return PipeSystemType.UndefinedSystemType; } /// /// /// /// /// Horizontal public static PipingSystemType GetPipingSystemType(this Pipe pipe) { var elementId = pipe.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM); return pipe.Document.GetElement(elementId) as PipingSystemType; } public static List GetPoints(this Pipe pipe) { return new List { pipe.GetLocationLine().StartPoint(), pipe.GetLocationLine().EndPoint() }; } /// /// 获取指定管道的管道系统 /// /// /// public static PipingSystemType GePipingSystemType(this Pipe pipe) { return pipe.Document.GetElement(pipe.MEPSystem.GetTypeId()) as PipingSystemType; } /// /// 获取横向管道系统,处于同一直线的一系列管道 /// /// 指定管道 /// public static List GetSystemPipe(this Pipe pipe) { List pipes = new List(); do { //if (pipe == null) //{ // break; //} //Line baseLine = pipe.GetLocationLine(); //pipes.Add(pipe); //for (int i = 0; i < pipes.Count; i++) //{ // Pipe tempPipe = pipes[i]; // List verPipes = tempPipe.GetFirstSameTypeElements(); // foreach (var element in verPipes) // { // Pipe tempVerPipe = element as Pipe; // if (tempVerPipe != null && tempVerPipe.GetCurveExt().IsParallel(baseLine, 0.001) && // pipes.All(e => e.Id.IntegerValue != tempVerPipe.Id.IntegerValue)) // { // pipes.Add(tempVerPipe); // } // } //} } while (false); return pipes; } /// /// 判断管道系统是否为坡度管 /// /// /// public static bool IsSlopPipe(this List pipes) { if (pipes == null || pipes.Count == 0) return false; double baseZ = pipes[0].GetPoints()[0].Z; foreach (var pipe in pipes) { List ends = pipe.GetPoints(); if (ends.Any(xyz => !xyz.Z.IsEqual(baseZ))) { return false; } } return true; } /// /// 设置管道直径 /// /// /// 直径 public static void SetDiameter(this Pipe pipe, double dia) { pipe.SetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, dia); //直径 } /// /// 获取管道的管材 /// /// public static string GetMaterial(this Pipe pipe) { string material = pipe.GetParameterString(BuiltInParameter.RBS_PIPE_MATERIAL_PARAM); return material; } /// /// 是否是相同的系统 /// /// /// /// public static bool IsSameSystem(this Pipe pipe, Element element) { var elementId = pipe.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM); if (elementId == null) return false; var refElementId = element.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM); if (refElementId == null) return false; if (elementId.IntegerValue == -1 || refElementId.IntegerValue == -1) return false; return elementId == refElementId; } /// /// 获取坡度设置 /// /// /// public static List GetSlopes(this Document doc) { List slopes = new List(); PipeSettings setting = PipeSettings.GetPipeSettings(doc); if (setting != null) { slopes = setting.GetPipeSlopes().ToList(); } return slopes; } /// /// 获取项目中所有管道类型 /// /// /// public static List GetPipeTypes(this Document doc) { List list = doc.GetElements(); return list; } /// /// 获取管道类型的 管段 信息 /// /// 管道类型 /// 所在项目 /// public static PipeSegment GetPipeSegment(this PipeType pipeType, Document doc) { PipeSegment pipeSegment = null; RoutingPreferenceRule rule = pipeType.RoutingPreferenceManager.GetRule(RoutingPreferenceRuleGroupType.Segments, 0); if (rule == null) return pipeSegment; pipeSegment = doc.GetElement(rule.MEPPartId) as PipeSegment; return pipeSegment; } /// /// 增加管道的管段信息 /// /// 管道类型 /// 管段Id /// public static int AddPipeSegment(this PipeType pipeType, ElementId segmentId) { return pipeType.AddRoutingRule(RoutingPreferenceRuleGroupType.Segments, segmentId, 0); } /// /// 移除管段类型 /// /// /// /// public static int RemovePipeSegment(this PipeType pipeType, int index) { return pipeType.RemoveRoutingRule(RoutingPreferenceRuleGroupType.Segments, index); } /// /// 获取管道类型的可用直径信息 /// /// 管道类型 /// 所在项目 /// public static List GetAvailableDias(this PipeType pipeType, Document doc) { PipeSegment pipeSegment = pipeType.GetPipeSegment(doc); List dias = new List(); if (pipeSegment != null) { dias = pipeSegment.GetSizes().Select(mepSize => mepSize.NominalDiameter.MmFromFt()).ToList(); } return dias; } #region 管道类型规则操作基础 /// /// 增加管道优先规则 /// /// 管道类型 /// 规则分组 /// 规则使用的element /// 规则在分组中的位置 /// public static int AddRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType, ElementId elementId, int index = 0) { RoutingPreferenceManager manager = pipeType.RoutingPreferenceManager; RoutingPreferenceRule rule = new RoutingPreferenceRule(elementId, ""); rule.AddCriterion(PrimarySizeCriterion.All()); manager.AddRule(groupType, rule, 0); return index; } /// /// 移除管道规则分组中制定位置的规则 /// /// 管道类型 /// 规则分组 /// 规则在该分组中的位置 /// public static int RemoveRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType, int index = 0) { RoutingPreferenceManager manager = pipeType.RoutingPreferenceManager; manager.RemoveRule(groupType, index); return index; } /// /// 获取管道优先规则分组中指定位置的规则 /// /// /// /// /// public static RoutingPreferenceRule GetRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType, int index = 0) { RoutingPreferenceRule rule = pipeType.RoutingPreferenceManager.GetRule(groupType, index); return rule; } public static ElementId GetRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType, double nd) { RoutingConditions conditions = new RoutingConditions(RoutingPreferenceErrorLevel.None); conditions.AppendCondition(new RoutingCondition(nd)); ElementId id = pipeType.RoutingPreferenceManager.GetMEPPartId(groupType, conditions); return id; } #endregion #region 动态修改连接件辅助使用方法 /// /// 在三通规则分组第一个位置添加制定类型三通 /// /// /// /// public static int AddFirstTee(this PipeType pipeType, ElementId id) { return pipeType.AddRoutingRule(RoutingPreferenceRuleGroupType.Junctions, id, 0); } /// /// 移除三通规则分组第一个位置的三通 /// /// public static void RemoveFirstTee(this PipeType pipeType) { pipeType.RemoveRoutingRule(RoutingPreferenceRuleGroupType.Junctions); } /// /// 获取第一个三通 /// /// /// public static FamilySymbol GetFirstTee(this PipeType pipeType) { Document doc = pipeType.Document; FamilySymbol fitting = null; RoutingPreferenceRule rule = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions); if (rule == null) return fitting; fitting = doc.GetElement(rule.MEPPartId) as FamilySymbol; return fitting; } /// /// 通过直径获取指定规则的三通,如果指定直径不存在对应三通,则默认去第一个设定的三通 /// /// 管道类型 /// 直径 /// public static FamilySymbol GetFirstTeeByNd(this PipeType pipeType, double nd) { Document doc = pipeType.Document; FamilySymbol fitting = null; ElementId elementId = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions, nd); if (elementId == null) { RoutingPreferenceRule rule = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions); if (rule == null) return fitting; elementId = rule.MEPPartId; } fitting = doc.GetElement(elementId) as FamilySymbol; return fitting; } #endregion } }