Linking to VBA Elements with User Attributes


   
  Applies To 
   
  Product(s): MicroStation
  Version(s): 08.11.09.578
  Environment:  Windows 7 32 bit,Windows 7 64 bit
  Area:  Programming
  Subarea:  VBA
  Original Author: Tristan Anderson, Bentley Technical Support Group
   

 Background Information

There are several ways to provide additional information about elements in a drawing. They are used most commonly for retrieving attribute data, creating functions, and attaching and evaluating. However, this data has drawbacks because the attribute data itself are also elements like texts, and they are linked to the basic elements of links.

These links can be seperate even during the conversion. For example, the DWG format can be lost because it only supports attribute data in cells (in DWG blocks). There is also the possibility of data linking directly with elements. The advantage here is the close link with the element itself. These linkages are not seperate elements, and must be maintained in the DWG format. This must be selected when saving to a DWG file in the options so that the application data is retained.

Steps

Here is a simple example that depends on the active model found in lines as a text attribute.

' Example using VBA to help attach strings as attributes to elements:
Const myID As Long = 22526   ' only example from Help
 
Sub AddStringAttribute(ele As element, str As String)
    Dim dblk As New DataBlock
    dblk.CopyString str, True
    ele.AddUserAttributeData myID, dblk
    ele.Rewrite
End Sub
 
Sub Attribute_append()
Dim Ee As ElementEnumerator
Dim Db As DataBlock
Set Ee = ActiveModelReference.GraphicalElementCache.Scan
Do While Ee.MoveNext
    If Ee.Current.Type = msdElementTypeLine Then
        AddStringAttribute Ee.Current, "This is an example text"
    End If
Loop
End Sub

Now, when this routine is started Attribute_Append, all the elements of the active model are first selected and checked to see whether it is a line in each case.

If there is a line, then the text "This is an example text" is attached as a custom attribute on the line.

After passing thrgouh the routine, this can be checked by using the Keying command "Analyze element", which starts the old analyze tool and selects a line. Under the tab "Attributes", the data will be displayed as shown in the example below:

Although the data is stored in binary form, the text can be seen from the text display. With the new element information tool, this data is also displayed, but only binary and not text mode.

It also immediately raises the question of how one can read such data again. The following is a small example which checks each line for the presence of this data and then outputs:

Sub Attribute_append()
Dim Ee As ElementEnumerator
Dim Db() As DataBlock
Dim sText As String
Set Ee = ActiveModelReference.GraphicalElementCache.Scan
Do While Ee.MoveNext
    If Ee.Current.Type = msdElementTypeLine Then
        Db = Ee.Current.GetUserAttributeData(myID)
        sText = ""
        If UBound(Db) >= 0 Then
            Db(0).CopyString sText, False
            If Len(sText) > 0 Then
                Debug.Print sText, Ee.Current.AsLineElement.startPoint.x, Ee.Current.AsLineElement.startPoint.y
            End If
        End If
    End If
Loop
End Sub

The output is shown below. It shows the found text and the xy coordinates of the starting point of the line.