选择实体操作过于常见,几乎随时都在使用。因此只贴出代码。不再赘述
选择一个实体:
/// <summary>
/// 提示选择实体
/// </summary>
/// <param name="id">实体ID</param>
/// <returns>true:取得成功 false:取得失败</returns>
public bool GetEntityId(out ObjectId id)
{
//获取当前文档
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//定义选择对象后的返回值
PromptEntityResult reEntityResult;
//获取对象
reEntityResult = ed.GetEntity("\n请选择一个对象");
if (reEntityResult.Status == PromptStatus.OK)
{
//返回取得的实体ID
id = reEntityResult.ObjectId;
return true;
}
//拾取失败
id = ObjectId.Null;
return false;
}
选择集:
/// <summary>
/// 提示选择实体
/// </summary>
/// <param name="id">实体ID</param>
/// <returns>true:取得成功 false:取得失败</returns>
public bool GetEntityIds(out ObjectId[] ids)
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionOptions pso = new PromptSelectionOptions();
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
//取得选择值
SelectionSet sst = psr.Value;
//得到所有id
ids = sst.GetObjectIds();
return true;
}
else
{
ids = null;
return false;
}
}
提示拾取一个点:
/// <summary>
/// 提示用户拾取一个点
/// </summary>
/// <param name="strPromtMsg">提示消息</param>
/// <returns>用户拾取的点</returns>
public Point3d? SelectPoint(string strPromtMsg)
{
// 创建存放用户拾取点信息
Point3d? pointForSelect = null;
// 得到CAD当前图档编辑器对象
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
// 提示用户拾取一个点, 并得到返回信息
PromptPointResult pointR = editor.GetPoint("\n" + strPromtMsg);
// 拾取成功
if (pointR.Status == PromptStatus.OK)
{
// 得到用户拾取的点信息
pointForSelect = pointR.Value;
}
// 返回结果
return pointForSelect;
}