Author Topic: Move multiple items into current package  (Read 563 times)

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Move multiple items into current package
« on: June 17, 2025, 11:10:54 pm »
Hi,
@Martindt asked how to move multiple elements that are on a Class diagram to to the current package. I asked Copilot to write me a script in JavaScript. He did, and I added it to my Model-Addin which I will probably upload to GIT within the next few days.
I will copy the complete topic, so that you can all see how easy it is to use AI.
Shimon

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Re: Move multiple items into current package
« Reply #1 on: June 17, 2025, 11:13:37 pm »
At the prompt (which is available in Edge, without any installation), I wrote the following:

I would like a Javascript in Enterprise Architect that moves all items I have on a current Diagram named "MoveToHere" to be moved into the current package of the diagram.
He replied as follows:

Below is an example of a JavaScript script you can run in Enterprise Architect’s built‐in scripting engine. This script checks that the currently open diagram is named "MoveToHere", then iterates over all the diagram objects (elements) and moves each element into the package that contains the diagram. You can paste this code into EA’s scripting window and execute it.


!INC Local Scripts.EAConstants-JScript

function MoveDiagramElementsToPackage() {
    // Get the currently open diagram
    var diagram = Repository.GetCurrentDiagram();
    if (diagram == null) {
        Session.Output("No diagram is open. Please open a diagram and try again.");
        return;
    }
   
    // Verify the diagram name is "MoveToHere"
    if (diagram.Name !== "MoveToHere") {
        Session.Output("The current diagram is not named 'MoveToHere'. Aborting the script.");
        return;
    }
   
    // Get the package that hosts the diagram
    var targetPackageID = diagram.PackageID;
    var targetPackage = Repository.GetPackageByID(targetPackageID);
    if (targetPackage == null) {
        Session.Output("Unable to locate the package associated with this diagram.");
        return;
    }
   
    Session.Output("Moving elements from diagram '" + diagram.Name + "' to package '" + targetPackage.Name + "'.");
   
    // Iterate through all diagram objects
    for (var i = 0; i < diagram.DiagramObjects.Count; i++) {
        var diagObj = diagram.DiagramObjects.GetAt(i);
        var element = Repository.GetElementByID(diagObj.ElementID);
        if (element != null) {
            // Only move elements that are not already in the target package
            if (element.PackageID != targetPackageID) {
                Session.Output("Moving element: " + element.Name);
                element.PackageID = targetPackageID;
                if (element.Update() === 0) {
                    Session.Output("Element " + element.Name + " updated successfully.");
                } else {
                    Session.Output("Failed to update element " + element.Name + ".");
                }
                // Refresh the element so that changes appear in the browser
                Repository.ReloadElement(element.ElementID);
            }
        }
    }
   
    // Refresh the diagram to reflect the latest changes
    Repository.ReloadDiagram(diagram.DiagramID);
    Session.Output("All applicable elements have been moved. Check your project browser for changes.");
}

// Execute the function
MoveDiagramElementsToPackage();



 

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Re: Move multiple items into current package
« Reply #2 on: June 17, 2025, 11:15:40 pm »
I had to remove the first line ( the include) as I inserted the Function into an existing Model-addin. I added a Menu Item that called the Function. This is the code that I wrote:

 if (ItemName == "MoveElementsToCurrentPackage") {
      outputMessage ("Sent from "+this.AddinName + "  " + ItemName ,this.AddinName  ,0 );
      
    MoveDiagramElementsToPackage();
   
   } 

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Re: Move multiple items into current package
« Reply #3 on: June 17, 2025, 11:19:12 pm »
This worked for one Class. I opened the console and saw that it failed on the following line:

 356: Repository.ReloadElement(element.ElementID); [!!!! Repository.ReloadElement is not a function]

I wrote this in the Copilot prompt, and he gave me the fix that works.

He also added an explanation on what the code does.



« Last Edit: June 27, 2025, 01:31:19 am by shimon »

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Re: Move multiple items into current package
« Reply #4 on: June 17, 2025, 11:20:00 pm »
The following is the code that works.


function MoveDiagramElementsToPackage() {
    // Get the currently open diagram
    var diagram = Repository.GetCurrentDiagram();
    if (diagram == null) {
        Session.Output("No diagram is open. Please open a diagram and try again.");
        return;
    }
   
    // Verify the diagram name is "MoveToHere"
    if (diagram.Name !== "MoveToHere") {
        Session.Output("The current diagram is not named 'MoveToHere'. Aborting the script.");
        return;
    }
   
    // Get the package that hosts the diagram
    var targetPackageID = diagram.PackageID;
    var targetPackage = Repository.GetPackageByID(targetPackageID);
    if (targetPackage == null) {
        Session.Output("Unable to locate the package associated with this diagram.");
        return;
    }
   
    Session.Output("Moving elements from diagram '" + diagram.Name + "' to package '" + targetPackage.Name + "'.");
   
    // Iterate through all diagram objects
    for (var i = 0; i < diagram.DiagramObjects.Count; i++) {
        var diagObj = diagram.DiagramObjects.GetAt(i);
        var element = Repository.GetElementByID(diagObj.ElementID);
        if (element != null) {
            // Only move elements that are not already in the target package
            if (element.PackageID != targetPackageID) {
                Session.Output("Moving element: " + element.Name);
                element.PackageID = targetPackageID;
                if (element.Update() === 0) {
                    Session.Output("Element " + element.Name + " updated successfully.");
                } else {
                    Session.Output("Failed to update element " + element.Name + ".");
                }
                // If you feel a refresh is required for the project browser, you can call:
                // Repository.RefreshModelView(targetPackageID);
            }
        }
    }
   
    // Refresh the diagram to reflect the latest changes
    Repository.ReloadDiagram(diagram.DiagramID);
    Session.Output("All applicable elements have been moved. Check your project browser for changes.");
}



// Execute the function
MoveDiagramElementsToPackage();

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13387
  • Karma: +566/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Move multiple items into current package
« Reply #5 on: June 17, 2025, 11:43:11 pm »
Repository.ReloadPackage() is probably better then Repository.RefreshModelView()

Geert

shimon

  • EA User
  • **
  • Posts: 162
  • Karma: +5/-0
    • View Profile
Re: Move multiple items into current package
« Reply #6 on: June 24, 2025, 04:26:20 pm »
Thanks Geert.

I uploaded the Addin to GitHub:
https://github.com/shimonj/ModelAddinStarter
If you have any comments or suggestions, please post them here.
Sincerely,
Shimon

P.S. I will probably create a You-Tube recording of how to use it, and to explain some coding decisions I made.