Book a Demo

Author Topic: Creating Connectors using Scripting  (Read 3346 times)

skranenburg

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Creating Connectors using Scripting
« on: November 20, 2020, 08:22:22 am »
Hi,

As the title says, I am trying to create connectors using the Sparx scripting feature (specifically Javascript), and keep running into errors that I haven't been able to fix. This is what I have so far:

function OnDiagramScript()
{
   // Get a reference to the current diagram
   var currentDiagram as EA.Diagram;
   currentDiagram = Repository.GetCurrentDiagram();
   m_repository = new COMObject("Scripting.FileSystemObject");

   if ( currentDiagram != null )
   {
      var source as EA.Element;
      var target as EA.Element;
      source = Repository.GetElementByGuid("{E4AF64F9-3720-4b89-97CS-93D90C3CF192}");
      target = Repository.GetElementByGuid("{E1FCC866-9DBE-4fc1-872E-A66D3F5845D5}");
      
      // Get a reference to any selected connector/objects
      
      var genConn as EA.Connector
      var Connectors as EA.Collection;
      Connectors = theElement.Connectors;
      genConn = Connectors.AddNew("", "Generalization");
      genConn.SupplierID = target.ElementID;
      genConn.ClientID = source.ElementID;
      
      genConn.Update;
      source.Connectors.Refresh;
      target.Connectors.Refresh;

      
   }
   else
   {
      Session.Prompt( "This script requires a diagram to be visible.", promptOK)
   }
}

OnDiagramScript();

I am not very experienced with this so I have been trying to patch scripts that I've found on the internet together. The error right now is that "TheElement" isn't defined. Can someone help me find what is wrong?

Thanks!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Creating Connectors using Scripting
« Reply #1 on: November 20, 2020, 08:42:51 am »
You never defined theElement anywhere. So of course it complains. Copy/paste from the Internet? That's what you get.

Try to set theElement to the element you target.

Basically: theElement is one EA element and it has a Connectors collection. There you can add a new connector with AddNew (like above). Either supplier or client is predefined (I always forget which, but guess the first) with the element's object ID. So only the other side needs to be referenced. The Refresh calls are for the bin. Update is doing the database magic to save the new connector.

q.
« Last Edit: November 20, 2020, 08:46:47 am by qwerty »

skranenburg

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Creating Connectors using Scripting
« Reply #2 on: November 20, 2020, 10:27:54 am »
Thank you Qwerty!

Ya, I'm new to scripting and Sparx so that was definitely a rookie mistake. I don't have any errors anymore but I don't see the connector on the diagram or in the properties of the blocks that were called.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Creating Connectors using Scripting
« Reply #3 on: November 20, 2020, 11:38:51 am »
That means you did not create it. Visibility on diagrams is a bit of Sparxian hacking. But when the Relations window does not show the connector it's not created.

Code: [Select]
connector = element1.Connectors.AddNew ("", "Dependency");
connector.SupplierID = element2.ElementID;
connector.Update ();

is what's basically needed (copied from my Scripting book; it's an abstract language here). This creates a dependency from element1 to element2.

q.

skranenburg

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Creating Connectors using Scripting
« Reply #4 on: November 21, 2020, 12:32:47 am »
That worked! Thank you!