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}");
            }
        }
    }
}
相关推荐
加号33 小时前
【C#】 Web API 自定义配置函数请求路径:从路由本质到灵活架构设计
开发语言·c#
happyprince6 小时前
11-Hugging Face Transformers 分布式与并行系统深度分析
分布式·c#·wpf
云樱梦海6 小时前
OpenClaw官方:OpenClaw Windows Node 实战(1.5K ★ 开源推荐)
windows·开源·github·openclaw
Z-D-K7 小时前
考验AI的“自我和意识“-AI对《红楼梦》后40回的改写(16)
人工智能·ai·aigc·交互·agi
csdn_aspnet7 小时前
C# list集合 多属性排序
c#·list·linq·排序
加号37 小时前
【WPF】 基于 Canvas 读取并渲染 DXF 文件的技术指南
c#·wpf
深度学习04077 小时前
SVN 独立服务端部署与客户端使用指南(CentOS/Rocky 9.5 + Windows)
windows·svn
爱分享软件的学长7 小时前
Tiled Map Editor 1.12.1 官方版下载(夸克网盘+百度网盘,SHA256校验)
windows·开源软件·软件下载
天下无敌笨笨熊8 小时前
SNMP协议开发心得
网络协议·c#
Dream-Y.ocean8 小时前
鸿蒙PC平台 Carnac 按键显示适配实战:从 Windows 到 HarmonyOS 的 Electron 迁移指南
windows·electron·harmonyos