Q How do I move an element using MicroStation VBA?

Questions similar to this appear on the Bentley Discussion Groups, typically the MicroStation Programming Forum.

A Divide and conquer! Split the problem into manageable chunks: you need to first locate an element, then figure out how to move it. In other words, a locate operation to get the element, followed by a datapoint from the user to get the new coordinates.

In VB terms, we should use a class that implements ILocateCommandEvents and prompts the user to locate an element. Once the user has accepted an element, we should start another class that implements IPrimitiveCommandEvents to obtain a target datapoint. Finally, compute the offset from the supplied locate datapoint to the target datapoint and move the element.

MoveElement MVBA

A solution is provided with the MoveElement MVBA project. The Main subroutine in modMain is the starting point for a user, with the keyin …

vba run [MoveElement]modMain.Main

This keyin creates an instance of class clsElementMover. The purpose of this class is to

  1. encapsulate two further classes that implement ILocateCommandEvents and IPrimitiveCommandEvents
  2. preserve state data as we switch from one class to the other as the user supplies data points

The three classes supplied by MoveElement.mvba are …

clsElementMover maintains a reference to the Element and locate datapoint found by clsLocator when the user accepts an element in its ILocateCommandEvents_Accept subroutine. At the same time, we start the clsGetTarget to solicit a target datapoint from the user.

This state diagram may help you to understand the logic underlying this implementation …

State Diagram for Move Element

For no other reason than to illustrate locate filtering techniques, the clsLocator class filters and locates only TextElements. You can remove the filter logic from ILocateCommandEvents_LocateFilter if you want to move any type of element, or apply an alternative filter if you want to move other types of element.

clsGetTarget has a IPrimitiveCommandEvents_Dynamics event handler that we use to display the moved element in temporary graphics. Its IPrimitiveCommandEvents_DataPoint subroutine supplies the target datapoint, at which time we can move and rewrite the element. Finally, we restart the entire operation ready for the next move operation.

The complete code is provided in the MoveElement MVBA project, supplied in a ZIP archive.