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}");
            }
        }
    }
}
相关推荐
水饺编程17 小时前
第5章,[Win32 章节] :贝塞尔样条曲线(一)
c语言·c++·windows·visual studio
fl17683117 小时前
基于C#WPF实现的内存加速球清理类似360加速球内存清理
开发语言·c#·wpf
辻弋20117 小时前
一键优化电源计划、游戏模式、独显强制、禁用后台录制——DeltaForceBooster专治三角洲行动卡顿掉帧,所有改动可一键还原
服务器·数据库·windows·游戏·电脑·php
言乐618 小时前
Python实现自动拉人脚本示例
开发语言·windows·python·django·pygame
深念Y19 小时前
OnlyOffice 打开来自 Windows/WPS 的 Office 文档时,出现排版错乱、文字空白、字体显示异常。
linux·windows·debian·onlyoffice·字体·wps·office
CoderIsArt19 小时前
OPC UA 完整介绍、官网、C# 简单使用
开发语言·c#
易点云徐源19 小时前
(已解决)adb devices 不显示设备,但 Windows 的 ADB Interface 正常:一次 WinUSB 与旧版 ADB 兼容性排查实战
windows·adb
奈斯先生Vector19 小时前
OpenScience 安装失败怎么办?Windows 环境、API 配置与本地服务排错指南
人工智能·windows·架构·开源·aigc·midjourney
AxureMost19 小时前
C盘清理工具 LightC
windows
界面开发小八哥19 小时前
界面控件DevExpress XAF v26.1新版亮点——AI Agent Skills
ai·c#·跨平台·devexpress·ui开发·xaf