3、Use Named Views使用命名视图
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.
命名视图存储在View表里,View表是图形数据库里的一个命名符号表。命名视图通过用Add方法将新视图添加到View表来创建。当我们往View表添加新的命名视图时,会生成一个默认的模型空间视图。
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方法,可以将命名视图从View表中删除。
Add a named view and set it current 添加命名视图并将其设置为当前视图
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)
' 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打开View表,读
ViewTable acViewTbl;
acViewTbl = acTrans.GetObject(acCurDb.ViewTableId,
OpenMode.ForRead) as ViewTable;
// Check to see if the named view 'View1' exists
//检查命名视图View1是否存在
if (acViewTbl.Has("View1") == false)
{
// Open the View table for write打开View表,写
acViewTbl.UpgradeOpen();
// Create a new View table record and name the view 'View1'
// 新建一个View表记录并命名为View1
ViewTableRecord acViewTblRec = new ViewTableRecord();
acViewTblRec.Name = "View1";
// Add the new View table record to the View table and the transaction
//添加到View表及事务
acViewTbl.Add(acViewTblRec);
acTrans.AddNewlyCreatedDBObject(acViewTblRec,true);
// Set 'View1' current置View1为当前视图
acDoc.Editor.SetCurrentView(acViewTblRec);
// Commit the changes提交事务
acTrans.Commit();
}
// Dispose of the transaction处置事务,回收内存
}
}
VBA/ActiveX Code Reference VBA/ActiveX代码参考
Sub CreateNamedView()
' Add a named view to the views collection
Dim viewObj As AcadView
Set viewObj = ThisDrawing.Views.Add("View1")
ThisDrawing.ActiveViewport.SetView viewObj
End Sub
Erase a named view 删除命名视图
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)
' 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;
// 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从View表删除命名视图
acViewTblRec.Erase();
// Commit the changes提交修改
acTrans.Commit();
}
// Dispose of the transaction
}
}
VBA/ActiveX Code Reference VBA/ActiveX代码参考
Sub EraseNamedView()
On Error Resume Next
Dim viewObj As AcadView
Set viewObj = ThisDrawing.Views("View1")
If Err = 0 Then
' Delete the view
viewObj.Delete
End If
End Sub