Q How do I find a view's screen index with a VBA macro?
A
Similar questions are posted now and then on the
Bentley Discussion Groups.
We wrote the example GetScreenIndex
function to illustrate a solution.
Q How do I change a view's settings with a VBA macro?
A
Similar questions are posted now and then on the
Bentley Discussion Groups.
We wrote the example ToggleFill
to illustrate two approaches to the first question:
change a view's fill mode.
We wrote the example RenderMode
to illustrate an approach to the second question:
change a view's render mode.
MicroStation works with one or more physical screens. The screen index tells you on which screen a particular view is presented. Unfortunately, VBA doesn't provide a method to find the screen index.
The following code shows how to get the screen index of a given view.
We use MDL functions that are available in one of the numerous libraries (DLLs) that accompany MicroStation.
To use an MDL function, we must first Declare
it,
to inform VBA about its call syntax and in which DLL it can be found …
Declare PtrSafe Function mdlWindow_screenNumGet Lib "stdmdlbltin.dll" (ByVal windowP As LongPtr) As Long Declare PtrSafe Function mdlWindow_viewWindowGet Lib "stdmdlbltin.dll" (ByVal viewNum As Long) As Long
The function GetScreenIndex
encapsulates the use of the MDL functions.
It also hides the inconvenient insistence by MDL on using zero-based indexing for most things,
including views and screens, instead of VBA's almost-consistent one-based indexing …
' --------------------------------------------------------------------- ' GetScreenIndex ' Get the screen index of the specified view ' Input: View number 1-based index ' Returns: Screen number 1-based index ' --------------------------------------------------------------------- Function GetScreenIndex(ByVal view As Integer) As Integer ' MDL functions all use zero-based indexing GetScreenIndex = 1 + mdlWindow_screenNumGet(mdlWindow_viewWindowGet(view - 1)) End Function
Finally, here's an example usage of GetScreenIndex
to show a view's screen index.
In this example, the view number is hard-coded: you would probably want to use this code in some more productive way …
' --------------------------------------------------------------------- ' Main entry point ' --------------------------------------------------------------------- Sub Main() Dim screen As Integer, _ view As Integer view = 3 screen = GetScreenIndex(view) Debug.Print "View " & CStr(view) & " is on screen " & CStr(screen) End Sub
The source code is provided in the VBA module ToggleFill.mvba
in
ZIP archive Toggle-Fill.zip.
Copy this file to a VBA folder, such as …
C:\Program Files\Bentley\Workspace\Standards\vba
Then you can use the module as supplied, or open it with the VBA Project Manager to examine the source code. If you want to use it without modification, the MicroStation keyin is …
vba run [toggle-fill]modToggleFill.Main <setting> [, <view number>]
The setting
is 0
or 1
. The view number is optional.
The Main
subroutine parses the command line into a setting
and view
.
In this example, the setting
value should be 0
or 1
: a 0
indicates that we want to turn off the fill attribute of the specified view;
a 1
indicates that we want to turn on the fill attribute of that view.
There are alternative procedures, ToggleFillQueueCommand
and ToggleFillVBA
, that
serve the same purpose but go about their task in quite different ways.
ToggleFillQueueCommand
is based on a recorded macro. The VBA Project Manager tends to sequence
MicroStation commands. It examines the internals of a dialog box to record a variable that is changed,
and queues the appropriate command in the macro. It's a pragmatic approach, but the result lacks elegance.
Also, it has the cosmetic drawback that it flashes the View Attributes dialog as it changes
the fill setting.
The ToggleFillVBA
procedure uses the MicroStation VBA object model. It references a View
object, applies the setting
value to the DisplayFill
property of that object,
then tells the View
to update its display. The result is succinct, elegant, and cosmetically appealing.
Procedure RenderMode
collects user keyin arguments from MicroStation's command line
and converts them into a view number and render mode. It expects a keyin of the form …
vba run [ViewControl]ViewUtilities.RenderMode <view>,<mode>' Example:
vba run [ViewControl]ViewUtilities.RenderMode 1,0' sets view 1 to wireframe
vba run [ViewControl]ViewUtilities.RenderMode 2,6' sets view 2 to smooth shading
The first argument is a view number in the range 1…8. The second argument is a render mode value in the range 0…11. The default render mode is 0 (wireframe).
If the keyin arguments appear to be valid, then RenderMode
continues to do the real work of changing the view render mode
in the ChangeViewRenderMode
procedure …
Option Explicit Option Base 0 Sub RenderMode() Dim view As Integer, _ nArgs As Integer Dim mode As MsdRenderingMode mode = msdRenderingModeWireFrame Dim args() As String args = Split(KeyinArguments, ",") nArgs = 1 + UBound(args) - LBound(args) If (1 < nArgs) Then mode = CInt(args(1)) If (0 < nArgs) Then view = CInt(args(0)) ChangeViewRenderMode view, mode End If End Sub
Procedure ChangeViewRenderMode
gets a View
object reference, sets its rendering mode, and updates the view …
Sub ChangeViewRenderMode(view As Integer, mode As MsdRenderingMode) On Error GoTo err_ChangeViewRenderMode Dim oView As view Set oView = ActiveDesignFile.Views(view) oView.RenderMode = mode oView.Redraw Exit Sub err_ChangeViewRenderMode: MsgBox "Error " & CStr(Err.Number) & ": " & Err.Description, vbOKOnly Or vbCritical, "ChangeViewRenderMode Error" End Sub
The source code for ChangeViewRenderMode
described above is supplied in
project file ViewControl.mvba
that is contained in
ZIP archive ViewControl.zip.
You can use this as supplied, or modify it
to suit your needs.
Copy this file to a VBA folder, such as …
C:\Program Files\Bentley\Workspace\Standards\vba
Post questions about MicroStation programming to the MicroStation Programming Forum.