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}");
            }
        }
    }
}
相关推荐
Abigail_chow5 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
行云流水剑6 小时前
【学习记录】深入解析 AI 交互中的五大核心概念:Prompt、Agent、MCP、Function Calling 与 Tools
人工智能·学习·交互
绿荫阿广6 小时前
互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(五):使用.NET为树莓派开发Wifi配网功能
c#·.net
love530love6 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
代码搬运媛8 小时前
“packageManager“: “[email protected]“ 配置如何正确启动项目?
windows·webpack
小道士写程序9 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
ou.cs9 小时前
c# :this() 和 :base()区别
开发语言·c#
汪小白JIY12 小时前
【C#】异步和多线程
c#·thread·async·task·threapool
宝桥南山13 小时前
DeepSeek - 尝试一下GitHub Models中的DeepSeek
microsoft·ai·微软·c#·github·.net
异常君13 小时前
Windows 与 Linux 虚拟内存机制对比:设计理念与实现差异
java·linux·windows