C#打开CAD,并执行命令
//AcadService.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace AcadService { public class GApplication { /// <summary> /// AutoCAD.Application对应的类型 /// </summary> System.Type type = null; /// <summary> /// AutoCAD.Application对应的对象 /// </summary> Object obj = null; /// <summary> /// 创建AutoCAD.Application对象 /// </summary> /// <returns></returns> public bool CreateInstance() { if (obj != null) { //已经创建了不能再创建了 return false; } type = System.Type.GetTypeFromProgID("AutoCAD.Application"); if (type == null) { //寻找AutoCAD.Application类型失败,可能没有安装AutoCAD return false; } try { obj = System.Activator.CreateInstance(type); return true; } catch { return false; } } /// <summary> /// 是否可见 /// </summary> public bool Visible { get { if (obj == null || type == null) return false; return (bool)type.InvokeMember("Visible", BindingFlags.GetProperty, null, obj, null); } set { if (obj == null || type == null) return; type.InvokeMember("Visible", BindingFlags.SetProperty, null, obj, new object[] {value }); } } /// <summary> /// 版本号 /// </summary> public string Version { get { if (obj == null || type == null) return null; return (string)type.InvokeMember("Version", BindingFlags.GetProperty, null, obj, null); } } /// <summary> /// 标题 /// </summary> public string Caption { get { if (obj == null || type == null) return null; return (string)type.InvokeMember("Caption", BindingFlags.GetProperty, null, obj, null); } } /// <summary> /// 执行程序acad.exe全路径 /// </summary> public string FullName { get { if (obj == null || type == null) return null; return (string)type.InvokeMember("FullName", BindingFlags.GetProperty, null, obj, null); } } /// <summary> /// 加载ARX 文件 /// </summary> /// <param name="Name"></param> public void LoadArx(string Name) { if (obj == null || type == null) return; type.InvokeMember("LoadArx",BindingFlags.InvokeMethod,null,obj,new object[]{Name}); } /// <summary> /// 退出 /// </summary> public void Quit() { if (obj == null || type == null) return; type.InvokeMember("Quit", BindingFlags.InvokeMethod, null, obj, null); } /// <summary> /// 更新 /// </summary> public void Update() { if (obj == null || type == null) return; type.InvokeMember("Update", BindingFlags.InvokeMethod, null, obj, null); } /// <summary> /// 缩放到所有 /// </summary> public void ZoomAll() { if (obj == null || type == null) return; type.InvokeMember("ZoomAll", BindingFlags.InvokeMethod, null, obj, null); } /// <summary> /// 缩放到范围 /// </summary> public void ZoomExtents() { if (obj == null || type == null) return; type.InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, obj, null); } /// <summary> /// 活动文档 /// </summary> public object ActiveDocument { get { if (obj == null || type == null) return null; return type.InvokeMember("ActiveDocument", BindingFlags.GetProperty, null, obj, null); } } } public class GDocument { private object obj = null; public GDocument(Object _obj) { obj = _obj; } /// <summary> /// 发送命令 /// </summary> /// <param name="Command"></param> public void SendCommand(string Command) { if (obj == null) return; obj.GetType().InvokeMember("SendCommand", BindingFlags.InvokeMethod, null, obj, new object[] { Command }); } /// <summary> /// 关闭文档 /// </summary> /// <param name="SaveChanges"></param> /// <param name="FileName"></param> public void Close(object SaveChanges, object FileName) { if (obj == null) return; obj.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, obj, new object[] { SaveChanges, FileName }); } } } //调用 GApplication app = new GApplication(); if (!app.CreateInstance()) { return; } app.Visible = true; GDocument doc = new GDocument(app.ActiveDocument); doc.SendCommand("XX命令 "); doc.Close(false, null); app.Quit();查看完整版本: C#打开CAD,并执行命令
Tags: AutoCAD.Application