SendToCAD.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace CollaborativePlatformMain.CADStartUtil
  10. {
  11. /// <summary>
  12. ///
  13. /// <para>文件名(File Name): SendToCAD.cs</para>
  14. ///
  15. /// <para>描述(Description): 发送命令到CAD</para>
  16. ///
  17. /// <para>数据表(Tables): nothing</para>
  18. ///
  19. /// <para>作者(Author): Ou Rui Song</para>
  20. ///
  21. /// <para>日期(Create Date): 2024年5月7日11:28:52</para>
  22. ///
  23. /// 修改记录(Revision History):
  24. /// R1:
  25. /// 修改作者:
  26. /// 修改日期:
  27. /// 修改理由:
  28. ///
  29. /// </summary>
  30. public class SendToCAD
  31. {
  32. [DllImport("user32.dll")]
  33. public static extern bool SetForegroundWindow(IntPtr hWnd);
  34. [DllImport("user32.dll", EntryPoint = "FindWindow")]
  35. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  36. [DllImport("user32.dll")]
  37. private static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
  38. public static void SendCommandToAutoCAD(string toSend, IntPtr hwnd)
  39. {
  40. const int WM_COPYDATA = 0x4A;
  41. COPYDATASTRUCT cds = new COPYDATASTRUCT();
  42. cds.dwData = new IntPtr(1);
  43. string data = toSend + "\0";
  44. cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
  45. cds.lpData = Marshal.StringToCoTaskMemAuto(data);
  46. SendMessageW(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
  47. Marshal.FreeCoTaskMem(cds.lpData);
  48. }
  49. private struct COPYDATASTRUCT
  50. {
  51. public IntPtr dwData;
  52. public int cbData;
  53. public IntPtr lpData;
  54. }
  55. }
  56. /// <summary>
  57. ///
  58. /// <para>文件名(File Name): AcadDirs.cs</para>
  59. ///
  60. /// <para>描述(Description): CAD注册表环境读取</para>
  61. ///
  62. /// <para>数据表(Tables): nothing</para>
  63. ///
  64. /// <para>作者(Author): Li Wen Jin</para>
  65. ///
  66. /// <para>日期(Create Date): 2023年10月20日13:43:58</para>
  67. ///
  68. /// 修改记录(Revision History):
  69. /// R1:
  70. /// 修改作者:
  71. /// 修改日期:
  72. /// 修改理由:
  73. ///
  74. /// </summary>
  75. public class AcadDirs
  76. {
  77. public List<AcadProductKey> Dirs { get; }
  78. /// <summary>
  79. /// 通过注册表获取本机所有cad的安装路径
  80. /// </summary>
  81. public AcadDirs()
  82. {
  83. Dirs = new List<AcadProductKey>();
  84. RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD");
  85. if (Adsk == null)
  86. {
  87. //Console.WriteLine("Adsk == null");
  88. return;
  89. }
  90. foreach (string ver in Adsk.GetSubKeyNames())
  91. {
  92. try
  93. {
  94. RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
  95. var curver = emnuAcad.GetValue("CurVer");
  96. if (curver == null)
  97. {
  98. //Console.WriteLine("curver == null");
  99. continue;
  100. }
  101. string app = curver.ToString();
  102. string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}";
  103. emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
  104. if (emnuAcad == null)
  105. {
  106. fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}";
  107. emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
  108. }
  109. var acadLocation = emnuAcad.GetValue("AcadLocation");
  110. if (acadLocation == null)
  111. {
  112. //Console.WriteLine("acadLocation == null");
  113. continue;
  114. }
  115. string location = acadLocation.ToString();
  116. if (File.Exists(location + "\\acad.exe"))
  117. {
  118. var produ = emnuAcad.GetValue("ProductName");
  119. if (produ == null)
  120. {
  121. //Console.WriteLine("produ == null");
  122. continue;
  123. }
  124. string productname = produ.ToString();
  125. var release = emnuAcad.GetValue("Release");
  126. if (release == null)
  127. {
  128. //Console.WriteLine("release == null");
  129. continue;
  130. }
  131. string[] strVer = release.ToString().Split('.');
  132. var pro = new AcadProductKey()
  133. {
  134. ProductKey = emnuAcad,
  135. Location = location,
  136. ProductName = productname,
  137. Version = double.Parse(strVer[0] + "." + strVer[1]),
  138. };
  139. Dirs.Add(pro);
  140. }
  141. }
  142. catch { }
  143. }
  144. }
  145. public struct AcadProductKey
  146. {
  147. /// <summary>
  148. /// 注册表位置
  149. /// </summary>
  150. public RegistryKey ProductKey;
  151. /// <summary>
  152. /// cad安装路径
  153. /// </summary>
  154. public string Location;
  155. /// <summary>
  156. /// cad名称
  157. /// </summary>
  158. public string ProductName;
  159. /// <summary>
  160. /// cad版本号 17.1之类的
  161. /// </summary>
  162. public double Version;
  163. }
  164. }
  165. }