Book a Demo

Author Topic: How to use JScript to create and draw dependencies  (Read 5200 times)

palim

  • EA User
  • **
  • Posts: 31
  • Karma: +1/-0
    • View Profile
How to use JScript to create and draw dependencies
« on: February 02, 2010, 02:37:58 am »
Hello everbody,

I`m trying to create some elements and add them to a diagram.
So far, this is working according to the example script 'Manage Diagrams Example' available within Enterprise Architect.

Now, I want to add some dependencies (like a realization) between these elements and draw it onto the diagram model as well.

Unfortunately, the reference API does not contain any examples of how to do something like this. It seems like there are no examples files available, too.

Could anybody give me some help or advice where to find the required information, please?


Best regards,
palim

mrf

  • EA User
  • **
  • Posts: 311
  • Karma: +0/-0
    • View Profile
Re: How to use JScript to create and draw dependen
« Reply #1 on: February 02, 2010, 11:47:13 am »
Hi palim,

I was certain we shipped a Scripting example that showed how to do this, but unfortunately it doesn't look like I was right...

Here's a JScript sample that shows how to create a connector between two elements:

Code: [Select]
/*
 * Script Name: ConnectorExample
 * Author: Michael Fraser (Sparx Systems)
 * Purpose: Example that creates two elements under the currently selected package in the
 * project browser and creates an association connector between them.
 * Date: 2010-02-02
 */
function ConnectorExample()
{
      var selectedPackage as EA.Package;
      selectedPackage = Repository.GetTreeSelectedPackage();

      Repository.EnsureOutputVisible( "Script" );
      Session.Output( "JScript ConnectorExample" );
      Session.Output( "=======================================" );
            
      if ( selectedPackage != null )
      {
            // Create client and supplier classes under the selected package
            var client as EA.Element;
            var supplier as EA.Element;
            var clientID, supplierID;
            
            client = selectedPackage.Elements.AddNew( "ClientClass", "Class" );
            client.Update();
            clientID = client.ElementID;
            
            supplier = selectedPackage.Elements.AddNew( "SupplierClass", "Class" );
            supplier.Update();
            supplierID = supplier.ElementID;

            selectedPackage.Elements.Refresh();
            
            // Get the connectors collection on the client element
            var connectors as EA.Collection;
            connectors = client.Connectors;
            
            // Create a new connector and set the appropriate details
            var c as EA.Connector;
            c = connectors.AddNew( "", "Association" );
            c.ClientID = clientID;
            c.SupplierID = supplierID;
            c.Direction( "Unspecified" );
            
            // Save the connector!
            c.Update();
      }
      else
      {
            Session.Prompt( "No package selected!", 0 );
      }
      
      Session.Output( "Done!" );
}

ConnectorExample();

Hope that helps! I'll try and get it included in the next release of EA  8-)
Best Regards,

Michael

[email protected]
"It is more complicated than you think." - RFC 1925, Section 2.8

palim

  • EA User
  • **
  • Posts: 31
  • Karma: +1/-0
    • View Profile
Re: How to use JScript to create and draw dependen
« Reply #2 on: February 02, 2010, 10:19:40 pm »
Hello Michael,

that`s exactly what I need.
Thank you very much for this code snippet.

One hint:
The (J-)script examples that are currently available in Enterprise Architect are missing the following line:
'Repository.EnsureOutputVisible( "Script" );'

Because of this, no output is displayed.

Guess you might want to fix this in further releases ;-)

Best regards,
palim

palim

  • EA User
  • **
  • Posts: 31
  • Karma: +1/-0
    • View Profile
Re: How to use JScript to create and draw dependen
« Reply #3 on: March 02, 2010, 07:51:17 am »
Hello,

I use the following code to create a dependcy between two elements (reqiurements):

function ConnectorExample()
{
      var selectedPackage as EA.Package;
      selectedPackage = Repository.GetTreeSelectedPackage();

      Repository.EnsureOutputVisible( "Script" );
      Session.Output( "JScript ConnectorExample" );
      Session.Output( "=======================================" );
            
      if ( selectedPackage != null )
      {
            // Create client and supplier classes under the selected package
            var client as EA.Element;
            var supplier as EA.Element;
            var clientID, supplierID;
            
            client = selectedPackage.Elements.AddNew( "ClientClass", "Requirement" );
            client.Update();
            clientID = client.ElementID;
            
            supplier = selectedPackage.Elements.AddNew( "SupplierClass", "Requirement" );
            supplier.Update();
            supplierID = supplier.ElementID;

            selectedPackage.Elements.Refresh();
            
            // Get the connectors collection on the client element
            var connectors as EA.Collection;
            connectors = client.Connectors;
            
            // Create a new connector and set the appropriate details
            var c as EA.Connector;
            c = connectors.AddNew( "", "Realization" );
            c.ClientID = clientID;
            c.SupplierID = supplierID;
            c.Direction( "source->destination" );
            
            // Save the connector!
            c.Update();
      }
      else
      {
            Session.Prompt( "No package selected!", 0 );
      }
      
      Session.Output( "Done!" );
}

ConnectorExample();


My problem is that the connector between the elements does not have a single arrow pointing to one element. How can I do this?

Also, can anybody give me the URL for sparx`s elements reference. I`ve already seen it, but unfortunately I`m not able to find it again :-(

Best regards,
palim

palim

  • EA User
  • **
  • Posts: 31
  • Karma: +1/-0
    • View Profile
Re: How to use JScript to create and draw dependen
« Reply #4 on: March 02, 2010, 08:39:54 am »
Hey there,

I found a solution:

Just replace the following line:
c.Direction( "source->destination" );

with that:
c.Direction( "Source -> Destination" );


Still, if anybody has the URL for this reference list, feel free to post ;-)

Best regards,
palim

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: How to use JScript to create and draw dependen
« Reply #5 on: March 02, 2010, 12:23:26 pm »
Quote
Hey there,

I found a solution:

Just replace the following line:
c.Direction( "source->destination" );

with that:
c.Direction( "Source -> Destination" );


Still, if anybody has the URL for this reference list, feel free to post ;-)

Best regards,
palim
I, too, have been caught by these spaces around the "->"...

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!