StartCADUtil.cs 5.2 KB

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