DimStyleTableRecord オブジェクトのインスタンスを作成し、Add メソッドを使用して DimStyleTable に追加することで、新しい寸法スタイルが作成されます。寸法スタイルをテーブルに追加する前に、Name プロパティで新しいスタイルの名前を設定します。
既存のスタイルまたは優先のスタイルをコピーすることもできます。コピー元のオブジェクトから寸法スタイルにコピーするには、CopyFrom メソッドを使用します。コピー元のオブジェクトは、別の DimStyleTableRecord オブジェクト、Dimension、Tolerance、または Leader オブジェクト、さらには Database オブジェクトでもかまいません。スタイル設定を別の寸法スタイルからコピーした場合、現在のスタイルが正確に複製されます。Dimension、Tolerance、または Leader オブジェクトからスタイル設定をコピーした場合、オブジェクトの優先設定を含む現在の設定が、スタイルにコピーされます。Database オブジェクトの現在のスタイルをコピーした場合、寸法スタイルに加えてすべての図面の優先設定が、新しいスタイルにコピーされます。
次の例では、3 つの寸法スタイルを新規作成し、現在の Database の設定、指定した寸法スタイル、および指定した寸法を、新しい寸法スタイルにコピーします。適切な設定を行ってからこの例を実行すると、異なる寸法スタイルが作成されていることが分かります。
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 Then acDimStyleTbl.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(); } }
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[寸法スタイル管理]コマンドを使用して、寸法スタイル管理を開きます。現在 3 つの寸法スタイルが表示されています。Style 1 は、黄色の寸法線です。Style 2 は Style 1 と同じです。Style 3 は青色の寸法線です。