[NoBrainer] Connecting Enterprise Architect Interfaces with PowerShell

In one of our projects we needed to create a lot of components that expose interfaces. The steps to design this in EA are quite tedious:

  1. Link the Interface to the diagram of our component.
  2. Create a ProvidedInterface and attach it to the component
  3. Set the name of the ProvidedInterface to the name of the Interface
  4. Select the Interface from the search dialogue of the ProvidedInterface (internally this will set the ClassifierID on the ProvidedInterface)
  5. Create a Realization connector from the ProvidedInterface to the actual target Interface

The end result would then look like this:

Enterprise Architect ProvidedInterface

Enterprise Architect ProvidedInterface

Luckily this is all too easy with PowerShell:

PARAM
(
  [Parameter(Mandatory = $true, Position = 0)]
  [ValidateNotNullOrEmpty()]
  [string] $ProvidedInterface
  ,
  [Parameter(Mandatory = $true, Position = 1)]
  [ValidateNotNullOrEmpty()]
  [string] $Interface
  ,
  [ValidateScript( { Test-Path($_); } )]
  [Parameter(Mandatory = $true)]
  [ValidateNotNullOrEmpty()]
  [string] $Model
  ,
  [switch] $RecreateConnector = $true
  ,
  [Parameter(Mandatory = $false)]
  [string] $EaInterop = 'Sparx Systems\EA\Interop.EA.dll'
)

BEGIN 
{
  $OutputParameter = $null;

  $EaInteropPathAndFileName = Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath $EaInterop;
  Add-Type -Path $EaInteropPathAndFileName;
  $ea = New-Object -ComObject EA.Repository;

  $result = $ea.OpenFile($Model);
}

PROCESS 
{
  $source = $ea.GetElementByGuid([Guid]::Parse($ProvidedInterface).ToString('B'));
  $target = $ea.GetElementByGuid([Guid]::Parse($Interface).ToString('B'));

  # delete all existing connectors
  if($RecreateConnector)
  {
    $cMax = $source.Connectors.Count;
    for($c = 0; $c -lt $cMax; $c++)
    {
      $source.Connectors.Delete(0);
      $source.Connectors.Refresh();
    }
  }

  # this will add a Realization connector 
  $connector = $source.Connectors.AddNew([string]::Empty, 'Realization');
  $connector.SupplierID = $target.ElementID;
  $result = $connector.Update();
  $source.Connectors.Refresh();

  # this will link the `ProvidedInterface` with the `Interface`
  $source.ClassifierID = $target.ElementID;
  $source.Name = $target.Name;
  $source.Update();
}

END 
{
  $ea.CloseFile();
  $ea.Exit();
}

Via Ctrl+2 we can retrieve the GUID of the Interface and the ProvidedInterface so that we can then call the actual script:

.\Set-ProvidedInterface.ps1 {7C666C2A-571D-45c4-B1BF-0A19C698A050} {2EA70063-2D70-4670-9F61-5864AF8E485A}

Normally we would have to specify the model on every call via the -Model parameter. As this will probably always be the same during a design session, we can preset this via $PSDefaultParameterValues:

$PSDefaultParameterValues.Add('Set-ProvidedInterface.ps1:Model', 'C:\...\myModel.eap')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: