用.NET的面板来显示多个AutoCAD实体的属性

2024-06-10 11:32

本文主要是介绍用.NET的面板来显示多个AutoCAD实体的属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文:Using a palette from .NET to display properties of multiple AutoCAD objects

本文仅翻译部分内容

经过短暂的插曲,我们又回到了演示如何用.NET在AutoCAD中实现基本用户界面的系列文章。这是目前为止该系列的文章列表:

  • Using a modal .NET dialog to display AutoCAD object properties
  • Using a modeless .NET dialog to display AutoCAD object properties
  • Using a modeless .NET dialog to display properties of multiple AutoCAD objects

在这篇文章中我们将换掉已经在前几篇系列文章中使用的无模式对话框,用AutoCAD内置的palette类(Autodesk.AutoCAD.Windows.PaletteSet)为例来替换它。

为什么要用Paletteset类呢?因为Paletteset类非常炫酷:它提供了停靠(docking),自动隐藏,支持透明度并且修复了我们在无模式对话框中遇到的恼人的焦点相关的问题。

最重要的是,实现这一切基本上不需要花费任何代价——实现它的工作几乎最小的。我从ObjectARX SDK的DockingPalette的示例中复制了大部分的代码,然后删除了我们项目不需要的部分。

下面是更新后的命令的实现。修改真的非常小,因为palette的实现都隐藏在新的TypeViewerPalette类里了。

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using CustomDialogs;namespace CustomDialogs
{public class Commands : IExtensionApplication{static TypeViewerPalette tvp;public void Initialize(){tvp = new TypeViewerPalette();DocumentCollection dm =Application.DocumentManager;dm.DocumentCreated +=new DocumentCollectionEventHandler(OnDocumentCreated);foreach (Document doc in dm){doc.Editor.PointMonitor +=new PointMonitorEventHandler(OnMonitorPoint);}}public void Terminate(){try{DocumentCollection dm =Application.DocumentManager;if (dm != null){Editor ed = dm.MdiActiveDocument.Editor;ed.PointMonitor -=new PointMonitorEventHandler(OnMonitorPoint);}}catch (System.Exception){// The editor may no longer// be available on unload}}private void OnDocumentCreated(object sender,DocumentCollectionEventArgs e){e.Document.Editor.PointMonitor +=new PointMonitorEventHandler(OnMonitorPoint);}private void OnMonitorPoint(object sender,PointMonitorEventArgs e){FullSubentityPath[] paths =e.Context.GetPickedEntities();if (paths.Length <= 0){tvp.SetObjectId(ObjectId.Null);return;};ObjectIdCollection idc = new ObjectIdCollection();foreach (FullSubentityPath path in paths){// Just add the first ID in the list from each pathObjectId[] ids = path.GetObjectIds();idc.Add(ids[0]);}tvp.SetObjectIds(idc);}[CommandMethod("vt",CommandFlags.UsePickSet)]public void ViewType(){tvp.Show();}}
}


至于TypeViewerPalette类:我通过从原来的TypeViewerForm类中把SetObjectId[s]()/SetObjectText() 协议迁移过来开始——这是其中最复杂的一部分,涉及通过一个可以从SetObjectText()成员变量暴露我们的面板的内容(我们作为一个用户控件定义并加载)。除了前面说的以外,其它的就只是复制和粘贴了。

这是C #代码:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Windows;
using TypeViewer;namespace CustomDialogs
{public class TypeViewerPalette{// We cannot derive from PaletteSet// so we contain itstatic PaletteSet ps;// We need to make the textbox available// via a static memberstatic TypeViewerControl tvc;public TypeViewerPalette(){tvc = new TypeViewerControl();}public void Show(){if (ps == null){ps = new PaletteSet("Type Viewer");ps.Style =PaletteSetStyles.NameEditable |PaletteSetStyles.ShowPropertiesMenu |PaletteSetStyles.ShowAutoHideButton |PaletteSetStyles.ShowCloseButton;ps.MinimumSize =new System.Drawing.Size(300, 300);ps.Add("Type Viewer 1", tvc);}ps.Visible = true;}public void SetObjectText(string text){tvc.typeTextBox.Text = text;}public void SetObjectIds(ObjectIdCollection ids){if (ids.Count < 0){SetObjectText("");}else{Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;DocumentLock loc =doc.LockDocument();using (loc){string info ="Number of objects: " +ids.Count.ToString() + "\r\n";Transaction tr =doc.TransactionManager.StartTransaction();using (tr){foreach (ObjectId id in ids){Entity ent =(Entity)tr.GetObject(id, OpenMode.ForRead);Solid3d sol = ent as Solid3d;if (sol != null){Acad3DSolid oSol =(Acad3DSolid)sol.AcadObject;// Put in a try-catch block, as it's possible// for solids to not support this property,// it seems (better safe than sorry)try{string solidType = oSol.SolidType;info +=ent.GetType().ToString() +" (" + solidType + ") : " +ent.ColorIndex.ToString() + "\r\n";}catch (System.Exception){info +=ent.GetType().ToString() +" : " +ent.ColorIndex.ToString() + "\r\n";}}else{info +=ent.GetType().ToString() +" : " +ent.ColorIndex.ToString() + "\r\n";}}tr.Commit();}SetObjectText(info);}}}public void SetObjectId(ObjectId id){if (id == ObjectId.Null){SetObjectText("");}else{Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;DocumentLock loc =doc.LockDocument();using (loc){Transaction tr =doc.TransactionManager.StartTransaction();using (tr){DBObject obj =tr.GetObject(id, OpenMode.ForRead);SetObjectText(obj.GetType().ToString());tr.Commit();}}}}}
}
你可以从 这里下载源码

这篇关于用.NET的面板来显示多个AutoCAD实体的属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048038

相关文章

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

Python如何调用另一个类的方法和属性

《Python如何调用另一个类的方法和属性》在Python面向对象编程中,类与类之间的交互是非常常见的场景,本文将详细介绍在Python中一个类如何调用另一个类的方法和属性,大家可以根据需要进行选择... 目录一、前言二、基本调用方式通过实例化调用通过类继承调用三、高级调用方式通过组合方式调用通过类方法/静

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

解决hive启动时java.net.ConnectException:拒绝连接的问题

《解决hive启动时java.net.ConnectException:拒绝连接的问题》Hadoop集群连接被拒,需检查集群是否启动、关闭防火墙/SELinux、确认安全模式退出,若问题仍存,查看日志... 目录错误发生原因解决方式1.关闭防火墙2.关闭selinux3.启动集群4.检查集群是否正常启动5.

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