ConduitJoinCommand.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Autodesk.Revit.Attributes;
  2. using Autodesk.Revit.DB;
  3. using Autodesk.Revit.DB.Electrical;
  4. using Autodesk.Revit.UI;
  5. using FWindSoft.Revit;
  6. using FWindSoft.Revit.Menu;
  7. using FWindSoft.Revit.Mep;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace LRH.Tool
  14. {
  15. [Transaction(TransactionMode.Manual)]
  16. [Regeneration(RegenerationOption.Manual)]
  17. [Button(ButtonName = "创建线管",Index =-1)]
  18. public class ConduitJoinCommand : ExternalCommand
  19. {
  20. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  21. {
  22. try
  23. {
  24. double length = 800d.MmToFt();
  25. var document = commandData.Application.ActiveUIDocument.Document;
  26. using (Transaction tran = new Transaction(document, "管道链接测试"))
  27. {
  28. try
  29. {
  30. tran.Start();
  31. var conduitType = document.GetConduitTypes().FirstOrDefault();
  32. if (conduitType == null)
  33. {
  34. TaskDialog.Show("提示", "不存在线管类型");
  35. }
  36. List<List<Conduit>> conduitGroups = new List<List<Conduit>>();
  37. XYZ basePoint = XYZ.Zero;
  38. List<Line> line1 = new List<Line>();
  39. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length));
  40. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  41. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  42. basePoint = basePoint + 3 * length*XYZ.BasisX;
  43. line1 = new List<Line>();
  44. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisY * length));
  45. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  46. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  47. basePoint = basePoint + 3 * length * XYZ.BasisX;
  48. line1 = new List<Line>();
  49. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length));
  50. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  51. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  52. basePoint = basePoint + 3 * length * XYZ.BasisX;
  53. line1 = new List<Line>();
  54. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length));
  55. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  56. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  57. basePoint = XYZ.Zero + 3 * length * XYZ.BasisY;
  58. basePoint = basePoint + 3 * length * XYZ.BasisX;
  59. line1 = new List<Line>();
  60. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length));
  61. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  62. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length));
  63. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  64. basePoint = basePoint + 3 * length * XYZ.BasisX;
  65. line1 = new List<Line>();
  66. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length));
  67. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisX * length));
  68. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length));
  69. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  70. basePoint = basePoint + 3 * length * XYZ.BasisX;
  71. line1 = new List<Line>();
  72. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  73. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisX * length));
  74. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length));
  75. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  76. basePoint = basePoint + 3 * length * XYZ.BasisX;
  77. line1 = new List<Line>();
  78. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisY * length));
  79. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  80. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length));
  81. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  82. basePoint = basePoint + 3 * length * XYZ.BasisX;
  83. line1 = new List<Line>();
  84. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length));
  85. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  86. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisZ * length));
  87. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  88. basePoint = basePoint + 3 * length * XYZ.BasisX;
  89. line1 = new List<Line>();
  90. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisY * length));
  91. line1.Add(basePoint.NewLine(basePoint + XYZ.BasisX * length));
  92. line1.Add(basePoint.NewLine(basePoint + -XYZ.BasisZ * length));
  93. conduitGroups.Add(CreateConduits(document, conduitType, line1));
  94. #region 创建连接件
  95. ConduitJoin conduitJoin = new ConduitJoin();
  96. foreach (List<Conduit> conduits in conduitGroups)
  97. {
  98. try
  99. {
  100. var count = conduits.Count;
  101. var elementWraps = conduits.Select(e => new JoinElementWraper(e)).ToList();
  102. switch (count)
  103. {
  104. case 3:
  105. {
  106. conduitJoin.Join3Curve(elementWraps, new JoinContext() { JunctionBoxPlace= JunctionBoxPlaceType.NoAdjustment });
  107. break;
  108. }
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. // throw;
  114. }
  115. }
  116. #endregion
  117. tran.Commit();
  118. }
  119. catch (Exception)
  120. {
  121. tran.RollBack();
  122. }
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. //throw;
  128. }
  129. return Result.Succeeded;
  130. //加入全局异常捕获
  131. }
  132. private List<Conduit> CreateConduits(Document document,ConduitType conduitType,List<Line> lines)
  133. {
  134. List<Conduit> conduits = new List<Conduit>();
  135. foreach (Line line in lines)
  136. {
  137. try
  138. {
  139. Transform angle45 = Transform.CreateRotationAtPoint(XYZ.BasisZ, Math.PI / 5, line.StartPoint());
  140. var useline = line.CreateTransformed(angle45) as Line;
  141. //LevelId可以使用不能使用null,又没有的情况。可以使用 ElementId.InvalidElementId
  142. conduits.Add(Conduit.Create(document, conduitType.Id, useline.StartPoint(), useline.EndPoint(), ElementId.InvalidElementId));
  143. }
  144. catch (Exception e)
  145. {
  146. }
  147. }
  148. return conduits;
  149. }
  150. }
  151. }