Ejemplo n.º 1
0
        public ShellWindow()
        {
            InitializeComponent();
            myapp = RevitRubyShellApplication.RevitRubyShell;

            this.Loaded += (s, e) =>
            {
                var defaultScripts = RevitRubyShellApplication.GetSettings().Root.Descendants("DefaultScript").ToArray();
                var lastCode       = myapp.LastCode;
                if (string.IsNullOrEmpty(lastCode))
                {
                    Code.Text = defaultScripts.Any() ? defaultScripts.First().Value.Replace("\n", "\r\n") : "";
                }
                else
                {
                    Code.Text = lastCode;
                }

                OutputBuffer = new TextBoxBuffer(_output);

                // Initialize IronRuby
                _rubyEngine  = myapp.RubyEngine;
                _rubyContext = (IronRuby.Runtime.RubyContext)Microsoft.Scripting.Hosting.Providers.HostingHelpers.GetLanguageContext(_rubyEngine);


                // redirect stdout to the output window
                _rubyContext.StandardOutput = OutputBuffer;
                KeyBindings();
            };

            this.Unloaded += (s, e) =>
            {
                myapp.LastCode = Code.Text;
            };
        }
Ejemplo n.º 2
0
        public Result OnStartup(UIControlledApplication application)
        {
            //Create panel
            var ribbonPanel = application.CreateRibbonPanel("Ruby scripting");
            var pushButton  = ribbonPanel.AddItem(
                new PushButtonData(
                    "RevitRubyShell",
                    "Open Shell",
                    typeof(RevitRubyShellApplication).Assembly.Location,
                    "RevitRubyShell.ShellCommand")) as PushButton;

            pushButton.LargeImage = GetImage("console-5.png");

            //Start ruby interpreter
            _rubyEngine = Ruby.CreateEngine();
            _scope      = _rubyEngine.CreateScope();

            // Warm up the Ruby engine by running some code on another thread:
            new SThread.Thread(
                () =>
            {
                var defaultScripts = GetSettings().Root.Descendants("OnLoad").ToArray();
                var script         = defaultScripts.Any() ? defaultScripts.First().Value.Replace("\n", "\r\n") : "";
                _rubyEngine.Execute(script, _scope);
            }).Start();

            RevitRubyShellApplication.RevitRubyShell = this;

            application.Idling += (sender, args) =>
            {
                var uiapp = sender as UIApplication;
                lock (this.Queue)
                {
                    if (this.Queue.Count <= 0)
                    {
                        return;
                    }

                    var task = this.Queue.Dequeue();

                    // execute the task!
                    try
                    {
                        task(uiapp);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error");
                    }
                }
            };

            var win = new ShellWindow();

            application.RegisterDockablePane(new DockablePaneId(DockGuid), "RevitRubyShell", win);

            return(Result.Succeeded);
        }