In the previous section, we went over reading the properties of elements that we have already selected directly or by using a fence.This is a particular interest when running VBA evaluation programs in batch mode on entire series of drawings that we don't want to have to individually open each drawing file to examine the elements.There are often certain criteria already set such as element type, lines, level information, font used in texts, etc.
"Element Scan Criteria" by which you can filter search results of data are also helpful. We can run the following variable for establishing such criteria:
Dim Sc As New ElementScanCriteria
This tool is very powerful and offers a variety of ways that the VBA editor helps us with this functionality by proposing us possible criteria.
After defining the element type for 'Sc', simply enter Sc followed by a period in the following line, and a new drop-down menu will appear with many options:
We see a long list of options that add or remove certain features (Include or Exclude). We control the criteria by which is selected. The best way to illustrate this is with an example. We are guided by the previous example to only select lines. We will accomplish this in two steps:
1. We will exclude all types of items out of the search: Sc.ExcludeAllTypes
2. We add only the lines for a search:
Sc.IncludeType msdElementTypeLine
When selecting the correct element, typing VBA helps us again with a large drop-down menu with all possible reference numbers:
Generally, the VBA provides selection options for all objects such as properties, methods, or additional parameters.
If the terms offered are not very clear, the Help center is there for additional explanations and help. If you choose such a concept, clicking the word with the cursor will bring up the help menu open to the selected word, which provides many topics and additional examples. By this definition, Sc now includes a filter definition. Only elements of type "Line" are permitted. This filter will scan the drawing and select those elements that fit the filter criteria.In the "Do While" loop, we set the query to only check for a line, as that is the filter we have chosen. This is shown in the following code:
Sub elementinfo () Dim ele As LineElement Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3D Dim Sc As New ElementScanCriteria Sc.ExcludeAllTypes Sc.IncludeType msdElementTypeLine Set Ee = ActiveModelReference.GraphicalElementCache.scan(Sc) Do While Ee.MoveNext Set ele = Ee.Current.AsLineElement startpunkt = 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 Loop End Sub
It is easily seen that the amount of output data can potentially be immense, and the Immediate window is no longer sufficient. This can easily be fixed but redirecting the data into a text file. To do this, we will open a file at the beginning, and close the file at the end.In the middle, we will forward the data into the first opened file instead of using debug.print.The following lines will be added to the program:
file = ActiveDesignFile.FullName + "- data.txt" Open file For Output As #1 ...... Print #1, “text” ...... Close #1
In the end, the program will look like this:
Sub elementinfo () Dim ele As LineElement Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3D Dim Sc As New ElementScanCriteria Sc.ExcludeAllTypes Sc.IncludeType msdElementTypeLine Dim file As String file = ActiveDesignFile.FullName + "- data.txt" Open file For Output As #1 Set Ee = ActiveModelReference.GraphicalElementCache.Scan(Sc) Do While Ee.MoveNext Set ele = Ee.Current.AsLineElement startpoint = ele.startPoint Endpoint = ele.EndPoint Print #1, "Startpoint is (xyz): " & startpoint.X, startpoint.Y, startpoint.Z Print #1, "is the end point (xyz):" & endpoint.X, endpoint.Y, endpoint.Z Loop Close #1 End Sub
The informal term ActiveDesignFile.FullName is the path and name of the open DGN file. The text file we are using to draw the data is easy to find because it is in the same directory as the original drawing. The Expression "For Output as #1" creates a new file if one is already present. If there is already an existing file, it will be overwritten with the new data. If you want to append the new data at the end of the existing file, the following line must be added:
Open file For Append As #1
<< RETURN TO PART 2 << >> CONTINUE ON TO PART 4 >>