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}");
            }
        }
    }
}
相关推荐
缺点内向4 小时前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
天地之于壹炁兮6 小时前
编程I/O入门指南:核心操作全解析
数据库·windows·microsoft
深圳市恒讯科技6 小时前
英国服务器Windows系统远程桌面安装与优化
运维·服务器·windows
yue0086 小时前
C# 分部类读取学生信息
开发语言·c#
聪明努力的积极向上7 小时前
【C#】事件简单解析
开发语言·c#
像风一样的男人@7 小时前
python --两个文件夹文件名比对(yolo 图和label标注比对检查)
windows·python·yolo
qq_12498707538 小时前
基于C#的贵州省黔北地区乡村避暑生活共享平台设计与实现(源码+论文+部署+安装)
c#·毕业设计·asp.net·生活
diannao72011 小时前
实时将大模型的解决方案转换为随机应变的机器人指令
开发语言·windows·python·机器人
奔跑吧邓邓子11 小时前
【C语言实战(75)】C语言内存探秘:泄漏检测与分析实战
linux·c语言·windows·内存·开发实战·泄露检测
LateFrames13 小时前
C# 中,0.1 在什么情况下不等于 0.1 ?
开发语言·c#