This Client Server article is republished in its entirety from 2003 for reference purposes.
By Bentley Technical Support Group04 August 2003
MicroStation allows you to group elements together quickly using Graphic Groups, which might be of interest to those who like to create their own system of IDs for their elements. This article will supply you with ideas for building a VBA application that allows you to get elements and set them to a specific graphic group number.
Our first example will ask the user for a Graphic Group number for which to search. Then it will scan for all elements. It will check to see whether or not the element is graphical, and whether or not the Graphic Group number matches the one we are seeking. If it does match our number, it will highlight the element and display the element type, then it will move on to the next element. At the end, it will display the total number of elements that match the Graphic Group number.
The first thing we want to do is declare our variables:
Sub ggGet() Dim elem As Element Dim ggnum As String Dim reply As Integer Dim Count As Integer Dim Msg3, Msg2, Msg, Style, Title Dim ScanCriteria As New ElementScanCriteria Dim Enumer As ElementEnumerator
getGraphicGr is the name of the macro we will create. elem is the Element object we will use to get each current element. The count variable will total up our elements with the specified Graphic Group number. ScanCriteria is what we use to search for specific elements (i.e. level, element type). Finally, the Enumer object is what we will use to grab all the elements, and loop through them.
The next lines of code will set our counter to "0," reset the scanner to start at the beginning of the design file, and populate our Enumer object with all the elements to loop through. The last line shows how to call up an InputBox to get the Graphic Group number for which the user wishes to search:
Count = 0 'Clear out last search criteria and set up scanner. ScanCriteria.Reset Set Enumer = Application.ActiveModelReference.Scan(ScanCriteria) 'Uses inputbox to get graphic group number from user. ggnum = InputBox("Select number of graphic group elements to display", "Get Graphic Group")
Next we will start our loop through the elements in the enumerator, and check to see if it is a graphical element. If it is, we add one to the counter variable and highlight the element:
'Loop continues until no more elements are found. Do While Enumer.MoveNext Set elem = Enumer.Current 'Grab current element If elem.IsGraphical Then If CStr(elem.GraphicGroup) = ggnum Then Count = Count + 1 elem.Redraw msdHilite 'Hilite current element 'Function call to get element name elemName = GetElementTypeName(elem.Type)
The last line of code calls up a subroutine (GetElementTypeName) that searches for the element type, and returns a string with the name of the element type. The subroutine can be a case switch statement that looks something like this:
'Retrieve element name. Public Function GetElementTypeName(ByVal eType As Integer) As String Select Case eType Case 1: GetElementTypeName = "Cell Header " Case 2: GetElementTypeName = "Line " Case 3: GetElementTypeName = "Line String " Case 4: GetElementTypeName = "Shape " Case 5: GetElementTypeName = "Text Node " Case 6: GetElementTypeName = "Curve " Case 7: GetElementTypeName = "Complex String " Case 8: GetElementTypeName = "Conic " Case 9: GetElementTypeName = "Complex Shape " Case 10: GetElementTypeName = "Ellipse " Case 11: GetElementTypeName = "Arc " Case 12: GetElementTypeName = "Text " Case 13: GetElementTypeName = "Surface " Case 14: GetElementTypeName = "Solid " Case 15: GetElementTypeName = "BSpline Pole " Case 16: GetElementTypeName = "Point String " Case 17: GetElementTypeName = "Cone " Case 18: GetElementTypeName = "BSpline Surface " Case 19: GetElementTypeName = "BSpline Boundary " Case 20: GetElementTypeName = "BSpline Knot " Case 21: GetElementTypeName = "BSpline Curve " Case 22: GetElementTypeName = "BSpline Weight " Case 23: GetElementTypeName = "Dimension " Case 24: GetElementTypeName = "Shared Cell Definition " Case 25: GetElementTypeName = "Shared Cell " Case 26: GetElementTypeName = "MultiLine " Case 27: GetElementTypeName = "Tag " End Select End Function
Next, we will display the element found in a VB MSGbox. If the user presses OK we will continue to the next element, or we will end the macro. We will also end all our other "If" statements as well:
'Display each element found Msg = "Element found is " + elemName ' Define message. Style = vbOK ' Define buttons. Title = "Display Graphic Group Elements" ' Define title. reply = MsgBox(Msg, Style, Title) If reply = vbOK Then 'Redraw element without hilite elem.Redraw msdNormalDraw Else 'If cancel button is pressed exits loop elem.Redraw msdNormalDraw Exit Sub End If End If End If Loop
The final step is to display the results. If the count variable is less than one, we assume no elements have this Graphic Group number, and we notify the user. We also end the macro.
If Count < 1 Then 'If no elements match GG# Msg3 = "No elements found with this GG#" Style = vbOK ' Define buttons. Title = "Display Graphic Group Elements" reply = MsgBox(Msg3, Style, Title) Else 'Display total no. of elements found Msg2 = "Total elements found with specified GG# " + CStr(Count) Style = vbOK ' Define buttons. Title = "Display Graphic Group Elements" ' Define title. reply = MsgBox(Msg2, Style, Title) End If End Sub
The next macro will ask the users to identify the Graphic Group number to which they wish to set their elements. Then it will set the elements that are in a fence or selection set to that value.
Again, we will first declare our variables:
Sub ggSet() Dim oElement As Element Dim ggnum As String Dim oEnumerator As ElementEnumerator Dim Count As Integer Dim Msg, Style, Title Dim reply As Integer
setGraphicGr is the name of our VBA macro. ggnum is the value we want to set the elements to. oEnumerator will be our enumerator object to scan through.
Next, we will open an InputBox to ask the user what value they wish to set the elements to:
'Uses inputbox to get graphic group number from user. ggnum = InputBox("Select graphic group you wish to set elements to ", "Set Graphic Group")
Then we will check to see if a fence or selection set is defined, or we will notify the user. Finally, we get the contents of the fence or selection set. We populate our oEnumerator object, loop through them, and set the Graphic Group values for each element. We also end the macro:
If ggnum = "" Then Else If ActiveDesignFile.Fence.IsDefined Then Set oEnumerator = ActiveDesignFile.Fence.GetContents Do While oEnumerator.MoveNext Set oElement = oEnumerator.Current oElement.GraphicGroup = ggnum oElement.Rewrite oElement.Redraw msdNormalDraw Count = Count + 1 Loop Else Count = 0 Set oEnumerator = ActiveModelReference.GetSelectedElements 'Moves through each element in selection set. Do While oEnumerator.MoveNext Set oElement = oEnumerator.Current oElement.GraphicGroup = ggnum oElement.Rewrite oElement.Redraw msdNormalDraw Count = Count + 1 Loop End If If Count = 0 Then 'Display each element found Msg = "No Fence Found. " ' Define message. Style = vbOK ' Define buttons. Title = "Information!" ' Define title. reply = MsgBox(Msg, Style, Title) End If End If End Sub
A full example called GGExample is attached to this article. Note that this example is from 2003.ggExample.zip
Client Server Archive
MicroStation Desktop TechNotes and FAQs
Bentley's Technical Support Group requests that you please confine any comments you have on this Wiki entry to this "Comments or Corrections?" section. THANK YOU!