c# 自动生成 autocad cuix

定义自定义类,用于表达cad netapi生成的命令

 1  public class myAutoCADNetCmd
 2     {
 3         public string DllName { get; set; }
 4         public string className { get; set; }
 5         public string MethodName { get; set; }
 6         public string cmdName { get; set; }
 7         public string HelpString { get; set; }
 8         public string IcoName { get; set; }
 9         public myAutoCADNetCmd(string _dllName, string _clsName, string _meName, string _cmdName, string _helpStr, string _icoName)
10         {
11             this.DllName = _dllName;
12             this.className = _clsName;
13             this.MethodName = _meName;
14             this.cmdName = _cmdName;
15             this.HelpString = _helpStr;
16             this.IcoName = _icoName;
17         }
18     }
19 
20     /// <summary>
21     /// 按程序集
22     /// </summary>
23     public class myAutoCADNetDll
24     {
25         public string DllName { get; set; }
26         public List<myAutoCADNetClass> ListsNetClass { get; set; }
27         public myAutoCADNetDll(Assembly ass, List<myAutoCADNetCmd> ListCmds)
28         {
29             this.ListsNetClass = new List<myAutoCADNetClass>();
30             this.DllName = ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4);
31             var cmdsByDll = ListCmds.Where(c => c.DllName == this.DllName).ToList();
32             List<string> clsNames = new List<string>();
33             foreach (myAutoCADNetCmd item in cmdsByDll) if (!clsNames.Contains(item.className)) clsNames.Add(item.className);
34 
35             foreach (string clsName in clsNames)
36             {
37                 var netCls = new myAutoCADNetClass(this.DllName, clsName, cmdsByDll);
38                 if (netCls.hasCmds) netCls.GetListCmds(cmdsByDll);
39                 if (netCls.ListsNetcmds != null) ListsNetClass.Add(netCls);
40             }
41         }
42     }
43 
44     /// <summary>
45     /// 程序集下按类分类命令
46     /// </summary>
47     public class myAutoCADNetClass
48     {
49         public string className { get; set; }
50         public string dllName { get; set; }
51         public List<myAutoCADNetCmd> ListsNetcmds { get; set; }
52         public bool hasCmds { get; set; }
53         public myAutoCADNetClass(string _dllName, string _clsName, List<myAutoCADNetCmd> ListCmds)
54         {
55             this.className = _clsName;
56             this.dllName = _dllName;
57             if (ListCmds.Count(c => c.DllName == this.dllName && c.className == this.className) > 0) hasCmds = true; else hasCmds = false;
58         }
59         public void GetListCmds(List<myAutoCADNetCmd> ListCmds)
60         {
61             if (this.hasCmds)
62             {
63                 this.ListsNetcmds = ListCmds.Where(c => c.DllName == this.dllName && c.className == this.className).ToList();
64             }
65             else this.ListsNetcmds = null;
66         }
67     }

导出dll文件的命令

 1 List<myAutoCADNetDll> listDllcmd = GetACADNetDllCmd(localDlllist); 

