You are currently reviewing an older revision of this page.
Background Information
It may be useful for various different 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 Keyin 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
)
Boolean
Dim
oLv
Level
LevelExist =
False
For
Each
In
ActiveDesignFile.Levels
If
UCase(oLv.Name) = UCase(LevelName)
Then
True
Exit
End
Next
Sub
Texte_on_Level()
neuerLevel
' Check whether the new name was given as a parameter:
neuerLevel = KeyinArguments
' If no parameter is given, use "LevelOfText":
Len(Trim(newLevel)) = 0
new
Level =
"LevelOfText"
' If the new level name does not exist, then create the level:
Not
LevelExist(newLevel)
ActiveDesignFile.AddNewLevel newLevel
Set
oLv = ActiveDesignFile.Levels(newLevel)
' Look for all text and move it to the new level:
Ee
ElementEnumerator
Sc
New
ElementScanCriteria
Sc.ExcludeAllTypes
Sc.IncludeType msdElementTypeText
Ee = ActiveModelReference.Scan(Sc)
Do
While
Ee.MoveNext
Ee.Current.Level = oLv
Ee.Current.Rewrite
Loop
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