You access the current view of a viewport in Model or Paper space by using the GetCurrentView method of the Editor object. The GetCurrentView method returns a ViewTableRecord object. You use the ViewTableRecord object to manipulate the magnification, position, and orientation of the view in the active viewport. Once the ViewTableRecord object has been changed, you update the current view of the active viewport with the SetCurrentView method.
用户通过使用 Editor 对象的 GetCurrentView 方法在模型或图纸空间中访问一个视口的当前视图。GetCurrentView 方法返回一个 ViewTableRecord 对象。使用 ViewTableRecord 对象可以操作活动视口中的视图的缩放比例、位置和方位。一旦 ViewTableRecord 对象被修改,就应该使用 SetCurrentView 方法更新活动视口的当前视图。
Some of the common properties that you will use to manipulate the current view are:
一些用户操作当前视图将要用到的公共属性:
This example code is a common procedure that is used by later examples. The Zoom procedure accepts four parameters to accomplish zooming to a boundary, panning or centering the view of a drawing, and scaling the view of a drawing by a given factor. The Zoom procedure expects all coordinate values to be provided in WCS coordinates.
本例代码是一个公共过程,它将被用于后面的部分的例子中。Zoom 过程接受四个参数以实现按边界缩放,平移或居中图形的视图,以及通过提供的比例因子缩放图形的视图。Zoom 过程假设所有的坐标值都是在 WCS 坐标系中提供的。
The parameters of the Zoom procedure are:
Zoom 过程的参数:
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Public Sub Zoom(ByVal pMin As Point3d, ByVal pMax As Point3d, _
ByVal pCenter As Point3d, ByVal dFactor As Double)
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim nCurVport As Integer = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))
'' 当没有点或仅有一个中心点时获得当前空间的范围 Get the extents of the current space when no points
'' or only a center point is provided
'' 检查当前是否是模型空间 Check to see if Model space is current
If acCurDb.TileMode = True Then
If pMin.Equals(New Point3d()) = True And _
pMax.Equals(New Point3d()) = True Then
pMin = acCurDb.Extmin
pMax = acCurDb.Extmax
End If
Else
'' 检查当前是否是图纸空间 Check to see if Paper space is current
If nCurVport = 1 Then
If pMin.Equals(New Point3d()) = True And _
pMax.Equals(New Point3d()) = True Then
pMin = acCurDb.Pextmin
pMax = acCurDb.Pextmax
End If
Else
'' 获得模型空间的范围 Get the extents of Model space
If pMin.Equals(New Point3d()) = True And _
pMax.Equals(New Point3d()) = True Then
pMin = acCurDb.Extmin
pMax = acCurDb.Extmax
End If
End If
End If
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
''获得当前视图 Get the current view
Using acView As ViewTableRecord = acDoc.Editor.GetCurrentView()
Dim eExtents As Extents3d
''转换 WCS 坐标系到 DCS Translate WCS coordinates to DCS
Dim matWCS2DCS As Matrix3d
matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection)
matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS
matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist, _
acView.ViewDirection, _
acView.Target) * matWCS2DCS
'' 如果指定了中心点,定义用于中心和缩放模式的范围的最小和最大点 If a center point is specified, define the
'' min and max point of the extents
'' for Center and Scale modes
If pCenter.DistanceTo(Point3d.Origin) <> 0 Then
pMin = New Point3d(pCenter.X - (acView.Width / 2), _
pCenter.Y - (acView.Height / 2), 0)
pMax = New Point3d((acView.Width / 2) + pCenter.X, _
(acView.Height / 2) + pCenter.Y, 0)
End If
'' 使用一个直线创建一个范围对象 译者注:此处可能有错误,因为直线只有 GeometricExtents 属性表示范围
'' Create an extents object using a line
Using acLine As Line = New Line(pMin, pMax)
eExtents = New Extents3d(acLine.Bounds.Value.MinPoint, _
acLine.Bounds.Value.MaxPoint)
End Using
'' 计算当前视图的宽度与高度的比率 Calculate the ratio between the width and height of the current view
Dim dViewRatio As Double
dViewRatio = (acView.Width / acView.Height)
'' 转换视图的范围 Tranform the extents of the view
matWCS2DCS = matWCS2DCS.Inverse()
eExtents.TransformBy(matWCS2DCS)
Dim dWidth As Double
Dim dHeight As Double
Dim pNewCentPt As Point2d
'' 检查中心点是否已提供(中心和缩放模式) Check to see if a center point was provided (Center and Scale modes)
If pCenter.DistanceTo(Point3d.Origin) <> 0 Then
dWidth = acView.Width
dHeight = acView.Height
If dFactor = 0 Then
pCenter = pCenter.TransformBy(matWCS2DCS)
End If
pNewCentPt = New Point2d(pCenter.X, pCenter.Y)
Else '' 配合窗口、范围和界限模式计算当前视图新的宽度和高度 Working in Window, Extents and Limits mode
'' Calculate the new width and height of the current view
dWidth = eExtents.MaxPoint.X - eExtents.MinPoint.X
dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y
'' 获得视图的中心点 Get the center of the view
pNewCentPt = New Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5), _
((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5))
End If
'' 检查新的宽度是否适合当前窗口 Check to see if the new width fits in current window
If dWidth > (dHeight * dViewRatio) Then dHeight = dWidth / dViewRatio
'' 调整大小并缩放视图 Resize and scale the view
If dFactor <> 0 Then
acView.Height = dHeight * dFactor
acView.Width = dWidth * dFactor
End If
''设置视图的中心 Set the center of the view
acView.CenterPoint = pNewCentPt
''设置当前视图 Set the current view
acDoc.Editor.SetCurrentView(acView)
End Using
'' 确认所做的修改 Commit the changes
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
static void Zoom(Point3d pMin, Point3d pMax, Point3d pCenter, double dFactor)
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
int nCurVport = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"));
// Get the extents of the current space no points
// or only a center point is provided
// 检查当前是否是模型空间 Check to see if Model space is current
if (acCurDb.TileMode == true)
{
if (pMin.Equals(new Point3d()) == true &&
pMax.Equals(new Point3d()) == true)
{
pMin = acCurDb.Extmin;
pMax = acCurDb.Extmax;
}
}
else
{
// 检查当前是否是图纸空间 Check to see if Paper space is current
if (nCurVport == 1)
{
// Get the extents of Paper space
if (pMin.Equals(new Point3d()) == true &&
pMax.Equals(new Point3d()) == true)
{
pMin = acCurDb.Pextmin;
pMax = acCurDb.Pextmax;
}
}
else
{
// 获得模型空间的范围 Get the extents of Model space
if (pMin.Equals(new Point3d()) == true &&
pMax.Equals(new Point3d()) == true)
{
pMin = acCurDb.Extmin;
pMax = acCurDb.Extmax;
}
}
}
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Get the current view
using (ViewTableRecord acView = acDoc.Editor.GetCurrentView())
{
Extents3d eExtents;
// Translate WCS coordinates to DCS
Matrix3d matWCS2DCS;
matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist,
acView.ViewDirection,
acView.Target) * matWCS2DCS;
// If a center point is specified, define the min and max
// point of the extents
// for Center and Scale modes
if (pCenter.DistanceTo(Point3d.Origin) != 0)
{
pMin = new Point3d(pCenter.X - (acView.Width / 2),
pCenter.Y - (acView.Height / 2), 0);
pMax = new Point3d((acView.Width / 2) + pCenter.X,
(acView.Height / 2) + pCenter.Y, 0);
}
// 使用一个直线创建一个范围对象 译者注:此处可能有错误,因为直线只有GeometricExtents属性表示范围 Create an extents object using a line
using (Line acLine = new Line(pMin, pMax))
{
eExtents = new Extents3d(acLine.Bounds.Value.MinPoint,
acLine.Bounds.Value.MaxPoint);
}
// 计算当前视图的宽度与高度的比率 Calculate the ratio between the width and height of the current view
double dViewRatio;
dViewRatio = (acView.Width / acView.Height);
// 转换视图的范围 Tranform the extents of the view
matWCS2DCS = matWCS2DCS.Inverse();
eExtents.TransformBy(matWCS2DCS);
double dWidth;
double dHeight;
Point2d pNewCentPt;
// 检查中心点是否已提供(中心和缩放模式) Check to see if a center point was provided (Center and Scale modes)
if (pCenter.DistanceTo(Point3d.Origin) != 0)
{
dWidth = acView.Width;
dHeight = acView.Height;
if (dFactor == 0)
{
pCenter = pCenter.TransformBy(matWCS2DCS);
}
pNewCentPt = new Point2d(pCenter.X, pCenter.Y);
}
else // 配合窗口、范围和界限模式计算当前视图新的宽度和高度 Working in Window, Extents and Limits mode
{
// Calculate the new width and height of the current view
dWidth = eExtents.MaxPoint.X - eExtents.MinPoint.X;
dHeight = eExtents.MaxPoint.Y - eExtents.MinPoint.Y;
// 获得视图的中心点 Get the center of the view
pNewCentPt = new Point2d(((eExtents.MaxPoint.X + eExtents.MinPoint.X) * 0.5),
((eExtents.MaxPoint.Y + eExtents.MinPoint.Y) * 0.5));
}
// 检查新的宽度是否适合当前窗口 Check to see if the new width fits in current window
if (dWidth > (dHeight * dViewRatio)) dHeight = dWidth / dViewRatio;
// 调整大小并缩放视图 Resize and scale the view
if (dFactor != 0)
{
acView.Height = dHeight * dFactor;
acView.Width = dWidth * dFactor;
}
// Set the center of the view
acView.CenterPoint = pNewCentPt;
// Set the current view
acDoc.Editor.SetCurrentView(acView);
}
// Commit the changes
acTrans.Commit();
}
}