The Paper Space Viewport

Paper-space viewports, represented by PViewport objects, are created for plotting purposes using the AddPViewport method as follows:

Set PViewportObject = PaperSpaceCollection.AddPViewport(CenterPoint, _

Width, Height)

Table 14-5 explains this method's parameters.

Table 14-5. AddPViewport Method Parameters

Name

Data Type

Description

CenterPoint

Variant

A three-element array of doubles that specifies the viewport's

center coordinates

Width

Double

A positive number that specifies the viewport's width

Height

Double

A positive number that specifies the viewport's height

There is currently no programmatic means to quickly create, select, or manipulate polygonal PViewports, since they are made up of LwPolyline entities with additional information to relate the LwPolyline to the Pviewport.

The following example shows how to set up four different viewports for paper space. It's quite a long section of code, so the paragraphs that follow split it up into more manageable chunks and explain each step.

Public Sub CreatePViewports()

Dim objTopVPort As AcadPViewport

Dim objFrontVPort As AcadPViewport

Dim objRightVPort As AcadPViewport

Dim objIsoMetricVPort As AcadPViewport

Dim objLayout As AcadLayout

Dim objAcadObject As AcadObject

Dim dblPoint(2) As Double

Dim dblViewDirection(2) As Double

Dim dblOrigin(1) As Double

Dim dblHeight As Double

Dim dblWidth As Double

Dim varMarginLL As Variant

Dim varMarginUR As Variant

ThisDrawing.ActiveSpace = acPaperSpace

Set objLayout = ThisDrawing.ActiveLayout dblOrigin(0) = 0: dblOrigin(1) = 0

objLayout.PlotOrigin = dblOrigin

If objLayout.PlotRotation = ac0degrees Or objLayout.PlotRotation = _

ac180degrees Then objLayout.GetPaperSize dblWidth, dblHeight

Else objLayout.GetPaperSize dblHeight, dblWidth End If objLayout.GetPaperMargins varMarginLL, varMarginUR

dblWidth = dblWidth - (varMarginUR(0) + varMarginLL(0)) dblHeight = dblHeight - (varMarginUR(1) + varMarginLL(1)) dblWidth = dblWidth / 2# dblHeight = dblHeight / 2#

'Clear the layout of old PViewports

For Each objAcadObject In ThisDrawing.PaperSpace

If TypeName(objAcadObject) = "IAcadPViewport" Then objAcadObject.Delete

End If

Next

'create Top Viewport dblPoint(0) = dblWidth - dblWidth * 0.5 '25 dblPoint(1) = dblHeight - dblHeight * 0.5 '75 dblPoint(2) = 0#

Set objTopVPort = ThisDrawing.PaperSpace.AddPViewport(dblPoint, _

dblWidth, dblHeight)

'need to set view direction dblViewDirection(0) = 0 dblViewDirection(1) = 0 dblViewDirection(2) = 1 objTopVPort.Direction = dblViewDirection objTopVPort.Display acOn ThisDrawing.MSpace = True

ThisDrawing.ActivePViewport = objTopVPort ThisDrawing.Application.ZoomExtents

ThisDrawing.Application.ZoomScaled 0.5, acZoomScaledRelativePSpace

'create Front Viewport dblPoint(0) = dblWidth - dblWidth * 0.5 dblPoint(1) = dblHeight + dblHeight * 0.5 dblPoint(2) = 0

Set objFrontVPort = ThisDrawing.PaperSpace.AddPViewport(dblPoint, _

dblWidth, dblHeight)

'need to set view direction dblViewDirection(o) = 0 dblViewDirection(l) = -1 dblViewDirection(2) = 0 objFrontVPort.Direction = dblViewDirection objFrontVPort.Display acOn ThisDrawing.MSpace = True

ThisDrawing.ActivePViewport = objFrontVPort ThisDrawing.Application.ZoomExtents

ThisDrawing.Application.ZoomScaled 0.5, acZoomScaledRelativePSpace

'create Right Viewport dblPoint(o) = dblWidth + dblWidth * 0.5 dblPoint(l) = dblHeight - dblHeight * 0.5 dblPoint(2) = 0

Set objRightVPort = ThisDrawing.PaperSpace.AddPViewport(dblPoint, _

dblWidth, dblHeight)

'need to set view direction dblViewDirection(0) = 1 dblViewDirection(l) = 0 dblViewDirection(2) = 0 objRightVPort.Direction = dblViewDirection objRightVPort.Display acOn ThisDrawing.MSpace = True

ThisDrawing.ActivePViewport = objRightVPort ThisDrawing.Application.ZoomExtents

