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

相关文章

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P

Linux lvm实例之如何创建一个专用于MySQL数据存储的LVM卷组

《Linuxlvm实例之如何创建一个专用于MySQL数据存储的LVM卷组》:本文主要介绍使用Linux创建一个专用于MySQL数据存储的LVM卷组的实例,具有很好的参考价值,希望对大家有所帮助,... 目录在Centos 7上创建卷China编程组并配置mysql数据目录1. 检查现有磁盘2. 创建物理卷3. 创

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

使用easy connect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题

《使用easyconnect之后,maven无法使用,原来需要配置-Djava.net.preferIPv4Stack=true问题》:本文主要介绍使用easyconnect之后,maven无法... 目录使用easGWowCy connect之后,maven无法使用,原来需要配置-DJava.net.pr

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti