Using a standalone VBA to access MicroStation design files


AskInga logo

Original Article Date: Feb 1, 2003 

This application will help you to automate tasks where no graphics are involved -- for reporting, or for bulk modifications.
Originally published in MSM magazine February 2003

During my support career I remember a lot of requests like this: “We need a tool which would go through hundreds of design files and perform a certain action on them without user interaction.” Such a task could be anything like:

  1. Creating a list of files and their reference files, which are attached to every single master file (in MicroStation V8, they are actually attached to every single model).
  2. Creating a list of cells in a DGN (cell name and how many of every particular cell is in there).
  3. Changing a symbology of certain elements.
.



What was common to all of these requests was: “We don’t need to start MicroStation for that. We want to have a standalone application.”

This article will help you to create such an application. You’ll be able to perform any of the above tasks and much more without having MicroStation launched. But needless to say that MicroStation V8 must be installed on a machine where you want to use your application.

You most likely know that MicroStation V8 is delivered with the Microsoft VBA environment integrated and there is also a specific MicroStation and DGN object library available. This library can be referenced in your Visual Basic project and this way you can create a standalone executable (.EXE), which will be able to access DGN files and any MicroStation settings like inside MicroStation. The only limitation is that there is no object, which would allow you to display graphics (without MicroStation). But this is not our task for this moment.

Let’s have a look at the steps that are necessary to build an application which would list all the models in a given design file, and for every single model display a list of attached reference files (and let’s skip nesting attachments, just to keep this task simple). In addition, though, we will display the properties of our design file like Author, Comments, Last saved, Last saved by, etc.

The first step in creating this application is starting Microsoft Visual Basic 6.0, then creating a new project and choosing option Standard EXE. This will open a window with one form in it, called Form1. On this form we place a couple of items:

  1. One single line TextBox for the user to specify the name of the design file to query. (We could put some nice and fancy Browse… button here too, but we are trying to keep this example as simple as possible.)
  2. One multiline TextBox to display the text information about the content of our design file.
  3. One CommandButton to start the code that will open the DGN and display the information in the above TextBox.
  4. One CommandButton to exit the application.



At the end, the form can look similar to the figure below.

  1. Simple form to display information from a design file.

 



The next step is to make sure that our application “knows” MicroStation and DGN objects. In Visual Basic, there is a standard way of doing that. Simply go to the menu and choose Project > References… That will open a window like the one shown below.

  1. Project References window

 



This shows the list of all the available object libraries (References) that can be used (referenced) in our project. The ones that are referenced are marked with the check mark in front. Now scroll down the list and find the one called Bentley MicroStation DGN 8.0 Object Library, check it by clicking on the square and hit the OK button. This makes sure that you can now use any of the objects related to a design file and MicroStation (except Views, etc.).

The next step is not really necessary, but it’s good to do this to have a more readable code and user-friendlier interface.

  1. Rename the Text1 to DesignFileName.
    Change the Text property to Enter a DGN name.
  2. Rename the Text2 to OutputText.
    Change the Text property to empty string.
    Set Multiline property to True.
    Set ScrollBars property to 3 – Both.
  3. Rename the Command1 to Execute.
    Change the Caption property to Execute.
    Change the Default property to True.
  4. Rename the Command2 to Close.
    Change the Caption property to Close.
  5. Rename the Form1 to MSV8main.
    Change the Caption property to MicroStation V8 Viewer
    Resize the form and items that it will look similar to the image below.

  1. Modified form to display information from a design file

 



Now it’s time to put some code in our application and let’s start with the easy part. Double click on the Close button. That brings you to the Code Window and places these line for you:

Private Sub Close_Click()
End Sub

Then go and add the code that the subroutine will look like:

Private Sub Close_Click()
Unload Me
End Sub

Then go back and double click on the Execute button. Again you are in the Code Window and now we will add the lines to open a design file, read its properties, list its models and for every model list its attachments.

So we start with:

Private Sub Execute_Click()

Then type:

dim oDgn as des

