创建图纸空间视口
 
 

Paper space viewports are created by creating instances of Viewport objects and adding them to the block reference used by a layout other than Model. The constructor for the Viewport object does not accept any parameters to create a new viewport object. Once an instance of a Viewport object is created, you can define its placement with the CenterPoint, Width and Height properties.

图纸空间视口是通过创建 Viewport 对象的实例并将其添加到用于非模型的布局的块引用而创建的。用于创建新的 Viewport 对象的 构造函数不接受任何参数.Viewport 对象的实例被创建后,用户就可以使用 CenterPointWidthHeight 的属性定义它的布置。

After creating the Viewport object, you can set properties of the view itself, such as viewing direction (ViewDirection property), lens length for perspective views (LensLength property), and grid display (GridOn property). You can also control properties of the viewport itself, such as layer (Layer property), linetype (Linetype property), and linetype scaling (LinetypeScale property).

创建 Viewport 对象后,用户可以设置它自己的视图的属性,像视图方向(ViewDirection 属性),透视图的镜头长度(LensLength 属性)和栅格的显示(GridOn 属性)。用户也可以控制视口自己的属性,像图层(Layer 属性),线型 (Linetype 属性), 和 线型比例 (LinetypeScale 属性)。

创建并启用浮动视口

This example sets paper space active, creates a floating viewport, defines the view for the viewport, and enables the viewport.

本例设置图纸空间为活动的,创建一个浮动视口,为视口定义视图并启动视口。

VB.NET

Imports System.Runtime.InteropServices
 
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, _
 EntryPoint:="?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")> _
Public Shared Function acedSetCurrentVPort(ByVal AcDbVport As IntPtr) As IntPtr
End Function
 
<CommandMethod("CreateFloatingViewport")> _
Public Sub CreateFloatingViewport()
  ''获得当前文档和数据库,并启动一个事务   Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      '' 以只读方式打开块表   Open the Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' Open the Block table record Paper space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), _
                                      OpenMode.ForWrite)
 
      '' Switch to the previous Paper space layout
      Application.SetSystemVariable("TILEMODE", 0)
      acDoc.Editor.SwitchToPaperSpace()
 
      '' Create a Viewport
      Dim acVport As Viewport = New Viewport()
      acVport.SetDatabaseDefaults()
      acVport.CenterPoint = New Point3d(3.25, 3, 0)
      acVport.Width = 6
      acVport.Height = 5
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acVport)
      acTrans.AddNewlyCreatedDBObject(acVport, True)
 
      '' Change the view direction
      acVport.ViewDirection = New Vector3d(1, 1, 1)
 
      '' Enable the viewport
      acVport.On = True
 
      '' Activate model space in the viewport
      acDoc.Editor.SwitchToModelSpace()
 
      '' Set the new viewport current via an imported ObjectARX function
      acedSetCurrentVPort(acVport.UnmanagedObject)
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub

C#

using System.Runtime.InteropServices;
 
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
 EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
extern static private int acedSetCurrentVPort(IntPtr AcDbVport);
 
[CommandMethod("CreateFloatingViewport")]
public static void CreateFloatingViewport()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开块表   Open the Block table for read
      BlockTable acBlkTbl;
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead) as BlockTable;
 
      // Open the Block table record Paper space for write
      BlockTableRecord acBlkTblRec;
      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                                      OpenMode.ForWrite) as BlockTableRecord;
 
      // Switch to the previous Paper space layout
      Application.SetSystemVariable("TILEMODE", 0);
      acDoc.Editor.SwitchToPaperSpace();
 
      // Create a Viewport
      Viewport acVport = new Viewport();
      acVport.SetDatabaseDefaults();
      acVport.CenterPoint = new Point3d(3.25, 3, 0);
      acVport.Width = 6;
      acVport.Height = 5;
 
      // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acVport);
      acTrans.AddNewlyCreatedDBObject(acVport, true);
 
      // Change the view direction
      acVport.ViewDirection = new Vector3d(1, 1, 1);
 
      // Enable the viewport
      acVport.On = true;
 
      // Activate model space in the viewport
      acDoc.Editor.SwitchToModelSpace();
 
      // Set the new viewport current via an imported ObjectARX function
      acedSetCurrentVPort(acVport.UnmanagedObject);
 
      // 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考

NoteTo set or modify aspects of the view (view direction, lens length, and so forth), the Viewport object's On property must be set to FALSE, and before you can set a viewport current the On property must be set to TRUE.

注意 若要设置或修改视图的外观(视图方向,透镜长度等等),视口对象的 On 属性必须设置成 FALSE,并且在设置一个视口为当前视口之前必须将 On 属性设置为 True。

创建四个浮动视口

This example takes the example from "Create and enable a floating viewport" and continues it by creating four floating viewports and setting the view of each to top, front, right, and isometric views, respectively. Each viewport is set to a scale of 1:2. To ensure there is something to see in these viewports, you may want to create a 3D solid sphere in Model space before trying this example.

本样例使用了“创建和启用浮动视口”中的例子,并以它为基础继续创建了四个浮动视口,同时分别将每个视口的视图设置为俯视图、主视图、右视图和等轴测视图。每个视图都设置比例为1:2。为确保在这些视口中能够看到一些内容,可以在进行本样例之前创建一个三维实体球体。

