CAD二次开发可以采用写扩展DLL的方式实现。该DLL的函数可以被CAD调用。
但是调用前,必须用命令netload 将该dll加载到CAD。
其实可以修改注册表,当CAD软件启动后,自动加载扩展DLL。
为此,我写了一个函数,很方便的修改注册表,达到自动加载dll的目的!
该函数支持各个版本cad。
string[] GetRegSubDir(RegistryKey autoCad, string startFlag)
{
string[] subKey = autoCad.GetSubKeyNames();
return subKey.Where(o => o.StartsWith(startFlag)).ToArray();
}
1 //strCadRegKey = "HLD_CAD_Import"; //唯一注册标识 可用公司名称和程序名称
2 //strDll dll文件路径
3 private bool WriteRegistryKey(string strDll,string strCadRegKey)
4 {
5 try
6 {
7 RegistryKey localMachine = Registry.LocalMachine;
8 RegistryKey SOFTWARE = localMachine.OpenSubKey("SOFTWARE", true);
9 RegistryKey Autodesk = SOFTWARE.OpenSubKey("Autodesk", true);
10 RegistryKey AutoCAD = Autodesk.OpenSubKey("AutoCAD", true);
11
12 int result = 0;
13 foreach (string subDir in GetRegSubDir(AutoCAD, "R"))
14 {
15 try
16 {
17 RegistryKey CadVersion = AutoCAD.OpenSubKey(subDir, true);
18
19 string AcadVersion = GetRegSubDir(CadVersion, "ACAD-").FirstOrDefault();
20 RegistryKey ACAD = CadVersion.OpenSubKey(AcadVersion, true);
21
22 RegistryKey Applications = ACAD.OpenSubKey("Applications", true);
23
24 //删除旧有记录
25 try
26 {
27 Applications.DeleteSubKeyTree(strCadRegKey);
28 }
29 catch (Exception ex)
30 { }
31
32 RegistryKey MXCAD = Applications.CreateSubKey(strCadRegKey);
33 MXCAD.SetValue("LOADCTRLS", 0x02);
34 MXCAD.SetValue("LOADER", strDll);
35 MXCAD.SetValue("MANAGED", 0x01);
36 result++;
37 }
38 catch (Exception ex)
39 {
40 if (showMessage)
41 System.Windows.Forms.MessageBox.Show(string.Format("注册时出错!{0}", ex.Message));
42 }
43 }
44
45 if (result > 0 && showMessage)
46 System.Windows.Forms.MessageBox.Show(string.Format("注册成功!"));
47 return true;
48 }
49 catch (Exception ex)
50 {
51
52 return false;
53 }
54 }