RevitCmdExecutor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. return msg;
  36. }
  37. /// <summary>
  38. /// 由Command的字符串转化为相应的命令
  39. /// </summary>
  40. /// <param name="revitCmd"></param>
  41. /// <returns></returns>
  42. private IServiceCommand GetServiceCommand(string revitCmd)
  43. {
  44. IServiceCommand command = null;
  45. string dllName = "ServiceMBI.dll";
  46. string className = "ServiceMBI.Commands." + revitCmd;
  47. string path = Path.Combine(AppBaseInfo.DllRunPath, dllName);
  48. Console.WriteLine(path);
  49. if (File.Exists(path))
  50. {
  51. Assembly tempAsembly = Assembly.LoadFrom(path);
  52. command = (tempAsembly.CreateInstance(className)) as IServiceCommand;
  53. }
  54. return command;
  55. }
  56. /// <summary>
  57. /// 将ReferFloors参数,添加至参数列表中
  58. /// </summary>
  59. /// <param name="param"></param>
  60. /// <param name="filePathList"></param>
  61. /// <param name="message"></param>
  62. /// <returns></returns>
  63. private string AddFilePathParam(string param, HashSet<string> filePathList, ref CustomMessage message)
  64. {
  65. string floorskey = ServiceMBIBuiltInParameter.ReferFloors;
  66. message = new CustomMessage(true, "Add Success");
  67. try
  68. {
  69. JObject jObject = JObject.Parse(param);
  70. //解析字符串获取Json存储树结构
  71. var keys = ServiceMBIBuiltInParameter.ReferFloors.Split('.');
  72. if (keys.Length == 2)
  73. {
  74. JArray checkItemsJArray = new JArray();
  75. foreach (string filePath in filePathList)
  76. {
  77. checkItemsJArray.Add(filePath);
  78. }
  79. if (jObject.ContainsKey(keys[0]))
  80. {
  81. var jcriteria = (JObject)jObject[keys[0]];
  82. jcriteria.Add(keys[1], checkItemsJArray);
  83. }
  84. else
  85. {
  86. JObject checkItemsJObject = new JObject();
  87. checkItemsJObject.Add(keys[1], checkItemsJArray);
  88. jObject.Add(keys[0], checkItemsJObject);
  89. }
  90. }
  91. return jObject.ToString();
  92. }
  93. catch (Exception e)
  94. {
  95. Console.WriteLine(e);
  96. message = new CustomMessage(false, "添加ReferFloors参数异常");
  97. }
  98. return null;
  99. }
  100. }
  101. }