识别和操纵活动视口
 
 

The active viewport is represented in the Viewports table by a record named "*Active", which is not a unique name as all tiled viewports currently displayed on the Model tab are named "*Active". Each tiled viewport that is displayed is assigned a number. The number of the active viewport can be obtained by:

活动视口在视口表中通过记录名 “*Active” 表示,它不是唯一的,模型标签中当前显示的所有平铺视口都被命名为 “*Active”。显示的每一个平铺视口都指定了一个编号。活动视口的编号可以通过下面方法获得:

Once you have the active viewport, you control its display properties, enable drafting aids for the viewport such as grid and snap, as well as the size of the viewport itself. Tiled viewports are defined by two corner points: lower-left and upper-right. The LowerLeftCorner and UpperRightCorner properties represent the graphic placement of the viewport on the display.

一旦获得活动视口,就可以控制它的显示属性,启用视口的像栅格和捕捉之类的辅助绘图功能,也包括视口自己的尺寸。平铺视口是使用左下角和右上角这两个角点定义的。LowerLeftCorner UpperRightCorner 属性表示图形在显示时视口的位置。

A single tiled viewport configuration has a lower-left corner of (0,0) and an upper-right corner of (1,1). The lower-left corner of the drawing window is always represented by the point of (0,0), and the upper-right corner is presented by (1,1) no matter the number of tiled viewports on the Model tab. When more than one tiled viewport is displayed, the lower-left and upper-right corners will vary but one viewport will have a lower-left corner of (0,0) and another will have an upper-right corner of (1,1)

一个简单的平铺视口配置有左下角点(0,0) 和 右上角点(1,1)。不论在模型标签上的平铺视口的数量是多少,图形窗口左下角点始终为(0,0),而右上角点为(1,1)的点也会出现。当显示的平铺视口数量大于一个时,左下和右上角将被改变,但是一个视口将有左下角点(0,0)而另外一个将有右上角点(1,1)。

These properties are defined as follows (using a four-way split as an example):

可以通过如下方式定义这些特性(以四向拆分为例):

在本例中:

使用两个水平的窗口创建一个新的平铺视图配置

The following example creates a two horizontal viewports as a named viewport configuration and redefines the active display.

下面的示例创建一个有两个视口的命名视口配置并重新定义为活动的。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("CreateModelViewport")> _
Public Sub CreateModelViewport()
  '' 获得当前数据库  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 Viewport table for read
      Dim acVportTbl As ViewportTable
      acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId, OpenMode.ForRead)
 
      '' 检查命名视图 'TEST_VIEWPORT' 是否存在  Check to see if the named view 'TEST_VIEWPORT' exists
      If (acVportTbl.Has("TEST_VIEWPORT") = False) Then
          '' 以写的方式打开视图表  Open the View table for write
          acVportTbl.UpgradeOpen()
 
          '' 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
          Dim acVportTblRecLwr As ViewportTableRecord = New ViewportTableRecord()
          acVportTbl.Add(acVportTblRecLwr)
          acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, True)
 
          '' 将新视口命名为 'TEST_VIEWPORT' 并指定它为图形窗口的下半部分   Name the new viewport 'TEST_VIEWPORT' and assign it to be
          '' the lower half of the drawing window
          acVportTblRecLwr.Name = "TEST_VIEWPORT"
          acVportTblRecLwr.LowerLeftCorner = New Point2d(0, 0)
          acVportTblRecLwr.UpperRightCorner = New Point2d(1, 0.5)
 
          '' 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
          Dim acVportTblRecUpr As ViewportTableRecord = New ViewportTableRecord()
          acVportTbl.Add(acVportTblRecUpr)
          acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, True)
 
          '' 将新视口命名为 'TEST_VIEWPORT' 并指定它为图形窗口的下半部分   Name the new viewport 'TEST_VIEWPORT' and assign it to be
          '' the upper half of the drawing window
          acVportTblRecUpr.Name = "TEST_VIEWPORT"
          acVportTblRecUpr.LowerLeftCorner = New Point2d(0, 0.5)
          acVportTblRecUpr.UpperRightCorner = New Point2d(1, 1)
 
          '' 若要指定新的视口作为活动视口,"*Active"名字的视口需要删除然后使用'TEST_VIEWPORT'重建   To assign the new viewports as the active viewports, the 
          '' viewports named '*Active' need to be removed and recreated
          '' based on 'TEST_VIEWPORT'.
 
          '' 遍历符号表的每一个对象  Step through each object in the symbol table
          For Each acObjId As ObjectId In acVportTbl
              ''以只读方式打开对象   Open the object for read
              Dim acVportTblRec As ViewportTableRecord
              acVportTblRec = acTrans.GetObject(acObjId, _
                                                OpenMode.ForRead)
 
              '' 判断是否是活动窗口,如果是就删除  See if it is one of the active viewports, and if so erase it
              If (acVportTblRec.Name = "*Active") Then
                  acVportTblRec.UpgradeOpen()
                  acVportTblRec.Erase()
              End If
          Next
 
          '' 复制新视口并把它设置成活动视口  Clone the new viewports as the active viewports
          For Each acObjId As ObjectId In acVportTbl
              ''以只读方式打开对象   Open the object for read
              Dim acVportTblRec As ViewportTableRecord
              acVportTblRec = acTrans.GetObject(acObjId, _
                                                OpenMode.ForRead)
 
              '' 判断是否是活动窗口,如果是就删除  See if it is one of the active viewports, and if so erase it
              If (acVportTblRec.Name = "TEST_VIEWPORT") Then
                  Dim acVportTblRecClone As ViewportTableRecord
                  acVportTblRecClone = acVportTblRec.Clone()
 
                  '' 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
                  acVportTbl.Add(acVportTblRecClone)
                  acVportTblRecClone.Name = "*Active"
                  acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, True)
              End If
          Next
 
          '' 使用新的平铺视口布局更新屏幕显示  Update the display with the new tiled viewports arrangement
          acDoc.Editor.UpdateTiledViewportsFromDatabase()
 
          '' 确认所做的修改  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;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("CreateModelViewport")]
