With the EA API we can do a lot of automation. For example, we can programmatically select one or more model elements in a diagram. Specifically, we select the graphical representation of model elements, the diagram objects. A frequently used application is to programmatically write information to the output window and double-click on a line in the Output window to display the model element in question in the Project Browser or in a diagram.
To write to the output window, we can use the following code:
repository.CreateOutputTab("Mein Tab Name"); //Um einen neuen Output-Tab zu erzeugen.
repository.EnsureOutputVisible("Mein Tab Name"); //Um sicherzustellen, dass das Output-Tab aktuell auch angezeigt wird.
repository.ClearOutput("Mein Tab Name"); //Optional kann der Inhalt dieses Tabs gelöscht werden, bevor etwas neues geschrieben wird.
repository.WriteOutput("Mein Tab Name", "Das betreffende Modell-Element: " + element.Name, element.ElementID ); //Link zur API Beschreibung
In order to react to the double-click event, the following call-back operation must be implemented:
//CallBack Funktion
//Param Repository: Liefert Zugriff auf das aktuelle EA-Repositorium
//Param tabName: Der Tab Name, in unserem Beispiel: "Mein Tab Name"
//Param lineText: Der Text der im Output-Fenster angezeigt wird. In unserem Fall: "Das betreffende Modell-Element: Class1"
//Param id: Die ElementID des betreffenden Modell-Element. Es kann natürlich jede beliebige ID übergeben werden. Aus dem Tab-Namen und dem Text muss der Element-Typ ermittelbar sein.
public void EA_OnOutputItemDoubleClicked (Repository repository, string tabNamse, string lineText, int id)
{
try
{
//Wenn kein Element gefunden wird, wird ein Fehler geworfen, daher das Try-Catch
Element elementById = repository.GetElementByID(id);
Diagram currentDiagram = repository.GetCurrentDiagram();
if(currentDiagram == null) return; //wir möchten das Diagramm-Element nur im gerade geöffneten Diagramm anzeigen.
currentDiagram.SelectedObjects.AddNew(id.ToString(), ""); //zum selektieren des Diagramm-Objekts fügen wir der Liste der selektierten Diagramm-Objekte die ID des zu selektierenden Modell-Elements hinzu.
currentDiagram.FindElementInDiagram(id); //Hiermit können wir den Fokus auf das Diagramm-Objekt legen, welches das Modell-Element mit der ElementID "id" darstellt.
}
catch (Exception)
{
throw;
}
}
In the example shown, it is assumed that the diagram with the relevant model element is already open. If this is not the case, the following must be carried out:
- A diagram object must be found, which represents the model element with the provided element ID “id”. This is best done with an SQL query.
- It must be determined in which diagram this diagram object is contained.
- Open the diagram.
However, it may be that the model element appears in several diagrams. This circumstance can now be treated differently. Pragmatically, the first found diagram can be opened.
We already know the desired behavior from other EA functions:
When tracing with [Ctrl + U]: With the key combination [Ctrl + U], we can display a model element in the diagram. If the model element is used in a diagram, the EA opens this diagram, selects the diagram object that represents the model element and focuses it. The diagram object is thus placed in the center of the visible diagram excerpt.
For model validation via the output window: We see the behavior when we start the model validation and find faulty model elements. These are listed in the Output window. Double-clicking on a line in the output window opens the diagram in which the model element is displayed graphically (as a diagram object). In case the model element is not used within a diagram, it is selected within the project browser.