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}");
            }
        }
    }
}
相关推荐
fruge33 分钟前
拆解热门网站交互:分析电商 APP 的下拉刷新动画实现
交互
Charles_go2 小时前
C#中级8、什么是缓存
开发语言·缓存·c#
用户83562907805112 小时前
如何在 C# 中自动化生成 PDF 表格
后端·c#
至天13 小时前
Windows 10/11 终端(Windows Terminal)右键菜单缺失恢复方法
windows·终端·windows 11·windows 10·terminal·右键菜单·windowsterminal
mudtools13 小时前
.NET如何快速集成飞书API的最佳实践
c#·.net·飞书
光年像素14 小时前
WMIC(Windows Management Instrumentation Command-line)命令大全
windows
ThreePointsHeat15 小时前
Unity 关于打包WebGL + jslib录制RenderTexture画面
unity·c#·webgl
a***976815 小时前
如何使用C#与SQL Server数据库进行交互
数据库·c#·交互
乘乘凉16 小时前
C#中的值传递和引用传递
java·开发语言·c#
q***575018 小时前
Redis服务安装自启动(Windows版)
数据库·windows·redis