This tip is the tenth tip in a series 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 got the definition of a symbol. In this tip we'll examine the advantages of using the GetPropertyAsCollection method.
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:
Using GetPropertyAsCollection
The function GetPropertyAsCollection is a very useful method of the Definition object that gets you a list of properties as a collection – this gives you more control on getting a list of properties than the GetRelatedObjects function.
For example, a Use Case definition contains a list of Use Case Steps – Use Case Step is a definition in itself. You can use the GetPropertyAsCollection to get all of the Use Case Step definitions as a collection, so that you may then work with each Use Case Step individually.
Let’s get Use Case Steps of a Use Case using the GetPropertyAsCollection method. To begin we will dimension a new variable called mySteps.
-
Within your code, under the line
Debug.Print "Definition = " & mydef.Name & " -- " & mydef.TypeName
type in the following:
Dim mySteps As OfCollection
Now we want to use the GetPropertyAsCollection method to get all of the Use Case Steps for the Use Case definition that we have a handle to.
-
Type in:
Set mySteps = mydef.GetPropertyAsCollection("Use Case Steps")
The GetPropertyAsCollection method needs the exact name of the property – in this case “Use Case Steps”.
Note: If you are unsure of the name of the property that you are getting, you should look it up in SAPROPS.CFG, contained in the encyclopedia path. Oftentimes the real name of a property is not the same as what shows up on a definition dialog; for example, in an Elementary Business Process of a Process Chart diagram, the "Locations" property is actually a rename – the real property is “Location Types”. You would only know this if you looked up the definition of an Elementary Business Process in saprops.cfg and saw that the property is actually called “Location Types” but has been ‘labeled’ “Locations”:
Property "Location Types" { Edit Listof "Location" Label "Locations" LENGTH 2000 HELP "Supporting Location Types (Matrix)" READONLY }
Let’s run through the code and see the results.
-
Make sure that all diagrams are closed in System Architect, reset the VBA project (click on the Reset button), and click on the Step Into button to step through the code.
As you walk through, and get the first symbol and definition, which is that of a class, you get an error message that says “Property does not exist”. This is because we are trying to get the property “Use Case Steps”, but this property is not contained in a class definition, only a Use Case definition.
We need to set up a while loop that only invokes the GetPropertyAsCollection method if the definition type is that of a Use Case. We have the definition, and the definition object has a property called TypeName. Let’s use it.
-
Before the line Dim mySteps As OfCollection, type:
If myDef.TypeName = “Use Case” Then
-
After the line Set mySteps = mydef.GetPropertyAsCollection("Use Case Steps"), end the loop by typing:
Else End If
Your code should now look as follows:
-
Close all System Architect diagrams, reset the project, and step into the code until you step through the first few Use Case definitions. You should be able to step through the definitions without any error messages popping up.
-
Stop at the Set mydef = Nothing command, with the Use Case definition Make Reservation captured (see picture below).
Let’s use the Immediate window to get some information about a Use Case Step.
-
In the Immediate window, let’s print the number of Use Case Steps for a Use Case, using the Count method. Type in the following and hit Enter:
? mySteps.Count
Now let’s use the GetItem method to get the name of one of the items in the collection. Let’s get the name of the second Use Case Step in the definition:
-
In the Immediate window, type in the following and hit Enter:
? mySteps.Item(2).Name
The name of the second Use Case Step in the definition will be printed to the Immediate Window.
Next tip
In this tip we used the GetPropertyAsCollection function of the Definition object to get a step of a Use Case. In the next tip we will create a loop to get steps of all Use Cases, and then wrap up this series of VBA tips.
- Continue with the next tip, or select File, Save Example and close the VBA environment by selecting File, Close and Return to SA2001.