Author Topic: Creating hyperlinks on a diagram from script  (Read 535 times)

Digo

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Creating hyperlinks on a diagram from script
« on: March 10, 2012, 03:29:45 am »
Hi,

I've just spent half a day trying to dynamically create hyperlinks on a diagram.

I've succeeded to create a hyperlink to a package:

Code: [Select]
     var newel as EA.Element;
      newel = thePackage.Elements.AddNew("$package://{555834AE-3C56-4860-AEDA-16DF8592A50F}", "Text");
      newel.Update();
      newel.Subtype = 19; // This one is important for hyperlinks to packages!!
      newel.Update();
      newel.Notes = "NEW LINK";
      newel.Update();
      AddElementToDiagram(newel, theDiagram);
      newel.Update();

with the AddElementToDiagram function being this:

Code: [Select]
function AddElementToDiagram(theElement, theDiagram)
{
      var diagramObjects as EA.Collection;
      var obj as EA.DiagramObject;

      theDiagram.Update()
      diagramObjects = theDiagram.DiagramObjects;
      obj = diagramObjects.AddNew( "l=200;r=400;t=200;b=400;", "" );
      obj.ElementID( theElement.ElementID );
      obj.Update();
      obj.Style = "BackColor=-1;BorderColor=-1;BorderWidth=-1;FontColor=-1;VSwimLanes=1;HSwimLanes=1;BorderStyle=0;" ;
      obj.Update();
      Session.Output("Added object " + theElement.Name)
}

I can't really explain the magic number 19 for subtype, but I've got it from the XMI export.

I did not however succeed to create a hyperlink to a diagram.  There's only a few differences from what I've done in the code above:

Code: [Select]
     newel = thePackage.Elements.AddNew("$diagram://{9F181280-8ECF-4bcd-9C28-4A3B138BC68C}", "Text");
      newel.Update();
      newel.Subtype = 0;      // This one is for hyperlinks diagram to diagrams.

I get something on my diagram, but it doesn't work.  If I compare the XMI export for my generated link and a manually added link, I see that seem to miss a tag called 'diagram' which holds the GUID of the diagram I'm referring to once more.  I can't find it back in the scripting object howerver (I've traced almost the entire object).

Code: [Select]
<UML:TaggedValue tag="diagram" value="EAID_9F181280_8ECF_4bcd_9C28_4A3B138BC68C"/>

Is there anyone who has ever succesfully added hyperlinks to diagrams and who can explain to me how I should do it?  Or someone who can tell me what I'm missing?

Thanks in advance and have a nice weekend,

Diego

JDT

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
  • It never hurts to help - [Eek the Cat]
    • View Profile
Re: Creating hyperlinks on a diagram from script
« Reply #1 on: December 15, 2017, 11:39:38 pm »
Hey there,

as I've been looking all over the place for a solution to create links to diagrams on an existing diagram, and only kept being pointed to this (unsanswered) post, I thought I'd actually post the solution when I found it.. And I did!

First things first: Diego's script actually *does* make a hyperlink. If you click it, the diagram gets selected in the project browser. However, what he wanted to achieve, is to make a diagram hyperlink that actually opens the diagram. The solution below manages to do so.

Analysis steps
  • Create a Diagram hyperlink manually by dragging the target diagram onto the diagram and selecting "Drop As..." -> Hyperlink.
  • Find the element ID of the created link
Code: [Select]
var diagram as EA.Diagram; // Set diagram to your current diagram

var diagramObject as EA.DiagramObject;
var element as EA.Element;

for (var index = 0; var index < diagram.DiagramObjects.Count; index++) {
  diagramObject = diagram.DiagramObjects.GetAt(index);
  element = Repository.GetElementByID(diagramObject.ElementID);

  Session.Output("Element: " + element.ElementID);
}

  • Analyse the element in the database
Code: [Select]
select * from t_object where object_id = '<insert ElementID here>'

Solution

What I found out, was that the Text element actually isn't a hyperlink (so setting the Subtype is not a good idea). It's a "normal" text element, but it has PDATA1 set to the DiagramID. I tried to set PDATA1 via script (some property?), but haven't yet found a "non-SQL" way of doing so. So here's the solution with a little bit of SQL to update PDATA1 at the end.. (If I find out how to get it done without SQL I will post it)

Code: [Select]
/**
 * Creates a link to a target diagram on a diagram.
 *
 * @param package Package where the source diagram is located.
 * @param diagram Source diagram onto which the link is created.
 * @param targetDiagram Target diagram for which the link that is creatde points.
/*
function createDiagramHyperlink(package, diagram, targetDiagram) {
// Make a hyperlink element
var hyperlink as EA.Element;
hyperlink = package.Elements.AddNew(targetDiagram.Name, "Text" );
hyperlink.Notes = targetDiagram.Name;
hyperlink.Update();

// Add the hyperlink to the diagram
var diagramObject as EA.DiagramObject;
diagramObject = diagram.DiagramObjects.AddNew("", "" );
diagramObject.ElementID = hyperlink.ElementID;
diagramObject.Update();
diagram.Update();

// Update the PDATA1 and set it to the target diagram's ID
Repository.Execute( "UPDATE t_object SET PDATA1='" + targetDiagram.DiagramID + "' where object_ID = '" + hyperlink.ElementID + "'" );
}

I hope this solution is useful to someone out there, even if it is an old post :-). Errors and ommissions excepted.

Cheers,
JDT

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7808
  • Karma: +171/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Creating hyperlinks on a diagram from script
« Reply #2 on: December 15, 2017, 11:50:29 pm »
<snip>.. (If I find out how to get it done without SQL I will post it)<snip>
You can't update PDATA fields (Miscdata) from the API.

Geert