Exemple #1
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);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }