4、Use Tiled Viewports使用平铺视口
AutoCAD usually begins a new drawing using a single tiled viewport that fills the entire graphics area. You can split the drawing area of the Model tab to display several viewports simultaneously. For example, if you keep both the full and the detail views visible, you can see the effects of your detail changes on the entire drawing. In each tiled viewport, you can do the following:
AutoCAD通常是用一个充满整个图形区域的单一平铺视口来开始一个新绘图。我们可以分割模型卡的绘图区域让它同时显示多个视口。例如,如果我们让完整视图和局部细节视图同时可见,那么我们就能看到对局部细节视图的修改在整个图形上的改变效果。对每个平铺视口,我们都可以进行下列操作:
· Zoom, set the Snap, Grid, and UCS icon modes, and restore named views in individual viewports 缩放,设置捕捉、栅格及UCS图标模式,在本视口重建命名视图;
· Draw from one viewport to another when executing a command 一个视口正在执行命令时到另一个视口去绘图;
· Name a configuration of viewports so you can reuse it 命名视口配置以便再利用;
You can display tiled viewports in various configurations. How you display the viewports depends on the number and size of the views you need to see. Tiled viewports in model space are stored in the Viewport table.
我们可以用不同的配置显示平铺视口,怎样显示取决于我们想要看到的视图的数量和大小。模型空间的平铺视口存储在Viewport表里。
For further information and illustrations describing viewports, see “Set Model Space Viewports” in the AutoCAD User's Guide.
描述视口的更多信息和示例,参见AutoCAD用户手册的“设置模型空间视口”。
Tiled viewports are stored in the Viewports table. Each record in the Viewports table represents a single viewport and unlike other table records, there might be multiple Viewport table records with the same name. Each of the records with the same name are used to control which viewports are displayed.
平铺视口存储于Viewport表,每条Viewport表记录代表单个视口,而且,不像别的表记录,在Viewport表里,可以有多条同名的表记录。同名的每条表记录都用来控制视口的显示。
For example, the Viewport table records named "*Active" represent the tiled viewports that are currently displayed on the Model tab.
举例,名为“*Active”的Viewport表记录代表当前显示在模型卡上的平铺视口。
Topics in this section本小节主题
· Identify and Manipulate the Active Viewport 辨别和操作活动视口
· Make A Tiled Viewport Current 使平铺视口成为当前视口
4.1、Identify and Manipulate the Active Viewport辨别和操作活动视口
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:
活动视口用Viewport表中名为“*Active”的记录表示,这个名字不是唯一的,因为当前在模型卡里显示的全部平铺视口的名字都是“*Active”。每个显示的视口被赋予一个编码。活动视口的编码可通过下列方式获得:
· Retrieving the value of the CVPORT system variable 检索系统变量CVPORT的值;
· Using the ActiveViewportId property of the Editor object to get the object id for the active viewport and then open the Viewport object to access its Number property 用Editor对象的ActiveViewportId属性获取活动视口的对象标识id,然后打开Viewport对象访问它的Number属性;
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):
这些特性详细说明如下(以四个视口为例):

In this example:
这个例子中:
· Viewport 1视口1 - LowerLeftCorner左下角 = (0, .5), UpperRightCorner右上角 = (.5, 1)
· Viewport 2视口2 - LowerLeftCorner左下角 = (.5, .5), UpperRightCorner右上角 = (1, 1)
· Viewport 3视口3 - LowerLeftCorner左下角 = (0, 0), UpperRightCorner右上角 = (.5, .5)
· Viewport 4视口4 - LowerLeftCorner左下角 = (.5, 0), UpperRightCorner右上角 = (1, .5)
Create a new tiled viewport configuration with two horizontal windows 创建一个有两个水平窗口的视口配置
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)
' 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)
' 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)
' 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)
' 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打开、读Viewport表
ViewportTable acVportTbl;
acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
OpenMode.ForRead) as ViewportTable;
// Check to see if the named view 'TEST_VIEWPORT' exists
//检查视图‘TEST_VIEWPORT’是否存在
if (acVportTbl.Has("TEST_VIEWPORT") == false)
{
// Open the View table for write更新View表
acVportTbl.UpgradeOpen();
// Add the new viewport to the Viewport table and the transaction
//添加新视口到Viewport表并添加事务记录
ViewportTableRecord acVportTblRecLwr = new ViewportTableRecord();
acVportTbl.Add(acVportTblRecLwr);
acTrans.AddNewlyCreatedDBObject(acVportTblRecLwr, true);
// Name the new viewport 'TEST_VIEWPORT' and assign it to be
// the lower half of the drawing window
//新视口命名为‘TEST_VIEWPORT’并将绘图窗口的下半部分赋给它
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
//添加新视口到Viewport表并添加事务记录
ViewportTableRecord acVportTblRecUpr = new ViewportTableRecord();
acVportTbl.Add(acVportTblRecUpr);
acTrans.AddNewlyCreatedDBObject(acVportTblRecUpr, true);
// Name the new viewport 'TEST_VIEWPORT' and assign it to be
// the upper half of the drawing window
//新视口命名为‘TEST_VIEWPORT’并将绘图窗口的上半部分赋给它
acVportTblRecUpr.Name = "TEST_VIEWPORT";
acVportTblRecUpr.LowerLeftCorner = new Point2d(0, 0.5);
acVportTblRecUpr.UpperRightCorner = new Point2d(1, 1);
// To assign the new viewports as the active viewports, the
// viewports named '*Active' need to be removed and recreated
// based on 'TEST_VIEWPORT'.
// 将新视口设为活动视口,需要删除名为'*Active'的视口并基于‘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添加新视口到Viewport表并添加事务记录
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 Code Reference
Sub CreateModelViewport()
' Create a new viewport
Dim vportObj As AcadViewport
Set vportObj = ThisDrawing.Viewports.Add("TEST_VIEWPORT")
' Split vportObj into 2 horizontal windows
vportObj.Split acViewport2Horizontal
' Now set vportObj to be the active viewport
ThisDrawing.ActiveViewport = vportObj
End Sub
4.2、Make A Tiled Viewport Current使平铺视口为当前视口
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属性来分辨Viewport表中 名为“*Active”的表记录。
Split a viewport, then iterate through the windows 分割视口并遍历个窗口
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)
' 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);
// 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 Code Reference
Sub SplitandInterateModelViewports()
' Get the active viewport
Dim vportObj As AcadViewport
Set vportObj = ThisDrawing.ActiveViewport
' Split the viewport into 2 windows
vportObj.Split acViewport2Horizontal
' Iterate through the viewports,
' highlighting each viewport and displaying
' the upper right and lower left corners
' for each.
Dim vport As AcadViewport
Dim LLCorner As Variant
Dim URCorner As Variant
For Each vport In ThisDrawing.Viewports
ThisDrawing.ActiveViewport = vport
LLCorner = vport.LowerLeftCorner
URCorner = vport.UpperRightCorner
MsgBox "Viewport: " & vport.Name & " is now active." & _
vbCrLf & "Lower left corner: " & _
LLCorner(0) & ", " & LLCorner(1) & vbCrLf & _
"Upper right corner: " & _
URCorner(0) & ", " & URCorner(1)
Next vport
End Sub
410

被折叠的 条评论
为什么被折叠?



