Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: survex on May 25, 2015, 04:56:16 am

Title: Apply layout to diagram via scripting
Post by: survex on May 25, 2015, 04:56:16 am
How to apply any layout (say spring) to diagram via scripting?
Title: Re: Apply layout to diagram via scripting
Post by: qwerty on May 25, 2015, 06:20:21 am
ProjectInterface.LayoutDiagramEx

q.
Title: Re: Apply layout to diagram via scripting
Post by: survex on May 27, 2015, 06:41:00 am
Here's a definition of the function:
LayoutDiagramEx (string DiagramGUID, long LayoutStyle, long Iterations, long LayerSpacing, long ColumnSpacing, boolean SaveToDiagram)

And description of LayoutStyle parameter:

LayoutStyle accepts the following options
•      Default Options:
    - lsDiagramDefault
    - lsProgramDefault
 
•      Cycle Removal Options:
    - lsCycleRemoveGreedy
    - lsCycleRemoveDFS
 
•      Layering Options:
    - lsLayeringLongestPathSink
    - lsLayeringLongestPathSource

lsLayeringOptimalLinkLength
 
•      Initialize Options:
    - IsInitializeNaive
    - IsInitializeDFSOut

IsInitializeDFSIn
 
•      Crossing Reduction Option:
    - lsCrossReduceAggressive

•      Layout Options - Direction

lsLayoutDirectionUp

lsLayoutDirectionDown

lsLayoutDirectionLeft

lsLayoutDirectionRight

And what should I pass as LayoutStyle parameter? What kind of long should it be?
Title: Re: Apply layout to diagram via scripting
Post by: qwerty on May 27, 2015, 06:47:17 am
Ah, that help... Look into the constants that are defined in the ScriptingLib MDG. Or probably you number the list which appears after ConstLayoutStyles Enum in the help.

q.
Title: Re: Apply layout to diagram via scripting
Post by: survex on May 27, 2015, 06:06:15 pm
Here's the working code snippet:

!INC Local Scripts.EAConstants-JScript

function main ()
{
      var DiagramGUID, LayoutStyle, Iterations, LayerSpacing, ColumnSpacing, SaveToDiagram;
      var Project as EA.Project;
      
      Project = Repository.GetProjectInterface();
      
      DiagramGUID = Project.GUIDtoXML( Repository.GetCurrentDiagram().DiagramGUID );
      
      //See ConstLayoutStyles in EAConstants-JScript
      //LayoutStyle = lsDiagramDefault
      LayoutStyle = lsCycleRemoveDFS | lsLayeringOptimalLinkLength | lsInitializeDFSOut | lsLayoutDirectionDown;
      
      Iterations = 4;
      LayerSpacing = 20;
      ColumnSpacing = 20;
      SaveToDiagram = false;

      Project.LayoutDiagramEx( DiagramGUID, LayoutStyle, Iterations, LayerSpacing, ColumnSpacing, SaveToDiagram );
}

main();