使用命名视图
 
 

You can name and save a view you want to reuse. When you no longer need the view, you can remove it.

可以命名和保存要重复使用的视图。如果不再需要该视图,可以删除它。

Named views are stored in the View table, one of the named symbol tables in a drawing database. A named view is created with the Add method to add a new view to the View table. When you add the new named view to the View table, a default model space view is created.

命名视图存储在视图表(ViewTable)中,它是图形数据库中的一个命名符号表。要创建新视图,请使用 Add 方法将新视图添加到视图表(ViewTable)中。当添加新的命名视图到视图表中时,将创建一个默认的模型空间视图。

You name the view when you create it. The name of the view can be up to 255 characters long and contain letters, digits, and the special characters dollar sign ($), hyphen (-), and underscore (_).

可以在创建视图时命名该视图。视图的名称最多可包含 255 个字符,包含字母、数字和特殊字符美元符号 ($)、连字符 (-) 和下划线 (_)。

A named view can be removed from the View table by simply use the Erase method of the ViewTableRecord object you want to remove.

要从视图表中删除命名视图,只需简单的使用你想删除的 ViewTableRecord 对象的 Erase 方法即可。

添加一个命名视图并置为当前视图

The following example adds a named view to the drawing and sets it current.

下面的示例添加一个命名视图到图形中并设置它为当前视图。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("CreateNamedView")> _
Public Sub CreateNamedView()
  '' 获得当前数据库  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 View table for read
      Dim acViewTbl As ViewTable
      acViewTbl = acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForRead)
 
      '' 检查命名视图 'View1' 是否已经存在  Check to see if the named view 'View1' exists
      If (acViewTbl.Has("View1") = False) Then
          '' 以写的方式打开视图表  Open the View table for write
          acViewTbl.UpgradeOpen()
 
          '' Create a new View table record and name the view "View1"
          Dim acViewTblRec As ViewTableRecord = 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()
      End If
 
      '' 销毁事务  Dispose of the transaction
  End Using
End Sub

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;
 
      // 检查命名视图 'View1' 是否已经存在  Check to see if the named view 'View1' exists
      if (acViewTbl.Has("View1") == false)
      {
          // 以写的方式打开视图表  Open the View table for write
          acViewTbl.UpgradeOpen();
 
          // Create a new View table record and name the view 'View1'
          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
  }
}
VBA/ActiveX 代码参考

删除命名视图

The following example erases a named view from the drawing.

下面的示例从图形中删除一个命名视图。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("EraseNamedView")> _
Public Sub EraseNamedView()
  '' 获得当前数据库  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 View table for read
      Dim acViewTbl As ViewTable
      acViewTbl = acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForRead)
 
      '' 检查命名视图 'View1' 是否已经存在  Check to see if the named view 'View1' exists
      If (acViewTbl.Has("View1") = True) Then
          '' 以写的方式打开视图表  Open the View table for write
          acViewTbl.UpgradeOpen()
 
          '' 获得命名视图  Get the named view
          Dim acViewTblRec As ViewTableRecord
          acViewTblRec = acTrans.GetObject(acViewTbl("View1"), OpenMode.ForWrite)
 
          '' 从视图表中移除命名视图  Remove the named view from the View table
          acViewTblRec.Erase()
 
          '' 确认所做的修改  Commit the changes
          acTrans.Commit()
      End If
 
      '' 销毁事务  Dispose of the transaction
  End Using
End Sub

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;
 
      // 检查命名视图 'View1' 是否已经存在  Check to see if the named view 'View1' exists
      if (acViewTbl.Has("View1") == true)
      {
          // 以写的方式打开视图表  Open the View table for write
          acViewTbl.UpgradeOpen();
 
          // 获得命名视图  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
  }
}
VBA/ActiveX 代码参考