Using VBA To Detect The Current Or Last Used Command


VBA does not provide a simple way to determine the current or last used command.  The closest thing is to use the functions

GetCExpressionValue("tcb->current_command")

and/or 

GetCExpressionValue("tcb->last_parsed_command")

These two commands return a command ID (VBA Type Long).  Through experimentation, one can determine the command ID for a particular command.  However, these command IDs may change between different versions of MicroStation.  To accommodate this, one should dynamically associate the command ID with the desired command.

Dim commandID As Long
CadInputQueue.SendCommand "MEASURE DISTANCE ICON"commandID = GetCExpressionValue("tcb->current_command")CommandState.StartDefaultCommand

If you need to keep track of multiple command IDs, a dictionary object can be used.  The following code will get the command IDs for all the measurement types:

Dim measurementTypes(5) As String, i As Long, commandID As Long
Dim commandBase As String, measurementCodes
    
measurementTypes(0) = "MEASURE DISTANCE ICON"
measurementTypes(1) = "MEASURE AREA ICON"
measurementTypes(2) = "MEASURE LENGTH ICON"
measurementTypes(3) = "MEASURE RADIUS ICON"
measurementTypes(4) = "MEASURE ANGLE ICON"
measurementTypes(5) = "MEASURE VOLUME ICON"     'only do this one if it is a 3D file
    
Set measurementCodes = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(measurementTypes)
    If Not (measurementTypes(i) Like "*VOLUME*") Or ActiveModelReference.Is3D Then
        CadInputQueue.SendCommand measurementTypes(i)
        commandID = GetCExpressionValue("tcb->current_command")
        If Not IsNull(commandID) Then
            measurementCodes.Add commandID, getCommandBase(measurementTypes(i))
        End If
    End If
Next
CommandState.StartDefaultCommand

Then you can later use the "Exists" method of the Dictionary object to determine if you have defined the command for a particular commandID.

There is one problem with this whole approach though.  This will only work if you know the command(s) you are looking for (Such as all the measuring commands).  If you are just generally trying to determine the current/last command used, you would need to have a exhaustive list of all the key-ins necessary to run those commands, something that may be hard to come by.