AutoCAD2013 以上利用AccoreConsole+ c# NetApi Windows Froms 封装
1# 封装类
public static class CmdHelper
{
/// <summary>
/// 调用AutoCAD 安装目录下的AccoreConsole.exe来实现批量处理图纸(Net Api dll)
/// </summary>
/// <param name="cmsStr">NetApi中注册的命令(commandmethod中写的命令)</param>
/// <param name="dllPath">AutoCAD Net Api的程序集的全路径</param>
/// <param name="dwgfn">dwg文件的全路径</param>
/// <param name="AccoreconsolePath">AutoCAD 安装目录下的AccoreConsole.exe的全路径</param>
public static void ExecuteCmd(string cmsStr,string dllPath,string dwgfn)
{
var acadInstallPath = string.Empty;
for (int i = ; i < DateTime.Now.Year + ; i++)
{
if (File.Exists(@"C:\Program Files\Autodesk\AutoCAD " + i + "\\Accoreconsole.exe"))
{
acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + i + "\\";
break;
}
}
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo()
{
FileName = "cmd.exe",
UseShellExecute=false,
CreateNoWindow=true,
RedirectStandardInput=true,
RedirectStandardOutput=true
};
Process pro = new Process() { StartInfo = psi };
pro.Start();
pro.StandardInput.WriteLine("\"" + acadInstallPath + "Accoreconsole.exe" + "\" /i " + "\"" + dwgfn + "\"");
pro.StandardInput.WriteLine("secureload");
pro.StandardInput.WriteLine("");
pro.StandardInput.WriteLine("netload");
pro.StandardInput.WriteLine("\""+ dllPath + "\"");
pro.StandardInput.WriteLine("filedia");
pro.StandardInput.WriteLine("");
pro.StandardInput.WriteLine(cmsStr);
pro.StandardInput.WriteLine("qsave");
pro.StandardInput.WriteLine("quit");
pro.StandardInput.WriteLine("exit");
}
/// <summary>
/// 调用AutoCAD 安装目录下的AccoreConsole.exe来实现批量处理图纸(scr 文件)
/// </summary>
/// <param name="scrFileName">scr 文件的全路径</param>
/// <param name="dwgfn">dwg文件的全路径</param>
/// <param name="AccoreconsolePath">AutoCAD 安装目录下的AccoreConsole.exe的全路径</param>
public static void ExecuteCmd(string scrFileName, string dwgfn)
{
var acadInstallPath = string.Empty;
for (int i = ; i < DateTime.Now.Year + ; i++)
{
if (File.Exists(@"C:\Program Files\Autodesk\AutoCAD " + i + "\\Accoreconsole.exe"))
{
acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + i + "\\";
break;
}
}
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo()
{
FileName = acadInstallPath+"Accoreconsole.exe",
Arguments = " /i " + "\"" + dwgfn + "\" /s "+"\"" + scrFileName + "\"",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
Process pro = new Process() { StartInfo = psi };
pro.Start();
pro.StandardInput.WriteLine("filedia");
pro.StandardInput.WriteLine("");
pro.StandardInput.WriteLine("exit");
} public static List<String> GetOutNetApiCmd(string dllPath)
{
List<String> strCmd = new List<string>();
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Assembly ass = Assembly.LoadFrom(dllPath);
foreach (var t in ass.GetTypes())
{
if (t.IsClass && t.IsPublic)
{
foreach (MethodInfo m in t.GetMethods())
{
if (m.IsPublic && m.GetCustomAttributes(true)!=null)
{
Attribute att = null;
foreach (var item in m.GetCustomAttributes(true))
{
if (item.GetType().Name == "CommandMethodAttribute") att = item as Attribute;
}
if (att!=null) strCmd.Add(m.CustomAttributes.ToList()[].ConstructorArguments[].Value.ToString());
}
}
}
}
return strCmd;
} private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AssemblyName assName = new AssemblyName(args.Name);
var acadInstallPath = string.Empty;
for (int i = ; i < DateTime.Now.Year+; i++)
{
if (File.Exists(@"C:\Program Files\Autodesk\AutoCAD "+i+"\\Accoreconsole.exe"))
{
acadInstallPath = @"C:\Program Files\Autodesk\AutoCAD " + i + "\\";
break;
}
}
return Assembly.LoadFile(acadInstallPath + args.Name+".dll");
}
}
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 = ; i < DateTime.Now.Year + ; 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 + +"中的任意一个版本, 无法启动本程序!");
}
4# 绘制用户界面

