As a first task, we will want to retrieve the properties of some lines and to draw a few individual lines in the currently active model of your open DGN file. Enter the following lines into the entry field for the VBA module:
Sub elementinfo () Dim ele As Line Element Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3D Set Ee = ActiveModelReference.GetSelectedElements Do While Ee.MoveNext If Ee.Current.Type = msdElementTypeLine Then Set ele = Ee.Current.AsLineElement startpoint = ele.startPoint Endpoint = ele.EndPoint Debug.Print "Start point is (xyz): " & startpoint.X, startpoint.Y, startpoint.Z Debug.Print "Endpoint is (xyz):" & endpoint.X, endpoint.Y, endpoint.Z End If Loop End Sub
There are only a few lines of code required to read the end points of all selected (method .GetSelectedElements) lines (type msdElementTypeLine) from the active model (Active Model Reference). One essential component is the Ee enumerator object. You can imagine an enumerator as a pot containing an indefinite amount of elements that can be accessed by various methods. By repeatedly applying the method .MoveNext, we can access each element of this set sequentially.This will continue until no more elements are left. To do this, we will use a "Do While", or a loop in which we access each item on the .Current method to pick out and examine each. Each lines has a beginning point, an end point, and x, y, and z coordinates. We will read them and print them in direct range.Changing window sizes in the editor might be something you would like to do to customize your VBA coding environment, like so:
In the example above, I have chosen 2 lines before I started my subroutine. The coordinates of the start and end points have been printed in the "Immediate" window.Because doing this with a larger amount of elements can be tedious, it would be helpful to instead define a fence and only evaluate the content inside of the fence. This can be done with a small change as shown below:
Sub elementinfo () Dim ele As Line Element Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3D No FNC As Fence Set fnc = ActiveDesignFile.Fence If Not fnc.IsDefined Then MsgBox "There was no fence defined" , vbCritical Exit Sub End If Set Ee = fnc.GetContents 'Set Ee = ActiveModelReference.GetSelectedElements Do While Ee.MoveNext If Ee.Current.Type = msdElementTypeLine Then Set ele = Ee.Current.AsLineElement startpoint = ele.startPoint Endpoint = ele.EndPoint Debug.Print "Startpoint is (xyz): " & startpoint.X, startpoint.Y, startpoint.Z Debug.Print "endpoint is (xyz):" & endpoint.X, endpoint.Y, endpoint.Z End If Loop End Sub
An element type fence is used to evaluate the contents inside of a fence. Therefore, it is checked in advance to determine if a fence was ever created. If not, you will get a message (MsgBox) on the screen and the process will be terminated (Exit Sub).
In the next section, we will generalize member selection by defining filter criteria and redirecting the output of the analysis to a file.
<< RETURN TO PART 1 << >> CONTINUE ON TO PART 3 >>