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 Files\Autodesk\AutoCAD " + i + "\\Accoreconsole.exe"))
 16                 {
 17                     acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + 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 Files\Autodesk\AutoCAD " + i + "\\Accoreconsole.exe"))
 55                 {
 56                     acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + 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 Files\Autodesk\AutoCAD "+i+"\\Accoreconsole.exe")) 
109                 {
110                     acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + 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 Files\Autodesk\AutoCAD " + i + "\\Accoreconsole.exe")){acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + 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 }

转载于:https://www.cnblogs.com/NanShengBlogs/p/11481129.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/568900.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

广域网、局域网和城域网的理解

定义 局域网&#xff1a;Local Area Network&#xff1b;简称LAN&#xff0c;是一个可连接家庭、学校、企业等有限区域的计算机网络 城域网&#xff1a;Metropolitan Area Network&#xff1b;简称MAN&#xff0c;是一个城市范围内所建立的计算机通信网 广域网&#xff1a;Wid…

C# 的一些便捷用法

分割字符串 这样可以用一串字符串分割并且分为数组 string[] arr System.Text.RegularExpressions.Regex.Split(str, "\r\n"); dynamic 动态类型 List<dynamic> 可以自定义内容并绑定列 List<dynamic> dy new List<dynamic>(); dy.Add(new{Cus…

队列与栈结构的相同点与不同点

https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#xff01; 如有转载&#xff0c;请注明出处&#xff08;如不注明&#xff0c;盗者必究&#xf…

SQL Server 输出 XML

一、概述 SELECT 查询将结果作为行集返回。在 SQL 查询中指定 FOR XML 子句&#xff0c;从而将该查询的正式结果作为 XML 来检索。FOR XML 子句可以用在顶级查询和子查询中。顶级 FOR XML 子句只能用在 SELECT 语句中。而在子查询中&#xff0c;FOR XML 可以用在 INSERT、UPDAT…

linux重定向输出命令

目录一、符号二、“>”举例2.1举例一2.2举例二三、“>>”举例3.1举例一3.2举例二https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#x…

python面向对象基础之类与实例

https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#xff01; 如有转载&#xff0c;请注明出处&#xff08;如不注明&#xff0c;盗者必究&#xf…

微信小程序进度条

<button classdown-img bindtapdownImg data-scr{{imageUrl}}>下载图片{{progress}}</button> downImg: function (e) {var _this this;// 获取图片地址(http://www.playsort.cn/...)//var img e.currentTarget.dataset.src;//var img ../pic/U1513P28T52D3414F…

mysql10.3修改默认存储路径

版本为10.3的用该种方式修改&#xff1a; 创建/data/mysql目录 mkdir -p /data/mysql 给这个目录至少要附加读写权限 chmod 777 /data/mysql -r 把mariadb服务停掉 systemctl stop mariadb 把/var/lib/mysql整个目录复制到新路径下 cp -r /var/lib/mysql/* /data/mysql 编辑ma…

计算机硬件系统和软件系统

https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#xff01; 如有转载&#xff0c;请注明出处&#xff08;如不注明&#xff0c;盗者必究&#xf…

修改mysql锁空间大小

A. 单次生效设置 进入mysql命令 mysql -uroot -p show variables like "%_buffer%";(不要忘记带;号&#xff0c;没有;号表示一条语句没有结束) 默认的innodb_buffer_pool_size8M tmp_table_size 4G max_heap_table_size 256M innodb_file_format Barracuda defa…

TypeError: ‘NoneType‘ object is not callable--python报错解决办法

报错为 翻译过来为&#xff1a;TypeError:“NoneType”对象不可调用 代码&#xff1a; def bibao_one():Jay "哎哟不错哦"def bibao_two():print(Jay)return bibao_two()#执行闭包 execute bibao_one()execute()我们在执行调用函数的时候&#xff0c;把括号去掉就…

洛谷 P2384 最短路题解

题目背景 狗哥做烂了最短路&#xff0c;突然机智的考了Bosh一道&#xff0c;没想到把Bosh考住了...你能帮Bosh解决吗&#xff1f; 他会给你100000000000000000000000000000000000%10金币w 题目描述 给定n个点的带权有向图&#xff0c;求从1到n的路径中边权之积最小的简单路径。…

IndentationError: expected an indented block --python报错 or IndentationError:unexpected indent

https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#xff01; 如有转载&#xff0c;请注明出处&#xff08;如不注明&#xff0c;盗者必究&#xf…

python闭包与装饰器的代码解释

代码&#xff1a; https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注&#xff01; 欢迎关注微信公众号&#xff1a;宝藏女孩的成长日记 让这个可爱的宝藏女孩在努力的道路上与你一起同行&#xff01; 如有转载&#xff0c;请注明出处&#xff08;如不注明&#xff0c…

ssh 双机互信

ssh主机互信主机101.9.101.13cd /root/.ssh生成公钥私钥/usr/bin/ssh-keygen -t rsa公钥&#xff1a;id_rsa.pub 私钥&#xff1a;id_rsa把主机上生成的公钥发送给备机 用scp 不是默认端口 -P 指定端口scp -P 6802 ./id_rsa.pub root10.9.10.11:/root/.ssh/备机101.9.101.11cd …

软件测试管理工具禅道开源版下载安装

下载 下载地址&#xff1a;https://www.zentao.net/ 点击下图的开源版 根据自己的电脑配置选择相应的安装包&#xff0c;例如我的电脑是windows 64位的&#xff0c;下载中文版&#xff0c;就如下图了 安装 根据你的下载路径&#xff0c;找到并点击你下载的.exe文件&#…

关于ptype_all和pypte_base中的pt_prev的说明[转]

不知道原帖&#xff0c;我是从这里看到了&#xff0c;解决了迷惑我很久的疑问&#xff0c;抄过来。 看见noble_shi兄弟"关于net_rx_action函数的若干问题"贴中关于pt_prev的问题&#xff0c; 本来想在论坛上找到一个相关的帖子的链接告诉他。但是发现咱们论坛上关于p…

禅道的基本使用(创建项目、维护部门、用户、产品、提出需求、创建测试用例等)

禅道的基本使用一、创建项目二、创建维护部门三、添加用户四、创建产品五、提出需求六、创建测试用例禅道作为一个缺陷的管理工具&#xff0c;对于测试者来说其必不可少&#xff0c;下面将介绍禅道的基本使用 一、创建项目 登录禅道&#xff0c;点击项目&#xff0c;创建一个…

C# 延迟初始化 LazyT

概念&#xff1a;延时初始化重点是延时&#xff0c;用时加载&#xff0c;意思是对象在使用的时候创建而不是在实例化的的时候才创建。延时加载主要应用的场景&#xff1a;数据层&#xff08;ADO.NET或Entity Framework等ORM&#xff0c;Java里面的Hibernate也用到了这种技术&am…

简述get 和 post 的主要区别——计算机网络

get是从服务器获取数据&#xff0c;post是向服务器传输数据post相比get更加安全。使用get&#xff0c;在传输过程中&#xff0c;数据被放在URL中&#xff0c;而post对于所有用户来说都是不可见的。受URL的控制&#xff0c;get方式提交的数据最多只能有1024字节&#xff0c;而po…