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}");
            }
        }
    }
}
相关推荐
小江的记录本5 小时前
【Linux】《Linux常用命令汇总表》
linux·运维·服务器·前端·windows·后端·macos
:mnong7 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
我是唐青枫7 小时前
C#.NET 分布式事务 深入解析:TCC、Saga、Outbox 与落地取舍
分布式·c#·.net
追烽少年x7 小时前
Windows API中线程异步API简介
windows
私人珍藏库7 小时前
[Windows] 绘画工具 Krita v5.3.1
人工智能·windows·媒体·工具·软件·多功能
CSharp精选营8 小时前
.NET 8 性能优化实战:让你的应用起飞
性能优化·c#·.net·技术干货
cy_cy00211 小时前
互动地面投影:优化展厅动线的智能向导
大数据·科技·人机交互·交互·软件构建
tumeng071112 小时前
Node.JS 版本管理工具 Fnm 安装及配置(Windows)
windows·node.js
左手厨刀右手茼蒿12 小时前
Flutter for OpenHarmony:mailer — 基于 SMTP 的极速邮件投递服务(适配鸿蒙 HarmonyOS Next ohos)
android·flutter·华为·交互·harmonyos
阿正的梦工坊12 小时前
Mac 的.zshrc在 Windows 上等价怎么做:设置环境变量
windows·macos