Example #1
0
        /// <summary>
        /// Handling the event of the generate button.
        /// It generates a new TwinCat configuration
        /// </summary>
        private void _BgeneratePLC_Click(object sender, EventArgs e)
        {
            //this.Open();
            //Debug.Assert(this._factory == null);
            try
            {
                OrderInfo order  = this._orders[this._fbNumber];                           // selects just the first item in the order.xml. TODO: connection with the AML SUC.
                Script    script = ScriptLoader.GetScript(order.ConfigurationInfo.Script); // get the script under AvailableConfiguration of the order item previously selected

                if (script == null)
                {
                    throw new ApplicationException(string.Format("Script '{0}' not found. Cannot start execution!", order.ConfigurationInfo.Script));
                }

                _runningScript = script;          // setting the running script with the one previously selected

                VsFactory fact = new VsFactory(); // VS factory to create the DTE Object and determine the VS version to integrate with TC

                if (_runningScript is ScriptEarlyBound)
                {
                    this._factory = new EarlyBoundFactory(fact);
                }
                else if (_runningScript is ScriptLateBound)
                {
                    this._factory = new LateBoundFactory(fact);
                }

                if (this._factory == null)
                {
                    throw new ApplicationException("Generator not found!");
                }

                OrderScriptContext context = new OrderScriptContext(this._factory, order);        // we need to set the context of the script

                _worker = new ScriptBackgroundWorker(/*this._factory,*/ _runningScript, context); // worker for asynchronous script execution

                // Showing TwinCat UI and keeping it open after the creation of the code
                _factory.IsIdeVisible     = true;
                _factory.IsIdeUserControl = true;
                _factory.SuppressUI       = true;

                // Script execution: In Workerthread.cs it does OnDoWork() which has the main functions: Initialization, Execution and Cleanup
                _worker.BeginScriptExecution();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }