[ARX]利用AutoCAD 2005.NET API进行AutoCAD的二次开发(一)
利用 2005.NET API进行AutoCAD的二次开发(一) --直线 大家好!从今天开始我向大家介绍如何利用AutoCAD 2005.NET API进行AutoCAD的二次开发。由于我只学过C#,因此在所讲的例子中一律使用C#。至于广大的vb.net程序员,我想编程的原理是一样的,并且因为vb.net和C#都属于.net语言,因此熟悉vb.net的朋友对理解C#程序是没有太大问题的(呵呵,如果有哪位愿意把相关的C#程序翻译成Vb.net程序,那就更好了)。 由于AutoCAD 2005.NET API是比较新的东西,我几乎没有找到比较系统的或参考,因此我会使用乐筑天下出版的《AutoCAD VBA 开发精彩实例教程》的实例来进行讲解。 好了,闲话少说,让我们直接进入 AutoCAD 2005.NET API的世界。 一、准备工作 这指的是如何添加相关的引用和命名空间。用AutoCAD 2005.NET API进行AutoCAD的二次开发,必须添加的引用是acdbmgd.dll和acmgd.dll,这可以在AutoCAD2005的安装目录下找到。还必须添加的是在visual studio.net的添加引用时在com选项下选择“AutoCAD 2005类型库”和“AutoCAD/ObjectDBX Common 16.0类型库”这两项。 然后在程序的开头加入以下命名空间: using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Colors; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using DBTransMan=Autodesk.AutoCAD.DatabaseServices.TransactionManager; using Autodesk.AutoCAD.Interop; using Autodesk.AutoCAD.Interop.Common; 这些命名空间是要在每一个程序中都要加入的。 二、第一个实例--创建直线 (1)在visual studio.net 中,新建一个类库工程,然后添加相关的引用及在程序的开头加入有关的命名空间(请参见一)。 (2)创建基准函数(AddLine)。基准函数就是与系统提供的方法一致的函数。其实现的步骤如下:l 在程序文件中加入以下私有字段: private Database db; //代表AutoCAD数据库对象 private DBTransMan tm; //代表AutoCAD事务处理管理器 private Transaction myT;// 代表AutoCAD事务处理 BlockTable bt; //代表AutoCAD块表 BlockTableRecord btr;// 代表AutoCAD块表记录对象l 在类的构造函数部分加入以下代码: db = HostApplicationServices.WorkingDatabase;//取得当前运行的AutoCAD的数据库对象 tm = db.TransactionManager;// 定义一事务处理管理器l 主程序AddLine public Line AddLine(Point3d pt1, Point3d pt2) { Line line;//声明一直线对象 try { Initialize();//相关的初始化工作,是一自定义函数 line = new Line(pt1, pt2);//给line对象赋于具体的值 btr.AppendEntity(line); tm.AddNewlyCreatedDBObject(line, true);//在AutoCAD中加入创建的直线 myT.Commit();//提交事务处理 } finally { DisposeAll();//有关的清理工作,是一自定义函数 } return line; }上面程序中的Initialize函数如下: public void Initialize() { myT = tm.StartTransaction();//开启事务处理bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false); //获取AutoCAD块表对象btr = (BlockTableRecord)tm.GetObject(bt, OpenMode.ForWrite, false); //获取AutoCAD块表记录对象 }DisposeAll函数如下:public void DisposeAll() { bt.Close();//关闭块表 btr.Close();//关闭块表记录 myT.Dispose();//销毁事务处理 }如果大家对上面的东西不是很清楚的话,也没有关系,因为你只要知道这是前期的准备工作,以后只要调用相关的函数就可以了。 (6)最后添加测试代码。请按照步骤(1)创建一新的类库工程,然后加入以下代码(请把上面的工程编译,然后在新的类库工程中添加该dll):
#region Using directives
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using ZHFArxLibrary;//引用刚编译好的dll所代表的命名空间
#endregion
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
//声明AutoCAD .NET命令
public static void AddLine()
{
Arx arx = new Arx();//Arx表示上面编译的dll中的类
Point3d ptSt = new Point3d(100, 100, 0);
Point3d ptEn = new Point3d(150, 100, 0);
arx.AddLine(ptSt, ptEn);
arx.AddLine(new Point2d(100, 120), new Point2d(150, 120));
arx.AddLine(ptSt, 50, 50);
arx.AddLineR(ptSt, 3, 50);
}
}
}
(7)运行程序。
把(6)中的工程编译。然后启动AutoCAD,在命令行中键入netload命令,在弹出的对话框中选择刚编译好的dll文件。在命令行中键入AddLine(就是你在(6)中声明的AutoCAD .NET命令),应该可以看到结果了。
(8)完整的程序代码请看下面的附件。
Arx.rar 老大,能否排版下代码?
比如加个背景什么的 (3)根据二维的起点和终点创建直线。 public Line AddLine(Point2d pt1, Point2d pt2) { Line line; line = AddLine(new Point3d(pt1.X, pt1.Y, 0), new Point3d(pt2.X, pt2.Y, 0)); //调用上面创建的基准函数 return line; }(4)已知起点和终点相对于起点的直角坐标,创建直线。public Line AddLine(Point3d pt1, double x, double y) { Line line; Point3d pt2 = new Point3d(); pt2.X = pt1.X + x; pt2.Y = pt1.Y + y; pt2.Z = pt1.Z; line = AddLine(pt1, pt2); return line; }(5) 已知起点和终点相对于起点的极坐标,创建直线。public Line AddLineR(Point3d pt1, double angle, double length) { Line line; Point3d pt2 = new Point3d(); pt2 = GetPointAR(pt1, angle, length); line = AddLine(pt1, pt2); return line; }其中,GetPointAR是已知一点和另一点相对于该点的极坐标,求另上点的绝对坐标。public Point3d GetPointAR(Point3d pt1,double angle,double length) { Point3d pt2=new Point3d(); pt2.X = pt1.X + length * Math.Cos(angle); pt2.Y = pt1.Y + length * Math.Sin(angle); pt2.Z = pt1.Z; return pt2;
} http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627
AutoCAD 2005 Expanded .NET C# Code Sample 请问楼主,可以按照你所示范的,在CAD2004下,做相应的程序吗
嗯,他们不兼容吧。2005带有激活,使用起来并不方便
谢谢! 假若不用类库怎么做,我想直接做一个运用程序,我的环境.net2003,cad2005) delphi 创建CAD OLE对象开发 delphi 创建CAD OLE对象开发 ??
非常非常的垃圾!
delphi的兼容性太差,D6导入cad的类型库居然好要报错,而且对很多属性,对象,方法都进行了改名,增加二次学习的难度,比起vb来,差得远! 但是不管是VB6,还是VB.net,C#如果采用ole的方式二次开发cad都存在效率不高,不能够自定义实体的问题,最好的还是C++,ARX,但是要学习ARX无疑要去追随一个注定要没落的MFC,于是就有了我们都一批眼巴巴的等待.net开发ARX的可怜的人,
.net开发ARX的资料,你究竟在哪里?
页:
[1]
2