课程目标:本节课教大家如何通过二次开发接口添加2个命令,一个命令向AutoCAD模型空间中添加直线,另一个命令删除直线
第一步:按照第一课的内容创建一个名叫"添加和删除直线"的新项目
第二步:填写添加直线的命令
[CommandMethod("AL")]
public void AddLine()
{
//获取当前活动文档
Document doc = Application.DocumentManager.MdiActiveDocument;
//开启事务
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
//打开块表
BlockTable acBlkTbl = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
//打开块表记录
BlockTableRecord acBlkTblRe = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//创建直线
Line l = new Line();
//设置直线起点
l.StartPoint = new Point3d(100, 100, 0);
//设置直线终点
l.EndPoint = new Point3d(1000, 1000, 0);
//添加到块表记录
acBlkTblRe.AppendEntity(l);
//添加到事务
trans.AddNewlyCreatedDBObject(l, true);
//提交事务
trans.Commit();
}
}
第三步:编写删除直线的命令
[CommandMethod("DL")]
public void DeleteLine()
{
//获取当前活动文档
Document doc = Application.DocumentManager.MdiActiveDocument;
//开启事务
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
//打开块表
BlockTable acBlkTbl = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
//打开块表记录
BlockTableRecord acBlkTblRe = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//遍历块表记录
foreach (var item in acBlkTblRe)
{
//获取直线
Line l = trans.GetObject(item, OpenMode.ForWrite) as Line;
//如果这个图元不是直线则l为null
if (l!=null)
{
//删除直线
l.Erase();
}
}
//提交事务
trans.Commit();
}
}
class完整代码如下:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 添加和删除直线
{
public class Class1
{
[CommandMethod("AL")]
public void AddLine()
{
//获取当前活动文档
Document doc = Application.DocumentManager.MdiActiveDocument;
//开启事务
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
//打开块表
BlockTable acBlkTbl = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
//打开块表记录
BlockTableRecord acBlkTblRe = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//创建直线
Line l = new Line();
//设置直线起点
l.StartPoint = new Point3d(100, 100, 0);
//设置直线终点
l.EndPoint = new Point3d(1000, 1000, 0);
//添加到块表记录
acBlkTblRe.AppendEntity(l);
//添加到事务
trans.AddNewlyCreatedDBObject(l, true);
//提交事务
trans.Commit();
}
}
[CommandMethod("DL")]
public void DeleteLine()
{
//获取当前活动文档
Document doc = Application.DocumentManager.MdiActiveDocument;
//开启事务
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
//打开块表
BlockTable acBlkTbl = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
//打开块表记录
BlockTableRecord acBlkTblRe = trans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//遍历块表记录
foreach (var item in acBlkTblRe)
{
//获取直线
Line l = trans.GetObject(item, OpenMode.ForWrite) as Line;
//如果这个图元不是直线则l为null
if (l!=null)
{
//删除直线
l.Erase();
}
}
//提交事务
trans.Commit();
}
}
}
}
第四步:编译项目
第五步:启动CAD并加载程序
第六步:测试命令
输入AL回车可以看到模型空间多了一条直线如下图
输入DL回车可以看到模型空间的直线都被删除了(原有的直线也会被删除)如下图: