StartCADUtil.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using DFBIM.CADNETCommon.XmlConfig;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using static CollaborativePlatformMain.CADStartUtil.AcadDirs;
  12. namespace CollaborativePlatformMain.CADStartUtil
  13. {
  14. /// <summary>
  15. ///
  16. /// <para>文件名(File Name): StartCADUtil.cs</para>
  17. ///
  18. /// <para>描述(Description): 启动CAD并打开图纸</para>
  19. ///
  20. /// <para>数据表(Tables): nothing</para>
  21. ///
  22. /// <para>作者(Author): Ou Rui Song</para>
  23. ///
  24. /// <para>日期(Create Date): 2024年5月7日11:31:34</para>
  25. ///
  26. /// 修改记录(Revision History):
  27. /// R1:
  28. /// 修改作者:
  29. /// 修改日期:
  30. /// 修改理由:
  31. ///
  32. /// </summary>
  33. public class StartCADUtil
  34. {
  35. public static void StartCADMath(bool isFloor = true, OpenDrawPathData openDrawPathData = null)
  36. {
  37. try
  38. {
  39. //获取本机cad路径
  40. AcadDirs regedits = new AcadDirs();
  41. //未安装CAD
  42. if (regedits.Dirs.Count == 0)
  43. {
  44. MessageBox.Show(DateTime.Now + "当前电脑未安装 AutoCAD ");
  45. return;
  46. }
  47. //未安装2019版CAD
  48. List<AcadProductKey> cadPathpks = regedits.Dirs.Where(x => x.ProductName.Equals("AutoCAD 2019 - 简体中文 (Simplified Chinese)")).ToList();
  49. if (cadPathpks.Count == 0)
  50. {
  51. Console.WriteLine(DateTime.Now + "当前电脑未安装 AutoCAD 2019 - 简体中文 (Simplified Chinese)");
  52. Console.ReadLine();
  53. return;
  54. }
  55. var pros = Process.GetProcessesByName("acad");
  56. if (pros.Length != 0)
  57. {
  58. string dPath = Global.GetDriveLetterExceptC();
  59. //已打开cad,写入一个文件,让cad监听文件
  60. if (isFloor)
  61. {
  62. TwoOpenCADConfigUtil twoOpenCADConfigUtil = new TwoOpenCADConfigUtil();
  63. twoOpenCADConfigUtil.Save(new TwoOpenData(true), string.Format(@"{0}\DFBIM\CMTwoOpenFloor.config", dPath));
  64. }
  65. else
  66. {
  67. OpenDrawConfigUtil openDrawConfigUtil = new OpenDrawConfigUtil();
  68. openDrawConfigUtil.Save(openDrawPathData, string.Format(@"{0}\DFBIM\OpenDrawPathData.config", dPath));
  69. }
  70. }
  71. else
  72. {
  73. string location = cadPathpks[0].Location;
  74. // 创建一个脚本文件并写入要执行的命令
  75. string scriptFile = location + "\\autocad_script.scr";
  76. if (isFloor) File.WriteAllText(scriptFile, "CMOPENCADFLOOR\n");
  77. else
  78. {
  79. string dPath = Global.GetDriveLetterExceptC();
  80. OpenDrawConfigUtil openDrawConfigUtil = new OpenDrawConfigUtil();
  81. openDrawConfigUtil.Save(openDrawPathData, string.Format(@"{0}\DFBIM\OpenDrawPathData.config", dPath));
  82. File.WriteAllText(scriptFile, "CMOPENCADDRAW\n");
  83. }
  84. // 启动AutoCAD进程并运行脚本文件
  85. ProcessStartInfo startInfo = new ProcessStartInfo
  86. {
  87. FileName = location + "\\acad.exe",
  88. Arguments = $"/b \"{scriptFile}\"",
  89. UseShellExecute = false,
  90. RedirectStandardError = true,
  91. RedirectStandardInput = true,
  92. RedirectStandardOutput = true,
  93. CreateNoWindow = true
  94. };
  95. //创建新的线程
  96. using (Process autocadProcess = new Process())
  97. {
  98. autocadProcess.StartInfo = startInfo;
  99. autocadProcess.Start();
  100. // 等待AutoCAD进程完成
  101. autocadProcess.WaitForInputIdle();
  102. // 删除脚本文件
  103. //File.Delete(scriptFile);
  104. }
  105. }
  106. }
  107. catch (Exception)
  108. {
  109. return;
  110. }
  111. }
  112. /// <summary>
  113. /// 关闭进程
  114. /// </summary>
  115. private static void KillProcess(int currentProcessId = -1)
  116. {
  117. if (currentProcessId == -1) currentProcessId = Process.GetCurrentProcess().Id;
  118. Process cadProcess = Process.GetProcessById(currentProcessId);
  119. if (!cadProcess.HasExited)
  120. {
  121. cadProcess.Kill();
  122. //log.Info($"成功关闭CAD应用程序 (PID: {cadProcess.Id})");
  123. }
  124. else
  125. {
  126. //log.Info($"CAD应用程序 (PID: {cadProcess.Id}) 已经退出。");
  127. }
  128. //kill infoer
  129. Process[] processes = Process.GetProcessesByName("WSCommCntr4");
  130. if (processes.Length != 0)
  131. {
  132. processes[0].Kill();
  133. //log.Info("kill infoer");
  134. }
  135. }
  136. }
  137. }