Introduction:
This script has been produced in response to requests from the Enterprise Architect community for a way to display all Connector information for a given diagram. The script displays this information in Enterprise Architect's Search window.
The script iterates over the 'Diagram Link' API collection for a given diagram to retrieve the details of each connector.
Installation Instructions:
- Open Enterprise Architect's Scripting window.
- You may create a New Group or place the script within an existing Group.
- Right click the Group and select the option "New JScript".
- Label the script "List Connectors".
- Copy the script below and paste it into the script "List Connectors".
- Save the script.
- Select the diagram of interest in the Project Browser. (If it is already open, ensure that changes are saved.)
- Select the script "List Connectors" and execute it by selecting the "Run script" button.
- All of the connectors for the selected diagram will appear within Search Window.
Please copy the script below:
/*
* Script Name: ListDiagramConnectors
*
* Author: Michael Fraser (Sparx Systems)
*
* Purpose: Lists relationships between objects that appear on the diagram that is
* currently selected in the project browser
*
* Date: 2009-12-17
*/
// ==========================================
// GLOBAL DEFINITIONS
// ==========================================
var DIAGRAM_OT = 8;
// The columns that will appear in the Model Search window
var SEARCH_SPECIFICATION = "<ReportViewData>" +
"<Fields>" +
"<Field name=\"CLASSGUID\" />" +
"<Field name=\"CLASSTYPE\" />" +
"<Field name=\"Client\" />" +
"<Field name=\"Supplier\" />" +
"<Field name=\"Direction\" />" +
"<Field name=\"Name\" />" +
"<Field name=\"Notes\" />" +
"<Field name=\"Client Aggregation\" />" +
"<Field name=\"Client Cardinality\" />" +
"<Field name=\"Client Role\" />" +
"<Field name=\"Supplier Aggregation\" />" +
"<Field name=\"Supplier Cardinality\" />" +
"<Field name=\"Supplier Role\" />" +
"</Fields>" +
"<Rows/>" +
"</ReportViewData>";
/*
* Main function
*/
function ListDiagramConnectors()
{
Repository.EnsureOutputVisible( "Script" );
Session.Output( "JScript: List Diagram Relationships" );
Session.Output( "=========================================" );
// Get the type of element selected in the Project Browser
var treeSelectedType = Repository.GetTreeSelectedItemType();
// Handling Code
switch ( treeSelectedType )
{
case DIAGRAM_OT:
{
// Code for when a diagram is selected
var theDiagram as EA.Diagram;
theDiagram = Repository.GetTreeSelectedObject();
// Create a DOM object to represent the search tree
var xmlDOM = new ActiveXObject( "MSXML2.DOMDocument.4.0" );
xmlDOM.validateOnParse = false;
xmlDOM.async = false;
Session.Output( "Working on diagram '" + theDiagram.Name + "' (Type=" +
theDiagram.Type + ", ID=" + theDiagram.DiagramID + ")" );
// Load the search template
if( xmlDOM.loadXML(SEARCH_SPECIFICATION) )
{
// Resolve the results node in the xml template
var node = xmlDOM.selectSingleNode( "//ReportViewData//Rows" );
// A connector has a one to many relationship with diagram (eg one connector
// may appear on many diagrams). The DiagramLink object represents an instance
// of a connector on a single diagram, and contains properties such as its
// geometry, visibility etc.
//
// Get all diagramLinks for this diagram
var diagramLinks as EA.Collection;
diagramLinks = theDiagram.DiagramLinks;
for ( var i = 0 ; i < diagramLinks.Count ; i++ )
{
// Get the current diagram link
var currentLink as EA.DiagramLink;
currentLink = diagramLinks.GetAt( i );
// Load the corresponding connector object for the link
var correspondingConnector as EA.Connector;
correspondingConnector = Repository.GetConnectorByID( currentLink.ConnectorID );
// Add the connector's details to the search data
AddRow( xmlDOM, node, correspondingConnector );
}
// Fill the Model Search window with the results
Repository.RunModelSearch( "", "", "", xmlDOM.xml );
}
else
{
Session.Prompt( "Failed to load search xml", 0 );
}
break;
}
default:
{
// Error message
Session.Prompt( "This script does not support items of this type.", 0 );
}
}
Session.Output( "Done!" );
}
/*
* Adds an entry for the method object 'theRelationship' to the xml row node 'rowsNode'
*/
function AddRow( xmlDOM, rowsNode, theRelationship )
{
// Cast theMethod for intellisense
var relationship as EA.Connector;
relationship = theRelationship;
// Create a Row node
var row = xmlDOM.createElement( "Row" );
// Get client details for the connector
var client as EA.Element;
var clientEnd as EA.ConnectorEnd;
client = Repository.GetElementByID( relationship.ClientID );
clientEnd = relationship.ClientEnd;
// Get supplier details for the connector
var supplier as EA.Element;
var supplierEnd as EA.ConnectorEnd;
supplier = Repository.GetElementByID( relationship.SupplierID );
supplierEnd = relationship.SupplierEnd;
// Add the Model Search row data to our DOM
AddField( xmlDOM, row, "CLASSGUID", relationship.ConnectorGUID );
AddField( xmlDOM, row, "CLASSTYPE", "connector" );
AddField( xmlDOM, row, "Client", client.Name );
AddField( xmlDOM, row, "Client", supplier.Name );
AddField( xmlDOM, row, "Direction", relationship.Direction );
AddField( xmlDOM, row, "Name", relationship.Name );
AddField( xmlDOM, row, "Notes", relationship.Notes );
AddField( xmlDOM, row, "Client Aggregation", clientEnd.Aggregation );
AddField( xmlDOM, row, "Client Cardinality", clientEnd.Cardinality );
AddField( xmlDOM, row, "Client Role", clientEnd.Role );
AddField( xmlDOM, row, "Supplier Aggregation", supplierEnd.Aggregation );
AddField( xmlDOM, row, "Supplier Cardinality", supplierEnd.Cardinality );
AddField( xmlDOM, row, "Supplier Role", supplierEnd.Role );
// Append the newly created row node to the rows node
rowsNode.appendChild( row );
}
/*
* Adds an Element to our DOM called Field which makes up the Row data for the Model Search window.
* <Field name "" value ""/>
*/
function AddField( xmlDOM, row, name, value )
{
var fieldNode = xmlDOM.createElement( "Field" );
// Create first attribute for the name
var nameAttribute = xmlDOM.createAttribute( "name" );
nameAttribute.value = name;
fieldNode.attributes.setNamedItem( nameAttribute );
// Create second attribute for the value
var valueAttribute = xmlDOM.createAttribute( "value" );
valueAttribute.value = value;
fieldNode.attributes.setNamedItem( valueAttribute );
// Append the fieldNode
row.appendChild( fieldNode );
}
ListDiagramConnectors();