In the previous section, we studied simple cells. However, we have not yet observed nested cells, so we will cover that next.
From the example of the cells and groups from the previous section (Part 7), I have generated new cells in each of the previous cells, which I called Cell2 through Cell6. When using the "Analyze Element" tool, the structure can be convoluted and confusing.
With this example, I want to demonstrate that the current way of evaluating cells has reached its limit. To get properties of much more complex cells, we would have a lot of lines of code to generate.
Instead, I will show you an easier way to accomplish this by using recursion. Specifically, in addition to the existing subroutine elementinfo where the evaluation will start, I will store the analysis of cells in another subroutine called "disassemble". The special thing about this routine is that it repeatedly calls itself, which is a process called recursion.
Below, I have put together an example:
Public datei As String Sub elementinfo() Dim ele As Element Dim ee As ElementEnumerator Dim startpoint, endpoint As Point3d Dim header, line As String Dim oL() As Element Dim Cellname As String If ActiveWorkspace.IsConfigurationVariableDefined("MyOutputFile") Then line = ActiveWorkspace.ConfigurationVariableValue("MyOutputFile") Else MsgBox "The variable MyOutputFile is not defined. Stopped Processing", vbCritical Exit Sub End If Set ee = ActiveModelReference.GraphicalElementCache.Scan() Do While ee.MoveNext Call disassemble(ee.Current, 1) Loop End Sub Sub disassemble(ele As Element, depth As Integer) Dim oL() As Element Dim line As String If ele.IsCellElement Then oL = ele.AsCellElement.GetSubElements.BuildArrayFromContents Open file For Append As #1 line = "" For i = 2 To depth line = line & " ;" Next line = line + "Depth: " & depth & ";Cell < " & ele.AsCellElement.Name & "> with " line = line & UBound(oL) - LBound(oL) + 1 & " Element" Print #1, line Close #1 For i = LBound(oL) To UBound(oL) Call disassemble(oL(i), depth + 1) Next Else Open file For Append As #1 line = "" For i = 2 To depth line = line & " ;" Next line = line + TypeName(ele) Print #1, line Close #1 End If End Sub
In the previous sub elementinfo, I only actually read the name of the file into which the data should be written. I save the name in a public variable file that is declared in the elementinfo outside the subroutine. *Caution*: Observe the declaration of the variable element file info if removed from the subroutine, otherwise the validity ranges of the variables overlap with the result that breaks down in the sub, and the filename will not be created. The actual analysis of the data is now decomposed in the subroutine. The sub elementinfo now calls the sub disassemble for each item in the drawing.
Disassemble only checks for whether or not the element in the routine that is called is a cell. If so, the sub will break down each of the elements called again, but otherwise it will write the details to a file.
In order to make the nesting work correctly, I gave it a depth with the same name, so that each variable and place is visually apparent in the output file by an addition of a column.
To do this, I added the following lines:
line = "" For i = 2 To depth line = line & " ;" Next
The resulting .csv file in Excel will look something like this:
This is, of course, just a suggestion on how to tackle the problem, and it serves as more of a visual presentation of the data read. Often, not all of the data is important and you are trying to look for a very specific piece of information such as text within cells that you want to read or modify. We will tackle changing data in a future article. In the next section, we will deal with the PropertyHandler which provides additional ways to read data.
<< RETURN TO PART 7 << >> CONTINUE ON TO PART 9 >>