Use key-in commands when learning VBA

Written by Rod Wing.

Use key-in commands when learning VBA

Non-programmers attempting to learn VBA can be easily overwhelmed by the complicated object definitions. While familiarizing yourself with VBA utilize MicroStation key-in commands as much as possible to ease the transition. Many MicroStation key-ins can be used in VBA using the CadInputQueue.SendKeyin method. As you become more proficient in VBA you can start replacing the key-in commands with more efficient MicroStation VBA methods.

Consider the two macros below, ChangeToByLevel1 and ChangeToByLevel2. They both change all elements in the active model to the Default level and use ByLevel symbology. Most MicroStation users can understand the first macro as it uses all MicroStation key-in commands to accomplish the task. The second macro uses more complex MicroStation VBA methods to directly modify the elements.

Sub ChangeToByLevel1()
    ' set desired symbology
    CadInputQueue.SendKeyin "lv=default"
    CadInputQueue.SendKeyin "co=bylevel"
    CadInputQueue.SendKeyin "lc=bylevel"
    CadInputQueue.SendKeyin "wt=bylevel"
    ' choose all the elements and change their symbology
    CadInputQueue.SendKeyin "choose all"
    CadInputQueue.SendKeyin "change level"
    CadInputQueue.SendKeyin "change color"
    CadInputQueue.SendKeyin "change linestyle"
    CadInputQueue.SendKeyin "change weight"
    CadInputQueue.SendKeyin "choose none"
End Sub


Sub ChangeToByLevel2()
    Dim es As ElementScanCriteria
    Dim ee As ElementEnumerator
    Dim lvDefault As Level
    Dim lsByLevel As LineStyle
    Dim elemArray() As Element
    Dim i As Long
    Dim iStart As Long
    Dim iEnd As Long
    ' set the output level and linestyle objects
    Set lvDefault = ActiveDesignFile.Levels.Find("Default")
    Set lsByLevel = ByLevelLineStyle
    ' define the scan criteria
    Set es = New ElementScanCriteria
    ' create the element array
    Set ee = ActiveModelReference.Scan(es)
    elemArray = ee.BuildArrayFromContents
    ' change the symbology of each element
    iStart = LBound(elemArray)
    iEnd = UBound(elemArray)
    For i = iStart To iEnd
        Set elemArray(i).Level = lvDefault
        elemArray(i).Color = -1
        elemArray(i).LineWeight = -1
        Set elemArray(i).LineStyle = lsByLevel
End Sub
Rod Wing

Rod Wing

Rod joined EnvisionCAD in March 2007 and has nearly three decades of experience working in the CADD industry. He led the IT evaluations of Bentley InRoads and AutoDesk Civil 3D at Wisconsin DOT in their overall CAiCE replacement evaluation. He also led the MicroStation V7 to V8 upgrade effort at Wisconsin DOT which included updating not only the CADD standards to accommodate the changes in the file format but also updating legacy MDL and macros to work in the new environment. Rod received a Bachelor of Science degree from the University of Wisconsin in Cartography and a Master of Science in Geographic and Cartographic Sciences from George Mason University. Rod has extensive experience in CADD as well as the areas of GIS, cartography, plotting, scanning, raster editing/manipulation, publishing, and file/data translations. In all of these areas, he has produced custom tools and developed workflows to enhance productivity between diverse sets of applications. He instructs MicroStation classes ranging from fundamentals to advanced applications.


Leave a Reply

Your email address will not be published. Required fields are marked *