ThisDrawing.Application.ZoomScaled 0.5, acZoomScaledRelativePSpace

'create Isometric Viewport dblPoint(0) = dblWidth + dblWidth * 0.5 dblPoint(l) = dblHeight + dblHeight * 0.5 dblPoint(2) = 0

Set objIsoMetricVPort = ThisDrawing.PaperSpace.AddPViewport(dblPoint, _

dblWidth, dblHeight)

'need to set view direction dblViewDirection(0) = 1 dblViewDirection(1) = -1 dblViewDirection(2) = 1

objIsoMetricVPort.Direction = dblViewDirection objIsoMetricVPort.Display acOn ThisDrawing.MSpace = True

ThisDrawing.ActivePViewport = objIsoMetricVPort ThisDrawing.Application.ZoomExtents

ThisDrawing.Application.ZoomScaled 0.5, acZoomScaledRelativePSpace

'make paper space active again and we're almost done ThisDrawing.ActiveSpace = acPaperSpace ThisDrawing.Application.ZoomExtents

'regen in all viewports ThisDrawing.Regen acAllViewports End Sub

First, this code sets the active space to be paper space and uses the active Layout object's GetPaperSize method to find the paper's size. Because the viewports need to stay inside the plottable area, the code finds out the margins' size. It uses the GetPaperMargins method, which returns two two-element arrays that hold the offset of the lower-left and upper-right plot area from the lower-left and upper-right paper corners.

ThisDrawing.ActiveSpace = acPaperSpace

Set objLayout = ThisDrawing.ActiveLayout dblOrigin(0) = 0: dblOrigin(1) = 0

objLayout.PlotOrigin = dblOrigin

If objLayout.PlotRotation = ac0degrees Or objLayout.PlotRotation = _

ac180degrees Then objLayout.GetPaperSize dblWidth, dblHeight Else objLayout.GetPaperSize dblHeight, dblWidth End If objLayout.GetPaperMargins varMarginLL, varMarginUR

dblWidth = dblWidth - (varMarginUR(0) + varMarginLL(0)) dblHeight = dblHeight - (varMarginUR(1) + varMarginLL(1)) dblWidth = dblWidth / 2# dblHeight = dblHeight / 2#

Figure 14-5 shows a template of a viewport labeled with the different parameters to create.

Figure 14-5. Viewport parameters

The code then halves the width and height dimensions to use as each viewpoint's size. It also uses this information to calculate each viewport's center.

The code then ensures that no old viewports exist, since they'd overlap or interfere with our new viewport.

'Clear the layout of old PViewports

For Each objAcadObject In ThisDrawing.PaperSpace

If TypeName(objAcadObject) = "IAcadPViewport" Then objAcadObject.Delete End If

Next

Now the code creates the viewports. The code is similar for each viewport. Here's how it creates the Front viewport:

'create Front Viewport dblPoint(0) = dblWidth - dblWidth * 0.5 dblPoint(1) = dblHeight + dblHeight * 0.5 dblPoint(2) = 0

Set objFrontVPort = ThisDrawing.PaperSpace.AddPViewport(dblPoint, _

dblWidth, dblHeight)

Next, the code sets the viewport's viewpoint with the Direction property, as follows:

'need to set view direction dblViewDirection(0) = 0 dblViewDirection(1) = -1 dblViewDirection(2) = 0

objFrontVPort.Direction = dblViewDirection

Next, the code sets what the viewport displays using the Display method to enable the viewport display, and then setting the MSpace method to allow editing of model space from a floating paper-space viewport. Finally, the code sets the drawing to fill the viewport by scaling it to half its size relative to paper-space units.

objFrontVPort.Display acOn

ThisDrawing.MSpace = True

ThisDrawing.ActivePViewport = objFrontVPort ThisDrawing.Application.ZoomExtents

ThisDrawing.Application.ZoomScaled 0.5, acZoomScaledRelativePSpace

After the code sets up all the viewports, the only thing left for it to do is regenerate the drawing to see all of the changes.

'regen in all viewports ThisDrawing.Regen acAllViewports

Figure 14-6 shows this code's result.

AutoCAD gives you a number of properties to control the viewport's view. You can also control the layer on which the viewport is created and the linetype used to create the viewport. For full details, see Appendix A.

K

/ \ iflTi fflTi iTFTi

N.

t*

K

Figure 14-6. Four-viewport view

Was this article helpful?

+1 -2

Responses

  • Ida
    How to set view height width model space autocad?
    8 years ago
  • sophia
    How to rotate viewport in autocad 2006?
    8 years ago

Post a comment