SketchUp Ruby Code Editor

This code editor plugin offers an easy-to-use and visually appealing way to create and modify Ruby scripts directly within SketchUp.

SketchUp Ruby Code Editor
SketchUp Ruby Code Editor

Description

editor-icon-512This code editor plugin offers an easy-to-use and visually appealing way to create and modify Ruby scripts directly within SketchUp. These scripts can then be used to create geometry, add functionality or add data within the SketchUp 3D modeling environment. The SketchUp Ruby API provides an extensive set of functions to automatize SketchUp in many ways or create scripted, computational geometry.

I created this editor mainly for two reasons: One is that when I write code, I permanently need to look up documentation. To make this easier, I added a bunch of reference webpages to a browser in a tab. Also, a dropdown above the editor contains some common code snippets that can be inserted at the current cursor position. Version 3.0 also introduces a code-completion feature that you can invoke at any point by clicking CTRL-SHIFT. It contains SketchUp’s classes and methods.

By the way: Check out my book, “Architectural Design with SketchUp: 3D Modeling, Extensions, BIM, Rendering, Making, and Scripting.” Chapter 6 provides an overview of Ruby scripting in SketchUp and has many tutorials for things to do with this editor.

The other reason I wrote this as a plugin that works right inside of SketchUp is that I wanted it to give easy access to the Ruby coding environment and behave a bit more like a real code editor. To accomplish this, I added a bit of jQuery magic to make it look good and have some additional features. I also added the excellent CodeMirror 3.1 syntax highlighting engine. This provides the editor with multi-language code highlighting, correct TAB control, bracket matching and many more features. Just load the plugin and give it a try to see for yourself!

The following video gives a brief overview of the current version:

Features

  • Execute multiline Ruby code directly in SketchUp
  • Open and save files of any size
  • Fast opening of large files
  • Nice and clean user interface
  • Tab key works as expected in editor (inserts spaces or tabs)
  • Indenting/dedenting of code blocks
  • Dropdown with code snippets inserts snippets at cursor
  • Code-completion for SU classes and methods (click CTRL-SPACE to activate)
  • On save, a backup file can be created
  • Text size dropdown selects editor font size
  • Line numbering in editor
  • Matching brackets highlighting
  • Undo in editor
  • Undo (of code execution) button next to run button
  • Single undo for entire code or multiple undo steps
  • Results window keeps track of history
  • Reliable editor dialog resizing
  • Tab and accesskey navigation
  • Reference web browser for common websites
  • Browser zoom (a little buggy!)
  • Style changer for user interface
  • Buttons to explore selection object properties and object attributes
  • Button for direct access to the Ruby console
  • Clean printing of code
  • Version update checking

Usage

Select Window > Ruby Code Editor or open the Ruby Code Editor toolbar.

Downloads

You can now find this plugin in Trimble’s Extension Warehouse. For an easy install (and update notifications), browse to it from inside SketchUp (2013 or newer) and install it directly.

Alternative Downloads

Download “SketchUp Ruby Code Editor (RBZ)” as_rubyeditor_3-2.rbz – Downloaded 7148 times – 413 KB

Download “SketchUp Ruby Code Editor (ZIP)” as_rubyeditor_3-2.zip – Downloaded 9241 times – 413 KB

To install these files, follow the SketchUp folks’ instructions. You can also find this plugin on GitHub.

Screenshots

Main editor window
Main editor window
Browser tab
Browser tab
Win7-style split-view of editor and SU (light UI)
Win7-style split-view of editor and SU (light UI)
Options page (dark UI)
Options page (dark UI)

Next Steps

Issues

  • Page zoom does not work well in reference browser tab – just use this as a backup if the font is unreadable
  • The editor doesn’t like to work when other code editors are running in SketchUp – Just restart SketchUp if there are problems

Revision History

  • Version 3.2 (4/25/2013):
    – Reorganized files and folders
  • Version 3.1 (3/6/2013)
    – Fixed default file bug
    – Updated jQuery cookie plugin
    – Fixed Tab problem
  • Version 3.0 (3/4/2013)
    – Got rid of settings file
    – Results feedback now wrapped in Paragraph
    – Improved feedback scrolling
    – Some CSS changes, separated CSS by theme
    – Two editor themes now: ambiance and eclipse
    – Updated Codemirror to 3.1
    – Updated jQuery
    – Updated jQuery UI
    – Improved editor stability, smoother scrolling
    – Set IE version number to 9 (allows for more features and better display)
    – Fixed SU class highlighting and added SU method highlighting
    – Added syntax highlighting for SketchUp classes and methods
    – Better editor closing handling
    – Changed some options for newer Codemirror
    – Fixed theme color options
    – Better error display
    – Fixed Mac rendering of results window
    – Fixed Mac default folder issue
  • Version 2.1 (2/26/2013)
    – Uses most recently used directory for file loading and saving
    – Fixed the outdated URLs in the browser
    – Update checking now uses www.sketchupplugins.com
  • Version 2.0 (4/12/2011)
    – Switched editor to use CodeMirror – sooo much better!
    – Added code coloring for several languages (Ruby, HTML, CSS, JavaScript, XML, C/Java)
    – Can handle large files easily
    – Added bracket matching
    – Added coloring changer
    – Disabling run button if code is not Ruby
    – Added some more options
    – Added option to disable single undo
    – Added color coding for SU classes
    – Fixed problem with loading/saving slashes in files
    – Updated jQuery and jQuery UI
    – Results window is now continuous and scrolls
    – Added some more code snippets to dropdown
    – Added toolbar item
    – Moved menu item to “Windows” menu
    – Added settings file for modifications
    – Added code printing
  • Version 1.2 (4/13/2010)
    – Updated jQuery UI to v. 1.8
    – Added object explorer button to show current selection details
    – Added attribute explorer button for selection attributes
    – Added button to show Ruby console
    – Modified code a bit
    – Changed buttons to images for space reasons
    – Fixed keyboard access (Alt+…)
    – Added pages to browser: Edges to Rubies, Ruby core and Google search
    – Added basic update checking
    – Enabled browser buttons again – use at your own risk…
    – Waiting icon shown while code executes
  • Version 1.1.2 (3/8/2010)
    – Removed nav buttons also for IE because they disabled code execution after a while
    – Renamed some code elements
    – Added wrapper module
  • Version 1.1.1 (2/25/2010):
    – Safari-related fixes (cookies, text insert, removed back button)
  • Version 1.1 (2/22/2010)
    – Changed webdialog internal name for compatibility
    – Better looking dropdowns under Windows
    – Fixed save filename preselect problem
    – Better code execution and error catching (thanks to Dan Rathbun)
    – Faster file loading for large files
    – Added cookie-based saving of preferences (expires after 365 days)
    – Added changeable user interface
    – Modified some references
  • Version 1.0 (2/3/2010)
    – First release

Credits

This editor was originally an extension of Jim Folz’s Web Console plugin.

In addition to Jim’s original code and whatever I added and modified, this editor uses the jQuery and jQuery UI libraries. Syntax highlighting is provided by the Codemirror 3.1 extension. I am also grateful for many comments and suggestions from Dan Rathbun and others at SCF.

  • Sam Morris

    Hi Alex,
    Sorry to trouble you – do you have any idea how I can solve this issue with RCE not opening. I have’t tried to use it for a couple of days but I could really do with getting it up and running. I know you may not be able to help but any advise you can offer would be greatly appreciated. No error messages when I run it, just a dialog at the bottom left of the screen that says ‘Select objects. Shift to extend select. Drag mouse to select multiple.” Is there any way I can get an error report to show up or any way to see why its not loading / running. Many thanks
    Sam

    • No worries. Have you tried opening the Ruby Console before you start the RCE? When you do that, do any error messages appear? Also, I am wondering if this has to do with Internet Explorer security settings (the dialog is based on that (unless you are on a mac).

      • Okay, let’s try this (and be careful): Open regedit.exe and browse to the key HKEY_CURRENT_USERSOFTWARESketchUpSketchUp 2016as_RubyCodeEditor. Then just right-click on “as_RubyCodeEditor” and delete the key and whatever is in it. I am guessing something in there got messed up. When you fix this, be careful not to delete anything else. You can severely screw up your machine in here.

        • Sam Morris

          Thanks. Before I hit delete please can you take a look at the attached image and confirm Im deleting the right thing…
          Cheers
          Sam https://uploads.disquscdn.com/images/91e53f06cb232d24aec5be8835fbf3a8053ca36bc08656466f83ba86c2253a3c.jpg

          • Sam Morris

            Meant to say, if I understand you correctly Im to delete the folder in the top left of the screen grab called as_RubyCodeEditor… is that right.

          • Seeing this, you could actually just double-click the “last_file” entry and delete its contents. I am guessing that there is some character in there that messes things up.

          • Sam Morris

            You sir are a legend! Sorted by the looks of it – thanks SOOO much. If I run into any errors I’ll let you know but it all appears to be working correctly now.
            Thanks a million!
            Sam

          • Sam Morris

            I did what you suggested by the way and just deleted the “last_file” entry – just in case anyone else has the same problem.
            Thanks again
            Sam

          • Great to hear it worked!

    • Sam Morris

      thanks Alex – should have thought to open Ruby Console – duh. I ran RCE and got this…

      Error: #
      C:/Users/sam.morris/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `split’
      C:/Users/sam.morris/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `initialize’
      C:/Users/sam.morris/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:522:in `new’
      C:/Users/sam.morris/AppData/Roaming/SketchUp/SketchUp 2016/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:522:in `block in ‘
      SketchUp:1:in `call’

      Its all dutch to me but if you can shed any light that would be amazing!
      Cheers
      Sam

      • Sam Morris

        I am on a PC – what do I need to check in my IE settings?

  • Sam Morris

    Hi Alex,
    I have a problem with Ruby Code Editor. I’ve used this great plugin extensively but had a crash earlier today and now can’t get it to load. I have tried uninstalling and reinstalling but to no avail. Do you have any clue as to why it wont work. It shows up as available in the window dropdown menu but when I click on it nothing happens. I’ve deleted the folder out of my plugins folder, restarted and installed from scratch but still no luck. If there is any further info I can provide to help diagnose this then please let me know.
    Many thanks
    Sam

    • Hi Sam, not sure, but this may have to do with the loading policy in SketchUp. I wonder if after a crash, SketchUp somehow blocks it. Under the extensions policy tab of the preferences, switch to unrestricted and see if that helps.

      • Sam Morris

        Thanks for the prompt response. I just checked and its already set to Unrestricted. Tried switching to Approve Unidentified, restarted and tested – no joy. Then set it back to Unrestricted and reopened but still no luck.

        • Sam Morris

          Everything shows up (menu item in Window dropdown, extension in extensions panel and icon on toolbar) but nothing happens when I go to run it.

  • Eugenio Collis

    Great analysis ! Incidentally , if someone is searching for a a form , We encountered a fillable form here https://goo.gl/KMXWaQ.

  • Hugh Myers

    I’ve larger displays and when I click on full window box in upper right corner RCE does not expand fully? Also I’ve noticed but not been able to determine the exact pattern Ctrl-A Ctrl-V fails on first attempt? Bracket and paren highlighting should only happen on the outside of the character—otherwise it is extremely difficult to see the cursor position. Lastly Could you steal color schemes from Sublime…I have trouble reading the editor with the current scheme. Aren’t USERS great!?! Seriously this should be folded into SU (with recompense and credit) as it is indispensable!!! A very big thank you Alex…

    • Hi Hugh,
      Thanks for the feedback. Very happy to hear you like the editor.
      It is indeed in need of an update, which I will do this fall. That should take care of any quirks in the editor.

  • Petr(Czech Republic)

    thank you Alex, Ruby Code Editor it´s great

  • Pedro

    Hello
    Need code to get the attributes to na xml file.
    Do you have something?
    Beste regards
    Pedro

    • You just need to write the attributes to a text file, name it something.xml and format the file as appropriate. I think there’s an XML parser in Ruby that you could use to write it but even a hand-coded export will work. Look on my site for my PHP to KML example as a guide.

  • mariocha

    Great tool. Very useful. Thanks a lot.
    One small glitch, It prevents SU to quit normally (beachball). Close the editor before quitting SU. Can this be prevented ?

    • Thanks for letting me know. I’ll make a note to test this for the next version.

  • Nejc Vidmar

    Dear prof. Alexander Schreyer,

    thank you for providing us with a such a great plugin, which really adds a missing part for SketchUp developers.

    But I do have one problem – I’ve defined a method (eg. simple hello_world), which I can not call from Ruby console. Should I call it from some namespace, as it seems that SketchUp doesn’t recongnize it if I just run it from your editor and call it from Rubz Console later…?

    Thank you in advance and best regards,
    Jernej Vidmar

    • Glad you like it.

      The editor is namespaced as AS_Extensions::AS_RubyEditor::RubyEditor and all methods are added to its instance.

      • Nejc Vidmar

        That explains it, thank you very much!

        Just as a side note: for me it works when calling just via “editor = AS_RubyEditor::RubyEditor”, no AS_Extensions needed…

  • Sam Morris

    I just tried running the plugin with the standard ruby console open and got this

    Error: #
    C:/Users/Alice.Tapson/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `split’
    C:/Users/Alice.Tapson/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `initialize’
    C:/Users/Alice.Tapson/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:526:in `new’
    C:/Users/Alice.Tapson/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:526:in `block in ‘
    SketchUp:1:in `call’

    Any thoughts?

    • Looks like some invalid input (WRT UTF-8 encoding). I’ll investigate more, but for now:

      If you can, check regedit.exe and look for the following key. Is there anything fishy in there? I am wondering if there’s a file location that has some invalid characters in it.

      HKEY_CURRENT_USERSoftwareSketchUpSketchUp 2015as_RubyCodeEditor

  • Sam Morris

    Hi,
    Im a big fan of this plugin and have been trying to install it on a colleagues machine but am having lots of problems. Where is the best place to contact you to resolve issues. The install appears to work, the icon appears but when I press it or try to open it from the main menu nothing happens. It worked the first time I opened it on this machine but now get nothing. Have tried restarting SU and restarting the computer but still no joy…
    Can you help?
    Thanks
    Sam

  • Barry Milliken

    Your editor has a problem with the Sketchup length object.
    I can put these 2 lines in the ruby console with no problem, but they cannot be executed from your editor:

    input = “42”
    lengthx = input.to_l

    same for these 2 lines:

    input = 42
    lengthx = input.to_l

    I get:

    Error: #

    C:/Users/Barry/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:270:in `==’

    • Yes, that is a weird quirk. I still need to find out why that’s happening. It goes away, however, if you do something afterwards with the result as in “p lengthx”

  • Hisham Hiz

    sir would you like get me some good rendering settings for sketch up for getting inerior ,exterior as a good setup for me please so please kind on my email id is hishamkp0@gmail.com

  • Kamil Kamiński

    Hi, I just wonder how to do a quick save? ctrl+s doesn’t work, and alt+s opens a window to save a new file, so the only way of saving is manually overriding existing file which isn’t very convincing D: I just want a simple keyboard shortcut like in most editors.

    • Good point. It just has “Save As” but no simple save… I’ll put it down as a feature request.

      • Kamil Kamiński

        That’s great, thanks :)

        • Sam Morris

          Would it also be possible to have a ‘recently opened’ tab so you don’t have to go searching too far…?
          Thanks
          Sam

  • Barry Milliken

    My First Extension “2DXY SIteSurvey tools” is finally live on the Extension Warehouse.
    I believe I can give a free license to anyone who helped me along the way, as your editor did.
    So if drawing Land Surveys is useful to you, download a free trial and send me a message.

    I looked at the API and wrote my first line of Ruby in January. Two dozen ruby scripts, over 2000 lines of code, a 4 page illustrated HTML user guide and 2 videos later, I’m ready to work on my next 2 planned extensions.

    So check out my videos and thanks again.

    • Hi Barry, thanks for sharing. I am always curious what people do with my plugins. Great to hear your extension is in the warehouse. I’ll keep your offer in mind for when I have some surveying needs.

  • Barry Milliken

    I tried Notepad++ for use when I need search and replace, however it doesn’t show special characters correctly (even though it allows me to specify which encoding scheme to use.) Your editor shows special characters correctly. What encoding type do you use?

    (I use the delta symbol “

    • Good point. The editor uses iso-8859-1. That should maybe rather be UTF-8.

      • Barry Milliken

        But I like that you show special characters. Notepad++ using UTF-8 does not. PS: any thought on my invalid escape character message that I posted below: It happens even with files that have no special characters.

        • I might add the option to have the user select the encoding. That should let everyone use whatever they like.

  • Barry Milliken

    I often get this message in the ruby console when I open or save files in your editor:

    Error: #<SyntaxError: : Invalid escape character syntax

    It doesn’t seem to cause any problem, but I have no idea what it means???

  • Innocence

    Ruby Code Editor is a terrific program Should be shipped in research. A period of time using normal To this day.An error occurred after use to WebDialog.Ruby Code Editor can not start.
    Error codes for the next

    Error: #
    C:/Users/BIM-OFFICE03/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `split’
    C:/Users/BIM-OFFICE03/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:225:in `initialize’
    C:/Users/BIM-OFFICE03/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:526:in `new’
    C:/Users/BIM-OFFICE03/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:526:in `block in ‘
    SketchUp:1:in `call’

    Reinstall the plug-in can not solve the problem

    • Strange. I haven’t had problems starting it. Which language settings are you using on your machine? And do you get the error when SketchUp starts or when you open the editor?

      • Innocence

        An error occurred to open the editor.
        SketchUp Function was normal.
        An error occurred before I use function “WebDialog” & “html window.location = “skp:test@”” .
        Ruby Code Editor can not be used after the end of the test.

  • Barry Milliken

    I just discovered that (for my purposes), I should have been using

    mod = Sketchup.active_model # Open model
    ent = mod.active_entities # All entities in model
    in all of my methods. I suggest you change to or add active_entities to your default new method window.

    • entities is more general but active_entities does make sense if you want the context entities. I think in the next version I will put the default code somewhere where a user can edit it.

  • Barry Milliken

    Forget about my idea for multiline comments. I just discovered that ruby supports multiline comments:
    =begin
    Create five points in three dimensions
    Each point is the vertex of a star shape
    =end

  • Barry Milliken

    More about loading methods: As I said before, because of separate module namespaces, any methods automatically loaded at startup are not available within the ruby editor. So I load each in a separate ruby editor window and execute them first, before referring to them. I tried instead to write a script in another ruby editor window with just a series of load or require commands: but that for some reason does not work??? (All scripts in all windows are wrapped in my same module.)
    I get a series of “true”s, but maybe these methods (load and require) will not load if already loaded for another module(?) That seems ridiculous but maybe I’m missing something.

    • You can add your modules wherever you like. Check the code below which adds a module to Sketchup. You are right, of course that anything executed from within the editor is in the editor’s namespace. This is by design because SketchUp plugins should only work on “their own stuff” and not mess with others. That’s why, for example, you couldn’t get the code below past Trimble in a plugin.

      module Sketchup::AS_Test
      def self.alert
      UI.messagebox “Alert”
      end
      end
      Sketchup::AS_Test.alert

      • Barry Milliken

        I understand. All my scripts are wrapped in my module only. BUT if I have this script in my plugins subfolder MYapp called mymethod.rb
        ###########
        module MYapp
        def self.mymethod
        #here is code
        end
        end
        ##########

        Then I write this is the editor
        ###########
        module MYapp
        load ‘MYapp/mymethod.rb’
        result = self.mymethod
        end

        ###########

        Then I execute this last from the editor. I get:

        “undefined method `mymethod’ for AS_RubyEditor::RubyEditor::MYapp:Module”

        By itself the load returns true so I know the file is getting loaded.
        SInce I’m calling the load from within the editor shouldn’t it be available?

        • Try declaring your method as:

          module Object::MYapp

          This should get you top-level access.

  • Barry Milliken

    two other suggestions:
    1. undo/redo
    2. add/remove multiline comment : adds/removes a “#” as first char in selected lines.
    Typically my rb files contain a single long method definition followed by a series of ruby commands that tests the method (sets arbitrary values for method inputs, calls the method, then puts and/or tests return values). I like to keep the test script for future testing of changes, but it needs to be commented out for regular use of the method.

  • Barry Milliken

    Please add a note that all code executed from the editor is wrapped in a “rubyeditor” module. This means that any other methods loaded automatically from the plugins folder on startup are NOT available to be called within any code run from ruby editor. I struggled for 2 days before I realized why some methods could not be found.
    Now I know to load all such methods through the editor first.
    Otherwise the editor is a great tool.

  • Barry Milliken

    How does smart indenting work?
    How does Code completion work?
    WOuld be nice to have find and find/replace

    • Hi Barry,
      Smart indent keeps the last indent for the next line. And code completion is Ctrl+Space, but it is not context sensitive. F&R is on my todo list…

  • hydro

    hi sorry for dumb question but how to install with SU 8?

    • No problem. Download the RBZ file first. Then go to Sketchup’s preferences and on the Extensions tab, click on Install Extension…

      • hydro

        ok thx for the fast reply. ok i understand but i cant find a rbz file.i have only found rb files.

  • Adib

    Dear professor Schreyer,
    I am an Engineering Alum from UMass Amherst, and just happened to stumble upon your very elegant solution for the lack of built in efficient code editing in sketchup, while I was looking for solutions for a project that I am working on. I am designing a embedded system project and I thought why not simulate it real time in sketchup. There is an Arduino perl interface and so I plan on having real world buttons and knobs modify things in virtual reality and I can see how it is actually all going to work before I build it. Your code editor has been TREMENDOUS help! Thank you.

    • Great to hear from you and about your project. If you like, feel free to share how it worked out. I am curious!

  • DaveSemon

    First, thanks for the work on the editor. Second, I’m having a strange issue. I downloaded and installed from your website the latest version (3.2), but when I run the plugin and check the ABOUT tab, it says it is version 3.1. I’ve ensured there are no 3.1 files in the Plugins Directory and have reinstalled several times. I’m using SketchUp 2014 Make. The reason I wanted to upgrade was that I was hoping it would fix a problem I’m having — code does not execute correctly from the editor, even though it works fine if I load into SketchUp as a standard plugin (rb)file. any insight you can provide will be greatly appreciated. Thanks again.

    • Hi Dave, thanks for your feedback.

      I think the version number is only an oversight typo. It should be the 3.2 version (check the Preferences > Extensions) window. You can get the latest files from GitHub here:
      https://github.com/alexschreyer/Ruby-Code-Editor-for-SketchUp

      Regarding the execution problems: In the options, uncheck the “wrap in undo” option and see if that fixes it. If not, then it would be great if you could isolate the problem in a small snippet.

      • DaveSemon

        Thanks for the quick reply. SketchUp says it is 3.2, so I guess it is the ABOUT page that needs updating. Also, when I click on CHECK FOR LATEST VERSION under OPTIONS, it tells me it is the latest version, but says 3.1.

        The code is something I copied from the Trimble – Geometry Tutorial (Stairs). The code works as expected if I save it as a .rb file and include it in my Plugins folder, but not from the code editor. The editor tells me: Nil result (no result returned or run failed).

        I’m trying to teach myself how to write Ruby scripts for SketchUp, so I’m very new at this.

        Here is the code:

        # First we pull in the standard API hooks.
        require ‘sketchup.rb’

        # Show the Ruby Console at start-up so we can
        # see any programming errors we may make.
        SKETCHUP_CONSOLE.show

        # Add a menu item to launch our plugin.
        UI.menu(“Plugins”).add_item(“Draw stairs”) {

        UI.messagebox(“I’m about to draw stairs!”)

        # Call our new method.
        draw_stairs

        }

        def draw_stairs

        # Create some variables.
        stairs = 10
        rise = 8
        run = 12
        width = 100

        # Get handles to our model and the Entities collection it contains.
        model = Sketchup.active_model
        entities = model.entities

        # Loop across the same code several times
        for step in 1..stairs

        # Calculate our stair corners.
        x1 = 0
        x2 = width
        y1 = run * step
        y2 = run * (step + 1)
        z = rise * step

        # Create a series of “points”, each a 3-item array containing x, y, and z.
        pt1 = [x1, y1, z]
        pt2 = [x2, y1, z]
        pt3 = [x2, y2, z]
        pt4 = [x1, y2, z]

        # Call methods on the Entities collection to draw stuff.
        new_face = entities.add_face pt1, pt2, pt3, pt4

        end

        end
        From the editor, it adds the menu item, but does not draw anything.
        Thanks again.

        • Works fine for me in the editor. It’s just that the code creates a menu item on the plugins menu, which you need to click for anything to happen. Give that a try.

          • DaveSemon

            Thanks for the reply. You are absolutely correct. As I said, I’m learning. I “expected” all the code to execute, but it did exactly what I put in the code — added a menu item and waited for the user to select that item. Again, great editor and thank you for your help.

          • No problem. That’s how I learned it, too. You can always ask questions here, too:
            http://sketchupfordesign.com/groups/scripting/

  • Michel Radino

    Can I alter the auto-fill-in? I thought to find it in some xml file, but can’t find it. I want to make it only show what can follow the previous word before the dot ;-)

    And is there a way to comment/uncomment a selected block?

    • You can modify the highlighting behaviour here: http://goo.gl/3QX90a
      I am sure there are more elegant implementations of the highlighting. Feel free to suggest improvements.

      • Michel Radino

        Well, I think it needs to be sitting in a XML file, like those ruby.xml files used in Notepad++
        Because it would be a huge list!
        And because I’m very new to Ruby, I want be able to alter the ruby code yet ;-)

        Why is it that hard to find a complete ruby editor/debugger. I can’t imagine that all those existing plugins are written without one.

        • That’s just how the codemirror folks have implemented the syntax highlighting. Not much I can do about it.
          For a debugger, you might want to look at the “SketchUp Bridge” (just Google it or find it on SketchUcation. That lets you use any external IDE.

  • Michel Radino

    Can you please add some debugging, like step-by-step processing, breakpoints and showing values hovering variables? ;-)

    • That would indeed be a nice feature. I won’t go there, but if you like, take a look at the Github project (link is in the article) and see if you can add some of that.

  • Michel Radino

    an other interface-bug:
    Sometimes while selecting text, deleting or moving with the mouse, the selection-start get stick. So just moving the mouse keep selecting and moving text!

    • I have noticed that, too. I think it’s the interface plugin. I’ll be looking into updates in January.

      • Michel Radino

        About that selecting problem in the user interface, I found out when it happens:
        When one is selecting text with the mouse, but leaves the window still holding the mouse-button down. Then release the mouse and trying to select something again ;-)

        PS: Why do you use mod, ent and sel? Every ruby documentation is using model, entities and selection.

        • I have to look at codemirror and see if that has been fixed in an update.

          I just found the three-letter versions shorter than writing the full words over and over. You can change it here:

          http://goo.gl/4xtb1B

  • Michel Radino

    BUG?

    I’m new to Ruby, but I think there is a little interface problem in the Ruby Code Editor 3.1:

    When using a block the text colour stays green-grey after the block ends. As in:
    html = <<-BLOCK

    TEST

    link

    BLOCK

    dlg.set_html html #BUG: This line stays green instead of pink/orange!

  • JH

    I love the code editor!! I’ve been unable to print code from it though. Any thoughts?

    • Do you get the small window popping up that has the code inside it? And are you on Win or Mac?

      • JH

        Sorry, for not including any computer specifics… Windows 7 Pro SP1, SketchUp Pro 8.0.16846, (IE10 and Chrome29 if printing is via an HTML file…)
        No window pops up. Pressing Alt+P does cause the print button to highlight. Any chance limited administrator privileges could prevent printing?

        • Hmmm… works on my machine. One thought: This requires a popup to be shown. Could it be that you disabled popups (check your internet settings)?

          • JH

            Popups were blocked, but even allowing them and restarting IE10 no window opens. It works fine on my computer at home, however, this is on my computer at work with limited administrator rights. If the code editor is trying to write an html file to the ‘as_rubyeditor’ folder I expect it will be blocked. No worries though, I will just print from my home computer. I love the editor! It has made learning writing ruby much easier by being able to practice writing new code and getting immediate feedback.

          • The editor actually just creates a dynamic javascript popup and copies the text into it. Therefore, it doesn’t need to write anything. You might just need to print the code in a different program. Glad to hear the editor is of use to you.

  • bertilda

    oh my god great

  • TM

    Please change your tb.show code at the bottom so the UI toolbar can stay closed on start of Sketchup for MAC OS

    #start TM edit

    as_rce_tb.show

    state = as_rce_tb.get_last_state

    if(state==TB_VISIBLE)

    as_rce_tb.restore

    end

    #end TM edit

    • Thanks for the suggestion, TM. I’ll consider this for the next release.

  • Mo

    Downloaded version 3.2. About dialog still says 3.1. Thanks for your work.

  • Bas

    When I open Ruby Code Editor window such error occurs: “Error: #
    C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:221:in `initialize’
    C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:515:in `new’
    C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:515
    C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:221:in `call’
    C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/as_plugins/as_rubyeditor/as_rubyeditor.rb:221”

    • This shouldn’t happen with the 3.1 version (was a 3.0 bug). Please re-download and try again.

    • Did you have a chance to try the newer version?

  • Version 3.0 had a silly bug that’s fixed now. Please update to 3.1

  • I just updated the Ruby Code Editor to v. 3.0. It now features code-completion for SketchUp classes and methods, a newer and more responsive version of the code editing engine, and various bugfixes.

  • I just updated the Ruby Code Editor to v.2.1.

  • SteveP

    On the mac, version 2.0 does not properly find the API documentation. Goes to a 404 page.

  • Philip McClelland

    Thanks Alexander for sharing what looks (visually) really great.
    However a few simple things are required before I would use it (and this is a real tease compared to editing Ruby scripts in Notepad).
    I am learning Ruby (from a Delphi background). My code crashes a lot and I need to reboot Skutchup regularly.
    When SU closes – so does your editor – bugger ! Notepad is still there.
    There is no list of recent files in your editor and even worse – I have to navigate to my file to reload – surely you can do better and offer
    an auto-load of the previous file in Options Menu.
    I would go a step further and have a script to auto run your editor when SU reboots. Not sure if SU has a autoboot facility. One way around this would be to write a (Delphi) App to call SU and press a few buttons to load your editor (and even parse it a filename), however this is a little clunky, goes against the spitit of your endeavors.
    I think man users would have similar sentiments.
    Are you looking to fix such simple shortcomings ?
    Phil McClelland
    phil@ultramag.com

    • Hi Phil. Thanks for the tips. I like the “reopen last file” feature. And even the auto-open feature should be easy to implement. I am actually working on an update (and have already fixed the annoyance of having to browse to the last used file) that I should be releasing soon.

      • I haven’t added the auto-load option yet, but now the open dialog goes to the last opened file. Please update to 2.1.

  • Scott

    Very nice. I would love to see this as a standalone app outside of SU, even as a web app. Any way to get the SU code coloring to work outside RCE (like TextMate)?

    • That code highlighter is called CodeMirror and is actually used by many online coding sites (for example the Google API playground). There is a SketchUp code highlighting file available on SketchUcation that works with Notepad++.

  • Alex,

    Thanks, nice tool. Can I ask please that the program remembers where you saved a file, so that I don’t have to browse the path each time I save

    Thanks!

  • TimRowledge

    Nice idea; should be a useful tool.
    One annoyance that always seems to get me more than it really should  – I can’t (on Mac. 10.7.4) get the code view to be anything other than the dark background. It makes it a bit difficult for me to read the text. Any idea what might fix this?
    tim  at rowledge dot org

    • jitanZhang

      Window style

  • Steve

    absolutely cool!

    • jitanZhang

      你好

  • Dave

    Nice tool.  the only problem I have is the black background behind the code.  My curser shows up black, so it is difficult to see.

    bossdave

  • Just added the RBZ version of this plugin. If you have SU v.8 M2 installed, give it a try!

    • Gjxiaolong88

      I translated a Chinese version, more offensive, thank you!

  • Newbie

    I just downloaded Sketchup8 and your ruby editor. I am running OS/X 10.6.8. Your editor comes up and all seems to be fine until I hit the “play button”. It then responds with “Running the code…Done. Feedback from Ruby: ” but nothing else (not even a newline) and the code does not seem to run. I typed the example code in from your screenshot above, I have even tried just typing in ‘puts “hi”‘. Same result. As if it never actually sends the code to Sketchup. Did I need to install something else as well? The built-in ruby console works, even when invoked from your editor.

    • Newbie

      Ok, I need to retract that comment. I tried again and loaded your sample scripts and they worked in Sketchup. I must be misunderstanding the point of the bottom section of the editor window. I thought it was like a console, so you could print things out, but all it does is give the “running” messages. Is this working as intended? Is there a way for me to get the output of “print” statements?

      • Yes, this is expected behavior. The Ruby output would show up in SketchUp’s Ruby Console. Just open both the editor and the console and you’ll see.

    • jitanZhang

      The same problem

      • I’ll look into the code soon again for an update. Please be patient…

  • tahw nikcufos

    Nice Tool – I frequently use it to test snippets, during PlugIn development. It does have a couple of minor quirks though. During text insertion/selection/editing – its seems to have problems maintaining focus as a text editor, and frequently behaves as a web browser would – i.e. context menus, keyboard shortcuts – I have to be careful where I place my mouse, to successfully cut/copy/paste text, or even get text to highlight. More often than not, the context menu that pops up, is the one for the browser, not the code editor.

    • I know. That’s one issue I have noticed, too. It’s just not as responsive as a native app would be (because it’s all JavaScript and it runs in IE). I wish Google could add Chrome to both the Windows and the Mac versions of SketchUp. This would make development much easier and would allow for better JavaScript rendering.
      In any case, I’ll see at some point soon whether there’s an update to the code editor available that might fix some issues.

  • I just updated the Ruby Code Editor to version 2.0. It now features reliable code highlighting. As always, mention bugs here.

  • Macgile

    Hi Alexander :)
    I use a lot ruby Code Editor for my developments.
    It’s really a very good plugin.

    However I encounter a bug.
    When I want commented on a line with the shortcut (AltGr +#) a inputbox opens but not on the good line.

    Best regard

    • Macgile

      sorry I have not seen that there was a post on the same subject. :(

  • Sérgio Bizello

    First, the ruby Code Editor for Sketchup is great.
    Now, the problem: I wrote a code that is part of a master code and it very well in Ruby Code Editor. But, after I copy and paste it in master code and run in Sketchup, it doesn´t works. I can´t solve this problem. What happens?

    • alexschreyer

      Very strange. Did you save the code and then paste or did you just copy from the editor? If you save, then some special characters don't get saved properly (see about page).
      The only other thing that happens in the editor is that the code gets wrapped in an undo group. But that shouldn't affect what's happening.

      • Sérgio Bizello

        Very strange too. Now it is working. How was solved? I copy the code form Ruby Code Editor and paste into a Web Console. After that copy it from Web console and paste into master rb code. It worked. Strange… but worked and has work. Really I don´t know what is this. Very thanks for your reply Alex. I will report some details this "bug" if I find the reasons. Thank you.

  • pierremail.com

    Bonjour,
    I tested your script as sketchup8 linux / ubuntu / wine.
    sketchup works perfectly, but your script does not work.
    by cons, if I use sketchup with windowsxp under vmware so, your script seems to work.
    Why it does not work under wine? would it be a wine problem?
    Bravo and thank you for your great tool!
    Thank you – Pierre Spanish user

  • Liqun

    It looks like very nice and light, and good for Ruby coding and testing in Sketchup as if it was part of it.

    • Liqun

      Can I run a Ruby within the Editor?

      • Liqun

        I find it!

  • GeniX

    Hi,

    I'm working on a AZERTY keyboard and in order to insert [ , ] , | and # I need acces to the ALT Gr key. Pressing the ALT Gr key in the code editor, results in a "pop-up" input field for some reason, preventing correct input in the editor.

    I'm working on Win 7 64 bit with Google Sketchup 6

    • alexschreyer

      Thanks for letting me know. That was an attempt at a code completion that I apparently hadn't removed. I'll take it out in the next version. In the meantime, look into the ui.js file and remove or comment (or modify) this block:

      // Autocomplete – Show input when CTRL SHIFT combination is pressed

      var isCtrl = false;

      var crPos = 0;

      $(document).keyup(function (e) {

      if(e.which == 17) isCtrl=false;

      }).keydown(function (e) {

      if(e.which == 17) isCtrl=true;

      if(e.which == 18 && isCtrl == true) {

      crPos = $('#console').getSelection().start;

      $('#autoplete').show();

      $('#autoplete_txt').focus();

      return false;

      }

      });

      Cheers, Alex

  • mark

    THis is great!!!! It makes those lookups to the Ruby and API so easy.

  • Rod

    Nice work, thank you, works fine on windows 7 64bit.

  • eran

    Good work!
    Thanks for the tool!

  • sw96500

    Great tool !

    Work well in my system: Windows XP, IE7, Sketchup Pro 7.0

  • Great idea!

    To get your sample code to work I had to add "Math." to sin function to prevent the error:
    (eval):9:in ‘initialize’: undefined method ‘sin’ for #<WebConsole:0x6e6d5d8>

    • Interesting. It works fine for me. Are you on a Mac?

  • Gerrit Hubers

    Dan, it (whatever you may call it (not a SU-tool though!)) was mentioned to show up in the Tools menu. Thats why my insignificant remark. Of course I found it where it should be. And later I checked the code where it is send to.
    I can't catch up with you guys but at least that I could figure out.
    Cheers.

    • Dan Rathbun

      OK.. gotcha.Sorry.. my bad! (I missed the typo.)

  • Gerrit Hubers

    Alexander,

    Hi, I'm Gerrit Hubers / Wo3Dan on the SU forums. Thanks for making this script/editor. Hopefully it's going to help me in getting to know how to write (simple) ruby scripts for SU. I liked Jim's Web Console for it was integrated in SU's environment. For me that's essential for understanding what happens when, when trying/altering snippets. So I'm really excited about this tool!!!
    One verrrrrrry small remark from a simple futur user. Your tool shows up in the 'Plugins' folder in SU, not in the 'Tools' folder.

    Thanks again,
    Gerrit

    • Gerrit Hubers

      Edit: Your tool shows up in the 'Plugins' menu in SU, not in the 'Tools' menu in SU.

      • Oops! Just a typo. Thanks for catching it. Of course the tool appears in the "Plugins" menu.

      • Dan Rathbun

        Because it's not a Tool. (Like a Pencil or Protractor.) It's a Utility (and in the Ruby subcatagory.)