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

相关文章

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

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 在不同

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

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

Git打标签从本地创建到远端推送的详细流程

《Git打标签从本地创建到远端推送的详细流程》在软件开发中,Git标签(Tag)是为发布版本、标记里程碑量身定制的“快照锚点”,它能永久记录项目历史中的关键节点,然而,仅创建本地标签往往不够,如何将其... 目录一、标签的两种“形态”二、本地创建与查看1. 打附注标http://www.chinasem.cn

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

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

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

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

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