名前の付いたビューを使用する(.NET)

後で再使用するビューには、名前を付けて保存できます。そのビューが必要なくなれば、削除できます。

名前の付いたビューは、図面データベース内の名前の付いたシンボル テーブルの 1 つである、ビュー テーブルに格納されます。名前の付いたビューは Add メソッドを使用して作成され、ビュー テーブルに新しいビューが追加されます。ビュー テーブルに新しい名前の付いたビューを追加すると、既定のモデル空間ビューが作成されます。

ビューを作成するときは名前を付ける必要があります。ビューの名前の長さは、文字、数字、特殊文字のドル記号($)、ハイフン(-)、アンダースコア(_) を含めて 255 文字までです。

名前の付いたビューをビュー テーブルから削除するには、削除する ViewTableRecord オブジェクトの Erase メソッドを使用します。

名前の付いたビューを追加して現在のビューに設定する

次の例では、名前の付いたビューを図面に追加して、現在のビューに設定します。

C# の例

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("CreateNamedView")]
public static void CreateNamedView()
{
    // Get the current database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the View table for read
        ViewTable acViewTbl;
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId,
                                        OpenMode.ForRead) as ViewTable;

        // Check to see if the named view 'View1' exists
        if (acViewTbl.Has("View1") == false)
        {
            // Open the View table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite);

            // Create a new View table record and name the view 'View1'
            using (ViewTableRecord acViewTblRec = new ViewTableRecord())
            {
                acViewTblRec.Name = "View1";

                // Add the new View table record to the View table and the transaction
                acViewTbl.Add(acViewTblRec);
                acTrans.AddNewlyCreatedDBObject(acViewTblRec, true);

                // Set 'View1' current
                acDoc.Editor.SetCurrentView(acViewTblRec);
            }

            // Commit the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

名前の付いたビューを削除する

次の例では、名前の付いたビューを図面から削除します。

C# の例

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("EraseNamedView")]
public static void EraseNamedView()
{
    // Get the current database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the View table for read
        ViewTable acViewTbl;
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId,
                                      OpenMode.ForRead) as ViewTable;

        // Check to see if the named view 'View1' exists
        if (acViewTbl.Has("View1") == true)
        {
            // Open the View table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite);

            // Get the named view
            ViewTableRecord acViewTblRec;
            acViewTblRec = acTrans.GetObject(acViewTbl["View1"],
                                             OpenMode.ForWrite) as ViewTableRecord;

            // Remove the named view from the View table
            acViewTblRec.Erase();

            // Commit the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}