0
votes

I am trying to add a legend to a digram partly following question: Enterprise Architect scripting with java - add CustomProperty but I have no luck in getting a visible legend on my diagram. Here is my code:

function addLegend()
{
   Repository.EnsureOutputVisible( "Script" );
   Repository.ClearOutput("Script");
   var pkg as EA.Package; 

   pkg = Repository.GetTreeSelectedPackage();   
   elements = pkg.Elements;
   var legend = elements.AddNew('Diagram Legend', 'Text');

   legend.Subtype = 76;
   legend.Update();
   elements.Refresh();
   var diagram as EA.Diagram;

   diagram = Repository.GetDiagramByID(10);
   Session.Output(diagram.Name);
   diagramObjects = diagram.DiagramObjects;


   diagramObject = diagramObjects.AddNew("l=100; r=100; t=100; b=500;", "");
   diagramObject.ElementID = legend.ElementID;
   diagramObjects.Refresh();
   diagram.Update();
}

` As said no legend is displayed - any help appreciated. My EA version is 12.0.

1
l=100;r=100 means you're specifying a zero width. Try r=200.Uffe
Yes agree but the size given does not affect the result, actually the code supplied by Geert makes it work. Changing the r= to e.g. 1000 just moves the legend to the right - apparently the size is not controlled by the parameters supplied to AddNew. but by the size of the titleJens R

1 Answers

1
votes

You don't save your newly created DiagramObject with a call to Update() and you don't need those calls to Refresh()

The call to update the diagram might also be undoing whatever you are trying to change to the diagram. Try it with the following slightly changed code (untested)

function addLegend()
{
   Repository.EnsureOutputVisible( "Script" );
   Repository.ClearOutput("Script");
   var pkg as EA.Package; 

   pkg = Repository.GetTreeSelectedPackage();   
   elements = pkg.Elements;
   var legend = elements.AddNew('Diagram Legend', 'Text');

   legend.Subtype = 76;
   legend.Update();
   var diagram as EA.Diagram;

   diagram = Repository.GetDiagramByID(10);
   Session.Output(diagram.Name);
   diagramObjects = diagram.DiagramObjects;


   diagramObject = diagramObjects.AddNew("l=100; r=100; t=100; b=500;", "");
   diagramObject.ElementID = legend.ElementID;
   diagramObject.Update();
}