You’ll see that Visual Basic immediately offers you a list of all the available objects (see the image above. By typing d, e, s, you know that the full name of this object is the DesignFile. Please note that the VB editor also makes sure that the capitalization is corrected after you leave this line.

So far we have this:

Private Sub Execute_Click()
Dim oDgn As DesignFile

Now we will need an object representing a list of all models in a design file.

Dim oModels As ModelReferences

Then an object for a single model.

Dim oModel As ModelReference

And then a similar pair for the attachments.

Dim oAttachments As Attachments
Dim oAttachment As Attachment

Then we will need a string variable to hold all the text information before actually displaying it to the user. This works much faster then adding any single line straight to the output TextBox.

Dim t As String

And finally two counters: one for models, one for attachments.

Dim m As Integer
Dim a As Integer

Now, before opening a DGN, let’s inform the user that our code is working:

OutputText = "Processing..."
DoEvents

That will make sure that the output TextBox shows Processing… while our application loads the MicroStation DLLs, open a design file and starts reading from it.

Here we have to open a design file, but also make sure that our application won’t crash if the user gives us a string that is not an existing design file name.

On Error GoTo notDGN
Set oDgn = OpenDesignFile(DesignFileName, True)

So by this we are trying to open a DGN with the name DesignFileName in a read-only mode (the second parameter True). If the OpenDesignFile() method fails, we jump to the end of our subroutine to the label notDGN and inform the user that the given name is Not a design file.

Exit Sub

notDGN:
OutputText = "Not a design file."
End Sub

From now on all the code lines will still appear sequentially, but above the Exit Sub line. We now have a design file open and can start reading from it. Let’s start with the file properties.

On Error Resume Next
With oDgn
t = t & "Author: " & .Author & vbNewLine
t = t & "Comments: " & .Comments & vbNewLine
t = t & "Company: " & .Company & vbNewLine
t = t & "Keywords: " & .Keywords & vbNewLine
t = t & "Manager: " & .Manager & vbNewLine
t = t & "Subject: " & .Subject & vbNewLine
t = t & "Title: " & .Title & vbNewLine
t = t & "Last saved: " & .DateLastSaved & vbNewLine
t = t & "Last saved by: " & .LastSavedBy & vbNewLine
t = t & "Editor: " & .Editor & vbNewLine
t = t & "Revision: " & .RevisionNumber & vbNewLine
t = t & vbNewLine
End With
On Error GoTo 0

The above lines simply take the oDgn.Author, oDgn.Comments, etc. properties append them to the t string variable, placing every property on a new line. The On Error Resume Next and On Error GoTo 0 lines are necessary to first skip the properties that are not defined and then return the error trapping to normal afterwards.

After that we can then go and step through all the models.

Set oModels = ActiveDesignFile.Models
m = 1
For Each oModel In oModels
t = t & "Model #" & m & ": " & oModel.Name & vbNewLine
' here we will add the code for the attachments
t = t & vbNewLine
m = m + 1
Next

It’s just a simple loop through all the models in our design file, displaying a counter (m) and appending a model name to the t string variable, placing every model on a new line (actually putting an extra empty line after each model). Then for each model we need to step through all the attachments.

Set oAttachments = oModel.Attachments
a = 1
For Each oAttachment In oAttachments
t = t & " Ref #" & a & ": "
t = t & oAttachment.LogicalName & ", "
t = t & oAttachment.LogicalDescription & ", "
t = t & oAttachment.DesignFile.FullName & vbNewLine
a = a + 1
Next

What this does is to take the current model oModel from the outer loop, getting its attachments oAttachments and then there is another (inner) simple loop through all the attachments in the current model, displaying a counter (a) and appending some basic attachment properties to the t string variable. Again one attachment per line.

Putting both the sections together we will get a code like this:

Set oModels = ActiveDesignFile.Models
m = 1
For Each oModel In oModels
t = t & "Model #" & m & ": " & oModel.Name & vbNewLine
Set oAttachments = oModel.Attachments
a = 1
For Each oAttachment In oAttachments
t = t & " Ref #" & a & ": "
t = t & oAttachment.LogicalName & ", "
t = t & oAttachment.LogicalDescription & ", "
t = t & oAttachment.DesignFile.FullName & vbNewLine
a = a + 1
Next
t = t & vbNewLine
m = m + 1
Next


The only necessary things at this moment are closing the design file.

oDgn.Close

and displaying all the gathered information to the output TextBox.

OutputText = t

After you’ve added all the above lines, you can start this application, then enter a design file name, and hit the Execute button. You should see the output similar to the image below.

  1. Application displaying information from a design file.



If all this works for you, you can now create a standalone application you will be able to execute straight from your operating system without starting the Visual Basic environment. Just go to menu Project > Project1 Properties. Rename the project to MsModelInfo then go menu File > Make MsModelInfo.EXE and in that dialog box hit the OK button. Then you can close Microsoft Visual Basic and you will be prompted to save your form and project) and finally test how your MsModelInfo.EXE works.

A small note at the end: Of course, the output can be formatted in a different way. The entire user interface can be much more sophisticated, but this is just a simple example showing that even with a few lines of a Basic code you can build a powerful tool that can help you in your everyday work.

The full code including the Visual Basic project can be found here:
275.zip

AskInga Article#275