Working With Levels In MicroStation VBA [CS]



 

This Client Server article is republished in its entirety from 2003 for reference purposes.

By Dan Tartaglia, Developer Support Analyst, Bentley Corporate Office
25 June 2003

Turning levels on and off

Before we go into detail on using MicroStation VBA, let's review how to turn a level on or off from within MicroStation V8. There are three settings that we need to modify. The first setting is:

If you open the Level Manager, the other two settings are:

Figure 1 shows the hierarchy of our level structure concerning the display of the elements:


Figure 1: Level structure concerning the display of the elements

It's easy to turn off a level in any of the above three settings using VBA, because if any one of the above settings is set, the level will not be displayed. Turning a level on can be tricky, because all three settings need to be set to display the level's elements.

As an exercise, do the following steps to see how each of the three settings affects each other:

  1. Open a DGN that contains at least one level that is used in the active model. Make sure the level is not set as the active level.
  2. Center at least one element that is on the level in the active view window.
  3. Open the Level Manager and Level Display (Ctrl+E) dialog boxes.
  4. Disable the level's Global Freeze setting. Enable its Global Display setting and Level Display dialog box's setting.
  5. Enable the level's Global Freeze setting. The elements on this level will disappear.
  6. Try to change the level's Global Display setting. Notice that this setting has no effect (the level always stays off). This is because the level's Global Freeze setting is enabled.
  7. Try clicking the level from the Level Display dialog box. Its display cannot be turned on or off for the same reason.
  8. Disable the level's Global Freeze setting and try changing the level's display using the Global Display setting. You can now turn the level's display on or off.
  9. Disable the level's Global Display setting.
  10. Try clicking the level from the Level Display dialog box. Its display still cannot be turned on because the Global Display setting is disabled.
  11. Enable the level's Global Display setting and try changing the level's display from the Level Display dialog box. Now the level's display can now be changed to on or off.

You should now see how these three settings affect one another in the order shown in Figure 1. We need to modify these settings in our VBA application.

How to turn the display of levels to Off

As I explained, any three of the settings that are shown in Figure 1 can be used to turn off the display of a level. Typically, the Level Display dialog box setting is the most commonly used for this purpose. Figure 2 will show what VBA properties are needed to set these three settings:


IsDisplayedInView Property
Read/Write Boolean
(syntax) object.IsDisplayedInView (View)[= Boolean]
(usage) oLevel.IsDisplayedInView(oView) = False


IsDisplayed Property

Read/Write Boolean
(syntax) object.IsDisplayed [= Boolean]
(usage) oLevel.IsDisplayed = False


IsFrozen Property

Read/Write Boolean
(syntax) object.IsFrozen [= Boolean]
(usage) oLevel.IsFrozen = True

Figure 2: The properties of the Level Object concerned with their display

 

The below example will turn off the three Level Object properties as shown in Figure 2 for the level "Bentley." The example will show you how to use all three of these properties, however, in a real world application you would normally use only one of the properties (most likely, the IsDisplayedInView Property).

 

* Tip: If a statement in a VBA macro begins with an apostrophe, it is only a comment. A lot of comments are added to these examples to explain what they do.

'-----------------------------------------------------------------------+
'
' name LevelOffExample
'
' author BSI 06/2003
'
'-----------------------------------------------------------------------+
Sub LevelOffExample()

Dim oLevel As Level
Dim oLevels As Levels
Dim oView As View
Dim viewIndex As Integer
Dim levelName As String

' We're going to change the display of this level
levelName = "Bentley"

' Sets the view object to View window 1
viewIndex = 1
Set oView = ActiveDesignFile.Views(viewIndex)

' Set the level's object with all the levels in the DGN
Set oLevels = ActiveDesignFile.Levels

' Iterate through all the levels
For Each oLevel In oLevels

' Process the level if it is found
If oLevel.Name = levelName Then

' Sets the Level Object
Set oLevel = ActiveDesignFile.Levels(levelName)

' Sets the Level Display in View property (Level Display dialog)
oLevel.IsDisplayedInView(oView) = False

' Sets the Global Display property (Level Manager dialog box)
oLevel.IsDisplayed = False

' Sets the Global Freeze property (Level Manager dialog box)
oLevel.IsFrozen = True

End If
Next oLevel

' Writes the change
oLevels.Rewrite

' Redraws the View windows
RedrawAllViews
End Sub

* All example code in this article can be copy/pasted into a MSVBA Project and should work correctly. The example code is created on V8 version 08.01.01.09.

How to turn the display of levels to On

To turn on the display of a level, all three of the settings will need to be set to display the level's elements. If any one of the settings is not set correctly, the level will not display. The information and properties in Figure 2 are also valid for turning a level on, just as it was for turning a level off. Besides the three Level properties, the rest of the code is exactly the same as the LevelOffExample() example.

To turn the display of the level to on, replace this code:

' Sets the Level Display in View property (Level Display dialog)
oLevel.IsDisplayedInView(oView) = False

' Sets the Global Display property (Level Manager dialog box)
oLevel.IsDisplayed = False

' Sets the Global Freeze property (Level Manager dialog box)
oLevel.IsFrozen = True
With this code:
' Make sure the Level Display property is True
If oLevel.IsDisplayedInView(oView) = False Then
oLevel.IsDisplayedInView(oView) = True
End If

' Make sure the Global Display property is True
If oLevel.IsDisplayed = False Then
oLevel.IsDisplayed = True
End If

' Make sure the Global Freeze property is False
If oLevel.IsFrozen = True Then
oLevel.IsFrozen = False
End If

What about Levels in a reference?

The same logic will work with levels in a reference attachment as well. The below example will change the display to on for the specified reference level "Bentley:"
'-----------------------------------------------------------------------+
'
' name RefLevelOnExample
'
' author BSI 06/2003
'
'-----------------------------------------------------------------------+
Sub RefLevelOnExample()

Dim oLevel As Level
Dim oLevels As Levels
Dim oView As View
Dim viewIndex As Integer
Dim levelName As String
Dim oAttachment As Attachment
Dim oAttachments As Attachments

' We're going to change the display of this level
levelName = "Bentley"

' Sets the view object to View window 1
viewIndex = 1
Set oView = ActiveDesignFile.Views(viewIndex)

Set oAttachments = ActiveModelReference.Attachments

' Iterate through each Reference
For Each oAttachment In oAttachments

' Set the Level's object with all the levels in the reference
Set oLevels = oAttachment.Levels

' Iterate through all the levels
For Each oLevel In oLevels

' Process the level if it is found
If oLevel.Name = levelName Then

' Sets the Level Object
Set oLevel = oAttachment.Levels(levelName)

' Make sure the Level Display property is True
If oLevel.IsDisplayedInView(oView) = False Then
oLevel.IsDisplayedInView(oView) = True
End If

' Make sure the Global Display property is True
If oLevel.IsDisplayed = False Then
oLevel.IsDisplayed = True
End If

' Make sure the Global Freeze property is False
If oLevel.IsFrozen = True Then
oLevel.IsFrozen = False
End If

' Rewrite the level status to the file
oAttachment.Levels.Rewrite

End If
Next oLevel
Next

' Send the command to Save Settings
CadInputQueue.SendCommand "fileDesign"

' Redraws the View windows
RedrawAllViews
End Sub

See Also

Client Server Archive

MicroStation Desktop TechNotes and FAQs

Comments or Corrections?

Bentley's Technical Support Group requests that you please confine any comments you have on this Wiki entry to this "Comments or Corrections?" section. THANK YOU!