005集—— 用户交互之CAD窗口选择图元实体(CAD—C#二次开发入门)

如下图:根据提示选择若干图形要素,空格或右键结束选择,返回图元的objectid,以便进一步操作图元实体。

代码如下:

cs 复制代码
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}");
            }
        }
    }
}
相关推荐
雨季66615 分钟前
C 语言学习指南:从入门到实战的系统路径
c#
JH307324 分钟前
Java 是值传递:深入理解参数传递机制
java·开发语言·windows
私人珍藏库38 分钟前
[Windows] Office Tool Plus V10.29.50.0
windows·工具·office·辅助
吕了了1 小时前
116Dism++备份系统,如何选择性的备份文件?
运维·windows·电脑·系统
MARS_AI_2 小时前
大模型呼叫技术:客服行业的智能化演进与云蝠实践
人工智能·自然语言处理·交互·信息与通信·agi
爱倒腾的老唐2 小时前
02、打不开某个网站
windows·笔记·电脑
Studying 开龙wu2 小时前
Windos 10系统安装OpenPose的CPU版本过程说明和Release版本直接使用
c++·windows
whm27772 小时前
Visual Basic 平面图形的几何变换
windows·visual studio
菜鸟小芯3 小时前
OpenHarmony环境搭建——01-Windows系统下安装Git
windows·git
吕了了3 小时前
电脑磁盘究竟如何分区才最合适?系统和软件如何管理才最好?给出推荐方案!
运维·windows·电脑·系统