• AutoCAD2013 以上利用AccoreConsole+ c# NetApi Windows Froms 封装


    1# 封装类

      1  public static class CmdHelper
      2     {
      3         /// <summary>
      4         /// 调用AutoCAD 安装目录下的AccoreConsole.exe来实现批量处理图纸(Net Api dll)
      5         /// </summary>
      6         /// <param name="cmsStr">NetApi中注册的命令(commandmethod中写的命令)</param>
      7         /// <param name="dllPath">AutoCAD Net Api的程序集的全路径</param>
      8         /// <param name="dwgfn">dwg文件的全路径</param>
      9         /// <param name="AccoreconsolePath">AutoCAD 安装目录下的AccoreConsole.exe的全路径</param>
     10         public static void ExecuteCmd(string cmsStr,string dllPath,string dwgfn)
     11         {
     12             var acadInstallPath = string.Empty;
     13             for (int i = 2013; i < DateTime.Now.Year + 2; i++)
     14             {
     15                 if (File.Exists(@"C:Program FilesAutodeskAutoCAD " + i + "\Accoreconsole.exe"))
     16                 {
     17                     acadInstallPath = @"C:Program FilesAutodeskAutoCAD " + i + "\";
     18                     break;
     19                 }
     20             }
     21             System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo()
     22             {
     23                 FileName = "cmd.exe",
     24                 UseShellExecute=false,
     25                 CreateNoWindow=true,
     26                 RedirectStandardInput=true,
     27                 RedirectStandardOutput=true
     28             };
     29             Process pro = new Process() { StartInfo = psi };
     30             pro.Start();
     31             pro.StandardInput.WriteLine(""" + acadInstallPath + "Accoreconsole.exe" + "" /i " + """ + dwgfn + """);
     32             pro.StandardInput.WriteLine("secureload");
     33             pro.StandardInput.WriteLine("0");
     34             pro.StandardInput.WriteLine("netload");
     35             pro.StandardInput.WriteLine("""+ dllPath + """);
     36             pro.StandardInput.WriteLine("filedia");
     37             pro.StandardInput.WriteLine("1");
     38             pro.StandardInput.WriteLine(cmsStr);
     39             pro.StandardInput.WriteLine("qsave");
     40             pro.StandardInput.WriteLine("quit");
     41             pro.StandardInput.WriteLine("exit");
     42         }
     43         /// <summary>
     44         /// 调用AutoCAD 安装目录下的AccoreConsole.exe来实现批量处理图纸(scr 文件)
     45         /// </summary>
     46         /// <param name="scrFileName">scr 文件的全路径</param>
     47         /// <param name="dwgfn">dwg文件的全路径</param>
     48         /// <param name="AccoreconsolePath">AutoCAD 安装目录下的AccoreConsole.exe的全路径</param>
     49         public static void ExecuteCmd(string scrFileName, string dwgfn)
     50         {
     51             var acadInstallPath = string.Empty;
     52             for (int i = 2013; i < DateTime.Now.Year + 2; i++)
     53             {
     54                 if (File.Exists(@"C:Program FilesAutodeskAutoCAD " + i + "\Accoreconsole.exe"))
     55                 {
     56                     acadInstallPath = @"C:Program FilesAutodeskAutoCAD " + i + "\";
     57                     break;
     58                 }
     59             }
     60             System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo()
     61             {
     62                 FileName = acadInstallPath+"Accoreconsole.exe",
     63                 Arguments = " /i " + """ + dwgfn + "" /s  "+""" + scrFileName + """,
     64                 UseShellExecute = false,
     65                 CreateNoWindow = true,
     66                 RedirectStandardInput = true,
     67                 RedirectStandardOutput = true
     68             };
     69             Process pro = new Process() { StartInfo = psi };
     70             pro.Start();
     71             pro.StandardInput.WriteLine("filedia");
     72             pro.StandardInput.WriteLine("1");
     73             pro.StandardInput.WriteLine("exit");
     74         }
     75 
     76         public static List<String> GetOutNetApiCmd(string dllPath)
     77         {
     78             List<String> strCmd = new List<string>();
     79             AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
     80             Assembly ass = Assembly.LoadFrom(dllPath);
     81             foreach (var t in ass.GetTypes())
     82             {
     83                 if (t.IsClass && t.IsPublic)
     84                 {
     85                     foreach (MethodInfo m in t.GetMethods())
     86                     {
     87                         if (m.IsPublic && m.GetCustomAttributes(true)!=null)
     88                         {
     89                             Attribute att = null;
     90                             foreach (var item in m.GetCustomAttributes(true))
     91                             {
     92                                 if (item.GetType().Name == "CommandMethodAttribute") att = item as Attribute;
     93                             }
     94                             if (att!=null) strCmd.Add(m.CustomAttributes.ToList()[0].ConstructorArguments[0].Value.ToString());
     95                         }
     96                     }
     97                 }
     98             }
     99             return strCmd;
    100         }
    101 
    102         private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    103         {
    104             AssemblyName assName = new AssemblyName(args.Name);
    105             var acadInstallPath = string.Empty;
    106             for (int i = 2013; i < DateTime.Now.Year+2; i++)
    107             {
    108                 if (File.Exists(@"C:Program FilesAutodeskAutoCAD "+i+"\Accoreconsole.exe")) 
    109                 {
    110                     acadInstallPath = @"C:Program FilesAutodeskAutoCAD " + i + "\";
    111                     break;
    112                 }
    113             }
    114             return Assembly.LoadFile(acadInstallPath + args.Name+".dll");
    115         }
    116     }

    2# 准备autocad的sdk放入文件夹,这个sdk的dll不要高于你的netapi的dll的版本,最好一致

     3# 在program.cs中做如下调整

     提前加入netapi的dll的依赖项,此处为了到处netapi的自定义命令的名称“GetOutNetApiCmd”方法可以成功执行,否则无法执行

      static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var acadInstallPath = string.Empty;
                for (int i = 2013; i < DateTime.Now.Year + 2; i++)
                {
                    if (File.Exists(@"C:Program FilesAutodeskAutoCAD " + i + "\Accoreconsole.exe"))
                    {
                        acadInstallPath = @"C:Program FilesAutodeskAutoCAD " + i + "\";
                        break;
                    }
                }
                if (acadInstallPath != string.Empty)
                {
                    var location = Application.StartupPath;
                    Assembly.LoadFrom(location + "\NetApiDll\AcDbMgd.dll");
                    Assembly.LoadFrom(location + "\NetApiDll\AcCoreMgd.dll");
                    Application.Run(new Form1());
                }
                else MessageBox.Show("本机没有安装AutoCAD2013-"+DateTime.Now.Year + 1+"中的任意一个版本, 无法启动本程序!");
            }

     4# 绘制用户界面

     5# 写完程序

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 using System.Windows.Forms;
    10 
    11 namespace MyAccoreConsole
    12 {
    13     public partial class Form1 : Form
    14     {
    15         public List<string> ListDwgFileNames { get; set; }
    16         public string SelectedDllName { get; set; }
    17         public string SelectedScrName { get; set; }
    18         public string SelectedCmdName { get; set; }
    19         public Form1()
    20         {
    21             InitializeComponent();
    22         }
    23 
    24         private void button1_Click(object sender, EventArgs e)
    25         {
    26             this.listBox1.Items.Clear();
    27             this.ListDwgFileNames = new List<string>();
    28             OpenFileDialog ofd = new OpenFileDialog() {
    29                 Filter="AutoCAD DwgFile *.dwg|*.dwg",
    30                 Multiselect=true,
    31                 ReadOnlyChecked=true
    32             };
    33             if (ofd.ShowDialog()== DialogResult.OK) this.ListDwgFileNames.AddRange(ofd.FileNames);
    34             foreach (var item in this.ListDwgFileNames) this.listBox1.Items.Add(item);
    35         }
    36 
    37         private void button3_Click(object sender, EventArgs e)
    38         {
    39             this.label2.Text = string.Empty;
    40             OpenFileDialog ofd = new OpenFileDialog()
    41             {
    42                 Filter = "AutoCAD NetApi Dll *.dll|*.dll",
    43                 Multiselect = false
    44             };
    45             if (ofd.ShowDialog() == DialogResult.OK) this.SelectedDllName = ofd.FileName;
    46             this.label2.Text = this.SelectedDllName;
    47         }
    48 
    49         private void button2_Click(object sender, EventArgs e)
    50         {
    51             this.label1.Text = string.Empty;
    52             OpenFileDialog ofd = new OpenFileDialog()
    53             {
    54                 Filter = "AutoCAD Scr File *.scr|*.scr",
    55                 Multiselect = false
    56             };
    57             if (ofd.ShowDialog() == DialogResult.OK) this.SelectedScrName = ofd.FileName;
    58             this.label1.Text = this.SelectedScrName;
    59         }
    60         private void comboBox1_DropDown(object sender, EventArgs e)
    61         {
    62             this.comboBox1.Items.Clear();
    63             var cms = CmdHelper.GetOutNetApiCmd(this.SelectedDllName);
    64             if (cms.Count>0) this.comboBox1.Items.AddRange(cms.ToArray());
    65         }
    66         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    67         {
    68             this.SelectedCmdName = this.comboBox1.Text;
    69         }
    70         private void button5_Click(object sender, EventArgs e)
    71         {
    72             foreach (var item in this.ListDwgFileNames)
    73             {
    74                 CmdHelper.ExecuteCmd(this.SelectedCmdName, this.SelectedDllName,item);
    75             }
    76             MessageBox.Show("Net Api 执行完成!!!!");
    77         }
    78 
    79         private void button4_Click(object sender, EventArgs e)
    80         {
    81             foreach (var item in this.ListDwgFileNames)
    82             {
    83                 CmdHelper.ExecuteCmd(this.SelectedScrName,item);
    84             }
    85             MessageBox.Show("SCR 批处理文件执行完成!!!!");
    86         }
    87     }
    88 }
  • 相关阅读:
    poj 3278 Catch That Cow(bfs+队列)
    poj 1265 Area(Pick定理)
    poj 2388 Who's in the Middle
    poj 3026 Borg Maze(bfs+prim)
    poj 2485 Highways
    变量引用的错误:UnboundLocalError: local variable 'range' referenced before assignment
    Sysbench硬件基准测试
    Sysbench-OLTP数据库测试
    字典
    操作列表
  • 原文地址:https://www.cnblogs.com/NanShengBlogs/p/11481129.html
Copyright © 2020-2023  润新知