Author Topic: Changing an element type and stereotype using JavaScript  (Read 2431 times)

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Changing an element type and stereotype using JavaScript
« on: July 03, 2024, 02:38:52 am »
With announcement that VBScript support will be deprecated in the future, we have decided to try and write some new scripts using JavaScript and hit some problems. In JavaScript the following 2 lines of code result on the following message: "Object doesn't support this property or method".

Code: [Select]
selectedObject.Type = "Class";
selectedObject.StereotypeEx = "ArchiMate3::ArchiMate_Requirement";

Similar VBScript code works fine. What are we missing?


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #1 on: July 03, 2024, 03:36:22 pm »
A complete stab in the dark. It is because of the Type property that is a preserved word or something?

Geert

ea0522

  • EA User
  • **
  • Posts: 109
  • Karma: +3/-0
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #2 on: July 03, 2024, 03:40:41 pm »
I've noticed something similar when writing JavaScript.
According to the specifications, a variable should be declared using "let" or "const". The "var" seems to be something from the past.
However, I have succesfully used the "var" keyword to declare a variable of a certain object type, e.g.:

Code: [Select]
function setElementName( theElement /* : EA.Element */ ) /* : void */
{
   var curElement   as EA.Element;
   curElement          = theElement;
   curElement.Name = "Name";
}

Still not very clear to me what the difference is between the use of "var" and "let" as "let as ..." generates compiler errors.
HTH
« Last Edit: July 03, 2024, 03:43:38 pm by ea0522 »

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1296
  • Karma: +119/-10
  • Its the results that count
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #3 on: July 03, 2024, 03:44:01 pm »
I posted some javascript code a while back on this forum that changes type and stereo type of elements. Do a search you might find it.
Happy to help
:)

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #4 on: July 03, 2024, 08:42:03 pm »
I posted some javascript code a while back on this forum that changes type and stereo type of elements. Do a search you might find it.
Thank you, much appreciated. I found several of your posts with JavaScript examples, the best one is perhaps https://sparxsystems.com/forums/smf/index.php/topic,48139.msg279730.html#msg279730, but nothing that changes the type and stereotype.

Furthermore, the script you shared on the message linked above very helpfully points to the Element API, but it is an older version and the link to go to the latest version is broken.

The biggest issue we have is that without any examples and/or documentation, none of us has time to figure out how how this should work in JavaScript. Since, it works in VBScript we may just have to bite the following when the functionality is removed.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #5 on: July 03, 2024, 08:55:13 pm »
The biggest issue we have is that without any examples and/or documentation, none of us has time to figure out how how this should work in JavaScript. Since, it works in VBScript we may just have to bite the following when the functionality is removed.

There are several sample scripts included in the installation. See you script library Local Scripts
The API documentation is the same for all languages.

Can you maybe post the rest of the script?
Your selectedObject object might not be of the right type. (e.g. EA.Diagram instead of EA.Element)

Geert

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #6 on: July 03, 2024, 10:27:07 pm »
Current rough script looks like

Code: [Select]
!INC Local Scripts.EAConstants-JScript

/*
 * This code has been included from the default Diagram Script template.
 * If you wish to modify this template, it is located in the Config\Script Templates
 * directory of your EA install path.
 *
 * Script Name:
 * Author:
 * Purpose:
 * Date:
 */

/*
 * Diagram Script main function
 */
