This tip is the seventh in a series of tips that describe System Architect's VBA and show you how to build a simple macro that gets information from the repository. In this series, you create a macro, use System Architect's object model to access an encyclopedia, access diagrams in the encyclopedia, get symbols on a diagram, and get information from the definition of a symbol.
In the last tip, we filtered the list of diagrams to get specific ones. This week we'll open a specific diagram, and get the symbols on that diagram.
If you have closed the work you did in the last tip, reopen it by performing the following steps:
- In System Architect, with the Samples encyclopedia open, select Tools, Macros, VBA Editor to open the VBA IDE.
- Within the IDE, open the Example module that we created in last week's tip. By taking the previous tips to this point, your code should look as follows:
Looking at symbols
For this section of the tutorial, we will look at how to garner and manipulate symbols on diagrams. We will change our code so that we only look at a particular diagram, and focus on the symbols on it.
Reading a particular diagram in
Let’s continue using SAObject’s ReadAll function, and read a particular diagram in. Looking in the Samples encyclopedia, you can see that there are two Use Cases (we printed them to the immediate window earlier):
- In System Architect’s browser, select the All Methods tab, and open the Use Case diagram selection. There are two Use Case diagrams: Hotel Reservation Use Case Context and Make Reservation.
- In the VBA code, in the line specifying the diagram name, replace the null name (open and closed parenthesis) with the name Make Reservation, as shown below:
Because we know that we are specifying a single diagram, let’s remove from our code the loop that walked diagrams for this next part of the tutorial.
-
From the end of the Dim myDiagram As SA2001.Diagram line, remove the ‘, i As Integer’
-
Remove the lines:
For i = 1 To myDiagrams .Count
and
Next i
-
Also, in the line Set myDiagram = myDiagrams.Item(i), change the ‘i’ to the number ‘1’.
Your code should now look as follows:
For the diagram print command, let’s specify that this is the diagram name, to separate it from the symbol names that we will soon go after.
-
In the line Debug.Print myDiagram.Name & “—“ & myDiagram.TypeName, change the line to look as follows:
Debug.Print “Diagram = “ & myDiagram.Name & “—“ & myDiagram.TypeName
Getting a list of all symbols on a diagram
Now we will get a list of symbols on a diagram. Our code will look similar to the code we wrote to get diagrams.
-
Under the line Debug.Print "Diagram = " & myDiagram.Name & " -- " & myDiagram.TypeNameType, type in the following comment to explain the upcoming block of code:
‘get a list of all symbols for the diagram
-
Type in the following to dimension a new variable mySymbols as an SAObjects collection:
Dim mySymbols As SA2001.SAObjects
Since we already have diagrams in the collection ‘myDiagram’, we can get symbols from there.
-
Type in the following:
Set mySymbols = mydiagram.GetAllSymbols
We will use the ReadAll command to get all the symbols on the diagram.
-
Type in:
mySymbols.ReadAll
-
Add the following command to print the number of symbols on the diagram:
Debug.Print mySymbols.Count
Step through the code (click the Step Into button on the menu). Notice that the number of symbols on the diagram is printed to the Immediate window. Your code for this section should look as follows:
Add a loop to get each symbol on the diagram
Finally, let's replace the count command, and instead add a loop to get each symbol on the diagram.
First let’s dimension a new variable that will catch each symbol as we walk the loop.
-
Add the following to the end of the code line Dim mySymbols As SA2001.SAObjects, so that the line looks as follows:
Dim mySymbols As AS20001.Saobjects, saSymbol As SA2001.Symbol
-
Begin the loop - add the following before the Debug.Print command:
For i =1 To mySymbols.Count Set saSymbol = mySymbols(i) Debug.Print mySymbols.Count
-
End the loop - add the following after the Debug.Print command:
Next i
-
Finally, remove the Count command from the Debug.Print statement, and instead add code to get each symbol’s name and type - change the Debug.Print mySymbols.Count command to look as follows:
Debug.Print “Symbol = ” & saSymbol.Name & “ -- “ & saSymbol.Typename mySymbols.Count
Your code for should look as follows:
- Step through the code (by clicking on the Step Into button) to make sure your code is working correctly – you should get each symbols name and type printed to the Immediate window as you step through.
Next tip
In this tip we got symbols on a diagram. In the next tip we'll get information about the symbols on the diagram.
- Continue with the next tip, or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.