The Model Space Viewport

Model-space viewports give you a number of windows in which to view and edit your model. The MAXACTVP system variable controls the maximum number of active viewports, and it's initially set to 64. Remember, though, that each new viewport adversely affects performance, so keep the number of active viewports to a minimum.

The following code shows how to create a model-space viewport. The paragraphs after the code explain this fairly involved procedure.

Public Sub CreateViewport()

Dim objViewPort As AcadViewport

Dim objCurrentViewport As AcadViewport

Dim varLowerLeft As Variant

Dim dblViewDirection(2) As Double

Dim strViewPortName As String strViewPortName = InputBox("Enter a name for the new viewport.") 'user cancelled

If strViewPortName = "" Then Exit Sub 'check if viewport already exists On Error Resume Next

Set objViewPort = ThisDrawing.Viewports.Item(strViewPortName) If Not objViewPort Is Nothing Then MsgBox "Viewport already exists" Exit Sub End If

'Create a new viewport

Set objViewPort = ThisDrawing.Viewports.Add(strViewPortName)

'Split the screen viewport into 4 windows objViewPort.Split acViewport4

For Each objCurrentViewport In ThisDrawing.Viewports If objCurrentViewport.LowerLeftCorner(0) = 0 Then If objCurrentViewport.LowerLeftCorner(1) = 0 Then 'this takes care of Top view dblViewDirection(0) = 0 dblViewDirection(1) = 0 dblViewDirection(2) = 1

objCurrentViewport.Direction = dblViewDirection

Else

'this takes care of Front view dblViewDirection(0) = 0 dblViewDirection(1) = -1 dblViewDirection(2) = 0

objCurrentViewport.Direction = dblViewDirection End If End If

If objCurrentViewport.LowerLeftCorner(0) = 0.5 Then If objCurrentViewport.LowerLeftCorner(1) = 0 Then 'this takes care of the Right view dblViewDirection(0) = 1 dblViewDirection(1) = 0 dblViewDirection(2) = 0

objCurrentViewport.Direction = dblViewDirection Else

'this takes care of the Isometric view dblViewDirection(0) = 1 dblViewDirection(1) = -1 dblViewDirection(2) = 1

objCurrentViewport.Direction = dblViewDirection End If End If

Next

'make viewport active to see effects of changes ThisDrawing.ActiveViewport = objViewPort

End Sub

First, use the Viewports collection's Add method to create a new viewport. This method has the following syntax:

Set ViewportObject = ViewportsCollection.Add(ViewportName)

This method has one parameter, ViewportName, a String that contains the new Viewport object's name.

Here's how the example code implements this method:

'Create a new viewport

Set objViewPort = ThisDrawing.Viewports.Add(strViewPortName)

Next, the code uses the Split method to create the windows in the viewport.

ViewportObject.Split NumberOfWindows

The NumberOfWindows parameter must be one of the AutoCAD AcViewportSplitType constants in Table 14-2.

Table 14-2. AcViewportSplitType Constants

Constant

Value Description acViewport2Horizontal 0

acViewport2Vertical 1

acViewport3Left 2

acViewport3Right 3

acViewport3Horizontal 4

acViewport3Vertical 5

acViewport3Above 6

acViewport3Below 7

acViewport4 8

Splits the viewport horizontally into two equal sections.

Splits the viewport vertically into two equal sections.

Splits the viewport into two vertical halves. The left half is split horizontally into two equal sections.

Splits the viewport into two vertical halves. The right half is split horizontally into two equal sections.

Splits the viewport horizontally into three equal sections.

Splits the viewport vertically into three equal sections.

Splits the viewport into two horizontal halves. The top half is a single viewport. The bottom half is split horizontally into two equal sections.

Splits the viewport into two horizontal halves. The bottom half is a single viewport. The top half is split horizontally into two equal sections.

Splits the viewport horizontally and vertically into four equal sections.

The following example shows how the code sample implements this method. For the effects of this method to become apparent, reset the viewport as the active viewport. You can reset the active viewport at the very end of the code, when it's time to view the changes.

'Split the screen viewport into 4 windows objViewPort.Split acViewport4

■ Note Although the code calls the Split method, the changes don't appear until the ActiveViewport property makes the viewport active. You have to do this even if the viewport was active before the call to Split. A viewport does not have to be active for this method to work, but you must reset it to active, or you won't see the method's results.

