MEPCurveExtend.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Autodesk.Revit.DB;
  5. using Autodesk.Revit.DB.Electrical;
  6. using Autodesk.Revit.DB.Mechanical;
  7. using Autodesk.Revit.DB.Plumbing;
  8. using SAGA.RevitUtils.Extends;
  9. namespace SAGA.RevitUtils.MEP
  10. {
  11. public static class MEPCurveExtend
  12. {
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="mepCurve"></param>
  18. /// <param name="start"></param>
  19. /// <param name="end"></param>
  20. /// <returns></returns>
  21. public static T Copy<T>(this T mepCurve, XYZ start, XYZ end) where T: MEPCurve
  22. {
  23. T element = default(T);
  24. Document doc = mepCurve.Document;
  25. ICollection<ElementId> source = doc.CopyElement(mepCurve.Id, XYZ.Zero);
  26. doc.Regenerate();
  27. if (source.Count > 0)
  28. {
  29. element = doc.GetElement(source.FirstOrDefault<ElementId>()) as T;
  30. element.UpdateLocation<T>(start, end);
  31. }
  32. return element;
  33. }
  34. public static Connector GetConnector(this MEPCurve mepCurve, XYZ point)
  35. {
  36. return mepCurve.ConnectorManager.Connectors.GetList().FirstOrDefault<Connector>(t=>t.Origin.IsEqual(point));
  37. }
  38. /// <summary>
  39. /// 获取MEPCurve的所以点集合
  40. /// </summary>
  41. /// <param name="MEPCurve"></param>
  42. /// <returns></returns>
  43. public static List<XYZ> GetPoints(this MEPCurve MEPCurve)
  44. {
  45. return new List<XYZ> { MEPCurve.Location.GetLine().StartPoint(), MEPCurve.Location.GetLine().EndPoint() };
  46. }
  47. /// <summary>
  48. /// 获取与指定Connector相连接的Connector
  49. /// </summary>
  50. /// <param name="mepCurve"></param>
  51. /// <param name="point"></param>
  52. /// <returns></returns>
  53. public static Connector GetReferrenceConnector(this MEPCurve mepCurve, XYZ point)
  54. {
  55. Connector connector = mepCurve.GetConnector(point);
  56. if (connector.IsConnected)
  57. {
  58. foreach (Connector connector2 in connector.AllRefs.GetList())
  59. {
  60. if ((connector2.Owner.Id.IntegerValue != mepCurve.Id.IntegerValue) && (connector2.ConnectorType != ConnectorType.Logical))
  61. {
  62. return connector2;
  63. }
  64. }
  65. }
  66. return null;
  67. }
  68. public static List<Connector> GetReferrenceConnectors(this MEPCurve mepCurve)
  69. {
  70. List<Connector> list = new List<Connector>();
  71. foreach (Connector connector in mepCurve.GetAllConnectors())
  72. {
  73. if (connector.IsConnected)
  74. {
  75. foreach (Connector connector2 in connector.AllRefs.GetList())
  76. {
  77. if (!connector2.Owner.Id.Equals(mepCurve.Id))
  78. {
  79. list.Add(connector2);
  80. }
  81. }
  82. }
  83. }
  84. return list;
  85. }
  86. public static void Refurbish<T>(this T mepCurve) where T: MEPCurve
  87. {
  88. LocationCurve location = mepCurve.Location as LocationCurve;
  89. mepCurve.Document.RotateElement(mepCurve, location.GetLine(), 6.2831853071795862);
  90. }
  91. public static Tuple<T, Line> ToTuple<T>(this T mepCurve) where T: MEPCurve
  92. {
  93. return new Tuple<T, Line>(mepCurve, mepCurve.GetCurve() as Line);
  94. }
  95. public static void UpdateLocation<T>(this T mepCurve, XYZ start, XYZ end) where T: MEPCurve
  96. {
  97. if (!start.IsEqual(end, 0.0))
  98. {
  99. LocationCurve location = mepCurve.Location as LocationCurve;
  100. Line axis = start.NewLine(end);
  101. location.Curve = axis;
  102. mepCurve.Document.RotateElement(mepCurve, axis, Math.PI*2);
  103. }
  104. }
  105. public static void UpdateMepCurve(this MEPCurve conduit, XYZ point, XYZ sourcePoint)
  106. {
  107. Curve curve = conduit.Location.GetCurve();
  108. bool flag = curve.StartPoint().IsEqual(sourcePoint, 0.0);
  109. LocationCurve location = conduit.Location as LocationCurve;
  110. Curve curve3 = null;
  111. if (flag)
  112. {
  113. curve3 = Line.CreateBound(point, curve.EndPoint());
  114. }
  115. else
  116. {
  117. curve3 = Line.CreateBound(curve.StartPoint(), point);
  118. }
  119. location.Curve = curve3;
  120. conduit.Document.Regenerate();
  121. }
  122. }
  123. }