Author Topic: Run Script by AddIn  (Read 6153 times)

alexM

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Run Script by AddIn
« 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" ?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Run Script by AddIn
« Reply #1 on: May 08, 2015, 12:28:48 am »
Check Geert Bellekens' add-in. It can do what you want.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13241
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Run Script by AddIn
« Reply #2 on: May 08, 2015, 01:03:15 am »
Hi Alex,

Indeed, as qwerty indicated, this is exactly the use case why EA-Matic was developed. With 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

alexM

  • EA Novice
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: Run Script by AddIn
« Reply #3 on: May 08, 2015, 10:38:01 pm »
It seems useful to addin debugging and testing too, Thanks a lot!!!  :)

z.kadlec

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Run Script by AddIn
« Reply #4 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

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile

z.kadlec

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Run Script by AddIn
« Reply #6 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...

 :)

Andreas Morgenstern

  • EA User
  • **
  • Posts: 26
  • Karma: +1/-0
    • View Profile
Re: Run Script by AddIn
« Reply #7 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;

        }

    }
}