You can identify each viewport using either the LowerLeftCorner or the UpperRightCorner property, each of which holds a two-element array of doubles. Figure 14-3 applies to the code sample using acViewport4.

1

2

3 (0.5,0.5)

4

(0,0) Figure 14-3. Viewports

Table 14-3 indicates the coordinates used to create the viewports as shown in Figure 14-3.

Table 14-3. Viewport Coordinates

Viewport LowerLeftCorner Property UpperRightCorner Property

The following code shows how this section's example code uses the LowerLeftCorner property to identify the viewports.

For Each objCurrentViewport In ThisDrawing.Viewports If objCurrentViewport.LowerLeftCorner(0) = 0 Then If objCurrentViewport.LowerLeftCorner(1) = 0 Then 'this takes care of Top view dblViewDirection(0) = 0 dblViewDirection(1) = 0 dblViewDirection(2) = 1

Else

'this takes care of Front view dblViewDirection(0) = 0 dblViewDirection(1) = -1 dblViewDirection(2) = 0

objCurrentViewport.Direction = dblViewDirection End If End If

If objCurrentViewport.LowerLeftCorner(0) = 0.5 Then If objCurrentViewport.LowerLeftCorner(1) = 0 Then 'this takes care of the Right view dblViewDirection(0) = 1 dblViewDirection(1) = 0 dblViewDirection(2) = 0

objCurrentViewport.Direction = dblViewDirection Else

'this takes care of the Isometric view dblViewDirection(0) = 1 dblViewDirection(1) = -1 dblViewDirection(2) = 1 objCurrentViewport.Direction = dblViewDirection End If End If Next

Set the Direction property for each viewport. This property has the following syntax:

ViewportObject.Direction = ViewDirection

ViewDirection is a three-element array of doubles that specifies the direction from which the drawing is viewed for that viewport.

Users typically have a Top, Front, Right, and maybe an isometric view of their drawing. Each of these views has a specific set of points relative to an imaginary target point at (0, 0, 0), which defines the direction of view. Table 14-4 lists the standard views and their Direction properties.

Table 14-4. Views and Their Direction Properties

View

Direction

Top

(0, 0, 1)

Bottom

(0, 0, -1)

Front

(0, -1, 0)

Back

(0, 1, 0)

Left

(-1, 0, 0)

Right

(1, 0, 0)

Northeast

(1, 1, 1)

Northwest

(-1, 1, 1)

Southeast

(1, -1, 1)

Southwest

(-1, -1, 1)

In this section's example, the following code sets the Top view's Direction property: 'this takes care of the Top view dblViewDirection(0) = 0: dblViewDirection(1) = 0: dblViewDirection(2) = 1 objCurrentViewport.Direction = dblViewDirection

When you finish setting up the viewports, reset the viewport to be the active viewport to see the effects of your changes. Here's how to set the active viewport in code using the ActiveViewport property:

ThisDrawing.ActiveViewport = ViewportObject

At the end of the macro, use the following code to see the code's effects: ThisDrawing.ActiveViewport = objViewPort

Figure 14-4 shows the result of running this macro on a drawing.

That may have seemed like a lot of work, but it's really not as difficult as it looks. Experimentation is your greatest ally when dealing with viewports. Remember to use the LowerLeftCorner and UpperRightCorner properties to determine which window you are dealing with.

You must activate a viewport before you can change it. To tell which viewport is active, look for the viewport with the bold border (as in Figure 14-6). Also, only the active viewport contains the crosshair cursor. When you finish making changes, reset the viewport to see them.

Figure 14-4 shows the result of running this macro on a drawing.

That may have seemed like a lot of work, but it's really not as difficult as it looks. Experimentation is your greatest ally when dealing with viewports. Remember to use the LowerLeftCorner and UpperRightCorner properties to determine which window you are dealing with.

You must activate a viewport before you can change it. To tell which viewport is active, look for the viewport with the bold border (as in Figure 14-6). Also, only the active viewport contains the crosshair cursor. When you finish making changes, reset the viewport to see them.

Was this article helpful?

0 0

Responses

  • sarah
    How to change view in autocad by VBA?
    8 years ago

Post a comment