如下图:根据提示选择若干图形要素,空格或右键结束选择,返回图元的objectid,以便进一步操作图元实体。
代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
namespace sc
{public class Class1{public static ObjectId GetEntity(string message){Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;PromptEntityOptions po = new PromptEntityOptions(message);PromptEntityResult pr = ed.GetEntity(po);return pr.ObjectId;}public static List<ObjectId> SelectEntities(string message){Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;List<ObjectId> objectIds = new List<ObjectId>();PromptSelectionOptions pso = new PromptSelectionOptions();pso.AllowDuplicates = true; // 允许用户选择多个实体 //pso.UsePickset = true; // 使用Pickset模式,这样可以返回用户选择的所有实体 PromptSelectionResult psr = ed.GetSelection(pso);if (psr.Status == PromptStatus.OK){SelectionSet selectionSet = psr.Value;foreach (SelectedObject selectedObj in selectionSet){objectIds.Add(selectedObj.ObjectId);}}return objectIds;}[CommandMethod("xx")]public void Msc(){List<ObjectId> objectIds = SelectEntities("请选择若干实体:");Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;try{foreach (ObjectId id in objectIds){ed.WriteMessage($"\nObjectId: {id}");}}catch (System.Exception ex){ed.WriteMessage($"\n错误: {ex.Message}");}}}
}