示例#1
0
        private static int RequestExecuteScript(ScriptData scriptData, ScriptRuntimeConfigs scriptRuntimeCfg, ScriptExecutorConfigs scriptExecConfigs)
        {
            if (extExecEventHandler != null)
            {
                extExecEventHandler.ScriptData           = scriptData;
                extExecEventHandler.ScriptRuntimeConfigs = scriptRuntimeCfg;

                // request command exec now
                extExecEvent.Raise();

                // wait until the script is executed
                if (scriptExecConfigs.WaitForResult)
                {
                    while (extExecEvent.IsPending)
                    {
                        ;
                    }
                    return(extExecEventHandler.Result);
                }
                // otherwise
                return(ScriptExecutorResultCodes.DelayedExecutionRequested);
            }

            return(ScriptExecutorResultCodes.FailedDelayedExecutionRequest);
        }
示例#2
0
        public ScriptRuntime(ScriptData scriptData, ScriptRuntimeConfigs scriptRuntimeCfg)
        {
            // setup a new id and timestamp
            ExecId        = CommonUtils.NewShortUUID();
            ExecTimestamp = Telemetry.GetTelemetryTimeStamp();

            // set data
            ScriptData           = scriptData;
            ScriptRuntimeConfigs = scriptRuntimeCfg;

            //env
            // get the state of variables before command execution; the command could potentially change the values
            EnvDict = new EnvDictionary();

            // apply application override hooks if given
            if (ScriptRuntimeConfigs.ControlledApp != null)
            {
                ControlledApp = ScriptRuntimeConfigs.ControlledApp;
            }
            if (ScriptRuntimeConfigs.App != null)
            {
                App = ScriptRuntimeConfigs.App;
            }
            if (ScriptRuntimeConfigs.UIControlledApp != null)
            {
                UIControlledApp = ScriptRuntimeConfigs.UIControlledApp;
            }
            if (ScriptRuntimeConfigs.UIApp != null)
            {
                UIApp = ScriptRuntimeConfigs.UIApp;
            }

            // determine event sender type
            if (ScriptRuntimeConfigs.EventSender != null)
            {
                // detemine sender type
                if (ScriptRuntimeConfigs.EventSender.GetType() == typeof(UIControlledApplication))
                {
                    UIControlledApp = (UIControlledApplication)ScriptRuntimeConfigs.EventSender;
                }
                else if (ScriptRuntimeConfigs.EventSender.GetType() == typeof(UIApplication))
                {
                    UIApp = (UIApplication)ScriptRuntimeConfigs.EventSender;
                }
                else if (ScriptRuntimeConfigs.EventSender.GetType() == typeof(ControlledApplication))
                {
                    ControlledApp = (ControlledApplication)ScriptRuntimeConfigs.EventSender;
                }
                else if (ScriptRuntimeConfigs.EventSender.GetType() == typeof(Application))
                {
                    App = (Application)ScriptRuntimeConfigs.EventSender;
                }
            }

            // prepare results
            ExecutionResult = ScriptExecutorResultCodes.Succeeded;
            TraceMessage    = string.Empty;
        }
示例#3
0
        public ScriptCommand(
            string scriptSource,
            string configScriptSource,
            string searchPaths,
            string arguments,
            string helpSource,
            string tooltip,
            string cmdName,
            string cmdBundle,
            string cmdExtension,
            string cmdUniqueName,
            string cmdControlId,
            string cmdContext,
            string engineCfgs)
        {
            ScriptData = new ScriptData {
                ScriptPath       = scriptSource,
                ConfigScriptPath = configScriptSource,
                CommandUniqueId  = cmdUniqueName,
                CommandControlId = cmdControlId,
                CommandName      = cmdName,
                CommandBundle    = cmdBundle,
                CommandExtension = cmdExtension,
                CommandContext   = cmdContext,
                HelpSource       = helpSource,
                Tooltip          = tooltip,
            };

            // build search paths
            List <string> searchPathList = new List <string>();

            if (searchPaths != null && searchPaths != string.Empty)
            {
                searchPathList = new List <string>(searchPaths.Split(Path.PathSeparator));
            }

            // build arguments
            List <string> argumentList = new List <string>();

            if (arguments != null && arguments != string.Empty)
            {
                argumentList = new List <string>(arguments.Split(Path.PathSeparator));
            }

            ScriptRuntimeConfigs = new ScriptRuntimeConfigs {
                SearchPaths   = searchPathList,
                Arguments     = argumentList,
                EngineConfigs = engineCfgs,
            };
        }
