Author Topic: Run script from API  (Read 1195 times)

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Run script from API
« on: May 01, 2014, 08:41:11 pm »
Hi guys,

in order to make my Addin's more generic and adjustable by the model and project file i tried to source out some functions from the hardcoded addin into some scripts.

However, i need to call the script from the addin and get back the return value.

Is there any way or maybe a functionality which i don't know, to do that?

Best regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Nizam Mohamed

  • EA User
  • **
  • Posts: 190
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #1 on: May 01, 2014, 09:55:16 pm »
AFAIK it wasn't possible to invoke EA's scripting engine from an add-in,

But you can use something like ScriptControl (.NET) to run the script. As you may already know, you can retrieve the Script information from t_script table.

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #2 on: May 01, 2014, 09:57:10 pm »
Yes, but with the search query i can only get the content of the script, but not execute it.

I need to execute it and get its return value.

Regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Nizam Mohamed

  • EA User
  • **
  • Posts: 190
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #3 on: May 01, 2014, 10:55:37 pm »
Yes, but for that we've to use ScriptControl (or something like that) to execute the script.

(As 'tis not possible in EA to run the script)

ScriptControl help
http://social.msdn.microsoft.com/Forums/vstudio/en-US/179a5554-84c5-4f77-b358-76b73b563b82/how-to-use-microsoft-script-control-in-vbnet
http://msdn.microsoft.com/en-us/magazine/cc302278.aspx

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #4 on: May 01, 2014, 11:17:46 pm »
Quite interessting.

So it is possible to retrieve the script by sql and save it as "script.vbs" and then load it from the executable.

However, I need to run it on the model to retrieve the data of the selected package. Do you have some ideas for? In the conditions outside of the EA-api I'm very new.

Do you have a little code snippet which could show for example:
How can I load a script, which is inside the model which just retrieves the name of the currently selected package?

Maybe i can get closer to my answer :-)

Thank you

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Nizam Mohamed

  • EA User
  • **
  • Posts: 190
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #5 on: May 02, 2014, 02:45:38 pm »
Hi Stefan.
Firstly, to retrieve the name of the currently selected package, have you considered using the API call? Repository.GetTreeSelectedPackage? This should do the trick.
But if your scenario is such that you have to execute a script for more logic.
The below sniplet should give a rough idea of how it could be achieved.
Code: [Select]
Dim SC As New ScriptControl
SC.AddObject("Repository", Repository, True) ' Adding Repository Object to be used in the Script
SC.AddCode(SourceScript)
' Script retrieved from Repository.SQLQuery("SELECT * FROM t_script")
result = SC.Run(FuncName)
'FuncName is the method in the script that you would like to call

the corresponding script will be something like
Code: [Select]
Function FuncName()
      ' Repository object was set before invoking the script!
      ' Get the currently selected element in the tree to work on
      dim thePackage
      set thePackage = Repository.GetTreeSelectedPackage()
      
      if not thePackage is nothing then
            FuncName= thePackage.Name;
      end if
End Function

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #6 on: May 02, 2014, 07:21:49 pm »
Dear Nizam,

thanks for your script.

The above montioned code works great, except the sc.RUN-command needs additional objects.

For completeness:

Code: [Select]
               SC.AddCode(Repo.SQLquery(""));
                Object[] runobject = { };
                    result = SC.Run("FuncName", runobject);
                }

Best regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

MathiasK

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #7 on: May 12, 2014, 11:45:17 pm »
Hi, may i add a question to this topic:

Is the SQLQuery method the key for efficient navigation between the different levels and components of a model ? (started by a Addin).

Greetings Mathias

qwerty

  • EA Guru
  • *****
  • Posts: 8952
  • Karma: +136/-123
  • I'm no guru at all
    • View Profile
Re: Run script from API
« Reply #8 on: May 13, 2014, 03:02:12 am »
Definitely it is the lowest level you can reach. So it is the fastest but the most likely to run into problems on EA version change (though that has happens almost never since V4 or so)

q.

Andreas Morgenstern

  • EA User
  • **
  • Posts: 25
  • Karma: +1/-0
    • View Profile
Re: Run script from API
« Reply #9 on: July 11, 2014, 10:27:08 pm »
Hi,

I've just started working with EA and find this discussion really interesting, since this is exactly what I was looking for. But I'm a Little bit confused. Is the code from :
Quote
Dear Nizam,

thanks for your script.

The above montioned code works great, except the sc.RUN-command needs additional objects.

For completeness:

Code: [Select]
               SC.AddCode(Repo.SQLquery(""));
                Object[] runobject = { };
                    result = SC.Run("FuncName", runobject);
                }

Best regards

Stefan

used in your addin, i.e. is it a VBA addin ?
If yes, I'm trying to achieve the same in an addin implemented in C#. Does this also work?

Andreas
 

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #10 on: July 13, 2014, 03:21:20 am »
Hi Andreas,

I used it in c#

Code: [Select]
               MSScriptControl.ScriptControl script = new ScriptControl();
                script.Language = "VBScript";
                script.AddObject("Repository", Repository);
            script.AddCode(SBO_enartalis.ENARTalis_main.delxml(Repository.SQLQuery("SELECT script from t_script WHERE Notes LIKE 'script_to_run'")));
                }
                Object[] runobject = {oguid, objecttype.ToString() };
                script.Run("Function_to_run", runobject);  

This works very well.

Currently I'm planning to create a little abstract for the call-for-paper for the EAUG in Munich this oktober. There are a few do's and dont's when working with scripts and the API.

Best regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Andreas Morgenstern

  • EA User
  • **
  • Posts: 25
  • Karma: +1/-0
    • View Profile
Re: Run script from API
« Reply #11 on: July 15, 2014, 06:08:58 pm »
Hi,

thanks for your help. Now it is running. May I add another question?
Have you ever tried to Exchange data between the addin and the script (in both directions)?

Andreas

Stefan Bolleininger

  • EA User
  • **
  • Posts: 309
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #12 on: July 15, 2014, 06:35:34 pm »
Hi Andreas,

Yes, I Do :-) I'm using the API-script-combination for handling extended workflows with broadcast-events and the configuration within the project model

e.g.

Code in API
Code: [Select]
               MSScriptControl.ScriptControl script = new ScriptControl();
                script.Language = "VBScript";
                script.AddObject("Repository", Repo);
                   script.AddCode(SBO_enartalis.ENARTalis_main.delxml(Repo.SQLQuery("SELECT script from t_script WHERE Notes LIKE '*scripttorun*'")));
                
                Object[] runobject = { };
                    output = script.Run("funvalue", runobject);
Output is the Return value from Script "scripttorun" and function "funvalue"

Code in Script:
Code: [Select]
Function funvalue()
funvalue = "Myreturnvalue"                                    
end Function

Best regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: Run script from API
« Reply #13 on: July 16, 2014, 10:55:21 pm »
This may be off-topic:

If I run a script from an addin, I would like to import it programmatically  to each model.

How should the script be imported?
As xmi (by GetRepository.CustomCommand("Repository", "ImportRefData", XML); command)
or by another way?

Thanks.

qwerty

  • EA Guru
  • *****
  • Posts: 8952
  • Karma: +136/-123
  • I'm no guru at all
    • View Profile
Re: Run script from API
« Reply #14 on: July 17, 2014, 08:17:15 am »
You could fiddle around with the t_scripts table. But the way you suggested is likely the cleaner one.

q.
« Last Edit: July 17, 2014, 08:17:30 am by qwerty »