Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: alexM on May 07, 2015, 11:47:38 pm

Title: Run Script by AddIn
Post by: alexM on May 07, 2015, 11:47:38 pm
Hi, i'm developing an AddIn to implement a particular "set of rules" in my requirement diagram.
The Addin is "event sensitive" in particular this one performs its instructions when a new connector is instantiated.
I had already developed my "set of rules" using Scripting so the question is: "is it possible to run a script by an AddIn? If yes, where can i find any reference for the syntax" ?
Title: Re: Run Script by AddIn
Post by: qwerty on May 08, 2015, 12:28:48 am
Check Geert Bellekens' add-in. It can do what you want.

q.
Title: Re: Run Script by AddIn
Post by: Geert Bellekens on May 08, 2015, 01:03:15 am
Hi Alex,

Indeed, as qwerty indicated, this is exactly the use case why EA-Matic (http://bellekens.com/ea-matic/) was developed. With EA-Matic (http://bellekens.com/ea-matic/) there's no more need to develop and deploy your own addin. You just need to select the EA events you want to use and then call your validation scripts from this event.

You can evaluate the add-in for free for a month.

Geert
Title: Re: Run Script by AddIn
Post by: alexM on May 08, 2015, 10:38:01 pm
It seems useful to addin debugging and testing too, Thanks a lot!!!  :)
Title: Re: Run Script by AddIn
Post by: z.kadlec on June 14, 2015, 01:16:26 am
OK,  but still, is it possible to call EA script  from Add-in?

thx

Zden[ch283]k Kadlec
Title: Re: Run Script by AddIn
Post by: qwerty on June 14, 2015, 07:51:05 pm
https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EASpecific/Script.cs

q.
Title: Re: Run Script by AddIn
Post by: z.kadlec on June 16, 2015, 03:20:04 am
thx. I cannot use the EAWrapper because my addin is in use since 2006 :-) :-) But the way of load and run is very helpful...

 :)
Title: Re: Run Script by AddIn
Post by: Andreas Morgenstern on July 07, 2015, 06:19:23 pm
Hi,

last year I had exactly the same problem. You can use MSScript Control. You can search the forum for the problem.
I also included some sample code I used for that purpose.

Andreas


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSScriptControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using MSScriptControl;
namespace AddInScriptInterop
{
    class ScriptInterop
    {
        public string[] ScriptNames { get; private set; }
        public string[] ScriptLanguage { get; private set; }
        private EA.Repository Repository;
        public void startControl(EA.Repository rep)
        {
            this.Repository = rep;
        }
        /// <summary>
        /// Extracts the Script names from the Repository
        /// </summary>
        /// <returns></returns>
        public string[] getScriptNames()
        {
            String xmlString = (Repository.SQLQuery("SELECT Notes from t_script WHERE Notes LIKE '*Script Name*'"));
            return
                ((from sc in
                 (
                     // we take notes and have to parse the value to get the script node!
                     from notes in (XDocument.Parse(xmlString).Descendants("Notes"))
                     select (XDocument.Parse(notes.Value))
                 )
             select (sc.Element("Script").Attribute("Name").Value)).ToArray<string>());
            
        }
        public string[] getScriptLanguages() {
            String xmlString = (Repository.SQLQuery("SELECT Notes from t_script WHERE Notes LIKE '*Script Name*'"));
        return
                (from sc in
                     (
                         // we take notes and have to parse the value to get the script node!
                        from notes in (XDocument.Parse(xmlString).Descendants("Notes"))
                        select (XDocument.Parse(notes.Value))
                    )
                 select (sc.Element("Script").Attribute("Language").Value)).ToArray<string>();
        }

        /// <summary>
        /// Execute a script. Data Exchange can either be done by passing parameters or by handing over global objects
        /// </summary>
        /// <param name="script">script to be executed</param>
        /// <param name="scriptlanguage">script language</param>
        /// <param name="runobject">paremters passed to the script</param>
        /// <param name="addObject">global objects added to the script control engine</param>
        /// <returns></returns>
        private int ExecuteScript(string script, string scriptlanguage, Object[] runobject, Object[] addObject)
        {
            MSScriptControl.ScriptControlClass control = new ScriptControlClass();
            control.Language = scriptlanguage;
            control.AllowUI = true;
            // Pass global objects
            control.AddObject("Repository", Repository);
            for (int i = 0; i < addObject.Length; i++)
            {
                string name = "AddObject" + i;
                control.AddObject(name, addObject);
            }
            int ret = -1;
            try
            {
                control.AddCode(script);
                ret = (int)control.Run("main", runobject);
            }
            catch
            {
                MessageBox.Show(control.Error.Description + " (line " + control.Error.Line + ")");
            }
            return ret;

        }

    }
}