function OnDiagramScript()
{
// Get a reference to the current diagram
var currentDiagram as EA.Diagram;
currentDiagram = Repository.GetCurrentDiagram();

Session.Output("Started");

if ( currentDiagram != null )
{
Session.Output(currentDiagram.Name);
// Get a reference to any selected connector/objects
var selectedConnector as EA.Connector;
var selectedElements as EA.Collection;
selectedElements = currentDiagram.SelectedObjects;
Session.Output(selectedElements.Count());

//selectedConnector = currentDiagram.SelectedConnector;


if ( selectedConnector != null )
{
// A connector is selected
}
else if (selectedElements.Count > 0 )
{
var objectCount = 0;

while (objectCount < selectedElements.Count())
{
Session.Output("Enumerating");
var currentElement as EA.Element;
currentElement = selectedElements.GetAt(objectCount);
Session.Output(currentElement.Type);
Session.Output(currentElement.Name);
currentElement.Type = "Class"; //Change the type - it fails
currentElement.Stereotype = ""; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx
currentElement.Update();
currentElement.Stereotype = "ArchiMate_Requirement"; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx
currentElement.Update();
//currentElement.StereotypeEx = "ArchiMate3::ArchiMate_Requirement"; //This also fails
objectCount = objectCount + 1;
}
}
else
{
// Nothing is selected
}
}
else
{
Session.Prompt( "This script requires a diagram to be visible.", promptOK)
}
}

OnDiagramScript();

All of this fails to execute
Code: [Select]
Session.Output(currentElement.Type);
Session.Output(currentElement.Name);
currentElement.Type = "Class"; //Change the type - it fails
currentElement.Stereotype = ""; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx
...
currentElement.Stereotype = "ArchiMate_Requirement"; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #7 on: July 03, 2024, 11:13:47 pm »
I don't even get that far

Quote
---------------------------
Error
---------------------------
Diagram Group.TestJavaScript
selectedElements.Count is not a function, Line:45
---------------------------
OK   
---------------------------
After fixing the syntax errors I got an error on trying to display the element.Type
Code: [Select]
Session.Output(currentElement.Type);That is because currentElement is an EA.DiagramObject, not an EA.Element

You would get exactly the same type of error in VBScript.

This works:

Code: [Select]
var currentDiagramObject as EA.DiagramObject;
currentDiagramObject = selectedElements.GetAt(objectCount);
var currentElement = Repository.GetElementByID(currentDiagramObject.ElementID);
Session.Output(currentElement.Type);
Session.Output(currentElement.Name);

Geert

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #8 on: July 04, 2024, 02:35:27 am »
Thanks Geert, much appreciated. Out of curiosity, what version are you using? We should we getting the same error you are getting with selectedElements.Count  but we are not.

This works and have not changed selectedElements.Count.

Code: [Select]
!INC Local Scripts.EAConstants-JScript

/*
 * This code has been included from the default Diagram Script template.
 * If you wish to modify this template, it is located in the Config\Script Templates
 * directory of your EA install path.
 *
 * Script Name:
 * Author:
 * Purpose:
 * Date:
 */

/*
 * Diagram Script main function
 */
function OnDiagramScript()
{
// Get a reference to the current diagram
var currentDiagram as EA.Diagram;
currentDiagram = Repository.GetCurrentDiagram();

Session.Output("Started");

if ( currentDiagram != null )
{
Session.Output(currentDiagram.Name);
// Get a reference to any selected connector/objects
var selectedConnector as EA.Connector;
var selectedElements as EA.Collection;
selectedElements = currentDiagram.SelectedObjects;
Session.Output(selectedElements.Count());

//selectedConnector = currentDiagram.SelectedConnector;


if ( selectedConnector != null )
{
// A connector is selected
}
else if (selectedElements.Count > 0 )
{
var objectCount = 0;

while (objectCount < selectedElements.Count())
{
Session.Output("Enumerating");
var currentDiagElement as EA.DiagramObject;
currentDiagElement = selectedElements.GetAt(objectCount);
var currentElement = Repository.GetElementByID(currentDiagElement.ElementID);
Session.Output(currentElement.Type);
Session.Output(currentElement.Name);
currentElement.Type = "Class"; //Change the type - it fails
currentElement.Stereotype = ""; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx
currentElement.Update();
currentElement.Stereotype = "ArchiMate_Requirement"; //Clear the Current Stereotype - it fails with both Stereotype and StereotypeEx
currentElement.Update();
//currentElement.StereotypeEx = "ArchiMate3::ArchiMate_Requirement"; //This also fails
objectCount = objectCount + 1;
}
}
else
{
// Nothing is selected
}
}
else
{
Session.Prompt( "This script requires a diagram to be visible.", promptOK)
}
}

