新しいブロックを作成するには、新しい BlockTableRecord オブジェクトを作成し、Add メソッドを使用して BlockTable オブジェクトに追加します。BlockTableRecord オブジェクトを作成したら、Name プロパティを使用して、ブロックと、AppendEntity メソッドでブロックが図面に挿入されたときに表示されるオブジェクトに名前を割り当てます。
次に、新しく作成した BlockTableRecord オブジェクトに、ジオメトリ オブジェクト、つまり他のブロックを追加します。オブジェクトは、AppendEntity メソッドで BlockTableRecord オブジェクトに追加されます。次に、新しい BlockReference オブジェクトを作成し、それをモデル空間または Layout オブジェクトに関連付けられている BlockTableRecord オブジェクトに追加することによって、ブロックのインスタンスを図面に挿入することができます。挿入されるブロックはブロック参照と呼ばれるオブジェクトです。
WBlock メソッドを使って、オブジェクトを別の図面ファイルに書き出すことによって、ブロックを作成することもできます。図面ファイルを、他の図面のブロックの定義として使用することができます。AutoCAD では、図面を他の図面に挿入するとブロックとして処理されます。
ブロックの定義の詳細については、製品のヘルプ システムで「概要 - ブロックを定義する」を参照してください。
次の例では、ブロックを定義し、その定義に円を追加します。その後で、ブロック参照としてブロックを図面に挿入します。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("CreatingABlock")> _
Public Sub CreatingABlock()
' Get the current database and start a transaction
Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
acCurDb = Application.DocumentManager.MdiActiveDocument.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
If Not acBlkTbl.Has("CircleBlock") Then
Using acBlkTblRec As New BlockTableRecord
acBlkTblRec.Name = "CircleBlock"
' Set the insertion point for the block
acBlkTblRec.Origin = New Point3d(0, 0, 0)
' Add a circle to the block
Using acCirc As New Circle
acCirc.Center = New Point3d(0, 0, 0)
acCirc.Radius = 2
acBlkTblRec.AppendEntity(acCirc)
acBlkTbl.UpgradeOpen()
acBlkTbl.Add(acBlkTblRec)
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True)
End Using
End Using
End If
' Save the new object to the database
acTrans.Commit()
' Dispose of the transaction
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("CreatingABlock")]
public void CreatingABlock()
{
// Get the current database and start a transaction
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!acBlkTbl.Has("CircleBlock"))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = "CircleBlock";
// Set the insertion point for the block
acBlkTblRec.Origin = new Point3d(0, 0, 0);
// Add a circle to the block
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(0, 0, 0);
acCirc.Radius = 2;
acBlkTblRec.AppendEntity(acCirc);
acBlkTbl.UpgradeOpen();
acBlkTbl.Add(acBlkTblRec);
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
}
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}
Sub CreatingABlock()
' Define the block
Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
insertionPnt(0) = 0
insertionPnt(1) = 0
insertionPnt(2) = 0
Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlock")
' Add a circle to the block
Dim center(0 To 2) As Double
Dim radius As Double
center(0) = 0
center(1) = 0
center(2) = 0
rad = 2
blockObj.AddCircle(center, rad)
End Sub