对AutoCAD进行二次开发可以使用:ObjectArx,VBA,VLisp。但在这里不借用它们,而是直接使用C#开发。
有类库和应用程序两种方式:
方法1:vs2010 开发AutoCAD 2008 类库
建立动态库,从AutoCAD命令行使用NETLOAD调入,然后执行其方法
一 创建项目
1,建一个wxindows窗体程序“项目”,设置输出为“类库”
2,添加引用--浏览--从AutoCAD2008的安装目录C:\Program Files\Autodesk\MDT 2008下,找到引用acdbmgd.dll和acmgd.dll
3,引用如下命名空间
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
4,方法名前,加特性 CommandMethod
5,完整代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
namespace ClassLibrary2
{
public class Class1
{
[CommandMethod("HelloWorld")]
public void HelloWorld()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("HelloWorld CAD!");
}
}
}
二 工程的目标框架框架版本
在 vs2010 中 开发,默认的版本是.NET Framework 4.0版本高,对于引用AutoCAD 2008,讲无法编译
在工程的属性中,目标框架是 改为 .NET Framework 2.0 或者.NET Framework 3.5 就可以了
三 调试的方法设置
vs2010生成 AutoCAD2008 类库调试方法
属性中 --调试-- 外部启动程序:
C:\Program Files\Autodesk\MDT 2008\acad.exe
四 启动AutoCAD 2008
1 在vs2010的开发环境,编译链接完成后,按F5键启动调试,等待自动启动AutoCAD 2008完成后,
2 在命令行输入:NETLOAD,弹出装入类库的对话框,
浏览找到刚编译形成的类库,ClassLibrary2\ClassLibrary2\bin\Debug\ClassLibrary2.dll
3 在命令行输入: HelloWorld,
将会提示是:未知的命令,原因是acdbmgd.dll acmgd.dll版本过高所致
五 重新引入较低版本的动态库
AutoCAD 2008 自带的动态库 acdbmgd.dll acmgd.dll
版本 17.1.0.0,运行时版本 v2.0.50727
版本高 ,在vs2010工程中引用后,虽可以生成的动态库,但在AutoCAD中NETLOAD可以装入,但执行其中的方法,提示是未知的命令,有两种方法:
1 引用AutoCAD的安装目录下的acdbmgd.dll 和acmgd.dll的版本是 17.1.0.0,从其属性中
将“复制本地” 改为 “False”,这样形成的dll 的方法,在AutoCAD中就可以认识,不再是未知的命令
2 可以到网上下载较低的版本,比如我下载如下的版本,添加引用它们就可以了
版本 16.2.54.0
运行时版本 v1.0.3705
===================================
方法2:c#建立一个操AutoCAD2008的应用程序
一 首先建立一个 基于WIndowsFormApplicaton的项目
二 类型库的添加引用
右击项目的“引用”--“添加引用”--从“COM”页,找到以下两个类型库
1 AutoCAD 2008 Type Library
引用名称 ----- 对应的动态库
AutoCAD ----- Autodesk.AutoCAD.Interop.dll 嵌入互操作类型 False
2 Autodesk AutoCAD Mechanical 1.0 Type Library
引用名称 ----- 对应的动态库
AcadmAuto ----- Interop.AcadmAuto.dll,嵌入互操作类型 False
AXDBLib ----- Autodesk.AutoCAD.Interop.Common.dll
GEAuto ----- Interop.GEAuto.dll
三 主要的操作函数
1 使用的文件中加入语句
using AutoCAD = Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using dbx = Autodesk.AutoCAD.Interop.Common;
2 注操作代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms;
using AutoCAD = Autodesk.AutoCAD.Interop;
using dbx = Autodesk.AutoCAD.Interop.Common;
using SmartSoft.ACAD;
namespace aotuCADwinFrm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AutoCADConnector acd=new AutoCADConnector();//生成操作类对象
dbx.AxDbDocument doc_as = acd.GetThisDrawing("c:\\doc_as.dwg", "");
dbx.AxDbDocument acddoc = acd.GetThisDrawing("c:\\D1.dwg", "");//打开图形文件
// dbx.AcadBlockReference brf = acd.GetBlockReference(acddoc,"pp");
acd.GetEntityReference(acddoc, doc_as);//删除不需要的实体
acd.Dispose();
}
}
}
3 使用的改造自网上的类
using System;
using AutoCAD = Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using dbx = Autodesk.AutoCAD.Interop.Common;
namespace SmartSoft.ACAD
{
///
/// 读取AutoCAD属性信息
///
public class AutoCADConnector : IDisposable
{
private AutoCAD.AcadApplication _Application;
private bool _Initialized;
private bool _Disposed;
private dbx.AxDbDocument doc_as;
#region 类初始化及析构操作
///
/// 类初始化,试图获取一个正在运行的AutoCAD实例,
/// 如果没有则新起动一个实例。
///
public AutoCADConnector()
{
try
{
//取得一个正在运行的AUTOCAD实例
this._Application = (AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");
}//end of try
catch
{
try
{
//建立一个新的AUTOCAD实例,并标识已经建立成功。
_Application = new AutoCAD.AcadApplicationClass();
_Initialized = true;
}
catch
{
throw new Exception("无法起动AutoCAD应用程序,确认已经安装");
}
}//end of catch
}//end of AutoCADConnector
~AutoCADConnector()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._Disposed && this._Initialized)
{
//如果建立了AUTOCAD的实列,调用QUIT方法以避免内存漏洞
this._Application.ActiveDocument.Close(false, "");
this._Application.Quit();
this._Disposed = true;
}
}
#endregion
#region 公共用户接口属性
///
/// 取得当前类所获得的AUTOCAD实例
///
public AutoCAD.AcadApplication Application
{
get
{
return _Application;
}
}//end of Application
#endregion
#region 公共用户接口方法
///
/// 根据给定的文件名以AxDbDocument类型返回该文档
///
public dbx.AxDbDocument GetThisDrawing(string FileName, string PassWord)
{
ACAD.AutoCADConnector Connector = new AutoCADConnector();
//这是AutoCAD2004的Programe ID
string programeID = "ObjectDBX.AxDbDocument.17";
AutoCAD.AcadApplication AcadApp = Connector.Application;
dbx.AxDbDocument dbxDoc;
dbxDoc = (dbx.AxDbDocument)AcadApp.GetInterfaceObject(programeID);
try
{
if (System.IO.File.Exists(FileName) == false) throw new Exception("文件不存在。");
dbxDoc.Open(FileName, PassWord);
}// end of try
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
dbxDoc = null;
}
return dbxDoc;
}//end of function GetThisDrawing
///
/// 根据当前文档和块名取得当前块的引用
///
public dbx.AcadBlockReference GetBlockReference(dbx.AxDbDocument thisDrawing, string blkName)
{
dbx.AcadBlockReference blkRef = null;
bool found = false;
string str="";
try
{
foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace)
{
str += entity.EntityName + ",";
if (entity.EntityName == "AcDbBlockReference")
{
blkRef = (dbx.AcadBlockReference)entity;
//System.Windows.Forms.MessageBox.Show(blkRef.Name);
if (blkRef.Name.ToLower() == blkName.ToLower())
{
found = true;
break;
}
}//end of entity.EntityName=="AcDbBlockReference"
}// end of foreach thisDrawing.ModelSpace
}//end of try
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message, "信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
thisDrawing = null;
}//end of catch
if (!found) blkRef = null;
return blkRef;
}//end of function GetBlockReference
///
/// 根据给定的块引用(dbx.AcadBlockReference)和属性名返回属性值
///
public object GetValueByAttributeName(dbx.AcadBlockReference blkRef, string AttributeName)
{
object[] Atts = (object[])blkRef.GetAttributes();
object attValue = null;
for (int i = 0; i < Atts.Length; i++)
{
dbx.AcadAttributeReference attRef;
attRef = (dbx.AcadAttributeReference)Atts[i];
if (attRef.TagString == AttributeName)
{
attValue = attRef.TextString;
break;
}
}//end of for i
return attValue;
}// end of function
//===
///
/// 根据当前文档和实体名取得当前块的引用
///
public dbx.AcadEntity GetEntityReference(dbx.AxDbDocument thisDrawing, dbx.AxDbDocument doc_as)//string entName)
{
dbx.AcadEntity entRef = null;
bool found = false;
string str = "",str2="";
try
{
foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace)
{
str += entity.EntityName + ",";
if (!(entity.TrueColor.ColorIndex == (Autodesk.AutoCAD.Interop.Common.AcColor)256 && entity.Layer.ToLower() == "tool_rou".ToLower()))
//if ( !(entity.Layer.ToLower() == "tool_rou".ToLower() && (entity.EntityName == "AcDbArc" || entity.EntityName == "AcDbCircle") ) )//"AcDbBlockReference")
{//不是我们需要的实体,删除
entRef = (dbx.AcadEntity)entity;
entRef.Delete();
}//end of entity.EntityName=="AcDbBlockReference"
}// end of foreach thisDrawing.ModelSpace
thisDrawing.SaveAs("c:\\ww.dwg", Type.Missing);
thisDrawing.DxfOut("c:\\dxf.dxf", Type.Missing, Type.Missing);
}//end of try
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message, "信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
thisDrawing = null;
}//end of catch
if (!found) entRef = null;
return entRef;
}//end of function GetBlockReference
#endregion
}//end of class CAutoCADConnector
}//end of namespace AutoCAD