Editor
在C#进行CAD二次开发时,Editor类是一个特别有用的类。它提供了大量常用的接口函数,比如:
// 拖动相关接口
public PromptPointResult Drag(SelectionSet selection, string message, DragCallback callback);
public PromptResult Drag(Jig jig);
public PromptPointResult Drag(PromptDragOptions options);
// 获取点
public PromptPointResult GetPoint(PromptPointOptions options);
public PromptPointResult GetPoint(string message);
// 获取选择集
public PromptSelectionResult GetSelection(PromptSelectionOptions options, SelectionFilter filter);
public PromptSelectionResult GetSelection(PromptSelectionOptions options);
public PromptSelectionResult GetSelection(SelectionFilter filter);
public PromptSelectionResult GetSelection();
// 调用选择集相关接口
public PromptSelectionResult SelectAll();
public PromptSelectionResult SelectAll(SelectionFilter filter);
public PromptSelectionResult SelectCrossingPolygon(Point3dCollection polygon);
public PromptSelectionResult SelectCrossingPolygon(Point3dCollection polygon, SelectionFilter filter);
public PromptSelectionResult SelectCrossingWindow(Point3d pt1, Point3d pt2, SelectionFilter filter, bool forceSubEntitySelection);
public PromptSelectionResult SelectCrossingWindow(Point3d pt1, Point3d pt2);
public PromptSelectionResult SelectCrossingWindow(Point3d pt1, Point3d pt2, SelectionFilter filter);
public PromptSelectionResult SelectFence(Point3dCollection fence);
public PromptSelectionResult SelectFence(Point3dCollection fence, SelectionFilter filter);
public PromptSelectionResult SelectImplied();
public PromptSelectionResult SelectLast();
public PromptSelectionResult SelectPrevious();
public PromptSelectionResult SelectWindow(Point3d pt1, Point3d pt2);
public PromptSelectionResult SelectWindow(Point3d pt1, Point3d pt2, SelectionFilter filter);
public PromptSelectionResult SelectWindowPolygon(Point3dCollection polygon);
public PromptSelectionResult SelectWindowPolygon(Point3dCollection polygon, SelectionFilter filter);
Editor相当于CAD的编辑器,因此提供了大量的接口供我们使用,这里列举了其中一部分。关于Editor接口函数,需要不断的积累掌握。
拖动类EntityJig
实现代码,以模仿CAD绘制直线的命令为例
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JigDemo
{
// 定义拖动类,继承EntityJig
public class LineJig : EntityJig
{
private Point3d m_ptStart;
private Point3d m_ptEnd;
private string m_sMessage;
private string[] m_sKeywords;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ptStart">直线起点</param>
/// <param name="sMessage">命令行信息</param>
/// <param name="sKeywords">关键字</param>
public LineJig(Point3d ptStart, string sMessage, params string[] sKeywords)
:base(new Line())
{
m_ptStart = ptStart;
m_sMessage = sMessage;
m_sKeywords = sKeywords;
((Line)Entity).StartPoint = ptStart;
}
/// <summary>
/// 取样函数
/// </summary>
/// <param name="prompts">拖拽信息提示类,可获取结果</param>
/// <returns></returns>
protected override SamplerStatus Sampler(JigPrompts prompts)
{
// 声明提示信息类
JigPromptPointOptions jppo = new JigPromptPointOptions(m_sMessage);
// 添加关键字
for (int i = 0; i < m_sKeywords.Length; i++)
{
jppo.Keywords.Add(m_sKeywords[i]);
}
char space = ' ';
jppo.Keywords.Add(space.ToString());
// 设置获取的信息类型
jppo.UserInputControls = UserInputControls.Accept3dCoordinates; // 3d坐标点
// 取消系统自动添加的关键字
jppo.AppendKeywordsToMessage = false;
PromptPointResult ppr = prompts.AcquirePoint(jppo);
m_ptEnd = ppr.Value;
return SamplerStatus.NoChange;
}
// 更新实体
protected override bool Update()
{
((Line)Entity).EndPoint = m_ptEnd;
return true;
}
// 获取实体
public Entity GetEntity()
{
return Entity;
}
}
}
// 调用相关代码
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
LineJig linejig = new LineJig(ptPre, "\n 指定下一点或[闭合(C)/放弃(U)]", new string[] { "C", "U" });
PromptResult pr = editor.Drag(linejig);
选择集
可以直接调用Editor的相关接口,过滤器SelectionFilter
示例:
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue value = new TypedValue((int)DxfCode.Start, "CIRCLE"); // 实体类型名称 圆
TypedValue[] values = new TypedValue[]{value}
// 过滤器
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.OK)
{
// 选择集
SelectionSet ss = psr.Value;
ObjectId[] selIds = ss.GetObjectIds();
}
}
Editor提供了丰富的接口可供我们调用
比如public PromptSelectionResule SelectionImplied(); 函数,可以获取图面上已经选中的实体到结果选择集里面,此时要注意在写命令时,添加如下参数
[CommandMethod("PickFirst" , CommandFlags.UserPickSet)]
- CommandMethod() 函数具有多种重载;
- CommandFlags 来表示命令函数具备的一些属性