A new dimension style is created by creating an instance of a DimStyleTableRecord object and then adding it to the DimStyleTable with the Add method. Before the dimension style is added to the table, set the name of the new style with the Name property.
新的标注样式可以通过创建一个 DimStyleTableRecord 对象的实例,然后使用 Add 方法把它添加到 DimStyleTable 中来创建。标注样式在添加到表中之前,请使用 Name 属性设置新样式的名字。
You can also copy an existing style or a style with overrides. Use the CopyFrom method to copy a dimension style from a source object to a dimension style. The source object can be another DimStyleTableRecord object, a Dimension, Tolerance, or Leader object, or even a Database object. If you copy the style settings from another dimension style, the current style is duplicated exactly. If you copy the style settings from a Dimension, Tolerance, or Leader object, the current settings, including any object overrides, are copied to the style. If you copy the current style of a Database object, the dimension style plus any drawing overrides are copied to the new style.
用户也可以复制一种现有样式或一组替代。使用 CopyFrom 方法,将标注样式从源对象复制到新标注样式中。源对象可以是另一个 DimStyleTableRecord 对象、标注、Tolerance 或 Leader 对象,甚至可以是 Database 对象。如果从另一个标注样式复制样式设置,则样式被精确复制。如果从标注、Tolerance 或 Leader 对象复制样式的设置,则当前设置,包括所有对象替代会被复制到新样式中。如果复制 Database 对象的样式,则活动标注样式加上所有的图形替代,会被复制到新样式。
This example creates three new dimension styles and copies the current settings from the current Database, a given dimension style, and a given dimension to each new dimension style respectively. By following the appropriate setup before running this example, you will find that different dimension styles have been created.
本样例创建三个新的标注样式,并将当前 Database、给定标注样式和给定标注的当前设置分别复制到各个新的标注样式中。如果在运行此样例之前进行适当的设置,将发现创建的是不同的标注样式。
创建新的图形并使其成为活动的图形。
在新的图形中创建线性标注。此标注应该是图形中仅有的对象。
将标注线的颜色更改为黄色。
将 DIMCLRD 系统变量改为 5(蓝色)。
运行以下样例:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CopyDimStyles")> _
Public Sub CopyDimStyles()
'' 获得当前数据库 Get the current database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开块表 Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
OpenMode.ForRead)
'' 以只读方式打开模型空间的块表记录 Open the Block table record Model space for read
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForRead)
Dim acObj As Object = Nothing
For Each acObjId As ObjectId In acBlkTblRec
'' Get the first object in Model space
acObj = acTrans.GetObject(acObjId, _
OpenMode.ForRead)
Exit For
Next
'' 以只读方式打开标注样式表 Open the DimStyle table for read
Dim acDimStyleTbl As DimStyleTable
acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId, _
OpenMode.ForRead)
Dim strDimStyleNames(2) As String
strDimStyleNames(0) = "Style 1 copied from a dim"
strDimStyleNames(1) = "Style 2 copied from Style 1"
strDimStyleNames(2) = "Style 3 copied from the running drawing values"
Dim nCnt As Integer = 0
''保存第一个标注样式的引用 Keep a reference of the first dimension style for later
Dim acDimStyleTblRec1 As DimStyleTableRecord = Nothing
'' 循环标注样式名数组 Iterate the array of dimension style names
For Each strDimStyleName As String In strDimStyleNames
Dim acDimStyleTblRec As DimStyleTableRecord
Dim acDimStyleTblRecCopy As DimStyleTableRecord = Nothing
''检查标注样式是否存在 Check to see if the dimension style exists or not
If acDimStyleTbl.Has(strDimStyleName) = False Then
If acDimStyleTbl.IsWriteEnabled = False ThenacDimStyleTbl.UpgradeOpen()
acDimStyleTblRec = New DimStyleTableRecord()
acDimStyleTblRec.Name = strDimStyleName
acDimStyleTbl.Add(acDimStyleTblRec)
acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, True)
Else
acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl(strDimStyleName), _
OpenMode.ForWrite)
End If
''确定新的标注样式是如何组装的 Determine how the new dimension style is populated
Select Case nCnt
'' Assign the values of the dimension object to the new dimension style
Case 0
Try
'' 指定一个对象给一个标注 Cast the object to a Dimension
Dim acDim As RotatedDimension = acObj
'' Copy the dimension style data from the dimension and
'' set the name of the dimension style as the copied settings
'' are unnamed.
acDimStyleTblRecCopy = acDim.GetDimstyleData()
acDimStyleTblRec1 = acDimStyleTblRec
Catch
'' Object was not a dimension
End Try
'' Assign the values of the dimension style to the new dimension style
Case 1
acDimStyleTblRecCopy = acDimStyleTblRec1
'' Assign the values of the current drawing to the dimension style
Case 2
acDimStyleTblRecCopy = acCurDb.GetDimstyleData()
End Select
'' Copy the dimension settings and set the name of the dimension style
acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy)
acDimStyleTblRec.Name = strDimStyleName
'' Dispose of the copied dimension style
acDimStyleTblRecCopy.Dispose()
nCnt = nCnt + 1
Next
'' 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("CopyDimStyles")]
public static void CopyDimStyles()
{
// 获得当前数据库 Get the current database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以只读方式打开模型空间的块表记录 Open the Block table record Model space for read
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
object acObj = null;
foreach (ObjectId acObjId in acBlkTblRec)
{
// Get the first object in Model space
acObj = acTrans.GetObject(acObjId,
OpenMode.ForRead);
break;
}
// Open the DimStyle table for read
DimStyleTable acDimStyleTbl;
acDimStyleTbl = acTrans.GetObject(acCurDb.DimStyleTableId,
OpenMode.ForRead) as DimStyleTable;
string[] strDimStyleNames = new string[3];
strDimStyleNames[0] = "Style 1 copied from a dim";
strDimStyleNames[1] = "Style 2 copied from Style 1";
strDimStyleNames[2] = "Style 3 copied from the running drawing values";
int nCnt = 0;
// Keep a reference of the first dimension style for later
DimStyleTableRecord acDimStyleTblRec1 = null;
// Iterate the array of dimension style names
foreach (string strDimStyleName in strDimStyleNames)
{
DimStyleTableRecord acDimStyleTblRec;
DimStyleTableRecord acDimStyleTblRecCopy = null;
// Check to see if the dimension style exists or not
if (acDimStyleTbl.Has(strDimStyleName) == false)
{
if (acDimStyleTbl.IsWriteEnabled == false) acDimStyleTbl.UpgradeOpen();
acDimStyleTblRec = new DimStyleTableRecord();
acDimStyleTblRec.Name = strDimStyleName;
acDimStyleTbl.Add(acDimStyleTblRec);
acTrans.AddNewlyCreatedDBObject(acDimStyleTblRec, true);
}
else
{
acDimStyleTblRec = acTrans.GetObject(acDimStyleTbl[strDimStyleName],
OpenMode.ForWrite) as DimStyleTableRecord;
}
// Determine how the new dimension style is populated
switch ((int)nCnt)
{
// Assign the values of the dimension object to the new dimension style
case 0:
try
{
// Cast the object to a Dimension
Dimension acDim = acObj as Dimension;
// Copy the dimension style data from the dimension and
// set the name of the dimension style as the copied settings
// are unnamed.
acDimStyleTblRecCopy = acDim.GetDimstyleData();
acDimStyleTblRec1 = acDimStyleTblRec;
}
catch
{
// Object was not a dimension
}
break;
// Assign the values of the dimension style to the new dimension style
case 1:
acDimStyleTblRecCopy = acDimStyleTblRec1;
break;
// Assign the values of the current drawing to the dimension style
case 2:
acDimStyleTblRecCopy = acCurDb.GetDimstyleData();
break;
}
// Copy the dimension settings and set the name of the dimension style
acDimStyleTblRec.CopyFrom(acDimStyleTblRecCopy);
acDimStyleTblRec.Name = strDimStyleName;
// Dispose of the copied dimension style
acDimStyleTblRecCopy.Dispose();
nCnt = nCnt + 1;
}
// 提交修改并销毁事务 Commit the changes and dispose of the transaction
acTrans.Commit();
}
}
Open the Dimension Style Manager using the DIMSTYLE command. You should now have three dimension styles listed. Style 1 should have a yellow dimension line. Style 2 should be the same as Style 1. Style 3 should have a blue dimension line.
利用 DIMSTYLE 命令打开标注样式管理器。现在应该列出了三种标注样式。样式 1 应该具有黄色的尺寸线,样式 2 应与样式 1 相同。样式 3 应具有蓝色尺寸线。