Thursday, 17 December 2009 03:32

Listing all Connectors in a Diagram

Written by 
Rate this item
(0 votes)

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:

  1. Open Enterprise Architect's Scripting window.
  2. You may create a New Group or place the script within an existing Group.
  3. Right click the Group and select the option "New JScript".
  4. Label the script "List Connectors".
  5. Copy the script below and paste it into the script "List Connectors".
  6. Save the script.
  7. Select the diagram of interest in the Project Browser. (If it is already open, ensure that changes are saved.)
  8. Select the script "List Connectors" and execute it by selecting the "Run script" button.
  9. 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();

Read 1833 times
sparxsystems

Sparx Systems


99 Albert Street
Creswick,
Victoria
3363
Australia
P: +61 3 5345 1140
F: +61 3 5345 1104

Sparx Systems specializes in high performance and scalable visual modeling tools for the planning, design and construction of software intensive systems. A Contributing Member of the Object Management Group (OMG), Sparx Systems is committed to realizing the potential of model-driven development based on open standards. The company’s flagship product, Enterprise Architect, has received numerous accolades since its commercial release in August, 2000. Enterprise Architect is the design tool of choice for over 300,000 registered users world-wide.

Website: community.sparxsystems.com
Login to post comments