Author Topic: Adding package on diagram with scriptlet (diagramObject is undefined)  (Read 3072 times)

Hurra

  • EA User
  • **
  • Posts: 183
  • Karma: +0/-0
    • View Profile
    • Find me at LinkedIn!
Hello!

I wanted to try out a scriptlet and want to add all childPackges in a diagram.

The setup:

Package contains childPackages and a diagram.
The scriptlet should add all the childPackages on the diagram.

The scriptlet:
Code: [Select]
function addPackagestoDiagram()
{
Repository.ClearOutput("Script");
Repository.EnsureOutputVisible("Script");

var currentDiagram as EA.Diagram;
var diagramPackage as EA.Package;
var childPackages as EA.Collection;
var diagramObjects as EA.Collection;
var diagramObject as EA.DiagramObject;
var currentPackage as EA.Package;

currentDiagram = Repository.GetCurrentDiagram();
diagramPackage = Repository.GetPackageByID(currentDiagram.PackageID);
childPackages = diagramPackage.Packages;
diagramObjects = currentDiagram.DiagramObjects;

for (i = 0; i < childPackages.Count; i++)
{
currentPackage = childPackages.GetAt(i);
diagramObject = diagramObjects.AddNew("","");
diagramObject.ElementID = currentPackage.Element.ElementID;
diagramObjects.Update();
diagramObject.Update();
}

currentDiagram.Update();
 

}
addPackagestoDiagram();

This line:
Code: [Select]
diagramObject.ElementID = currentPackage.Element.ElementID;

yields diagramObject is undefinied.

I've been looking at:
https://sparxsystems.com/forums/smf/index.php?topic=39128.0
and that is how it's done.

I've also tried changing
Code: [Select]
diagramObject.ElementID = currentPackage.Element.ElementID;
to
Code: [Select]
diagramObject.ElementID(currentPackage.Element.ElementID);
as in the linked thread.

But the problem occurs either way, giving diagramObject is undefinied.

What am I missing? 🤔

Thanks!
always learning!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #1 on: November 09, 2022, 07:53:06 pm »
it looks like the previous call to create a new DiagramObject failed (and returned NULL)

Code: [Select]
diagramObject = diagramObjects.AddNew("","");
Check the diagramObject variable for NULL before continuing.

Geert

Hurra

  • EA User
  • **
  • Posts: 183
  • Karma: +0/-0
    • View Profile
    • Find me at LinkedIn!
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #2 on: November 09, 2022, 08:49:26 pm »
it looks like the previous call to create a new DiagramObject failed (and returned NULL)

Code: [Select]
diagramObject = diagramObjects.AddNew("","");
Check the diagramObject variable for NULL before continuing.

Geert

This
Code: [Select]
if (diagramObject == null)
{
    Session.Output("Geert were right as usual!");
}
indeed confirm this.

But how do I create the diagramObject then?
always learning!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #3 on: November 09, 2022, 09:04:11 pm »
like that, but there is something going wrong.

You can play a bit with the parameters (supply an name, and a position string) or try to see what diagramObjects.GetLastError() returns

Geert

Hurra

  • EA User
  • **
  • Posts: 183
  • Karma: +0/-0
    • View Profile
    • Find me at LinkedIn!
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #4 on: November 09, 2022, 09:16:42 pm »
like that, but there is something going wrong.

You can play a bit with the parameters (supply an name, and a position string) or try to see what diagramObjects.GetLastError() returns

Geert

Yeah, I tried
Code: [Select]
diagramObject = diagramObjects.AddNew(currentPackage.Name, currentPackage.ObjectType);
based on https://sparxsystems.com/enterprise_architect_user_guide/16.0/add-ins___scripting/collection.html
-> AddNew(string Name, string Type)

But the diagramObjects.GetLastError() returns:
Code: [Select]
Script security: No create permission
Huh?

It's a local QEAX-file, I haven't touched security, it's not even enabled.
always learning!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #5 on: November 09, 2022, 09:34:29 pm »
Code: [Select]
diagramObject = diagramObjects.AddNew(currentPackage.Name, currentPackage.ObjectType);
Yeah, those are not the correct parameters for DiagramObjects

Try something like this:

Code: [Select]
diagramObjects.AddNew("l=10;r=100;t=10;b=200;", "")
And make sure to test your script as a regular script, before testing it as a scriptlet.

Geert

Hurra

  • EA User
  • **
  • Posts: 183
  • Karma: +0/-0
    • View Profile
    • Find me at LinkedIn!
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #6 on: November 09, 2022, 11:03:55 pm »
...
Geert
I guess the permission warning is the major issue.
Code: [Select]
diagramObjects.AddNew("l=10;r=100;t=10;b=200;", "")This still results:
diagramObjects.GetLastError() = "Script security: No Create permission"

This is also the case when I run it as a regular script on the diagram.
always learning!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13136
  • Karma: +547/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #7 on: November 09, 2022, 11:28:41 pm »
Must be something special with the diagram I guess. Have you tried with a new diagram?

I just tried this little (VB)script in v16.1 on a qea model, and it works just fine

Code: [Select]
sub main
dim diagram as EA.Diagram
dim package as EA.Package
set package = Repository.GetPackageByGuid("{A5CBF53F-2033-4c9a-B505-3341E5595393}")
set diagram = Repository.GetDiagramByGuid("{9585B09C-28C5-485b-AF25-79F1D70EAA18}")
dim diagramObjects as EA.Collection
dim diagramObject as EA.DiagramObject

set diagramObjects = diagram.DiagramObjects
set diagramObject = diagramObjects.AddNew("l=10;r=100;t=10;b=200;", "")
diagramObject.ElementID = package.Element.ElementID
diagramObject.Update
end sub

main

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13573
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #8 on: November 10, 2022, 12:00:53 am »
diagramObjects.GetLastError() = "Script security: No Create permission"
Never seen that. And I'm not using the "security" often. So sounds there is a new granularity. Are you running that as admin?

q.

Takeshi K

  • EA User
  • **
  • Posts: 542
  • Karma: +33/-1
    • View Profile
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #9 on: November 10, 2022, 11:16:51 am »
Hello all,

As the help says, scriptlets cannot update something.

https://sparxsystems.com/enterprise_architect_user_guide/16.0/modeling_fundamentals/scriptlets.html
Quote
Code in Scriptlets cannot alter or update elements in the model database.

HTH,
--
t-kouno

qwerty

  • EA Guru
  • *****
  • Posts: 13573
  • Karma: +395/-301
  • I'm no guru at all
    • View Profile
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #10 on: November 10, 2022, 07:23:06 pm »
I would bet that Repository.Execute works in those "scriptlets". Just a guess, but that would be sparxian...

q.

MichPaule

  • EA User
  • **
  • Posts: 53
  • Karma: +0/-0
    • View Profile
Re: Adding package on diagram with scriptlet (diagramObject is undefined)
« Reply #11 on: July 09, 2024, 09:43:15 pm »
As the help says, scriptlets cannot update something.

https://sparxsystems.com/enterprise_architect_user_guide/16.0/modeling_fundamentals/scriptlets.html
Quote
Code in Scriptlets cannot alter or update elements in the model database.
This seems not to be entirely true.
I'm able to alter stereotyped tagged value contents using scriptlets and the changes are immediately reflected in the model database (checked with DB Browser for SQLite).
Maybe the sentence is to be read verbatim ==> elements cannot be altered but their tagged values can...
If I'd only have more time I gladly would dig into this topic. :(
BR

Michael