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.
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();
Comments
Thank you for this JScript. I'm using a modified version of it to find conveyed items. What I can't get to work is "find in all diagrams" from the model search window when this script was executed - It always returns ALL diagrams of the model, not only the ones that have the connection. Is there a way to modify the script so this works?
by Reinhard Claus on Wed, 29/02/2012 - 04:29.
A bit like: // Load the corresponding connector object for the link var correspondingConnector as EA.Connector; correspondingConnector = Repository.GetConnectorByID( currentLink.ConnectorID ); //test for connector type = Need if ( correspondingConnector.Stereotype == "Need" || correspondingConnector.Stereotype == "has part" || correspondingConnector.Stereotype == "hasPart" ) { // Add the connector's details to the search data AddRow( xmlDOM, node, correspondingConnector, relState ); } you mean?
by Nic Plum on Fri, 09/03/2012 - 07:35.
Is there any way in which we can list all the relationships in a big model for a company using scripts?
by Tania Jacob on Fri, 14/01/2011 - 10:30.
Why doesn't list this sample search script in EA's Model Search window as stated in EA user guide ch 11.1.1? Am I missing something here?
by Manfred Kröpfli... on Fri, 22/10/2010 - 20:17.
1) See the API for DiagramLink: (http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/diagramlinks.htm)
The IsHidden property indicates whether the connector is visible on the diagram.
2) We ship a library of scripting examples, which includes the script VBScript - File IO Example. This script makes use of the ActiveX Scripting.FileSystemObject, which could be used to write out your CSV file. While the example is written in VBScript, it could quite easliy be ported to JScript. For an example on using ActiveX objects in JScript, see the Model Search example scripts.
3) The Information Items a connector conveys are not currently exposed to the Automation Interface. There has been some discussion on the forum in the past regarding how to access this information through querying the database directly (see http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1194955844). For further information on querying the database through the automation interface, see the Repository.SQLMethod() definition http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/repository3.htm
by Sparx Systems on Mon, 19/04/2010 - 11:50.
To sort or filter connectors of a certain type, the connector attribute MetaType is probably of the most interest to you (consult the SDK entry for Connector for more information http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/...).
Try the following changes:
1. Add a field entry to the XML in the variable SEARCH_SPECIFICATION at the top of the script called 'Type'
2. Populate the newly added field in the method AddRow() with a line such as:
AddField( xmlDOM, row, "Type", relationship.MetaType )
3. This will add an extra column that displays the Connector's type in the search results. You should then be able to sort or group your search results by this column.
If you want to show only connectors of a certain type we would recommend adding a condition before the call to AddRow() eg:
if ( correspondingConnector.MetaType == "Association" )
AddRow( xmlDOM, node, correspondingConnector );
by Sparx Systems on Thu, 18/03/2010 - 07:35.
Excellent. Slowly getting there! 1) Does this show all connectors for the diagram or just visible ones? Can it be restricted to visible ones? 2) As the EA search pane won't export the resulting table as CSV is it possible to get a script to output this (any clues)? It's a pain having to cut and paste and massage the result to get something that can be represented as a table in a document. If I can somehow include the Information Items conveyed by a connector then I'm almost there.
by Nic Plum on Thu, 18/03/2010 - 22:23.
Very helpful. If I wanted to get a subset of the links for the diagram then presumably the diagramLinks = theDiagram.DiagramLinks; would need to be modified. How can one filter or limit this collection of links so that only links of a certain type are shown? Is there a good reference for the script language that shows what objects/ methods are available?
by Nic Plum on Wed, 03/03/2010 - 20:13.