Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Digo on March 10, 2012, 03:29:45 am

Title: Creating hyperlinks on a diagram from script
Post by: Digo on March 10, 2012, 03:29:45 am

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.Subtype = 19; // This one is important for hyperlinks to packages!!
      newel.Notes = "NEW LINK";
      AddElementToDiagram(newel, theDiagram);

with the AddElementToDiagram function being this:

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

      diagramObjects = theDiagram.DiagramObjects;
      obj = diagramObjects.AddNew( "l=200;r=400;t=200;b=400;", "" );
      obj.ElementID( theElement.ElementID );
      obj.Style = "BackColor=-1;BorderColor=-1;BorderWidth=-1;FontColor=-1;VSwimLanes=1;HSwimLanes=1;BorderStyle=0;" ;
      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.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,

Title: Re: Creating hyperlinks on a diagram from script
Post by: JDT 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
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);

Code: [Select]
select * from t_object where object_id = '<insert ElementID here>'


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;

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

// 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.

Title: Re: Creating hyperlinks on a diagram from script
Post by: Geert Bellekens 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.

Title: Re: Creating hyperlinks on a diagram from script
Post by: willberger on April 04, 2018, 10:38:43 pm
Hey Guys, this thread really helped me to get scripting hyperlinks to point to a different sequence diagram to work.  Thank you all.  Hope this snippet helps someone out a bit.

var hyperlink as EA.Element;
                  hyperlink = srcDiagPackage.Elements.AddNew("$diagram://"+diagram.DiagramGUID,"Text");
                  hyperlink.Subtype = 0; // This one is important for hyperlinks to diagrams to work!!
                  // Required Update the PDATA1 and set it to the target diagram's ID
                  Repository.Execute( "UPDATE t_object SET PDATA1='" + diagram.DiagramID + "' where object_ID = " + hyperlink.ElementID + "" );
                  var diagramObject as EA.DiagramObject;
                  diagramObject = sourceDiagram.DiagramObjects.AddNew("","");
                  diagramObject.ElementID = hyperlink.ElementID;
                  if (con.EndPointX <= con.StartPointX) {
                     // Connector End Point id  <--- Place Hyper link to the left
                     diagramObject.left = con.EndPointX-35;
            = con.EndPointY+10;
                     diagramObject.right = con.EndPointX-15;
                     diagramObject.bottom = con.EndPointY-10;   
                  else if  (con.EndPointX > con.StartPointX) {
                     // Connector End Point id  ---> Place Hyper link to the right
                     diagramObject.left = con.EndPointX+15;
            = con.EndPointY+10;
                     diagramObject.right = con.EndPointX+35;
                     diagramObject.bottom = con.EndPointY-10;   
                  //diagramObject.ElementDisplayMode = templateElement.ElementDisplayMode;
                  //diagramObject.Style = templateElement.Style;
Title: Re: Creating hyperlinks on a diagram from script
Post by: roman104 on October 08, 2018, 04:59:34 am
Hi All,
Thank You for this topic, It helped me to put the 'Package Hyperlink' on current diagram

Please, don't forget Reload the diagram at the end of all actions.


Example: This script is located in Diagram scripts group.

option explicit

!INC Local Scripts.EAConstants-VBScript

' *********************** > SCRIPT SUMMARY < ************************* 01-DIA-PackageHyperlink
'> Script Name:   01-DIA-PackageHyperlink
'> Author:       Roman Kazicka
'> Purpose:    Better navigation in model repository via diagrams
'>            the goal is to automatically generate hyperlink to package on current diagram
'> Category:   Model Navigation
'> Description: Create on visible Diagram 'PackageHyperlink' to current Package in 'Project Browser'
'> Trigger:      Via context menu in current diagram
'> Input:      Selected Package in ProjectBrowser, Open diagram on which will be created hyperlink
'> Output:      Package hyperling on current diagram  with note #ModelNavigation#
'> Date:       20181007
'> Notes:      
' TODO 1:

'__________________________> Script Summary < ____________________________01-DIA-PackageHyperlink
' Script Name:            01-DIA-Package Hyperlink
' Author:                Roman Kazicka
' Purpose:                Better navigation in the model via diagram Package Hyperlink
' Description:             
' Trigger:              via script in Diagram, (the same thing can be done via Project Browser script with the same name 01-PB-PackageLink
' Date:                2081006

' Diagram Script main function
sub OnDiagramScript()

dim currentDiagram as EA.Diagram
set currentDiagram = Repository.GetCurrentDiagram()
' This check is valuable in run mode
' Diagram have to visible
if not currentDiagram is nothing then
 dim  thePackage as EA.Package;

dim  newHyperlinkel as EA.Element;

set thePackage= Repository.GetTreeSelectedPackage()
set newHyperlinkel = thePackage.Elements.AddNew("$package://"& thePackage.PackageGUID , "Text")
      newHyperlinkel.Subtype = 19 '// This one is important for hyperlinks to packages!!
      newHyperlinkel.Notes = "#ModelNavigation#"

  AddElementToDiagram newHyperlinkel, currentDiagram
'If diagram is not visibe -
Session.Prompt "This script requires a diagram to be visible", promptOK
end if

end sub
sub AddElementToDiagram(theElement, theDiagram)

      dim diagramObjects as EA.Collection
      dim obj as EA.DiagramObject

      set diagramObjects = theDiagram.DiagramObjects
       set obj = diagramObjects.AddNew( "l=200;r=400;t=-300;b=-400;", "TEXT" )
  set obj = diagramObjects.AddNew( "", "TEXT" )
      obj.ElementID( theElement.ElementID )
      obj.Style = "BackColor=-1;BorderColor=255BorderWidth=1;FontColor=-1;VSwimLanes=1;HSwimLanes=1;BorderStyle=0;"

      Session.Output("Added object " & theElement.Name)

end sub