CAD二次开发 学习笔记-未完待续...
用EntityJig动态绘制一条直线
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
[assembly: CommandClass(typeof(JigTest.LineJig))]
namespace JigTest
{
///
/// 命令类
///
public class LineJig
{
///
/// 命令方法
///
[CommandMethod("linejig")]
public void DoIt()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions options = new PromptPointOptions("\n 指定起点");
ed.WriteMessage("\n 准备接收用户输入的点");
PromptPointResult result = ed.GetPoint(options);//阻塞方法
ed.WriteMessage("\n 已获取用户输入的点");
ed.WriteMessage($"\n 起点坐标:{result.Value.X},{result.Value.Y},{result.Value.Z}");
Database db = Application.DocumentManager.MdiActiveDocument.Database;
LineJigImpl jig = new LineJigImpl(result.Value);
Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(jig.GetEntity());
tr.AddNewlyCreatedDBObject(jig.GetEntity(),true);
tr.Commit();
}
}
}
///
/// Jig实现类
///
class LineJigImpl : EntityJig
{
Point3d m_StartPoint, m_EndPoint;
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
///
/// 构造函数
///
/// 起点
public LineJigImpl(Point3d start) : base(new Line())
{
editor.WriteMessage("\n 执行构造函数");
m_StartPoint = start;
editor.WriteMessage($"\n m_StartPoint初始值:{m_StartPoint}");
editor.WriteMessage($"\n m_EndPoint默认值:{m_EndPoint}");
}
///
/// 动态/即时/实时采样器
///
/// 即时提示
///
protected override SamplerStatus Sampler(JigPrompts prompts)
{
editor.WriteMessage("\n 执行Sampler;");
JigPromptPointOptions options = new JigPromptPointOptions("\n 指定终点");
options.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted;
PromptPointResult result = prompts.AcquirePoint(options);
Point3d tempPoint = result.Value;
if (tempPoint != m_EndPoint) m_EndPoint = tempPoint;
else return SamplerStatus.NoChange;
if (result.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
else return SamplerStatus.OK;
}
///
/// (动态/即时/实时)更新显示/动态显示
///
///
protected override bool Update()
{
editor.WriteMessage("\n 执行Update;");
try
{
((Line)Entity).StartPoint = m_StartPoint;
((Line)Entity).EndPoint = m_EndPoint;
editor.WriteMessage($"\n m_EndPoint当前值:{m_EndPoint}");
}
catch (System.Exception)
{
return false;
}
return true;
}
///
/// 获取实体,便于外部调用
///
///
public Entity GetEntity()
{ return Entity; }
}
}
运行结果
列出实体信息
[CommandMethod("listEntity")]
public void ListEntity()
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
ed.WriteMessage($"\ndb.BlockTableId:{db.BlockTableId}");
ed.WriteMessage($"\nBlockTableRecord.ModelSpace:{BlockTableRecord.ModelSpace}");
ed.WriteMessage($"\nbt[BlockTableRecord.ModelSpace]:{bt[BlockTableRecord.ModelSpace]}");
ed.WriteMessage($"\n BlockBeginId:{btr.BlockBeginId}");
ed.WriteMessage($"\n BlockEndId:{btr.BlockEndId}");
ed.WriteMessage($"\n btr.Name:{btr.Name}");
ed.WriteMessage($"\n btr.PathName:{btr.PathName}");
ed.WriteMessage($"\n btr.Comments:{btr.Comments}");
ed.WriteMessage($"\n --------------------------------");
int count = 0;
foreach (ObjectId entityId in btr)
{
ed.WriteMessage($"\n 这是模型空间的第{count + 1}个实体,详细信息如下:");
ed.WriteMessage($"\nName:{entityId.ObjectClass.Name}");
ed.WriteMessage($"\nAppName:{entityId.ObjectClass.AppName}");
ed.WriteMessage($"\nDxfName:{entityId.ObjectClass.DxfName}");
count++;
}
ed.WriteMessage($"\n块表-模型空间-实体个数:{count}");
}
}
运行结果