using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CollaborativePlatformMain.CADStartUtil
{
///
///
/// 文件名(File Name): SendToCAD.cs
///
/// 描述(Description): 发送命令到CAD
///
/// 数据表(Tables): nothing
///
/// 作者(Author): Ou Rui Song
///
/// 日期(Create Date): 2024年5月7日11:28:52
///
/// 修改记录(Revision History):
/// R1:
/// 修改作者:
/// 修改日期:
/// 修改理由:
///
///
public class SendToCAD
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
public static void SendCommandToAutoCAD(string toSend, IntPtr hwnd)
{
const int WM_COPYDATA = 0x4A;
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.dwData = new IntPtr(1);
string data = toSend + "\0";
cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
cds.lpData = Marshal.StringToCoTaskMemAuto(data);
SendMessageW(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
Marshal.FreeCoTaskMem(cds.lpData);
}
private struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
}
///
///
/// 文件名(File Name): AcadDirs.cs
///
/// 描述(Description): CAD注册表环境读取
///
/// 数据表(Tables): nothing
///
/// 作者(Author): Li Wen Jin
///
/// 日期(Create Date): 2023年10月20日13:43:58
///
/// 修改记录(Revision History):
/// R1:
/// 修改作者:
/// 修改日期:
/// 修改理由:
///
///
public class AcadDirs
{
public List Dirs { get; }
///
/// 通过注册表获取本机所有cad的安装路径
///
public AcadDirs()
{
Dirs = new List();
RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD");
if (Adsk == null)
{
//Console.WriteLine("Adsk == null");
return;
}
foreach (string ver in Adsk.GetSubKeyNames())
{
try
{
RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
var curver = emnuAcad.GetValue("CurVer");
if (curver == null)
{
//Console.WriteLine("curver == null");
continue;
}
string app = curver.ToString();
string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}";
emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
if (emnuAcad == null)
{
fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}";
emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
}
var acadLocation = emnuAcad.GetValue("AcadLocation");
if (acadLocation == null)
{
//Console.WriteLine("acadLocation == null");
continue;
}
string location = acadLocation.ToString();
if (File.Exists(location + "\\acad.exe"))
{
var produ = emnuAcad.GetValue("ProductName");
if (produ == null)
{
//Console.WriteLine("produ == null");
continue;
}
string productname = produ.ToString();
var release = emnuAcad.GetValue("Release");
if (release == null)
{
//Console.WriteLine("release == null");
continue;
}
string[] strVer = release.ToString().Split('.');
var pro = new AcadProductKey()
{
ProductKey = emnuAcad,
Location = location,
ProductName = productname,
Version = double.Parse(strVer[0] + "." + strVer[1]),
};
Dirs.Add(pro);
}
}
catch { }
}
}
public struct AcadProductKey
{
///
/// 注册表位置
///
public RegistryKey ProductKey;
///
/// cad安装路径
///
public string Location;
///
/// cad名称
///
public string ProductName;
///
/// cad版本号 17.1之类的
///
public double Version;
}
}
}