In the previous sections, we have always read out the same data: the endpoints of lines. We will now move in different directions, because in addition to lines, more complex data structures have diverse properties.Outputting to a .csv file on the other hand we will leave as simple as it is now, because we already have the skeleton built for us.
First, we will no longer restrict the choice of selected elements because the selection is made by all grapic elements of the current model without speicifying scan criteria.
Set Ee = ActiveModelReference.GraphicalElementCache.Scan()
Now it is important to check what type of element we are looking at in each case, because we can no longer assume that we are looking at a line as the only variable.
Dim ele As Element
This change also has consequences for the subsequent evaluation of the element "ele". Because we were previously only looking at line elements, we were only able to read the .startpoint property. There is now a common element and we need to check the type and the type line reading endpoints done by using .AsLineElement.Here is an example trying out the evaluation of the lines:
Sub elementinfo() Dim ele As Element Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3d Dim topofLine, Line As String Dim file As String If ActiveWorkspace.IsConfigurationVariableDefined("MyOutputFile") Then file = ActiveWorkspace.ConfigurationVariableValue("MyOutputFile") Else MsgBox "The variable MyOutputFile is not defined. Stopped Processing", vbCritical Exit Sub End If Open file For Append As #1 Set Ee = ActiveModelReference.GraphicalElementCache.Scan() Do While Ee.MoveNext Set ele = Ee.Current If ele.Type = msdElementTypeLine Then Line = ele.Type & ";From (xy) = " & ele.AsLineElement.startPoint.X & "," & ele.AsLineElement.startPoint.Y & ";" Line = Line & "After (xy) = " & ele.AsLineElement.EndPoint.X & "," & ele.AsLineElement.EndPoint.Y Print #1, Line End If Loop Close #1 End Sub
The result in excel might look something like this:
Because LineStrings, text, circles, and other more complex elements are not yet considered, we will now add functionality for them.
Because circles have no points on them, we will be reading the center and radius. We see here an ellipse here instead of a circle. In mathematics, a circle is just a special case of an ellipse, so therefore it can be treated as an ellipse. The VBA editor helps again here with the selection with a dropdown menu containing possible types of elements:
The verification of the circuits might look like this:
If ele.Type = msdElementTypeEllipse Then Line = ele.Type & ";Center (xy) = " & ele.AsEllipticalElement.CenterPoint.X & "," & ele.AsEllipticalElement.CenterPoint.Y & ";" Line = line & "; Radius =" & ele.AsEllipticalElement.PrimaryRadius Print # 1, Line End If
Here is the output I got when I added a circle to the drawing:
Text elements would have additional data, such as the text height and the text itself. Here is a possible approach for retrieving data from text elements:
If ele.Type = msdElementTypeText Then Line = ele.Type & ";Origin (xy) = " & ele.AsTextElement.Origin.X & "," & ele.AsTextElement.Origin.Y & ";" Line = line & "Height:" & ele.AsTextElement.TextStyle.Height & ";" Line = line & "Text" & ele.AsTextElement.Text Print # 1, Line End If
Here is the output I got when I added a text file to the drawing:
Before we turn to the next section with more complex data, here is a summary of the current approach:
Sub elementinfo() Dim ele As Element Dim Ee As ElementEnumerator Dim startpoint, endpoint As Point3d Dim header, Line As String Dim file As String If ActiveWorkspace.IsConfigurationVariableDefined("MyOutputFile") Then file = ActiveWorkspace.ConfigurationVariableValue("MyOutputFile") Else MsgBox "The variable MyOutputFile is not defined. Stopped Processing", vbCritical Exit Sub End If Open file For Append As #1 Set Ee = ActiveModelReference.GraphicalElementCache.Scan() Do While Ee.MoveNext Set ele = Ee.Current If ele.Type = msdElementTypeLine Then line = ele.Type & ";From (xy) = " & ele.AsLineElement.startPoint.X & "," & ele.AsLineElement.startPoint.Y & ";" line = line & "After (xy) = " & ele.AsLineElement.EndPoint.X & "," & ele.AsLineElement.EndPoint.Y Print #1, line End If If ele.Type = msdElementTypeEllipse Then line = ele.Type & ";Center (xy) = " & ele.AsEllipticalElement.CenterPoint.X & "," & ele.AsEllipticalElement.CenterPoint.Y & ";" line = line & "; Radius = " & ele.AsEllipticalElement.PrimaryRadius Print #1, line End If If ele.Type = msdElementTypeText Then line = ele.Type & ";Origin (xy) = " & ele.AsTextElement.Origin.X & "," & ele.AsTextElement.Origin.Y & ";" line = line & "Hoehe: " & ele.AsTextElement.TextStyle.Height & ";" line = line & "Text: " & ele.AsTextElement.Text Print #1, line End If Loop Close #1 End Sub
<< RETURN TO PART 4 << >> CONTINUE TO PART 6 >>