VB.NET

Imports System.Runtime.InteropServices
 
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<DllImport("acad.exe", CallingConvention:=CallingConvention.Cdecl, _
 EntryPoint:="?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")> _
Public Shared Function acedSetCurrentVPort(ByVal AcDbVport As IntPtr) As IntPtr
End Function
 
<CommandMethod("FourFloatingViewports")> _
Public Sub FourFloatingViewports()
  ''获得当前文档和数据库,并启动一个事务   Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      '' 以只读方式打开块表   Open the Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' Open the Block table record Paper space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), _
                                      OpenMode.ForWrite)
 
      '' Switch to the previous Paper space layout
      Application.SetSystemVariable("TILEMODE", 0)
      acDoc.Editor.SwitchToPaperSpace()
 
      Dim acPt3dCol As Point3dCollection = New Point3dCollection()
      acPt3dCol.Add(New Point3d(2.5, 5.5, 0))
      acPt3dCol.Add(New Point3d(2.5, 2.5, 0))
      acPt3dCol.Add(New Point3d(5.5, 5.5, 0))
      acPt3dCol.Add(New Point3d(5.5, 2.5, 0))
 
      Dim acVec3dCol As Vector3dCollection = New Vector3dCollection()
      acVec3dCol.Add(New Vector3d(0, 0, 1))
      acVec3dCol.Add(New Vector3d(0, 1, 0))
      acVec3dCol.Add(New Vector3d(1, 0, 0))
      acVec3dCol.Add(New Vector3d(1, 1, 1))
 
      Dim dWidth As Double = 2.5
      Dim dHeight As Double = 2.5
 
      Dim acVportLast As Viewport = Nothing
      Dim nCnt As Integer = 0
 
      For Each acPt3d As Point3d In acPt3dCol
          Dim acVport As Viewport = New Viewport()
          acVport.SetDatabaseDefaults();
          acVport.CenterPoint = acPt3d
          acVport.Width = dWidth
          acVport.Height = dHeight
 
          '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acVport)
          acTrans.AddNewlyCreatedDBObject(acVport, True)
 
          '' Change the view direction
          acVport.ViewDirection = acVec3dCol(nCnt)
 
          '' Enable the viewport
          acVport.On = True
 
          '' Record the last viewport created
          acVportLast = acVport
 
          '' Increment the counter by 1
          nCnt = nCnt + 1
      Next
 
      If acVportLast <> Nothing Then
          '' Activate model space in the viewport
          acDoc.Editor.SwitchToModelSpace()
 
          '' Set the new viewport current via an imported ObjectARX function
          acedSetCurrentVPort(acVportLast.UnmanagedObject)
      End If
 
      '' 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit()
  End Using
End Sub

C#

using System.Runtime.InteropServices;
 
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
 EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
extern static private int acedSetCurrentVPort(IntPtr AcDbVport);
 
[CommandMethod("FourFloatingViewports")]
public static void FourFloatingViewports()
{
  // Get the current document and database, and start a transaction
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开块表   Open the Block table for read
      BlockTable acBlkTbl;
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead) as BlockTable;
 
      // Open the Block table record Paper space for write
      BlockTableRecord acBlkTblRec;
      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                                      OpenMode.ForWrite) as BlockTableRecord;
 
      // Switch to the previous Paper space layout
      Application.SetSystemVariable("TILEMODE", 0);
      acDoc.Editor.SwitchToPaperSpace();
 
      Point3dCollection acPt3dCol = new Point3dCollection();
      acPt3dCol.Add(new Point3d(2.5, 5.5, 0));
      acPt3dCol.Add(new Point3d(2.5, 2.5, 0));
      acPt3dCol.Add(new Point3d(5.5, 5.5, 0));
      acPt3dCol.Add(new Point3d(5.5, 2.5, 0));
 
      Vector3dCollection acVec3dCol = new Vector3dCollection();
      acVec3dCol.Add(new Vector3d(0, 0, 1));
      acVec3dCol.Add(new Vector3d(0, 1, 0));
      acVec3dCol.Add(new Vector3d(1, 0, 0));
      acVec3dCol.Add(new Vector3d(1, 1, 1));
 
      double dWidth = 2.5;
      double dHeight = 2.5;
 
      Viewport acVportLast = null;
      int nCnt = 0;
 
      foreach (Point3d acPt3d in acPt3dCol)
      {
          Viewport acVport = new Viewport();
          acVport.SetDatabaseDefaults();
          acVport.CenterPoint = acPt3d;
          acVport.Width = dWidth;
          acVport.Height = dHeight;
 
          // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acVport);
          acTrans.AddNewlyCreatedDBObject(acVport, true);
 
          // Change the view direction
          acVport.ViewDirection = acVec3dCol[nCnt];
 
          // Enable the viewport
          acVport.On = true;
 
          // Record the last viewport created
          acVportLast = acVport;
 
          // Increment the counter by 1
          nCnt = nCnt + 1;
      }
 
      if (acVportLast != null)
      {
          // Activate model space in the viewport
          acDoc.Editor.SwitchToModelSpace();
 
          // Set the new viewport current via an imported ObjectARX function
          acedSetCurrentVPort(acVportLast.UnmanagedObject);
      }
 
      // 保存新对象到数据库中   Save the new objects to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考