RevitCmdExecutor.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. command.Execute(combineParam, ref msg);
  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. jObject.Add(floorskey, new JArray((filePathList ?? new HashSet<string>()).ToArray()));
  71. return jObject.ToString();
  72. }
  73. catch (Exception e)
  74. {
  75. Console.WriteLine(e);
  76. message = new CustomMessage(false, "添加ReferFloors参数异常");
  77. }
  78. return null;
  79. }
  80. }
  81. }