本文主要是介绍用.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的标注样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!