OnDiagramScript();

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #9 on: July 04, 2024, 02:58:52 am »
I've tested this with version 16.1 as a javascript script (not Jscript)

Geert

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #10 on: July 04, 2024, 03:15:52 am »
Same version. What I cannot check right now is how we ended with JScript when I thought we created a JavaScript script.

Also, I am sure I want to ask this after an online search but what is the difference and why do they look so horribly and confusedly similar?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #11 on: July 04, 2024, 02:47:25 pm »
Jscript is Microsofts copy of Javascript.

The main difference is that it's being interpreted and executed by a different application.

There are a few differences in syntax. One of them is the way to create objects from other libraries.

Geert

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1063
  • Karma: +28/-8
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #12 on: July 10, 2024, 12:37:17 am »
Please find below a working JavaScript for reference, oddly selectedObjects.Count() does not error and have not time to research what is the difference.

Code: [Select]
!INC Local Scripts.EAConstants-JavaScript

/*
 * This code has been included from the default Diagram Script template.
 * If you wish to modify this template, it is located in the Config\Script Templates
 * directory of your EA install path.
 *
 * Script Name:
 * Author:
 * Purpose:
 * Date:
 */

/*
 * Diagram Script main function
 */
function OnDiagramScript()
{
// Get a reference to the current diagram
var currentDiagram as EA.Diagram;
currentDiagram = Repository.GetCurrentDiagram();

if ( currentDiagram != null )
{
// Get a reference to any selected connector/objects
var selectedConnector as EA.Connector;
var selectedObjects as EA.Collection;
selectedConnector = currentDiagram.SelectedConnector;
selectedObjects = currentDiagram.SelectedObjects;

if ( selectedConnector != null )
{
// A connector is selected
}
else if ( selectedObjects.Count > 0 )
{
// One or more diagram objects are selected
var objectCount = 0;

while (objectCount < selectedObjects.Count())
{
Session.Output("Enumerating");
var currentDiagElement as EA.DiagramObject;
currentDiagElement = selectedObjects.GetAt(objectCount);
var currentElement = Repository.GetElementByID(currentDiagElement.ElementID);
Session.Output(currentElement.Type);
Session.Output(currentElement.Name);
currentElement.Type = "Class";
currentElement.Stereotype = "";
currentElement.Update();
currentElement.Stereotype = "ArchiMate_Requirement";
currentElement.Update();
objectCount = objectCount + 1;
}
}
else
{
// Nothing is selected
}
}
else
{
Session.Prompt( "This script requires a diagram to be visible.", promptOK)
}
}

OnDiagramScript();

ea0522

  • EA User
  • **
  • Posts: 109
  • Karma: +3/-0
    • View Profile
Re: Changing an element type and stereotype using JavaScript
« Reply #13 on: July 10, 2024, 03:38:05 pm »
Why use both "selectedObjects.Count()" and "selectedObjects.Count"?
Count is a property and should not be used as a method?

Further do you get the desired ArchiMate type?
When I use something similar in my scripts, I end up with stereotype "ArchiMate::ArchiMate_Requirement" which is an ArchiMate2 reference instead of ArchiMate3.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13071
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an element type and stereotype using JavaScript
« Reply #14 on: July 10, 2024, 04:32:06 pm »
Yeah, you should really only use the fully qualified stereotype. Otherwise EA will be guessing which profile you want it from and use the first one it finds.

so something like
Code: [Select]
element.SteretoypEx = "ArchiMate3::ArchiMate_Requirement"
PS. Check for uppercase/lowercase because that matters in these cases

Geert