public static void CreateModelViewport()
{
  // 获得当前数据库  Get the current database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 启动一个事务  Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开视口表   Open the Viewport table for read
      ViewportTable acVportTbl;
      acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
                                     OpenMode.ForRead) as ViewportTable;
 
      // 检查命名视图 'TEST_VIEWPORT' 是否存在  Check to see if the named view 'TEST_VIEWPORT' exists
      if (acVportTbl.Has("TEST_VIEWPORT") == false)
      {
          // 以写的方式打开视图表  Open the View table for write
          acVportTbl.UpgradeOpen();
 
          // 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
          ViewportTableRecord acVportTblRecLwr = new ViewportTableRecord();
          acVportTbl.Add(acVportTblRecLwr);
          acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, true);
 
          // 将新视口命名为 'TEST_VIEWPORT' 并指定它为图形窗口的下半部分   Name the new viewport 'TEST_VIEWPORT' and assign it to be
          // the lower half of the drawing window
          acVportTblRecLwr.Name = "TEST_VIEWPORT";
          acVportTblRecLwr.LowerLeftCorner = new Point2d(0, 0);
          acVportTblRecLwr.UpperRightCorner = new Point2d(1, 0.5);
 
          // 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
          ViewportTableRecord acVportTblRecUpr = new ViewportTableRecord();
          acVportTbl.Add(acVportTblRecUpr);
          acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, true);
 
          // 将新视口命名为 'TEST_VIEWPORT' 并指定它为图形窗口的下半部分   Name the new viewport 'TEST_VIEWPORT' and assign it to be
          // the upper half of the drawing window
          acVportTblRecUpr.Name = "TEST_VIEWPORT";
          acVportTblRecUpr.LowerLeftCorner = new Point2d(0, 0.5);
          acVportTblRecUpr.UpperRightCorner = new Point2d(1, 1);
 
          // 若要指定新的视口作为活动视口,"*Active"名字的视口需要删除然后使用'TEST_VIEWPORT'重建   To assign the new viewports as the active viewports, the 
          // viewports named '*Active' need to be removed and recreated
          // based on 'TEST_VIEWPORT'.
 
          // 遍历符号表的每一个对象  Step through each object in the symbol table
          foreach (ObjectId acObjId in acVportTbl)
          {
              //以只读方式打开对象   Open the object for read
              ViewportTableRecord acVportTblRec;
              acVportTblRec = acTrans.GetObject(acObjId,
                                                OpenMode.ForRead) as ViewportTableRecord;
 
              // 判断是否是活动窗口,如果是就删除  See if it is one of the active viewports, and if so erase it
              if (acVportTblRec.Name == "*Active")
              {
                  acVportTblRec.UpgradeOpen();
                  acVportTblRec.Erase();
              }
          }
 
          // 复制新视口并把它设置成活动视口  Clone the new viewports as the active viewports
          foreach (ObjectId acObjId in acVportTbl)
          {
              //以只读方式打开对象   Open the object for read
              ViewportTableRecord acVportTblRec;
              acVportTblRec = acTrans.GetObject(acObjId,
                                                OpenMode.ForRead) as ViewportTableRecord;
 
              // 判断是否是活动窗口,如果是就删除  See if it is one of the active viewports, and if so erase it
              if (acVportTblRec.Name == "TEST_VIEWPORT")
              {
                  ViewportTableRecord acVportTblRecClone;
                  acVportTblRecClone = acVportTblRec.Clone() as ViewportTableRecord;
 
                  // 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
                  acVportTbl.Add(acVportTblRecClone);
                  acVportTblRecClone.Name = "*Active";
                  acTrans.AddNewlyCreatedDBObject(acVportTblRecClone, true);
              }
          }
 
          // 使用新的平铺视口布局更新屏幕显示  Update the display with the new tiled viewports arrangement
          acDoc.Editor.UpdateTiledViewportsFromDatabase();
 
          // Commit the changes
          acTrans.Commit();
      }
 
      // Dispose of the transaction
  }
}
VBA/ActiveX 代码参考