親オブジェクトを決定する(.NET)

グラフィカル オブジェクトは、モデル空間やペーパー空間などの BlockTableRecord オブジェクトに追加されます。モデル空間やペーパー空間を表すブロックを参照するには、BlockTable オブジェクトを使用します。特定の空間ではなく現在の空間で作業を行う場合は、CurrentSpaceId プロパティを使用して、現在のデータベースから現在の空間の ObjectId を取得します。

モデル空間およびペーパー空間のブロック テーブル レコードの ObjectId は、プロパティを使用して BlockTable オブジェクトから、または DatabaseServices 名前空間の SymbolUtilityServices クラスの GetBlockModelSpaceId および GetBlockPaperSpaceId メソッドを使用して取得できます。

モデル空間、ペーパー空間、または現在の空間にアクセスする

次の例は、モデル空間、ペーパー空間、または現在の空間に関連付けられたブロック テーブル レコードにアクセスする方法を示しています。ブロック テーブル レコードを参照すると、ブロック テーブル レコードに新しい行が追加されます。

C# の例

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("AccessSpace")]
public static void AccessSpace()
{
    // 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 for read
        BlockTableRecord acBlkTblRec;

        // Request which table record to open
        PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
        pKeyOpts.Message = "\nEnter which space to create the line in ";
        pKeyOpts.Keywords.Add("Model");
        pKeyOpts.Keywords.Add("Paper");
        pKeyOpts.Keywords.Add("Current");
        pKeyOpts.AllowNone = false;
        pKeyOpts.AppendKeywordsToMessage = true;

        PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

        if (pKeyRes.StringResult == "Model")
        {
            // Get the ObjectID for Model space from the Block table
            acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                            OpenMode.ForWrite) as BlockTableRecord;
        }
        else if (pKeyRes.StringResult == "Paper")
        {
            // Get the ObjectID for Paper space from the Block table
            acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                                            OpenMode.ForWrite) as BlockTableRecord;
        }
        else
        {
            // Get the ObjectID for the current space from the database
            acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId,
                                            OpenMode.ForWrite) as BlockTableRecord;
        }

        // Create a line that starts at 2,5 and ends at 10,7
        using (Line acLine = new Line(new Point3d(2, 5, 0),
                                      new Point3d(10, 7, 0)))
        {
            // Add the new object to the block table record and the transaction
            acBlkTblRec.AppendEntity(acLine);
            acTrans.AddNewlyCreatedDBObject(acLine, true);
        }

        // Save the new line to the database
        acTrans.Commit();
    }
}