RevitCmdExecutor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* ==============================================================================
  2. * 功能描述:RevitCmdExecutor
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/4/16 11:46:37
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using Newtonsoft.Json.Linq;
  12. using System.Windows.Forms;
  13. using SAGA.DotNetUtils;
  14. using ServiceMBI.Modes;
  15. namespace Client.Start
  16. {
  17. /// <summary>
  18. /// RevitCmdExecutor
  19. /// </summary>
  20. public class RevitCmdExecutor : IRevitCommandExcutor
  21. {
  22. public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList)
  23. {
  24. //由revitCmd生成实体类
  25. //实体类传入参数、参考楼层列表
  26. //执行方法,返回执行结果
  27. Console.WriteLine("准备执行命令");
  28. IServiceCommand command = GetServiceCommand(revitCmd);
  29. if (command == null) return $"命令 {revitCmd} 解析异常";
  30. CustomMessage addMessage = null;
  31. string combineParam = AddFilePathParam(param, filePathList, ref addMessage);
  32. if (addMessage?.Result == false) return addMessage.ResultMsg;
  33. string msg = "";
  34. msg=command.Execute(combineParam);
  35. MessageBox.Show("命令执行完成 ");
  36. return msg;
  37. }
  38. /// <summary>
  39. /// 由Command的字符串转化为相应的命令
  40. /// </summary>
  41. /// <param name="revitCmd"></param>
  42. /// <returns></returns>
  43. private IServiceCommand GetServiceCommand(string revitCmd)
  44. {
  45. IServiceCommand command = null;
  46. string dllName = "ServiceMBI.dll";
  47. string className = "ServiceMBI.Commands." + revitCmd;
  48. string path = Path.Combine(AppBaseInfo.DllRunPath, dllName);
  49. Console.WriteLine(path);
  50. if (File.Exists(path))
  51. {
  52. Assembly tempAsembly = Assembly.LoadFrom(path);
  53. command = (tempAsembly.CreateInstance(className)) as IServiceCommand;
  54. }
  55. return command;
  56. }
  57. /// <summary>
  58. /// 将ReferFloors参数,添加至参数列表中
  59. /// </summary>
  60. /// <param name="param"></param>
  61. /// <param name="filePathList"></param>
  62. /// <param name="message"></param>
  63. /// <returns></returns>
  64. private string AddFilePathParam(string param, HashSet<string> filePathList, ref CustomMessage message)
  65. {
  66. string floorskey = ServiceMBIBuiltInParameter.ReferFloors;
  67. message = new CustomMessage(true, "Add Success");
  68. try
  69. {
  70. JObject jObject = JObject.Parse(param);
  71. //解析字符串获取Json存储树结构
  72. var keys = ServiceMBIBuiltInParameter.ReferFloors.Split('.');
  73. if (keys.Length == 2)
  74. {
  75. JArray checkItemsJArray = new JArray();
  76. foreach (string filePath in filePathList)
  77. {
  78. checkItemsJArray.Add(filePath);
  79. }
  80. if (jObject.ContainsKey(keys[0]))
  81. {
  82. var jcriteria = (JObject)jObject[keys[0]];
  83. jcriteria.Add(keys[1], checkItemsJArray);
  84. }
  85. else
  86. {
  87. JObject checkItemsJObject = new JObject();
  88. checkItemsJObject.Add(keys[1], checkItemsJArray);
  89. jObject.Add(keys[0], checkItemsJObject);
  90. }
  91. }
  92. return jObject.ToString();
  93. }
  94. catch (Exception e)
  95. {
  96. Console.WriteLine(e);
  97. message = new CustomMessage(false, "添加ReferFloors参数异常");
  98. }
  99. return null;
  100. }
  101. }
  102. }