You are currently reviewing an older revision of this page.
Background Information
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.
Steps
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 exists, 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 Exit Function End If Next End Function Sub Texte_on_Level() 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" End If ' If the new level name does not exist, then create the level: If Not LevelExist(newLevel) Then ActiveDesignFile.AddNewLevel newLevel End If 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 Sc.ExcludeAllTypes Sc.IncludeType msdElementTypeText Set Ee = ActiveModelReference.Scan(Sc) Do While Ee.MoveNext Set Ee.Current.Level = oLv Ee.Current.Rewrite Loop End Sub
This procedure can also be used in conjunction with the allocation of priorities for levels to make sure, for example, that texts are always visible.
See Also
Changing the presentation order of Layers