Author Topic: Is there any way to call javascript/vbscript created in Sparx from C#?  (Read 991 times)

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
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

Helmut Ortmann

  • EA User
  • **
  • Posts: 884
  • Karma: +37/-1
    • View Profile
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
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7752
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
The specific class to look at is Script.cs
This is used by 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

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
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
« Last Edit: March 30, 2017, 04:12:21 pm by monicashishodia »

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
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.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 5882
  • Karma: +71/-79
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
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
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
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
« Last Edit: March 30, 2017, 07:22:33 pm by monicashishodia »

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 5882
  • Karma: +71/-79
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Cool!   Thanks, Monica!

Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
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

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2436
  • Karma: +29/-2
    • View Profile
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
The Sparx Team
support@sparxsystems.com

monicashishodia

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Is there any way to call javascript/vbscript created in Sparx from C#?
« Reply #10 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
« Last Edit: March 31, 2017, 04:46:01 pm by monicashishodia »

Nizam

  • EA Novice
  • *
  • Posts: 13
  • Karma: +2/-0
  • Model Sharing - Simplified
    • View Profile
    • Professional Model Collaboration

Start sharing EA Models to wider teams now!!.
View EA Diagram Online or Take a Trial Try Now