用.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

相关文章

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

C#利用Free Spire.XLS for .NET复制Excel工作表

《C#利用FreeSpire.XLSfor.NET复制Excel工作表》在日常的.NET开发中,我们经常需要操作Excel文件,本文将详细介绍C#如何使用FreeSpire.XLSfor.NET... 目录1. 环境准备2. 核心功能3. android示例代码3.1 在同一工作簿内复制工作表3.2 在不同

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

在.NET项目中嵌入Python代码的实践指南

《在.NET项目中嵌入Python代码的实践指南》在现代开发中,.NET与Python的协作需求日益增长,从机器学习模型集成到科学计算,从脚本自动化到数据分析,然而,传统的解决方案(如HTTPAPI或... 目录一、CSnakes vs python.NET:为何选择 CSnakes?二、环境准备:从 Py

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

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.