步骤一:
新建 C# 类库项目 HelloArx
![](https://i-blog.csdnimg.cn/blog_migrate/5ce3bee7cde03a63fc033b657036d395.png)
找到CAD2008的安装目录,添加acdbmgd.dll和acmgd.dll的引用
![](https://i-blog.csdnimg.cn/blog_migrate/229332b8f5ef753669edf2ddf9aa0ff3.png)
修改Class1.cs如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
namespace HelloArx
{
public class Class1
{
// 加载实体到数据库
public static ObjectId AppendEntity(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true );
trans.Commit();
}
return entId;
}
// 由两点创建直线
public static ObjectId AddLine(Point3d startPt, Point3d endPt)
{
Line ent = new Line(startPt, endPt);
ObjectId entId = AppendEntity(ent);
return entId;
}
}
}
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
namespace HelloArx
{
public class Class1
{
// 加载实体到数据库
public static ObjectId AppendEntity(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true );
trans.Commit();
}
return entId;
}
// 由两点创建直线
public static ObjectId AddLine(Point3d startPt, Point3d endPt)
{
Line ent = new Line(startPt, endPt);
ObjectId entId = AppendEntity(ent);
return entId;
}
}
}
步骤三:
新建一个类NewCmd用来建立Cad命令
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using
System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass( typeof (HelloArx.NewCmd))]
namespace HelloArx
{
class NewCmd
{
// 新建一个命令
[CommandMethod( " test " )]
public void Test()
{
Point3d ptSt = new Point3d( 0 , 0 , 0 );
Point3d ptEnd = new Point3d( 10 , 20 , 54 );
Class1.AddLine(ptSt, ptEnd);
}
}
}
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass( typeof (HelloArx.NewCmd))]
namespace HelloArx
{
class NewCmd
{
// 新建一个命令
[CommandMethod( " test " )]
public void Test()
{
Point3d ptSt = new Point3d( 0 , 0 , 0 );
Point3d ptEnd = new Point3d( 10 , 20 , 54 );
Class1.AddLine(ptSt, ptEnd);
}
}
}
步骤四:
右击项目->属性->调试->启动外部程序输入cad的路径
比如F:\Program Files\AutoCAD 2008\acad.exe
Shift+Ctrl+B编译之后。生成HelloArx.dll
打开Cad2008输入命令netload找到HelloArx.dll然后输入命令test
可以看到效果。
![](https://i-blog.csdnimg.cn/blog_migrate/214b41e7dcb16f30785dfc503ab24faa.png)
如果看不到,就输入zoom e查看
本文源码:http://files.cnblogs.com/greatverve/csharp-cad.rar