5# 写完程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace MyAccoreConsole
{
public partial class Form1 : Form
{
public List<string> ListDwgFileNames { get; set; }
public string SelectedDllName { get; set; }
public string SelectedScrName { get; set; }
public string SelectedCmdName { get; set; }
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
this.listBox1.Items.Clear();
this.ListDwgFileNames = new List<string>();
OpenFileDialog ofd = new OpenFileDialog() {
Filter="AutoCAD DwgFile *.dwg|*.dwg",
Multiselect=true,
ReadOnlyChecked=true
};
if (ofd.ShowDialog()== DialogResult.OK) this.ListDwgFileNames.AddRange(ofd.FileNames);
foreach (var item in this.ListDwgFileNames) this.listBox1.Items.Add(item);
} private void button3_Click(object sender, EventArgs e)
{
this.label2.Text = string.Empty;
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "AutoCAD NetApi Dll *.dll|*.dll",
Multiselect = false
};
if (ofd.ShowDialog() == DialogResult.OK) this.SelectedDllName = ofd.FileName;
this.label2.Text = this.SelectedDllName;
} private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = string.Empty;
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "AutoCAD Scr File *.scr|*.scr",
Multiselect = false
};
if (ofd.ShowDialog() == DialogResult.OK) this.SelectedScrName = ofd.FileName;
this.label1.Text = this.SelectedScrName;
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
this.comboBox1.Items.Clear();
var cms = CmdHelper.GetOutNetApiCmd(this.SelectedDllName);
if (cms.Count>) this.comboBox1.Items.AddRange(cms.ToArray());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.SelectedCmdName = this.comboBox1.Text;
}
private void button5_Click(object sender, EventArgs e)
{
foreach (var item in this.ListDwgFileNames)
{
CmdHelper.ExecuteCmd(this.SelectedCmdName, this.SelectedDllName,item);
}
MessageBox.Show("Net Api 执行完成!!!!");
} private void button4_Click(object sender, EventArgs e)
{
foreach (var item in this.ListDwgFileNames)
{
CmdHelper.ExecuteCmd(this.SelectedScrName,item);
}
MessageBox.Show("SCR 批处理文件执行完成!!!!");
}
}
}
AutoCAD2013 以上利用AccoreConsole+ c# NetApi Windows Froms 封装的更多相关文章
- AutoCAD2013 以上利用AccoreConsole+ c# NetApi 批量处理图纸
AccoreConsole听起来有点拗口,其中文名可以叫做AutoCAD控制台或者无头AutoCAD.一句话概括,它是快速启动AutoCAD运行微环境,高效的处理图纸.你可以如同DOS命令行一样操作命 ...
- AUTOCAD2013 以上利用ACCORECONSOLE+ SCR后台批量清理图纸
无意中浏览到南胜大神的博客,https://www.cnblogs.com/NanShengBlogs/p/10957489.html 受此启发,特意改装此方法用于批量清理图纸,效果极佳. 详细介绍详 ...
- 利用SSH协议在Windows下使用PuTTY连接Ubuntu
利用SSH协议在Windows下使用PuTTY连接Ubuntu Ubuntu部分 首先我们要为Ubuntu配置一下环境,让它支持ssh服务,我们要做的其实也很简单,就一下两步: 安装OpenSSH软件 ...
- frp服务利用云主机实现Windows远程连接
frp服务利用云主机实现Windows远程连接 1.下载所需要的安装包 https://github.com/fatedier/frp/releases 下载 frp_0.44.0_linux_amd ...
- Windows 7 封装篇(一)【母盘定制】[手动制作]定制合适的系统母盘
Windows 7 封装篇(一)[母盘定制][手动制作]定制合适的系统母盘 http://www.win10u.com/article/html/10.html Windows 7 封装篇(一)[母盘 ...
- Windows 7 封装与定制不完全教程
Windows 7 封装与定制不完全教程 从定制Win7母盘到封装详细教程 手把手教你定制WIN7小母盘 Windows 7 封装与定制不完全教程 [教程] Windows 7 封装与定制不完全教程( ...
- Windows系统封装教程
Windows系统封装教程
- 如何定位Release 版本中程序崩溃的位置 ---利用map文件 拦截windows崩溃函数
1 案例描述 作为Windows程序员,平时最担心见到的事情可能就是程序发生了崩溃(异常),这时Windows会提示该程序执行了非法操作,即将关闭.请与您的供应商联系.呵呵,这句微软的“名 ...
- 利用cwRsync客户端将Windows下文件同步到Linux
这里不描述Linux服务端安装配置rsync服务的过程,有需要可以在网络上查找相关教程. 1.安装cwRsync客户端下载地址:http://itefix.no/cwrsync/下载文件cwRsync ...
随机推荐
- git status: HEAD detached from origin/master问题的解决
问题:执行git status,提示: HEAD detached from origin/master 原因:分支选错了,后续的提交都提交到了一个匿名分支之上,整个状态是游离了的 解决方法: 1.查 ...
- R语言:载入rjava(xlsx)包报错
先安装JRE,在电脑中添加环境变量: 电脑-右键-属性-高级系统设置-环境变量-用户变量下新建:变量名:JAVA-HOME,变量值:JRE安装路径(到jre1.8***这个文件夹就行了) 系统变量下找 ...
- PHP ajax 实现三级联动
在一个单独JS页面中,利用ajax实现三级联动,做成一个三级联动形式,以便于以后随时调用 JS代码: $(document).ready(function(e) { $("#sanji&qu ...
- [MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 环境依然和原来一样. ...
- 性能测试 | 理解单线程的Redis为何那么快?
前言 Redis是一种基于键值对(Key-Value)的NoSQL数据库,Redis的Value可以由String,hash,list,set,zset,Bitmaps,HyperLogLog等多种数 ...
- 约束布局ConstraintLayout详解
约束布局ConstraintLayout详解 转 https://www.jianshu.com/p/17ec9bd6ca8a 目录 1.介绍 2.为什么要用ConstraintLayout 3.如何 ...
- input 表单
1:datalist元素,一般与input组建配合使用,以定义可能输入的值,例如: <!DOCTYPE html> <html lang="en"> < ...
- [转]Android使用WebView定位问题
文章转自:https://www.jianshu.com/p/d32d3641741f 最近遇到了一个问题,有一个需求是使用 WebView 来加载一个网页url,H5通过js来获取位置定位信息.以前 ...
- [转]html里a标签中href调用js的几种方法
文章转自:https://blog.csdn.net/best_luxi/article/details/45062301 我们常用的在a标签中有点击事件: 1. a href=”javascript ...
- SSM整合junit单元测试之org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
想用SSM做一点小测试,项目整合完毕,直接使用junit测试mybatis,出现如下错误(SuperTest类中进行了spring运行环境加载): 解决思路: 检查mapper接口与mapper.xm ...