创建、修改和复制维度样式 (.NET)
通过创建对象的实例,然后将其添加到 with the 方法中,可以创建新的维度样式。在将维度样式添加到表中之前,请使用属性设置新样式的名称。DimStyleTableRecordDimStyleTableAddName 您还可以复制现有样式或具有覆盖的样式。使用该方法将维度样式从源对象复制到维度样式。源对象可以是另一个对象、 、 、 或对象,甚至可以是对象。如果从其他尺寸样式复制样式设置,则会完全复制当前样式。如果从 、 或对象复制样式设置,则当前设置(包括任何对象覆盖)将复制到该样式中。如果复制对象的当前样式,则尺寸样式以及任何绘图覆盖都将复制到新样式中。CopyFromDimStyleTableRecordDimensionToleranceLeaderDatabaseDimensionToleranceLeaderDatabase 复制维度样式和覆盖本示例创建三个新的尺寸样式,并将当前设置分别从当前、给定尺寸样式和给定尺寸复制到每个新尺寸样式。在运行此示例之前,按照相应的设置操作,您会发现已创建不同的尺寸样式。Database
VB.NETImports 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 Then acTrans.GetObject(acCurDb.DimStyleTableId, OpenMode.ForWrite)
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
C#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) acTrans.GetObject(acCurDb.DimStyleTableId, OpenMode.ForWrite);
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();
}
}
VBA/ActiveX 代码参考Sub CopyDimStyles()
Dim newStyle1 As AcadDimStyle
Dim newStyle2 As AcadDimStyle
Dim newStyle3 As AcadDimStyle
Set newStyle1 = ThisDrawing.DimStyles. _
Add("Style 1 copied from a dim")
Call newStyle1.CopyFrom(ThisDrawing.ModelSpace(0))
Set newStyle2 = ThisDrawing.DimStyles. _
Add("Style 2 copied from Style 1")
Call newStyle2.CopyFrom(ThisDrawing.DimStyles. _
Item("Style 1 copied from a dim"))
Set newStyle2 = ThisDrawing.DimStyles. _
Add("Style 3 copied from the running drawing values")
Call newStyle2.CopyFrom(ThisDrawing)
End Sub
使用 DIMSTYLE 命令打开维度样式管理器。现在应该已经列出了三维样式。样式 1 应具有黄色尺寸线。样式 2 应与样式 1 相同。样式 3 应具有蓝色尺寸线。 相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-30 04:29
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.