以下将列举几个简单的二、三维开发的小例子:
//定位
public void Zoom()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Point2d pt1 = new Point2d(0, 0);
Point2d pt2 = new Point2d(2, 2);
string cmd = string.Format("Zoom {0},{1} {2},{3} ", pt1.X, pt1.Y, pt2.X, pt2.Y);
doc.SendStringToExecute(cmd, true, false, false);
}
//画线、文字、圆
private void DrawLine_Click(object sender, EventArgs e)
{
//这句话和m_DocumentLock.Dispose();不加有时候会报错
DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Point3d startPoint = new Point3d(0, 0, 0); //直线起点
Point3d endPoint = new Point3d(300, 300,300); //直线终点
Line line;// = new Line(startPoint, endPoint); //新建一直线对象
//DBText acText;//新建文字对象
//Circle circle; //圆
BlockTableRecord btr;//要加入直线,我们必须打开模型空间
BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它
//我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
Transaction trans;
//使用 TransactionManager 的 StartTransaction()成员来开始事务处理
trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
//现在创建直线
line = new Line(startPoint, endPoint);
//acText = new DBText();
//acText.Position = new Point3d(0, 0, 0);
//acText.Height = 50;
//acText.TextString = "Test show text";
//circle = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 50);
bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
//使用当前的空间 Id 来获取块表记录——注意我们是打开它用来写入
btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
//现在使用 btr 对象来加入直线
btr.AppendEntity(line);
trans.AddNewlyCreatedDBObject(line, true); //并确定事务处理知道要加入直线!
//一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
trans.Commit();
//…然后销毁事务处理,因为我们已经完成了相关的操作(事务处理不是数据库驻留对象,可以销毁)
trans.Dispose();
m_DocumentLock.Dispose();
}
三维图形实例
private void button4_Click(object sender, EventArgs e)
{
DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Solid3d t = Torus(200,30);
Move(t,new Point3d(100,100,0));
ToModelSpace(t);
Solid3d b = Box(100,300,100);
Move(b, new Point3d(-300,-300,0));
ToModelSpace(b);
m_DocumentLock.Dispose();
}
///
/// 添加实体到模型空间
///
/// 要添加的实体
/// 实体ObjectId
private ObjectId ToModelSpace(Entity entity) {
Database db = HostApplicationServices.WorkingDatabase;
ObjectId id;
using(Transaction trans=db.TransactionManager.StartTransaction()){
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
id = modelSpace.AppendEntity(entity);
trans.AddNewlyCreatedDBObject(entity,true);
trans.Commit();
trans.Dispose();
}
return id;
}
///
/// 创建圆环体
///
/// 主圆半径
/// 圆环半径
/// 圆环体
private Solid3d Torus(double majorRadius,int minorRadius) {
Solid3d solid = new Solid3d();
solid.CreateTorus(majorRadius,minorRadius);
return solid;
}
///
/// 创建长方体
///
/// 长
/// 宽
/// 高
///
private Solid3d Box(double x,double y, double z) {
Solid3d solid = new Solid3d();
solid.CreateBox(x,y,z);
return solid;
}
///
/// 以原点为基点,指定目标点移动实体
///
/// 实体对象
/// 目标点
private void Move(Entity entity,Point3d point) {
Matrix3d matrix = Matrix3d.Displacement(point-new Point3d());
entity.TransformBy(matrix);
}
选择图形、删除图形
//选择图形
private void chooseObj_Click(object sender, EventArgs e)
{
DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
PromptEntityOptions pEntityOptions = new PromptEntityOptions("Select a Entity");
PromptEntityResult pEntityResult;
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
pEntityResult = ed.GetEntity(pEntityOptions);
if (pEntityResult.Status != PromptStatus.OK)
{
ed.WriteMessage("没有选中!请重新选择!");
}
else
{
ed.WriteMessage("你选择的图形ID是: " + pEntityResult.ObjectId);
Entity entity = (Entity)trans.GetObject(pEntityResult.ObjectId, OpenMode.ForRead, true);
if (!entityList.Equals(entity)) { entityList.Add(entity); }
if (ids.IndexOf(pEntityResult.ObjectId) == -1)
{
this.textBox1.Text += pEntityResult.ObjectId + ",";
ids.Add(pEntityResult.ObjectId);
}
else { ed.WriteMessage("已选择该图形!"); }
}
trans.Commit();
trans.Dispose();
m_DocumentLock.Dispose();
}
//删除选中图形
private void button1_Click(object sender, EventArgs e)
{
DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
//Database db = null;
foreach (ObjectId id in ids)
{
Entity entity = (Entity)trans.GetObject(id, OpenMode.ForWrite, true);
if (entity == null || entity.IsErased == true || entity is ProxyEntity)
{
continue;
}
entity.Erase(true);
}
trans.Commit();
trans.Dispose();
this.textBox1.Text = "";
ids.Clear();
entityList.Clear();
m_DocumentLock.Dispose();
}
在开发过程和本篇文章中参考了《[深入浅出AutoCAD.NET二次开发]》李冠亿著这本书,讲的很详细,很适合入门,大家如果有兴趣可以找来看看。