StartCADUtil.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. static string location;
  35. public static void StartCADMath()
  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. //已打开cad
  59. }
  60. else
  61. {
  62. location = cadPathpks[0].Location;
  63. // 创建一个脚本文件并写入要执行的命令
  64. //string scriptFile = location + "\\autocad_script.scr";
  65. //File.WriteAllText(scriptFile, "DFBIMDwgComparisonCooperate\n");
  66. // 启动AutoCAD进程并运行脚本文件
  67. ProcessStartInfo startInfo = new ProcessStartInfo
  68. {
  69. FileName = location + "\\acad.exe",
  70. //Arguments = $"/b \"{scriptFile}\"",
  71. UseShellExecute = false,
  72. RedirectStandardError = true,
  73. RedirectStandardInput = true,
  74. RedirectStandardOutput = true,
  75. CreateNoWindow = true
  76. };
  77. using (Process autocadProcess = new Process { StartInfo = startInfo })
  78. {
  79. autocadProcess.Start();
  80. // 等待AutoCAD进程完成
  81. autocadProcess.WaitForInputIdle();
  82. // 删除脚本文件
  83. //File.Delete(scriptFile);
  84. }
  85. }
  86. }
  87. catch (Exception)
  88. {
  89. return;
  90. }
  91. }
  92. }
  93. }