リージョンを BlockTableRecord オブジェクトに追加するには、Region オブジェクトのインスタンスを作成し、それを BlockTableRecord に追加します。BlockTableRecord オブジェクトに追加する前に、閉じたループを形成するオブジェクトに基づいてリージョンを計算する必要があります。CreateFromCurves 関数は、オブジェクトの入力配列によって形成されるすべての閉じたループからリージョンを作成します。CreateFromCurves メソッドは、DBObjectCollection オブジェクトを受け取って返します。
AutoCAD は、閉じた 2D および平面 3D ポリラインを個別のリージョンに変換し、次に閉じた平面ループを形成するポリライン、線分、および曲線を変換します。3 つ以上の曲線が端点を共有しているときは、その結果のリージョンは、任意です。このため、実際に CreateFromCurves メソッドで複数のリージョンが作成されることがあります。作成された各リージョンを BlockTableRecord オブジェクトに追加する必要があります。
次の例では、単一の円からリージョンを作成します。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AddRegion")]
public static void AddRegion()
{
// Get the current document and 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 write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create an in memory circle
using (Circle acCirc = new Circle())
{
acCirc.Center = new Point3d(2, 2, 0);
acCirc.Radius = 5;
// Adds the circle to an object array
DBObjectCollection acDBObjColl = new DBObjectCollection();
acDBObjColl.Add(acCirc);
// Calculate the regions based on each closed loop
DBObjectCollection myRegionColl = new DBObjectCollection();
myRegionColl = Region.CreateFromCurves(acDBObjColl);
Region acRegion = myRegionColl[0] as Region;
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acRegion);
acTrans.AddNewlyCreatedDBObject(acRegion, true);
// Dispose of the in memory circle not appended to the database
}
// Save the new object to the database
acTrans.Commit();
}
}