Inheritance: IExternalApplication
Esempio n. 1
0
        /// <summary>
        /// Overload this method to implement an external command within Revit.
        /// </summary>
        /// <returns>
        /// The result indicates if the execution fails, succeeds, or was canceled by user. If it does not
        /// succeed, Revit will undo any changes made by the external command.
        /// </returns>
        /// <param name="commandData">An ExternalCommandData object which contains reference to Application and View
        /// needed by external command.</param><param name="message">Error message can be returned by external command. This will be displayed only if the command status
        /// was "Failed".  There is a limit of 1023 characters for this message; strings longer than this will be truncated.</param><param name="elements">Element set indicating problem elements to display in the failure dialog.  This will be used
        /// only if the command status was "Failed".</param>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // FIXME: somehow fetch back message after script execution...
            var executor = new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, message, elements);

            string source;

            using (var reader = File.OpenText(_scriptSource))
            {
                source = reader.ReadToEnd();
            }

            var result = executor.ExecuteScript(source);

            message = executor.Message;
            switch (result)
            {
            case (int)Result.Succeeded:
                return(Result.Succeeded);

            case (int)Result.Cancelled:
                return(Result.Cancelled);

            case (int)Result.Failed:
                return(Result.Failed);

            default:
                return(Result.Succeeded);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Open a window to let the user enter python code.
        /// </summary>
        /// <returns></returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var messageCopy = message;
            var gui         = new IronPythonConsole();

            gui.consoleControl.WithConsoleHost((host) =>
            {
                // now that the console is created and initialized, the script scope should
                // be accessible...
                new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, messageCopy, elements)
                .SetupEnvironment(host.Engine, host.Console.ScriptScope);

                host.Console.ScriptScope.SetVariable("__window__", gui);

                // run the initscript
                var initScript = RevitPythonShellApplication.GetInitScript();
                if (initScript != null)
                {
                    var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
                    scriptSource.Execute(host.Console.ScriptScope);
                }
            });

            var dispatcher = Dispatcher.FromThread(Thread.CurrentThread);

            gui.consoleControl.WithConsoleHost((host) =>
            {
                host.Console.SetCommandDispatcher((command) =>
                {
                    if (command != null)
                    {
                        // Slightly involved form to enable keyboard interrupt to work.
                        var executing = true;
                        var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
                        while (executing)
                        {
                            if (operation.Status != DispatcherOperationStatus.Completed)
                            {
                                operation.Wait(TimeSpan.FromSeconds(1));
                            }
                            if (operation.Status == DispatcherOperationStatus.Completed)
                            {
                                executing = false;
                            }
                        }
                    }
                });
            });
            gui.ShowDialog();
            return(Result.Succeeded);
        }
        /// <summary>
        /// Open a window to let the user enter python code.
        /// </summary>
        /// <returns></returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var messageCopy = message;
            var gui         = new IronPythonConsole();

            gui.consoleControl.WithConsoleHost((host) =>
            {
                // now that the console is created and initialized, the script scope should
                // be accessible...
                new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, messageCopy, elements)
                .SetupEnvironment(host.Engine, host.Console.ScriptScope);

                host.Console.ScriptScope.SetVariable("__window__", gui);

                // run the initscript
                var initScript = RevitPythonShellApplication.GetInitScript();
                if (initScript != null)
                {
                    var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
                    scriptSource.Execute(host.Console.ScriptScope);
                }
            });
            var commandCompletedEvent = new AutoResetEvent(false);
            var externalEventHandler  = new IronPythonExternalEventDispatcher(gui, commandCompletedEvent);
            var externalEvent         = ExternalEvent.Create(externalEventHandler);

            gui.consoleControl.WithConsoleHost((host) =>
            {
                var oldDispatcher = host.Console.GetCommandDispatcher();
                host.Console.SetCommandDispatcher((command) =>
                {
                    //externalEventHandler.Enqueue(() => oldDispatcher(command));
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });

                host.Editor.SetCompletionDispatcher((command) =>
                {
                    externalEventHandler.Enqueue(command);
                    externalEvent.Raise();
                    commandCompletedEvent.WaitOne();
                });
            });
            gui.Topmost = true;
            gui.Title   = "RevitPythonShell (non-modal)";
            gui.Show();
            return(Result.Succeeded);
        }
Esempio n. 4
0
        public override int Execute(params string[] parameters)
        {
            //load the application
            if (!RevitPythonShellApplication.applicationLoaded)
            {
                RevitPythonShellApplication.OnLoaded();
            }

            var dialog = new ConfigureCommandsForm();

            dialog.ShowDialog();

            MessageBox.Show("Restart Navisworks to see changes to the commands in the Ribbon", "Configure NavisPythonShell", MessageBoxButtons.OK, MessageBoxIcon.Information);

            return(0);
        }
