TestCommand.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using Autodesk.Revit.Attributes;
  10. using Autodesk.Revit.DB;
  11. using Autodesk.Revit.DB.Plumbing;
  12. using Autodesk.Revit.UI;
  13. using Autodesk.Revit.UI.Selection;
  14. using SAGA.DotNetUtils.Extend;
  15. using SAGA.DotNetUtils.Logger;
  16. using SAGA.MBI.Calc;
  17. using SAGA.MBI.DataArrange;
  18. using SAGA.MBI.Interaction;
  19. using SAGA.MBI.ModeInfoEdit;
  20. using SAGA.MBI.RevitExport;
  21. using SAGA.MBI.Tools;
  22. using SAGA.MBI.WinView.Login;
  23. using SAGA.MBI.WinView.Upload;
  24. using SAGA.RevitUtils;
  25. using SAGA.RevitUtils.Extends;
  26. using SAGA.RevitUtils.MEP;
  27. using WPfPointInfo;
  28. using Color = Autodesk.Revit.DB.Color;
  29. namespace SAGA.MBI
  30. {
  31. /// <summary>
  32. /// 上传当前楼层数据
  33. /// </summary>
  34. [Transaction(TransactionMode.Manual)]
  35. [Regeneration(RegenerationOption.Manual)]
  36. public class ExportMbiModelCommand : ExternalCommand, IExternalCommandAvailability
  37. {
  38. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  39. {
  40. try
  41. {
  42. //WinPointInfoTest win = new WinPointInfoTest();
  43. //win.ShowDialog();
  44. var result = MessageShow.Question2("是否是整个项目导出");
  45. switch (result)
  46. {
  47. case System.Windows.Forms.DialogResult.Yes:
  48. {
  49. var floors = DalUploadFloor.GetHasFileFloors();
  50. List<CalcContext> contexts = new List<CalcContext>();
  51. foreach (UploadFloor floor in floors)
  52. {
  53. contexts.Add(new CalcContext(floor.MFloor));
  54. }
  55. MBIModelInfoUpload.UpdateMbiInfo(contexts);
  56. break;
  57. }
  58. case System.Windows.Forms.DialogResult.No:
  59. {
  60. MbiElementManager.ExecuteExport(ExternalDataWrapper.Current.Doc);
  61. var floorId = ExternalDataWrapper.Current.Doc.PathName.GetFileName();
  62. MBIModelInfoUpload.UploadMbiInfo(floorId);
  63. break;
  64. }
  65. }
  66. ////MbiElementManager.ExecuteExport(ExternalDataWrapper.Current.Doc);
  67. ////var floorId = ExternalDataWrapper.Current.Doc.PathName.GetFileName();
  68. ////MBIModelInfoUpload.UploadMbiInfo(floorId);
  69. }
  70. catch (Exception e)
  71. {
  72. MessageShow.Show(e);
  73. return Result.Cancelled;
  74. }
  75. return Result.Succeeded;
  76. }
  77. //public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  78. //{
  79. // return true;
  80. //}
  81. }
  82. /// <summary>
  83. /// 管道系统类型
  84. /// </summary>
  85. [Transaction(TransactionMode.Manual)]
  86. [Regeneration(RegenerationOption.Manual)]
  87. public class UpdatePipeSystemCommand : ExternalCommand, IExternalCommandAvailability
  88. {
  89. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  90. {
  91. try
  92. {
  93. var pickElement=ExternalDataWrapper.Current.UiApp.PickElement("请选择水管", new PipeFilter());
  94. if (pickElement == null)
  95. {
  96. return Result.Succeeded;
  97. }
  98. using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc,"连接"))
  99. {
  100. try
  101. {
  102. tran.Start();
  103. var equioment = ExternalDataWrapper.Current.UiApp.PickElement("选择设备");
  104. var pipeConnector = pickElement.GetConnectors(Domain.DomainPiping).FirstOrDefault(c => !c.IsConnected);
  105. var equipmentConnector = equioment.GetConnectors(Domain.DomainPiping).FirstOrDefault(c => !c.IsConnected);
  106. if (pipeConnector != null && equipmentConnector != null)
  107. {
  108. pipeConnector.ConnectTo(equipmentConnector);
  109. }
  110. tran.Commit();
  111. }
  112. catch (Exception ex)
  113. {
  114. MessageShow.Show(ex);
  115. tran.RollBack();
  116. }
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. MessageShow.Show(e);
  122. return Result.Cancelled;
  123. }
  124. return Result.Succeeded;
  125. }
  126. //public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  127. //{
  128. // return true;
  129. //}
  130. public class PipeFilter : ISelectionFilter
  131. {
  132. public bool AllowElement(Element elem)
  133. {
  134. return elem is Pipe;
  135. }
  136. public bool AllowReference(Reference reference, XYZ position)
  137. {
  138. return true;
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 管道系统类型
  144. /// </summary>
  145. [Transaction(TransactionMode.Manual)]
  146. [Regeneration(RegenerationOption.Manual)]
  147. public class CheckLocationCommand : ExternalCommand, IExternalCommandAvailability
  148. {
  149. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  150. {
  151. try
  152. {
  153. var pickElement = ExternalDataWrapper.Current.UiApp.PickElement("请选择设备", new EquipmentFilter()) as FamilyInstance;
  154. if (pickElement == null)
  155. {
  156. return Result.Succeeded;
  157. }
  158. using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc, "显示"))
  159. {
  160. try
  161. {
  162. tran.Start();
  163. var doc = ExternalDataWrapper.Current.Doc;
  164. var s = pickElement.GetLocationPointMBIXYZ();
  165. DirectShape ds = DirectShape.CreateElement(doc,new ElementId(BuiltInCategory.OST_GenericModel));
  166. var solid = CreateSphereAt(s, 1);
  167. ds.AppendShape(new List<GeometryObject>() { solid });
  168. SetColor(ds,new Color(255, 0, 0));
  169. tran.Commit();
  170. ExternalDataWrapper.Current.UiApp.SetShowElements(new List<Element>() { ds });
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageShow.Show(ex);
  175. tran.RollBack();
  176. }
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. MessageShow.Show(e);
  182. return Result.Cancelled;
  183. }
  184. return Result.Succeeded;
  185. }
  186. #region 静态方法
  187. /// <summary>
  188. /// 替换视图中的图形,来修改颜色
  189. /// </summary>
  190. public static void SetColor( Element elem, Color color, View view = null,
  191. int transparency = 0)
  192. {
  193. var ogs = new OverrideGraphicSettings();
  194. #region 表面填充图案
  195. //可见
  196. ogs.SetProjectionFillPatternVisible(true);
  197. //颜色
  198. ogs.SetProjectionFillColor(color);
  199. //透明度
  200. ogs.SetSurfaceTransparency(transparency);
  201. #endregion
  202. #region 截面填充图案
  203. //可见
  204. ogs.SetCutFillPatternVisible(true);
  205. //颜色
  206. ogs.SetCutFillColor(color);
  207. //填充图案
  208. #endregion
  209. if (view != null)
  210. {
  211. view.SetElementOverrides(elem.Id, ogs);
  212. }
  213. else
  214. {
  215. elem.Document.ActiveView.SetElementOverrides(elem.Id, ogs);
  216. }
  217. }
  218. /// <summary>
  219. /// 使用指定的球心和半径创建球体
  220. /// </summary>
  221. static public Solid CreateSphereAt(
  222. XYZ centre,
  223. double radius)
  224. {
  225. // 使用标准的全局坐标系创建 Frame
  226. Frame frame = new Frame(centre,
  227. XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ);
  228. // 创建一个Z轴方向的半圆闭合曲线(注意所有的坐标都是相对全局坐标系的)
  229. Arc arc = Arc.Create(
  230. centre - radius * XYZ.BasisZ,
  231. centre + radius * XYZ.BasisZ,
  232. centre + radius * XYZ.BasisX);
  233. Line line = Line.CreateBound(
  234. arc.EndPoint(),
  235. arc.StartPoint());
  236. CurveLoop halfCircle = new CurveLoop();
  237. halfCircle.Append(arc);
  238. halfCircle.Append(line);
  239. List<CurveLoop> loops = new List<CurveLoop>(1);
  240. loops.Add(halfCircle);
  241. return GeometryCreationUtilities
  242. .CreateRevolvedGeometry(
  243. frame, loops, 0, 2 * Math.PI);
  244. }
  245. #endregion
  246. public class EquipmentFilter : ISelectionFilter
  247. {
  248. public bool AllowElement(Element elem)
  249. {
  250. return elem is FamilyInstance;
  251. }
  252. public bool AllowReference(Reference reference, XYZ position)
  253. {
  254. return true;
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// 测试多项目读取
  260. /// </summary>
  261. [Transaction(TransactionMode.Manual)]
  262. [Regeneration(RegenerationOption.Manual)]
  263. public class TestMultipleReadDocCommand : ExternalCommand
  264. {
  265. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  266. {
  267. try
  268. {
  269. var floors = DalProjectTree.GetAllFloors(false);
  270. var floor = floors.FirstOrDefault();
  271. var doc = ExternalDataWrapper.Current.App.OpenDocumentFile(floor.FullPath);
  272. var eles = doc.GetAllElements();
  273. string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),$"{floor}_{System.DateTime.Now.Millisecond}.txt");
  274. //File.Create(path);
  275. foreach (Element element in eles)
  276. {
  277. File.AppendAllText(path,$"{element.Id}:{element.Name}\r\n");
  278. }
  279. }
  280. catch (Exception e)
  281. {
  282. MessageShow.Show(e);
  283. return Result.Cancelled;
  284. }
  285. return Result.Succeeded;
  286. }
  287. public override bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  288. {
  289. return true;
  290. }
  291. }
  292. /// <summary>
  293. /// 掉转阀件方向
  294. /// </summary>
  295. [Transaction(TransactionMode.Manual)]
  296. [Regeneration(RegenerationOption.Manual)]
  297. public class ReDirectionCommand : ExternalCommand
  298. {
  299. public override Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  300. {
  301. using (Transaction tran = new Transaction(ExternalDataWrapper.Current.Doc, "调整阀门"))
  302. {
  303. tran.Start();
  304. try
  305. {
  306. var pickElement = ExternalDataWrapper.Current.UiApp.PickElement("请选择阀件", typeof(FamilyInstance)) as FamilyInstance;
  307. if (pickElement == null)
  308. {
  309. return Result.Succeeded;
  310. }
  311. var handling = pickElement.HandOrientation;
  312. var facing = pickElement.FacingOrientation;
  313. var location = pickElement.GetLocationPoint();
  314. var axis = location.NewLine(location + handling.CrossProduct(facing));
  315. var connectors = pickElement.GetAllConnectors();
  316. List<Connector> refConnectors = new List<Connector>();
  317. foreach (var connector in connectors)
  318. {
  319. if (!ConnectorType.Physical.HasFlag(connector.ConnectorType))
  320. {
  321. continue;
  322. }
  323. var tempRefs = connector.GetReferenceConnectors();
  324. foreach (var tempRef in tempRefs)
  325. {
  326. if (ConnectorType.Physical.HasFlag(tempRef.ConnectorType)&& tempRef.IsConnectedTo(connector))
  327. {
  328. connector.DisconnectFrom(tempRef);
  329. refConnectors.Add(tempRef);
  330. }
  331. }
  332. }
  333. ElementTransformUtils.RotateElement(pickElement.Document, pickElement.Id, axis, Math.PI);
  334. pickElement.Document.Regenerate();
  335. connectors = pickElement.GetAllConnectors();
  336. for (int i = 0; i < refConnectors.Count; i++)
  337. {
  338. var refCon = refConnectors[i];
  339. if (refCon == null)
  340. continue;
  341. var maxLength = double.MaxValue;
  342. Connector bConnector = null;
  343. foreach (var connector in connectors)
  344. {
  345. var newLength = refCon.Origin.Subtract(connector.Origin).GetLength();
  346. if (newLength < maxLength&&!connector.IsConnected)
  347. {
  348. maxLength = newLength;
  349. bConnector = connector;
  350. }
  351. }
  352. if (bConnector != null)
  353. {
  354. bConnector.ConnectTo(refCon);
  355. }
  356. }
  357. tran.Commit();
  358. }
  359. catch (Exception e)
  360. {
  361. tran.RollBack();
  362. MessageShow.Show(e);
  363. return Result.Cancelled;
  364. }
  365. }
  366. return Result.Succeeded;
  367. }
  368. public override bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
  369. {
  370. return true;
  371. }
  372. }
  373. }