用.NET创建并且覆盖AutoCAD的标注样式

2024-06-10 11:32

本文主要是介绍用.NET创建并且覆盖AutoCAD的标注样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文:Creating and overriding AutoCAD dimension styles using .NET

上周末有一封电子邮件发来了一份请求::

我一直在寻找一种创建标注样式重定义的方法,但一直没有真的没有成功。我写了一个程序,在程序中我创建了几个标注样式,但是总是会丢失标注的重定义。

这似乎是一个很好的重定义话题,所以这篇文章包含一些简单的代码,创建一个样式和两个几乎相同的线性标注:都使用我们新创建的样式但第二个尺寸包含了一些尺寸样式重定义,我们通过附加扩展实体数据(xData)附加到尺寸上来实现这一功能
以下是C #代码:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;namespace DimStyleOverrideTest
{public class Commands{[CommandMethod("ODS")]public void OverrideDimensionStyle(){Database db =HostApplicationServices.WorkingDatabase;Transaction tr =db.TransactionManager.StartTransaction();using (tr){// Open our dimension style table to add our// new dimension styleDimStyleTable dst =(DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForWrite);// Create our new dimension styleDimStyleTableRecord dstr =new DimStyleTableRecord();dstr.Dimtad = 2;dstr.Dimgap = 0.3;dstr.Name = "MyStyle";// Add it to the dimension style tableObjectId dsId = dst.Add(dstr);tr.AddNewlyCreatedDBObject(dstr, true);// Now create two identical dimensions, one// next to the other, using our dimension// styleAlignedDimension ad1 =new AlignedDimension(Point3d.Origin,new Point3d(5.0, 0.0, 0.0),new Point3d(2.5, 2.0, 0.0),"Standard dimension",dsId);// The only thing we change is the text stringAlignedDimension ad2 =new AlignedDimension(new Point3d(5.0, 0.0, 0.0),new Point3d(10.0, 0.0, 0.0),new Point3d(7.5, 2.0, 0.0),"Overridden dimension",dsId);/*Now we'll add dimension overrides for DIMTADand DIMGAP via XDataDimension variable group codes are:DIMPOST     3DIMAPOST    4DIMSCALE   40DIMASZ     41DIMEXO     42DIMDLI     43DIMEXE     44DIMRND     45DIMDLE     46DIMTP      47DIMTM      48DIMTOL     71DIMLIM     72DIMTIH     73DIMTOH     74DIMSE1     75DIMSE2     76DIMTAD     77DIMZIN     78DIMAZIN    79DIMTXT    140DIMCEN    141DIMTSZ    142DIMALTF   143DIMLFAC   144DIMTVP    145DIMTFAC   146DIMGAP    147DIMALTRND 148DIMALT    170DIMALTD   171DIMTOFL   172DIMSAH    173DIMTIX    174DIMSOXD   175DIMCLRD   176DIMCLRE   177DIMCLRT   178DIMADEC   179DIMDEC    271DIMTDEC   272DIMALTU   273DIMALTTD  274DIMAUNIT  275DIMFRAC   276DIMLUNIT  277DIMDSEP   278DIMATMOVE 279DIMJUST   280DIMSD1    281DIMSD2    282DIMTOLJ   283DIMTZIN   284DIMALTZ   285DIMALTTZ  286DIMUPT    288DIMATFIT  289DIMTXSTY  340DIMLDRBLK 341DIMBLK    342DIMBLK1   343DIMBLK2   344DIMLWD    371DIMLWE    372Variables have different types: these can be found inthe ObjectARX Reference - search for "Dimension StyleOverrides"*/ResultBuffer rb =new ResultBuffer(new TypedValue[8]{new TypedValue((int)DxfCode.ExtendedDataRegAppName, "ACAD"),new TypedValue((int)DxfCode.ExtendedDataAsciiString, "DSTYLE"),new TypedValue((int)DxfCode.ExtendedDataControlString, "{"),new TypedValue((int)DxfCode.ExtendedDataInteger16, 77  // DIMTAD),new TypedValue((int)DxfCode.ExtendedDataInteger16, 4   // Below),new TypedValue((int)DxfCode.ExtendedDataInteger16, 147 // DIMGAP),new TypedValue((int)DxfCode.ExtendedDataReal, 0.5      // Larger),new TypedValue((int)DxfCode.ExtendedDataControlString, "}")});// Set the XData on our objectad2.XData = rb;rb.Dispose();// Now let's open the current space and add our two// dimensionsBlockTableRecord btr =(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);btr.AppendEntity(ad1);btr.AppendEntity(ad2);tr.AddNewlyCreatedDBObject(ad1, true);tr.AddNewlyCreatedDBObject(ad2, true);// And commit the transaction, of coursetr.Commit();}}}
}
更新

OK, I missed the obvious on this one (as does happen from time-to-time, as regular readers will by now be aware). Rather than setting the overrides directly via XData, there are handy properties belonging to the dimension’s managed interface that do this for you. Very cool. So we can reduce the code to the following:

好吧,我完全没有意识到这一明显的做法(这的确也时有会发生,普通读者现在也知道了)。与其通过直接重写XDATA,其实Dimesion的接口属性中有更方便的方法来实现。实现的方法很酷。所以我们可以减少代码如下:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;namespace DimStyleOverrideTest
{public class Commands{[CommandMethod("ODS")]public void OverrideDimensionStyle(){Database db =HostApplicationServices.WorkingDatabase;Transaction tr =db.TransactionManager.StartTransaction();using (tr){// Open our dimension style table to add our// new dimension styleDimStyleTable dst =(DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForWrite);// Create our new dimension styleDimStyleTableRecord dstr =new DimStyleTableRecord();dstr.Dimtad = 2;dstr.Dimgap = 0.3;dstr.Name = "MyStyle";// Add it to the dimension style tableObjectId dsId = dst.Add(dstr);tr.AddNewlyCreatedDBObject(dstr, true);// Now create two identical dimensions, one// next to the other, using our dimension// styleAlignedDimension ad1 =new AlignedDimension(Point3d.Origin,new Point3d(5.0, 0.0, 0.0),new Point3d(2.5, 2.0, 0.0),"Standard dimension",dsId);// The only thing we change is the text stringAlignedDimension ad2 =new AlignedDimension(new Point3d(5.0, 0.0, 0.0),new Point3d(10.0, 0.0, 0.0),new Point3d(7.5, 2.0, 0.0),"Overridden dimension",dsId);// Isn't this easier?ad2.Dimtad = 4;ad2.Dimgap = 0.5;// Now let's open the current space and add our two// dimensionsBlockTableRecord btr =(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);btr.AppendEntity(ad1);btr.AppendEntity(ad2);tr.AddNewlyCreatedDBObject(ad1, true);tr.AddNewlyCreatedDBObject(ad2, true);// And commit the transaction, of coursetr.Commit();}}}
}


这篇关于用.NET创建并且覆盖AutoCAD的标注样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

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)总结

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

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

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

Python清空Word段落样式的三种方法

《Python清空Word段落样式的三种方法》:本文主要介绍如何用python-docx库清空Word段落样式,提供三种方法:设置为Normal样式、清除直接格式、创建新Normal样式,注意需重... 目录方法一:直接设置段落样式为"Normal"方法二:清除所有直接格式设置方法三:创建新的Normal样

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro