赞
踩
2021-08-28
对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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。