Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Stefan Bolleininger 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
-
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.
-
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
-
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
-
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
-
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.
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
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
-
Dear Nizam,
thanks for your script.
The above montioned code works great, except the sc.RUN-command needs additional objects.
For completeness:
SC.AddCode(Repo.SQLquery(""));
Object[] runobject = { };
result = SC.Run("FuncName", runobject);
}
Best regards
Stefan
-
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
-
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.
-
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 :
Dear Nizam,
thanks for your script.
The above montioned code works great, except the sc.RUN-command needs additional objects.
For completeness:
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
-
Hi Andreas,
I used it in c#
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
-
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
-
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
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:
Function funvalue()
funvalue = "Myreturnvalue"
end Function
Best regards
Stefan
-
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.
-
You could fiddle around with the t_scripts table. But the way you suggested is likely the cleaner one.
q.