Book a Demo

Author Topic: Trouble with script & multiple elements  (Read 6005 times)

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Trouble with script & multiple elements
« on: February 14, 2014, 07:06:06 am »
I have the script below that works well for defining a relationship for a single element however I need to loop through a SQL query for all elements that the source element links to however am unsure how to do so. Can someone please help?

Code: [Select]
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= Repository.GetElementByID(9675)
               client.Update();
               supplier = Repository.GetElementByID(9676)
               supplier.Update();
            
               clientID = 9675;
           supplierID = 9676;

           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!" );

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #1 on: February 14, 2014, 07:28:13 am »
You don't need SQL in that case. Just iterate through SourceElem.Connectors and find Repository.GetElementByID(Connector.SupplierID) (or .ClientID depending how your connectors are oriented).

q.

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Trouble with script & multiple elements
« Reply #2 on: February 14, 2014, 09:18:29 am »
The issue i am having is how to plug a variable into say supplier = Repository.GetElementByID(testdestination)
« Last Edit: February 14, 2014, 10:10:49 am by motivatedgorilla »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #3 on: February 14, 2014, 11:42:16 am »
I'm no JScripter but
Code: [Select]
con = Elem.Connectors.GetAt(0);
e1 = Repository.GetElementByID(con.ClientID);
should work.

q.

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Trouble with script & multiple elements
« Reply #4 on: February 14, 2014, 12:19:42 pm »
Quote
I'm no JScripter but
Code: [Select]
con = Elem.Connectors.GetAt(0);
e1 = Repository.GetElementByID(con.ClientID);
should work.

q.

Thanks qwerty. I'm not wanting to iterate through the connectors. I want to iterate through an array e.g. var testdestination = [9676,1234,9899]; which would then feed into Repository.GetElementByID(testdestination)
« Last Edit: February 14, 2014, 12:20:24 pm by motivatedgorilla »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #5 on: February 14, 2014, 09:14:42 pm »
Hm. Then what is the issue?

q.

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Trouble with script & multiple elements
« Reply #6 on: February 17, 2014, 05:24:56 am »
Quote
Hm. Then what is the issue?

q.

The issue is supplier = Repository.GetElementByID(testdestinationelement);. I get an "Internal application" error despite the value being an integer.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #7 on: February 17, 2014, 06:36:42 am »
Hmm. That could have a couple of reasons. Have you tried with supplying a simple integer. And then by first passing the array contents to an integer before passing that to the Get method?

Could also be a long/int issue.

q.
« Last Edit: February 17, 2014, 06:54:32 am by qwerty »

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Trouble with script & multiple elements
« Reply #8 on: February 17, 2014, 06:48:29 am »
Quote
Hmm. That could have a couple of reasons. Have you tried with supplying a simple integer. And then by first passing the array contents to an integer before passing that to the Get method?

q.

Yes. I was able to resolve the issue although it makes no sense. I needed to include quotation marks around the integer in the array e.g. "2".

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #9 on: February 17, 2014, 06:56:26 am »
The values are some Win32 objects (variants). No idea how to create a dedicated integer in your language, but coercing from a nice string seems to work then.

q.

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Trouble with script & multiple elements
« Reply #10 on: February 17, 2014, 07:12:51 am »
Quote
The values are some Win32 objects (variants). No idea how to create a dedicated integer in your language, but coercing from a nice string seems to work then.

q.

What do you mean by "The values are some Win32 objects (variants)"?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Trouble with script & multiple elements
« Reply #11 on: February 17, 2014, 10:05:34 am »
Maybe I was mislead by he way it's treated in Perl where the arguments are passed via Win32 OLE objects (variants). In Perl it does not matter whether you use a string or a number. Both will be converted the right way. But IIRC the parameters are generally passed as objects, not as primitives. To be honest, I'm not very sure how parameters are passed to the API of EA.

q.