Sparx Systems Forum

Discussion => Suggestions and Requests => Topic started by: monicashishodia on March 29, 2017, 07:52:20 pm

Title: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 29, 2017, 07:52:20 pm
Hello All

I have a current requirement where i am converting scripts written in EA to c# code. As an alternative, I am trying to execute a jacascript/vbscript,craeted in Eenterprise Architecht, from C# using EA's scripting console and using command supported by this console such as

i(nclude) name - executes the named script item; name is of the format GroupName.ScriptName (spaces are allowed
in names) ? - (without the !) lists commands


Is there any API exposed by sparx to call/access this scripting console so that i don't have to re-invent the wheel (script can be written in javascript/vbscript and i am not the original author of it). I came across some content where we can write c# code in Enterprise architect as well.
http://www.sparxsystems.com/enterprise_architect_user_guide/13.0/automation/cs_script_example.html

But as i said, scripts are going to be either in javascript or vbscript, so can i access scripting's console in my c# code (outside EA, accessing EA's interop) and call these javascript/vbscript in EA?

Is there any other way to achieve this? (calling script in EA from C#).

Any help is highly appreciated.

Regards,
Monica
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: Helmut Ortmann on March 29, 2017, 08:35:47 pm
Hi,

have a look in Geerts excellent open source software Navigator on GITHub.

There are examples. You'll find classes which very well encapsulate the task.

Regards,

Helmut
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: Geert Bellekens on March 29, 2017, 09:50:48 pm
The specific class to look at is Script.cs (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EASpecific/Script.cs)
This is used by EA-Matic (https://bellekens.com/ea-matic/) to execute functions defined in those scripts.

One of the things that I haven't implemented yet is removing all the as EA.xxx definitions after the dim statements. These are not standard and are rejected by the scripting engine when trying to evaluate a script.
So you can only use scripts that don't use the dim typing.

One day, whey I have lots of time (or when someone pays me  for it ;D) I'll implement that feature.

Geert
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 30, 2017, 03:49:44 pm
Thanks Helmut for pointing in right direction.

I found very useful content there.

And Geert....thanks again for helping us out.

I am trying another approach as well where i can call vbscript/javascript from command line using tools such as cscript and wscript. But in the script, i am not able to create repository object (i tried using createobject method).

Also if i include !INC Local Scripts.EAConstants-JScript (or VBScript), the code does not comply.

How shall i go about it? Executing scripts using command line?


Regards,
Monica
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 30, 2017, 05:41:47 pm
I figured it out!!!
Now able to run jscript from command line using cscript after modification in original script which was created in EA.

Thanks again for help.
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: Paolo F Cantoni on March 30, 2017, 05:49:05 pm
I figured it out!!!
Now able to run jscript from command line using cscript after modification in original script which was created in EA.

Thanks again for help.
You will be showing us what you did, won't you?  ;)

Paolo
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 30, 2017, 07:11:16 pm
HI Paolo

I have few scripts created in EA and objective was to run these outside EA and preferably from command line. So i exported these scripts in .js file and done few changes such as
1. create repository using below code
Repository= new ActiveXObject("EA.Repository");
2. I am passing packages, elements,diagrams in function and consuming them through local variables so while converting, i initialized these variables as null instead of EA.XXX(package/element/diagram) and find the respective element using GUID. for example

before (In EA)
function testPackage(package);
var testPackage as EA.Package;
testPackage =testPackage;

After (in jscript outside EA)

function testPackage(packageGUID);
var testPackage=null;
Repository.GetPackageByGUID(packageGUID);

3. Calling Repository.CloseFile() and Repository.Exit() to kill the EA instance so that after script execution there is no extra running EA instance (i found out 20+ instances running in task manager as i was testing the script and not releasing EA instance created through jscript)
4. Repository.Openfile2(eapfile,username,password).
As i am trying to access EAP file outside EA, i need to give additional information to access the file.
5. Replace few things in code accordingly such as instead of calling Session.Output to print something on console used WScript.Echo

Regards,
Monica
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: Paolo F Cantoni on March 31, 2017, 10:47:28 am
Cool!   Thanks, Monica!

Paolo
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 31, 2017, 03:07:26 pm
Hi Paolo

when i was trying to get help on my problem, browsing through content published by sparx. The user guide available on sparx website points to version 9.3 as below:

http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/automation/repository3.html

Is there any updated version which points to latest version (13)?

Regards,
Monica
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: KP on March 31, 2017, 03:17:48 pm
Hi Paolo

when i was trying to get help on my problem, browsing through content published by sparx. The user guide available on sparx website points to version 9.3 as below:

http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/automation/repository3.html

Is there any updated version which points to latest version (13)?

Regards,
Monica

Yes. http://www.sparxsystems.com/enterprise_architect_user_guide/13.0/automation/repository3.html (http://www.sparxsystems.com/enterprise_architect_user_guide/13.0/automation/repository3.html)
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: monicashishodia on March 31, 2017, 03:36:51 pm
Thanks KP!!

I have one more query.

Is there any way to initiate execution of jscript/vbscript from outside EA?
Scripts are available in Enterprise Architect but can I initiate the execution using EAP file as i can access repository through it.

Regards,
Monica
Title: Re: Is there any way to call javascript/vbscript created in Sparx from C#?
Post by: Nizam on November 25, 2017, 05:01:42 pm
Just came across this, FWIW .. refer to
http://www.sparxsystems.com/forums/smf/index.php/topic,5417.msg124889.html#msg124889