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}");
            }
        }
    }
}
相关推荐
CoderIsArt2 小时前
参数系统的基类Parameter抽象类
c#
Blossom.1183 小时前
Web3.0:互联网的去中心化未来
人工智能·驱动开发·深度学习·web3·去中心化·区块链·交互
盛夏绽放3 小时前
Python字符串常用方法详解
开发语言·python·c#
Tummer83637 小时前
C#+WPF+prism+materialdesign创建工具主界面框架
开发语言·c#·wpf
ghost1438 小时前
C#学习第23天:面向对象设计模式
开发语言·学习·设计模式·c#
yngsqq9 小时前
(for 循环) VS (LINQ) 性能比拼 ——c#
c#·solr·linq
想做后端的小C10 小时前
C# 面向对象 构造函数带参无参细节解析
开发语言·c#·面向对象
炯哈哈10 小时前
【上位机——WPF】App.xml和Application类简介
xml·开发语言·c#·wpf·上位机
bestcxx10 小时前
c# UTC 时间赋值注意事项
c#·utc
酷炫码神10 小时前
C#运算符
开发语言·c#