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}");
            }
        }
    }
}
相关推荐
xbzb13 分钟前
Windows 启动故障修复知识整理:蓝屏 / 无限重启 / 盘符 X / 无 PE 盘
windows
我命由我1234542 分钟前
Mac 操作系统 - 一些使用记录
运维·windows·学习·系统安全·运维开发·mac·学习方法
c#上位机13 小时前
C#上位机项目实战——Task.Run执行带参数的方法
c#·上位机
kakakahahahaha16 小时前
Windows Steam 游戏存档在哪里?定位和恢复排查流程
windows·其他·电脑·笔记本电脑·内容运营·软件需求
Xy-unu16 小时前
Windows 开启外接显示器 HDR 后外接显示器无信号、无法扩展模式故障记录
windows
troy12818 小时前
Codex 安全盲区:代码漏洞生成实测
windows·python·ci/cd·pycharm·django·github·fastapi
其实防守也摸鱼19 小时前
常见安全架构中 Shiro 的认证确认机制解析
运维·服务器·数据库·windows·github
Lost of 程序猿19 小时前
用 YARP 为 .NET 微服务搭一座“立交桥“:路由转发与生产配置实践
c#·asp.net
五_谷_丰_登21 小时前
windows批处理脚本(bat)问题解决方案大全——持续更新
windows·bat
YCOSA20251 天前
迎双节 雨晨 Windows 11 IoT 企业版 LTSC 26H1 轻装 28000.2956
windows·物联网