示例#4
0
        /// Run the script and print the output to a new output window.
        public static int ExecuteScript(ScriptData scriptData, ScriptRuntimeConfigs scriptRuntimeCfg, ScriptExecutorConfigs scriptExecConfigs = null)
        {
            // make sure there is base configs
            scriptExecConfigs = scriptExecConfigs == null ? new ScriptExecutorConfigs() : scriptExecConfigs;

            if (mainThreadId != 0)
            {
                if (Thread.CurrentThread.ManagedThreadId == mainThreadId)
                {
                    return(ExecuteScriptNow(scriptData, scriptRuntimeCfg, scriptExecConfigs));
                }
                else
                {
                    return(RequestExecuteScript(scriptData, scriptRuntimeCfg, scriptExecConfigs));
                }
            }

            return(ScriptExecutorResultCodes.ExecutorNotInitialized);
        }
示例#5
0
        private static int ExecuteScriptNow(ScriptData scriptData, ScriptRuntimeConfigs scriptRuntimeCfg, ScriptExecutorConfigs scriptExecConfigs)
        {
            // create runtime
            var runtime = new ScriptRuntime(scriptData, scriptRuntimeCfg);

            // determine which engine to use, and execute
            if (EnsureTargetScript(ref runtime))
            {
                switch (runtime.EngineType)
                {
                case ScriptEngineType.IronPython:
                    ExecuteManagedScript <IronPythonEngine>(ref runtime);
                    break;

                case ScriptEngineType.CPython:
                    ExecuteManagedScript <CPythonEngine>(ref runtime);
                    break;

                case ScriptEngineType.CSharp:
                    ExecuteManagedScript <CLREngine>(ref runtime);
                    break;

                case ScriptEngineType.Invoke:
                    ExecuteManagedScript <InvokableDLLEngine>(ref runtime);
                    break;

                case ScriptEngineType.VisualBasic:
                    ExecuteManagedScript <CLREngine>(ref runtime);
                    break;

                case ScriptEngineType.IronRuby:
                    ExecuteManagedScript <IronRubyEngine>(ref runtime);
                    break;

                case ScriptEngineType.DynamoBIM:
                    ExecuteManagedScript <DynamoBIMEngine>(ref runtime);
                    break;

                case ScriptEngineType.Grasshopper:
                    ExecuteManagedScript <GrasshoppertEngine>(ref runtime);
                    break;

                case ScriptEngineType.Content:
                    ExecuteManagedScript <ContentEngine>(ref runtime);
                    break;

                case ScriptEngineType.HyperLink:
                    ExecuteManagedScript <HyperlinkEngine>(ref runtime);
                    break;

                default:
                    // should not get here
                    throw new PyRevitException("Unknown engine type.");
                }
            }
            else
            {
                runtime.ExecutionResult = ScriptExecutorResultCodes.MissingTargetScript;
            }

            // Log results
            int result = runtime.ExecutionResult;

            if (scriptExecConfigs.SendTelemetry)
            {
                ScriptTelemetry.LogScriptTelemetryRecord(ref runtime);
            }

            // GC cleanups
            var re = runtime.ExecutionResult;

            runtime.Dispose();
            runtime = null;

            // return the result
            return(result);
        }