Esempio n. 5
0
        /// <summary>
        /// Overload this method to implement an external command within Revit.
        /// </summary>
        /// <returns>
        /// The result indicates if the execution fails, succeeds, or was canceled by user. If it does not
        /// succeed, Revit will undo any changes made by the external command.
        /// </returns>
        int Execute(ref string message, params string[] parameters)
        {
            // FIXME: somehow fetch back message after script execution...
            var executor = new ScriptExecutor(RevitPythonShellApplication.GetConfig());

            string source;

            using (var reader = File.OpenText(_scriptSource))
            {
                source = reader.ReadToEnd();
            }

            var result = executor.ExecuteScript(source, _scriptSource);

            message = executor.Message;

            return(result);
        }
        /// <summary>
        /// Read in the commands from the XML file and display them in the
        /// list.
        /// </summary>
        private void ConfigureCommandsForm_Load(object sender, EventArgs e)
        {
            _commands = RevitPythonShellApplication.GetCommands(
                RevitPythonShellApplication.GetSettings()).ToList();
            lstCommands.DataSource = _commands;

            _searchPaths = RevitPythonShellApplication.GetConfig().GetSearchPaths().ToList();
            lstSearchPaths.DataSource = _searchPaths;

            _variables = RevitPythonShellApplication.GetConfig().GetVariables().AsEnumerable().ToList();
            lstVariables.DataSource    = _variables;
            lstVariables.DisplayMember = "Key";

            string initScriptPath = RevitPythonShellApplication.GetInitScriptPath();

            txtInitScript.Text = initScriptPath;

            string startupScriptPath = RevitPythonShellApplication.GetStartupScriptPath();

            txtStartupScript.Text = startupScriptPath;
        }
Esempio n. 7
0
        /// <summary>
        /// Open a window to let the user enter python code.
        /// </summary>
        /// <returns></returns>
        public override int Execute(params string[] parameters)
        {
            //load the application
            if (!RevitPythonShellApplication.applicationLoaded)
            {
                RevitPythonShellApplication.OnLoaded();
            }

            var gui = new IronPythonConsole();

            gui.consoleControl.WithConsoleHost((host) =>
            {
                // now that the console is created and initialized, the script scope should
                // be accessible...
                new ScriptExecutor(RevitPythonShellApplication.GetConfig())
                .SetupEnvironment(host.Engine, host.Console.ScriptScope);

                host.Console.ScriptScope.SetVariable("__window__", gui);

                // run the initscript
                var initScript = RevitPythonShellApplication.GetInitScript();
                if (initScript != null)
                {
                    try
                    {
                        var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
                        scriptSource.Execute(host.Console.ScriptScope);
                    }
                    catch (Exception ex)
                    {
                        Forms.MessageBox.Show(ex.ToString(), "Something went horribly wrong!");
                    }
                }
            });

            var dispatcher = Dispatcher.FromThread(Thread.CurrentThread);

            gui.consoleControl.WithConsoleHost((host) =>
            {
                host.Console.SetCommandDispatcher((command) =>
                {
                    if (command != null)
                    {
                        // Slightly involved form to enable keyboard interrupt to work.
                        var executing = true;
                        var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
                        while (executing)
                        {
                            if (operation.Status != DispatcherOperationStatus.Completed)
                            {
                                operation.Wait(TimeSpan.FromSeconds(1));
                            }
                            if (operation.Status == DispatcherOperationStatus.Completed)
                            {
                                executing = false;
                            }
                        }
                    }
                });
                host.Editor.SetCompletionDispatcher((command) =>
                {
                    var executing = true;
                    var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
                    while (executing)
                    {
                        if (operation.Status != DispatcherOperationStatus.Completed)
                        {
                            operation.Wait(TimeSpan.FromSeconds(1));
                        }
                        if (operation.Status == DispatcherOperationStatus.Completed)
                        {
                            executing = false;
                        }
                    }
                });
            });
            gui.ShowDialog();
            return(0);
        }
 /// <summary>
 /// Save changes to RevitPythonShell.xml and close Dialog.
 /// </summary>
 private void btnCommandSave_Click(object sender, EventArgs e)
 {
     RevitPythonShellApplication.WriteSettings(_commands, _searchPaths, _variables, txtInitScript.Text, txtStartupScript.Text);
     Close();
 }