The Bentley VBA newsgroup (see Bentley Discussion Groups) regularly receives postings asking some question about Tag data.
Q This article answers some common questions, such as …
A Those questions are answered here …
Tags are MicroStation type 37 elements. A Tag's primary purpose is to store a data value. The Tag may or may not be displayed, but it's value exists whether or not you can see it. In many ways, tags behave like any other graphic element: you can move, rotate, scale, symbolize, and delete a Tag. The primary difference between Tags and most other graphic element types is their association with a host element (subsequently called the Host in this article). The Host element can be almost any type of MicroStation graphic element: there is nothing special about it.
A Tag’s association with a Host is through the Host’s Element ID (the unique 64-bit ID assigned automatically to each and every element in a DGN file). Use MicroStation’s Analyse Element tool to examine a Tag element and see this association. The screen-shot below shows a green shape element (the Host) and a couple of Tag elements …
I used the Analyse Element tool to see information about the Tag that displays Stephan 3 …
Note the Type Specific Details relating to a Tag element, and the clumsy and inaccurate phrase Assoc id of element attached to. The Assoc id is a hangover from earlier generations of MicroStation: it should more properly read Element ID of Host Element. In this case, the Host element is the green rectangle, whose Element ID is 192.
The most obvious property of Tags is that they 'belong’ to a Host element: when you move the Host, the Tags move with it; when you delete a Host, its Tags are also deleted. MicroStation maintains this relationship Host-owns-Tag through its dependency logic. That is, the tag is dependent on its host.
Occasionally, confusion sets in: some folks get the impression that tag data are somehow a part of the Host. Let’s get this straight: when you tag an element, the element becomes the Host for one or more tags. But, the Host itself is not changed. It already has an Element ID, and each Tag is associated with the Host by referencing that ID. The process of tagging adds new Tag elements to your DGN file, but the host element is not affected.
Because Tag elements can store & display different data types, their definition is stored in a Tag Set. You define and review Tag Set definitions through the MicroStation menu Element|Tags|Define, which opens the Tag Sets dialog …
In this example, we have a Tag Set named stephan. This Tag Set contains three Tag definitions named IntTag, RealTag, and StringTag. Those names are arbitrary: you would probably choose a set of names appropriate to your organisation’s needs. Select a Tag definition and press the Edit button to pop the Define Tag dialog …
Here, the Tag named IntTag is defined to hold an integer number with a default value of zero. A Tag may also be defined as a container for a real (floating-point) number or a character string. Tag Set Definitions and their Tag Definitions are stored as non-graphic elements in the design file. The definitions are available to all models that are part of that file. You can create the definitions in a project seed file to ensure that the same Tag Sets are always defined in your design files.
MicroStation VBA provides properties and methods to create, manipulate, and read Tag data. A common requirement is to read tag data and render it in a report. You might want to export tag data in a CSV or XML file, for example.
To read tag data, first identify a Host having associated tags …
Dim oHost As Element
… ' Read oHost element from file
If (oHost. HasAnyTags) Then
' process tags
End If
Once you’ve found a Host, get an array of tag elements …
Dim oHost As Element …' Read oHost element from fileIf (oHost. HasAnyTags) Then Dim oTags() As TagElement oTags = oHost.GetTags ()' process tag arrayEnd If
For each tag element, extract its value …
Dim oHost As Element …' Read oHost element from fileIf (oHost. HasAnyTags) Then Dim oTags() As TagElement Dim nTags As Integer Dim i As Integer Dim vtValue As Variant oTags = oHost.GetTags ()' process tag arrayFor i = Lbound (oTags) To Ubound (oTags) vtValue = oTags(i).Value Debug.Print "Tag [" & CStr(i) & "]" = " & CStr(vtValue) Next i End If
Update a tag’s value …
Dim oHost As Element …' Read oHost element from fileIf (oHost. HasAnyTags) Then Dim oTags() As TagElement Dim nTags As Integer Dim i As Integer Dim vtValue As Variant oTags = oHost.GetTags ()' process tag arrayFor i = Lbound (oTags) To Ubound (oTags) oTags(i).Value = i' assign some valueoTags(i).Redraw msdDrawingModeNormal' display the new valueoTags(i).Rewrite' update the tag in the DGN fileNext i End If
Note how we update individual tag elements. We don’t need to update the host element. In other words, modifying a tag element doesn’t affect its Host.
You can find more examples in the VBA documentation. Search for TagElement and related methods and properties.
We've created a sample VBA project to illustrate how to locate a tagged object and display its tag values.
The project includes a regular code module, modTags that includes a function TraceTags.
TraceTags gets an array of tags from a host element and prints their values to the VBA IDE Immediate window.
The code to locate a tagged object is contained in the clsTaggedObjectLocator module.
clsTaggedObjectLocator is a class that Implements ILocateCommandEvents.
The key procedure of this class is the ILocateCommandEvents_LocateFilter subroutine,
which tests an element to see if it has any tags …
Private Sub ILocateCommandEvents_LocateFilter(ByVal oElement As Element, Point As Point3d, accepted As Boolean)
If (oElement.HasAnyTags) Then
accepted = True
Else
accepted = False
End If
End Sub
You can download a complete MicroStation VBA project that implements a tag location and tracing tool.
The
TagLocator Project
is in a ZIP archive. Unpack the archive to a suitable location, such as
C:\Program Files\Bentley\Workspace\Standards\VBA, then start MicroStation and load TagsLocator
with the VBA project manager.
Some portions of this article were first published by LA Solutions Ltd in MicroStation Manager magazine online, on Bentley Systems, Inc. website.