It may be useful for various applications to bring all existing elements of a certain type in a drawing to a common level. Because this is tedious to do by hand, it makes sense to use VBA.
Below, I show a small example for how to use VBA tools to bring all text in a model to a new level and to create the new level if it does not already exist.
To test to see whether or not the new level exist, I used a helper function called "LevelExist" which returns true or false depending on whether there is already a level with the desired name. The tool is called by the subroutine "Text_on_Level". If no parameter is given, the new level will be named "LevelOfText". Alternatively, you can give the name of the level to put the text in as a parameter at the start of the VBA routine. If the .mvba project file is loaded, the following Key-in can be used:
vba run Text_on_Level NewName
The parameter "NewName" will be the name used for the newly created level.
Here is an example for how to do this:
' Function for checking the existence of a level
Private Function LevelExist(LevelName As String) As Boolean
Dim oLv As Level
LevelExist = False
For Each oLv In ActiveDesignFile.Levels
If UCase(oLv.Name) = UCase(LevelName) Then
LevelExist = True
Dim oLv As Level
Dim neuerLevel As String
' Check whether the new name was given as a parameter:
neuerLevel = KeyinArguments
' If no parameter is given, use "LevelOfText":
If Len(Trim(newLevel)) = 0 Then
newLevel = "LevelOfText"
' If the new level name does not exist, then create the level:
If Not LevelExist(newLevel) Then
Set oLv = ActiveDesignFile.Levels(newLevel)
' Look for all text and move it to the new level:
Dim Ee As ElementEnumerator
Dim Sc As New ElementScanCriteria
Set Ee = ActiveModelReference.Scan(Sc)
Do While Ee.MoveNext
Set Ee.Current.Level = oLv
This procedure can also be used in conjunction with the allocation of priorities for levels to make sure that texts are always visible.
Changing the presentation order of Layers