通过反射机制导出AutoCAD Net Dll的注册的命令
 1 /// <summary>
 2         /// 通过反射机制导出AutoCAD Net Dll的注册的命令
 3         /// </summary>
 4         /// <param name="ass">包含AutoCAD Net Dll的注册的命令的Dll</param>
 5         /// <returns></returns>
 6         public List<myAutoCADNetDll> GetACADNetDllCmd(List<string> dllFileNames)
 7         {
 8             List<myAutoCADNetDll> listDlls = new List<myAutoCADNetDll>();
 9             #region//收集自定义命令
10             try
11             {
12                 foreach (var item in dllFileNames)
13                 {
14                     Assembly ass = Assembly.LoadFrom(item);
15                     List<myAutoCADNetCmd> ListCmds = new List<myAutoCADNetCmd>();
16                     foreach (var t in ass.GetTypes())
17                     {
18                         if (t.IsClass && t.IsPublic)
19                         {
20                             foreach (MethodInfo method in t.GetMethods())
21                             {
22                                 if (method.IsPublic && method.GetCustomAttributes(true).Length > 0)
23                                 {
24                                     CommandMethodAttribute cadAtt = null; myAutoCADcmdAttribute myAtt = null;
25                                     foreach (var att in method.GetCustomAttributes(true))
26                                     {
27                                         if (att.GetType().Name == typeof(CommandMethodAttribute).Name) cadAtt = att as CommandMethodAttribute;
28                                         if (att.GetType().Name == typeof(myAutoCADcmdAttribute).Name) myAtt = att as myAutoCADcmdAttribute;
29                                     }
30                                     if (myAtt != null && cadAtt != null)
31                                     {
32                                         ListCmds.Add(new myAutoCADNetCmd(Path.GetFileNameWithoutExtension(
33                                             ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4)),
34                                             t.Name, method.Name, cadAtt.GlobalName, myAtt.HelpString, myAtt.IcoFileName));
35                                     }
36                                     else
37                                     {
38                                         if (cadAtt != null && myAtt == null)
39                                         {
40                                             ListCmds.Add(new myAutoCADNetCmd(Path.GetFileNameWithoutExtension(
41                                                 ass.ManifestModule.Name.Substring(0, ass.ManifestModule.Name.Length - 4)),
42                                                 t.Name, method.Name, cadAtt.GlobalName, "", ""));
43                                         }
44                                     }
45                                 }
46                             }
47                         }
48                     }
49                     if (ListCmds.Count > 0)
50                     {
51                         listDlls.Add(new myAutoCADNetDll(ass, ListCmds));
52                     }
53                 }
54             }
55             catch (System.Exception ex)
56             {
57                 Application.ShowAlertDialog(ex.Message);
58             }
59             #endregion
60             return listDlls;
61         }

生成cuix文件

 1 public void AddMenusToAutoCAD(List<myAutoCADNetDll> dicCmds)
 2         {
 3             string strCuiFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\KFWH_CMD.cuix";
 4             string strGpName = "KFWH_CMDGroup";//defined a group name
 5             CustomizationSection myCsec = new CustomizationSection() { MenuGroupName = strGpName };
 6             MacroGroup mg = new MacroGroup("KFWH_CMD", myCsec.MenuGroup);
 7             StringCollection scMyMenuAlias = new StringCollection();
 8             scMyMenuAlias.Add("KFWH_CMD_POP");
 9             PopMenu pmParent = new PopMenu("KFWH_CMD", scMyMenuAlias, "KFWH_CMD", myCsec.MenuGroup);
10             foreach (var dll in dicCmds)//each assembly commmands
11             {
12                 PopMenu pmCurDll = new PopMenu(dll.DllName, new StringCollection(), "", myCsec.MenuGroup);
13                 PopMenuRef pmrdll = new PopMenuRef(pmCurDll, pmParent, -1);
14                 foreach (var cls in dll.ListsNetClass)
15                 {
16                     if (cls.hasCmds)
17                     {
18                         PopMenu pmCurcls = new PopMenu(cls.className, new StringCollection(), "", myCsec.MenuGroup);
19                         PopMenuRef pmrcls = new PopMenuRef(pmCurcls, pmCurDll, -1);
20                         foreach (var cmd in cls.ListsNetcmds)
21                         {
22                             MenuMacro mm = new MenuMacro(mg, cmd.MethodName, cmd.cmdName, cmd.DllName + "_" + cmd.className + "_" + cmd.cmdName, MacroType.Any);
23                             //指定命令宏的说明信息,在状态栏中显示
24                             if (cmd.HelpString != string.Empty) mm.macro.HelpString = cmd.HelpString;
25                             if (cmd.IcoName != string.Empty) mm.macro.LargeImage = mm.macro.SmallImage = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\KFWH_CMD_ICON\\" + cmd.IcoName;
26                             //指定命令宏的大小图像的路径
27                             PopMenuItem newPmi = new PopMenuItem(mm, cmd.MethodName, pmCurcls, -1);
28                             //newPmi.MacroID = cmd.DllName + "_" + cmd.className + "_" + cmd.cmdName;
29                         }
30                     }
31                 }
32             }
33             //myCsec.MenuGroup.AddToolbar("kfwh_cmd");
34             if (myCsec.SaveAs(strCuiFileName)) this.mycuiFileName = strCuiFileName;
35         }

 

加载cuix

1  if (this.mycuiFileName != string.Empty) Application.LoadPartialMenu(this.mycuiFileName);
2  Application.SetSystemVariable("MENUBAR", 1);

 

posted @ 2020-09-27 20:22  南胜NanSheng  阅读(1335)  评论(0编辑  收藏  举报