Paper space viewports are created with the AddPViewport method. This method requires a center point and the width and height of the new viewport. Before creating the viewport, use the ActiveSpace property to set paper space as the current space (normally done by setting TILEMODE to 0).
After creating the PViewport object, you can set properties of the view itself, such as viewing direction (Direction 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).
Create and enable a floating viewport
This example switches AutoCAD to paper space, creates a floating viewport, sets the view, and enables the viewport.
Sub Ch9_SwitchToPaperSpace()
' Set the active space to paper space
ThisDrawing.ActiveSpace = acPaperSpace
' Create the paperspace viewport
Dim newVport As AcadPViewport
Dim center(0 To 2) As Double
center(0) = 3.25
center(1) = 3
center(2) = 0
Set newVport = ThisDrawing.PaperSpace. _
AddPViewport(center, 6, 5)
' Change the view direction for the viewport
Dim viewDir(0 To 2) As Double
viewDir(0) = 1
viewDir(1) = 1
viewDir(2) = 1
newVport.direction = viewDir
' Enable the viewport
newVport.Display True
' Switch to model space
ThisDrawing.MSpace = True
' Set newVport current
' (not always necessary but a good idea)
ThisDrawing.ActivePViewport = newVport
' Zoom Extents in model space
ZoomExtents
' Turn model space editing off
ThisDrawing.MSpace = False
' ZoomExtents in paperspace
ZoomExtents
End Sub
The order of steps in the preceding code is important. In general, things must be done in the same order they would be done at the AutoCAD command line. The only unexpected actions involve defining the view and enabling the viewport.
Create four floating viewports
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 view is scaled to half the scale of paper space. To ensure there is something to see in these viewports, you may want to create a 3D solid sphere before trying this example.
Sub Ch9_FourPViewports()
Dim topVport, frontVport As AcadPViewport
Dim rightVport, isoVport As AcadPViewport
Dim pt(0 To 2) As Double
Dim viewDir(0 To 2) As Double
ThisDrawing.ActiveSpace = acPaperSpace
ThisDrawing.MSpace = True
' Take the existing PViewport and make it the topVport
pt(0) = 2.5: pt(1) = 5.5: pt(2) = 0
Set topVport = ThisDrawing.ActivePViewport
'No need to set Direction for top view
topVport.center = pt
topVport.width = 2.5
topVport.height = 2.5
topVport.Display True
ThisDrawing.MSpace = True
ThisDrawing.ActivePViewport = topVport
ZoomExtents
ZoomScaled 0.5, acZoomScaledRelativePSpace
'Create and setup frontVport
pt(0) = 2.5: pt(1) = 2.5: pt(2) = 0
Set frontVport = ThisDrawing.PaperSpace. _
AddPViewport(pt, 2.5, 2.5)
viewDir(0) = 0: viewDir(1) = 1: viewDir(2) = 0
frontVport.direction = viewDir
frontVport.Display acOn
ThisDrawing.MSpace = True
ThisDrawing.ActivePViewport = frontVport
ZoomExtents
ZoomScaled 0.5, acZoomScaledRelativePSpace
'Create and setup rightVport
pt(0) = 5.5: pt(1) = 5.5: pt(2) = 0
Set rightVport = ThisDrawing.PaperSpace. _
AddPViewport(pt, 2.5, 2.5)
viewDir(0) = 1: viewDir(1) = 0: viewDir(2) = 0
rightVport.direction = viewDir
rightVport.Display acOn
ThisDrawing.MSpace = True
ThisDrawing.ActivePViewport = rightVport
ZoomExtents
ZoomScaled 0.5, acZoomScaledRelativePSpace
'Create and set up isoVport
pt(0) = 5.5: pt(1) = 2.5: pt(2) = 0
Set isoVport = ThisDrawing.PaperSpace. _
AddPViewport(pt, 2.5, 2.5)
viewDir(0) = 1: viewDir(1) = 1: viewDir(2) = 1
isoVport.direction = viewDir
isoVport.Display acOn
ThisDrawing.MSpace = True
ThisDrawing.ActivePViewport = isoVport
ZoomExtents
ZoomScaled 0.5, acZoomScaledRelativePSpace
'Finish: Perform a regen in all viewports
ThisDrawing.Regen True
End Sub