This article answers two questions about MicroStation view capture. MicroStation has up to eight visible views of a CAD model. You can capture any view as a raster image, in variety of formats (i.e. JPEG, BMP, etc.) …
Q How do I capture the contents of a MicroStation view as an image?
A
The CAPTURE VIEW
command does exactly what you want. It lets you save a view's contents
to a raster file. You can start the command from
MicroStation's Utilities|Image menu …
Alternatively, you can provide a manual keyin. It's always worth taking the time to understand MicroStation's keyin commands,
because you can attach them to function keys, your own icons, or use them in VBA macros.
To use the CAPTURE VIEW
command, follow these steps …
CAPTURE VIEW
CAPTURE VIEW;SELVIEW 1
(assuming you want to capture view 1)Q How can I automate screen capture using VBA?
A
I wrote a simple VBA subroutine CaptureView
. Subroutine CaptureView
starts the CAPTURE VIEW
command.
It also
sets the image file path and file name in the Capture Screen Output dialog and closes
the dialog automatically. That is, you don't have a modal dialog hanging around.
In this example, Subroutine Main
specifies the view number and file name to be used.
If you want to automate screen capture in a batch file, you would make a similar call after opening
each design file. You could also use VBA's CommandKeyin
property to let your user
pass the file name on the command line (e.g. vba run CaptureView.Main <file spec.>
).
Option Explicit' --------------------------------------------------------------------- ' CaptureView illustrates how to automate MicroStation view capture ' using VBA ' --------------------------------------------------------------------- ' Notice: ' Sample code provided by LA Solutions Ltd. ' This code is supplied with no claim for fitness for ' purpose and is not supported. You may freely copy this code ' provided this notice is retained in full. ' --------------------------------------------------------------------- ' For more VBA tips visit our web site ' http://www.la-solutions.co.uk/content/Publications.htm ' --------------------------------------------------------------------- ' This module references a class module CaptureScreenModalHandler ' that Implements IModalDialogEvents ' --------------------------------------------------------------------- ' MicroStation keyin: ' vba run CaptureView.Main ' ---------------------------------------------------------------------
Sub Main()' I've hard-coded the view number and file name here ' to illustrate the call syntax
CaptureView 1, "D:\temp\msview1.jpg" End Sub' --------------------------------------------------------------------- ' CaptureView automates the process of the CAPTURE VIEW command ' ---------------------------------------------------------------------
Sub CaptureView(viewNum As Integer, fileSpec As String)' Send a keyin that can be a command string
CadInputQueue.SendKeyin "null" Dim modalHandler As New CaptureScreenModalHandler AddModalDialogEventsHandler modalHandler modalHandler.FilePath = fileSpec' The following statement opens modal dialog "Capture Screen Output"
CadInputQueue.SendKeyin "capture view" CadInputQueue.SendKeyin "selview " & CStr(viewNum) RemoveModalDialogEventsHandler modalHandler CommandState.StartDefaultCommand End Sub
The code that handles the modal Capture Screen Output dialog is in a class CaptureScreenModalHandler
that Implements IModalDialogEvents
. The class has a FilePath
property
that provides the file name and file path information for the dialog's internal data.
Option Explicit' --------------------------------------------------------------------- ' CaptureView illustrates how to automate MicroStation view capture ' using VBA ' --------------------------------------------------------------------- ' Notice: ' Sample code provided by LA Solutions Ltd. ' This code is supplied with no claim for fitness for ' purpose and is not supported. You may freely copy this code ' provided this notice is retained in full. ' --------------------------------------------------------------------- ' For more VBA tips visit our web site ' http://www.la-solutions.co.uk/content/Publications.htm ' --------------------------------------------------------------------- ' This class handles MicroStation's 'Capture Screen Output' dialog ' and is used by the subroutine CaptureView in module CaptureView ' ---------------------------------------------------------------------
Private m_strFilePath As String Implements IModalDialogEvents' --------------------------------------------------------------------- ' Dialog Closed event handler does nothing ' ---------------------------------------------------------------------
Private Sub IModalDialogEvents_OnDialogClosed(ByVal DialogBoxName As String, ByVal DialogResult As MsdDialogBoxResult) End Sub' --------------------------------------------------------------------- ' FilePath Property ' ---------------------------------------------------------------------
Public Property Let FilePath(s As String) m_strFilePath = s End Property' --------------------------------------------------------------------- ' Dialog Opened event handler ' ---------------------------------------------------------------------
Private Sub IModalDialogEvents_OnDialogOpened(ByVal DialogBoxName As String, DialogResult As MsdDialogBoxResult) If DialogBoxName = "Capture Screen Output" Then Const Separator As String = "\" Dim path As String, _ file As String Dim pos As Integer' Split file specification into name and path
pos = InStrRev(m_strFilePath, Separator) If (0 < pos) Then path = Left(m_strFilePath, pos) file = Mid(m_strFilePath, pos + 1) End If' Start a command
CadInputQueue.SendCommand "MDL COMMAND MGDSHOOK,fileList_setFilterCmd *.jpg" CadInputQueue.SendCommand "MDL COMMAND MGDSHOOK,fileList_setDirectoryCmd " & path CadInputQueue.SendCommand "MDL COMMAND MGDSHOOK,fileList_setFileNameCmd " & file' Remove the following line to let the user close the dialog box.
DialogResult = msdDialogBoxResultOK End If' Capture Screen Output
End Sub
The source code above is available in a ZIP archive CaptureView.zip. Use the VBA editor's Import command to import the modules into your project.
Q How do I capture an image of a graphic element?
A Use the Element.GetPicture method in VBA, or Element.DrawToEnhancedMetafile if using a .NET language.
Search MicroStation VBA help for an example of Element.GetPicture.
Post questions about VBA to the MicroStation Programming Forum.