使平铺视口成为当前视口
 
 

You enter points and select objects in the current viewport. To make a viewport current, use the CVPORT system variable and specify the viewport by its number that you want to set current.

输入点和选择对象操作是在当前视口中进行的。要将某个视口置为当前视口,请使用 CVPORT 系统变量并指定你想设置为当前的视口的编号。

You can iterate through existing viewports to find a particular viewport. To do this, identify the Viewport table records with the name "*Active" using the Name property.

可以遍历现有视口以查找特定视口。要执行此任务,首先使用 Name 属性标识视口表记录并命名为"*Active"。

拆分视口,然后遍历各个窗口

This example splits the active viewport into two horizontal windows. It then iterates through all the tiled viewports in the drawing and displays the viewport name and the lower-left and upper-right corner for each viewport.

本例将活动视口拆分为两个水平窗口,然后遍历图形中的所有平铺视口并显示视口名称以及每个视口的左下角点和右上角点。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("SplitAndIterateModelViewports")> _
Public Sub SplitAndIterateModelViewports()
  '' 获得当前数据库  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 write
      Dim acVportTbl As ViewportTable
      acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId, _
                                     OpenMode.ForWrite)
 
      '' 以写的方式打开活动视口  Open the active viewport for write
      Dim acVportTblRec As ViewportTableRecord
      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _
                                        OpenMode.ForWrite)
 
      Dim acVportTblRecNew As ViewportTableRecord = New ViewportTableRecord()
 
      '' 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
      acVportTbl.Add(acVportTblRecNew)
      acTrans.AddNewlyCreatedDBObject(acVportTblRecNew, True)
 
      '' 为新的视口指定名字为'*Active' Assign the name '*Active' to the new Viewport
      acVportTblRecNew.Name = "*Active"
 
      '' 为新视口指定左下角点为现在的值  Use the existing lower left corner for the new viewport
      acVportTblRecNew.LowerLeftCorner = acVportTblRec.LowerLeftCorner
      '' Get half the X of the existing upper corner
      acVportTblRecNew.UpperRightCorner = New Point2d(acVportTblRec.UpperRightCorner.X, _
                                                      acVportTblRec.LowerLeftCorner.Y + _
                                                      ((acVportTblRec.UpperRightCorner.Y - _
                                                        acVportTblRec.LowerLeftCorner.Y) / 2))
      '' 重新计算活动视口的角点   Recalculate the corner of the active viewport
      acVportTblRec.LowerLeftCorner = New Point2d(acVportTblRec.LowerLeftCorner.X, _
                                                  acVportTblRecNew.UpperRightCorner.Y)
 
      '' 使用新的平铺视口布局更新屏幕显示  Update the display with the new tiled viewports arrangement
      acDoc.Editor.UpdateTiledViewportsFromDatabase()
 
      '' 遍历符号表的每一个对象  Step through each object in the symbol table
      For Each acObjId As ObjectId In acVportTbl
          ''以只读方式打开对象   Open the object for read
          Dim acVportTblRecCur As ViewportTableRecord
          acVportTblRecCur = acTrans.GetObject(acObjId, _
                                               OpenMode.ForRead)
 
          If (acVportTblRecCur.Name = "*Active") Then
              Application.SetSystemVariable("CVPORT", acVportTblRecCur.Number)
 
              Application.ShowAlertDialog("Viewport: " & acVportTblRecCur.Number & _
                                          " is now active." & _
                                          vbLf & "Lower left corner: " & _
                                          acVportTblRecCur.LowerLeftCorner.X & ", " & _
                                          acVportTblRecCur.LowerLeftCorner.Y & _
                                          vbLf & "Upper right corner: " & _
                                          acVportTblRecCur.UpperRightCorner.X & ", " & _
                                          acVportTblRecCur.UpperRightCorner.Y)
          End If
      Next
 
      '' 提交修改并销毁事务  Commit the changes and dispose of the transaction
      acTrans.Commit()
  End Using
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("SplitAndIterateModelViewports")]
public static void SplitAndIterateModelViewports()
{
  // 获得当前数据库  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 write
      ViewportTable acVportTbl;
      acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
                                     OpenMode.ForWrite) as ViewportTable;
 
      // 以写的方式打开活动视口  Open the active viewport for write
      ViewportTableRecord acVportTblRec;
      acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId,
                                        OpenMode.ForWrite) as ViewportTableRecord;
 
      ViewportTableRecord acVportTblRecNew = new ViewportTableRecord();
 
      // 添加新的视口到视口表和事务中  Add the new viewport to the Viewport table and the transaction
      acVportTbl.Add(acVportTblRecNew);
      acTrans.AddNewlyCreatedDBObject(acVportTblRecNew, true);
 
      // 为新的视口指定名字为'*Active' Assign the name '*Active' to the new Viewport
      acVportTblRecNew.Name = "*Active";
 
      // 为新视口指定左下角点为现在的值  Use the existing lower left corner for the new viewport
      acVportTblRecNew.LowerLeftCorner = acVportTblRec.LowerLeftCorner;
 
      // Get half the X of the existing upper corner
      acVportTblRecNew.UpperRightCorner = new Point2d(acVportTblRec.UpperRightCorner.X,
                                                      acVportTblRec.LowerLeftCorner.Y +
                                                      ((acVportTblRec.UpperRightCorner.Y -
                                                        acVportTblRec.LowerLeftCorner.Y) / 2));
 
      // 重新计算活动视口的角点   Recalculate the corner of the active viewport
      acVportTblRec.LowerLeftCorner = new Point2d(acVportTblRec.LowerLeftCorner.X,
                                                  acVportTblRecNew.UpperRightCorner.Y);
 
      // 使用新的平铺视口布局更新屏幕显示  Update the display with the new tiled viewports arrangement
      acDoc.Editor.UpdateTiledViewportsFromDatabase();
 
      // 遍历符号表的每一个对象  Step through each object in the symbol table
      foreach (ObjectId acObjId in acVportTbl)
      {
          //以只读方式打开对象   Open the object for read
          ViewportTableRecord acVportTblRecCur;
          acVportTblRecCur = acTrans.GetObject(acObjId,
                                               OpenMode.ForRead) as ViewportTableRecord;
 
          if (acVportTblRecCur.Name == "*Active")
          {
              Application.SetSystemVariable("CVPORT", acVportTblRecCur.Number);
 
              Application.ShowAlertDialog("Viewport: " + acVportTblRecCur.Number +
                                          " is now active." +
                                          "\nLower left corner: " +
                                          acVportTblRecCur.LowerLeftCorner.X + ", " +
                                          acVportTblRecCur.LowerLeftCorner.Y +
                                          "\nUpper right corner: " +
                                          acVportTblRecCur.UpperRightCorner.X + ", " +
                                          acVportTblRecCur.UpperRightCorner.Y);
          }
      }
 
      // 提交修改并销毁事务  Commit the changes and dispose of the transaction
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考