sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [plugin] Ruby Code Editor - UPDATED to v3.0 3/4/2013

    Scheduled Pinned Locked Moved Plugins
    56 Posts 11 Posters 39.8k Views 11 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      @alexschreyer said:

      I have also not tested this on a mac.

      For the PC, put the following META tag in the HEAD section, so the browser displays Themed Controls instead of those old ugly plain Win 3.x style controls (ie buttons, scrollbars, etc.)

      The Mac (Safari) should just ignore the tag.

      HTML

      
      	<!-- Use MS Common Controls ver 6+ if available -->
      	<!--  (also known as XP style themed controls.)  -->
      	<META HTTP-EQUIV="MSThemeCompatible" CONTENT="Yes">
      
      

      _

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        @alexschreyer said:

        @thomthom: The bottom of the page disappears when the window width gets so small that the file name breaks into the next row. I wanted to keep the layout fluid so that elements adjust on resize, but maybe I can find a better way to arrange the page more reliably.

        I'd like to see the rb filename on the line below the toolbar (as a step towards future tabbed multi-snippet interface where the filename would be on the tabs anyway.)

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @unknownuser said:

          (No output?) The Ruby Console reports no error - but does output the puts statements.
          @alexschreyer said:

          @thomthom: Not sure why there is no output. I didn't touch Jim's code that deals with capturing the Ruby response - does this also happen with the original Web Console?

          Yes it happens with the original WebConsole.

          (1) Keep in mind that the output pane in the WebConsole (currently) displays the result of the eval method, NOT the output stream of STDERR and/or STDOUT as the SUConsole does. (This is why for multiple statements you see only the result from the LAST statement.)

          (2) The puts method returns nil so you won't see anything if Jim's code stripped textstring "nil" out.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @alexschreyer said:

            @Chris Fullmer: Looking at the result capture code, it appears to convert characters such as the newline into HTML characters before sending it to the dialog. I tried to find a way around this - to no avail. Sending a newline \n to the execute_script doesn't work for me. Apparently that's why Jim put these conversions in there. I'll try later if there is an easier way to "encode" these before sending and "decode" them in Javascript in the dialog.

            Yes the HTML conversion may be necessary for the WebDialog side but not for the STDOUT.

            Alex YOU caused the HTML tags to also go to STDOUT by inserting the line "p r" AFTER the conversion gsub! statements. IF "p r" is needed, move it up before the conversion statements.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @dan rathbun said:

              (2) The puts method returns nil so you won't see anything if Jim's code stripped textstring "nil" out.

              OK if a var evaluates to nil, and then you convert it to a String with .to_s, the result is an empty length String.

              So line 73 needs a nil test, thus:

              r!=nil ? r=r.to_s : r='nil'

              (Don't just have "r" as the boolean expression, because false values would get set to 'nil' instead of 'false'.)

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • alexschreyerA Offline
                alexschreyer Extension Creator
                last edited by

                Thanks, Dan, for going through this so thoroughly. Everyone, please feel free to suggest code improvements. I would like to make this as useful as possible. Of course, I'll mention every help on the "About" tab.

                @Dan: I'll incorporate your revisions into the output code. That is really the part here that I had looked at the least.

                I like the idea of having tabbed multiple files. With the current tab support that should be relatively easy to implement.

                And thanks for the hint on the Windows UI look for IE. I didn't even know about the tag.

                Cheers,
                Alex

                Author of "Architectural Design with SketchUp":
                http://sketchupfordesign.com/

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  I think I see another problem. If an error occurs don't you need to abort the operation instead of committing it?

                  Try something like this:

                  
                    add_action_callback("exec") do |webconsole_dialog, p|
                      v = webconsole_dialog.get_element_value('console').strip
                      # puts v    # what's this for -- testing ??
                      r = nil
                      begin
                        Sketchup.active_model.start_operation "RubyEditor"
                        begin # evaluation
                          r = eval v
                        rescue => e
                          r = e
                          raise # pass to outer rescue clause
                        end # eval
                      rescue
                        Sketchup.active_model.abort_operation 
                      else # only do if NO errors
                        Sketchup.active_model.commit_operation
                      ensure # always do this
                        r!=nil ? r = r.to_s ; r='nil'
                        p r
                        r.gsub!(/ /, "&nbsp;")
                        r.gsub!(/\n/, "<br>")
                        r.gsub!(/'/, "&rsquo;")
                        r.gsub!(/`/, "&lsquo;")
                        r.gsub!(/</, "&lt;")
                        webconsole_dialog.execute_script("document.getElementById('results').innerHTML='#{r}'")
                      end
                    end # callback
                  
                  

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    Alex.. you did not change the Name of the DefaultSettings Key ('WebCon'), so your plugin is overwriting the values for Jim's standard WebConsole in the registry.

                    There is always the possibilty that some people may still wish to use Jim's "plain Jane" version from time to time, even with yours installed.

                    You should setup a unique Keyname for your defaults.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • alexschreyerA Offline
                      alexschreyer Extension Creator
                      last edited by

                      I'll change the dialog name. Must have overlooked that one. I changed the class name.

                      In the meantime, I figured out how to get linebreaks from a file into a dialog.

                      filename = UI.openpanel
                      f = File.new(filename,"r")
                      text = f.readlines.join.gsub!(/\n/, '<KghBr31sD>')
                      
                      ndlg = UI;;WebDialog.new("Ruby Web Console", true, "WebCon123", 600, 400, 100, 100, true)
                      ndlg.set_html ("<html><head></head><body><textarea style='width;100%;height;100%' name='mybox' id='mybox'></textarea></body></html>")
                      ndlg.show {
                        ndlg.execute_script("var text = '#{text}';var asddsa = text.replace(/<KghBr31sD>/g,String.fromCharCode(13));document.getElementById('mybox').value = asddsa")
                      }
                      

                      I think I saw the method (i.e. using a gibberish tag) discussed recently in the developers forum. I'll give that a try soon.

                      Cheers,
                      Alex

                      Author of "Architectural Design with SketchUp":
                      http://sketchupfordesign.com/

                      1 Reply Last reply Reply Quote 0
                      • chrisglasierC Offline
                        chrisglasier
                        last edited by

                        @alexschreyer said:

                        @chrisglasier: ... It can very well work with my editor.

                        It is not your editor it belongs to Jim; what you did, as I did, was to dress it up. But I don't want to focus on individual efforts but more on why the cognoscenti allow software companies to fob us off with metaphors for paperwork rather than providing digital machines.

                        Take for example your original cheat sheets and my/Jim's Sketchup API digital device which provided an opportunity to link up many other devices like the console. The style is not an issue and you are more adept at implementing the links than an amateur and reluctant coder like me; the code is freely available here.

                        I am very disappointed that you ignored this opportunity to exploit a way to link up data (that belongs to objects that have names) and digital devices (that can automate common tasks).

                        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                        1 Reply Last reply Reply Quote 0
                        • alexschreyerA Offline
                          alexschreyer Extension Creator
                          last edited by

                          @chrisglasier: I see now where you are coming from. My idea with my revision of Jim's Web Console was to make it more functional and spruce up the visuals but keep it easy enough for the beginning coder. I'll look again at your approach with the namesets but it seemed to me at the time to complicate things. I guess I just don't fully understand it yet.

                          I am all for collaboration and combining efforts, though.

                          Cheers,
                          Alex

                          Author of "Architectural Design with SketchUp":
                          http://sketchupfordesign.com/

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by

                            Just a note on personal preference.

                            If you use the META tag I gave you previously, the buttons (New, Open, Save, Quit) take on the look of the nice XP style buttons (rounded corners & which hilite on hover, etc.) They look just as they do in native dialogs.

                            The jQuery buttons you have used take up more space (especilally height-wise) and have the look of the old MS Frontpage styling, which I have always hated.

                            I prefer the native XP look.

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • alexschreyerA Offline
                              alexschreyer Extension Creator
                              last edited by

                              @Dan: You are right, going with the OS styling also makes sense to keep elements looking "native" on Win and Mac.

                              I wanted to go with the jQuery UI framework, though, so that a) the dialog looks exactly the same on Win and Mac and b) later I could offer different styling to users, so that they can pick their preference from a dropdown. The UI system has various nice templates:

                              Link Preview Image
                              ThemeRoller | jQuery UI

                              jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether you're building highly interactive web applications or you just need to add a date picker to a form control, jQuery UI is the perfect choice.

                              favicon

                              (jqueryui.com)

                              One question: I have to save some preferences (like the UI selection) locally. Can I reliably use cookies or should I go with an ini file?

                              Cheers,
                              Alex

                              Author of "Architectural Design with SketchUp":
                              http://sketchupfordesign.com/

                              1 Reply Last reply Reply Quote 0
                              • thomthomT Offline
                                thomthom
                                last edited by

                                Cookies can be purged by the user or utilities that clean the browser data. Safest is to store it separately.

                                Thomas Thomassen — SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  @thomthom said:

                                  Cookies can be purged by the user or utilities that clean the browser data. Safest is to store it separately.

                                  Agreed.

                                  You can use Sketchup.write_default and Sketchup.read_default to save settings in the Registry (Win32) or plist files (Mac) but they have a few disadvantages.

                                  Once you create a key, you cannot remove it, so they can become cluttered with old setting keys that are not used anymore.

                                  And they can be only 1 level of heirarchy, even though the Windows Registry supports any numbers of levels. (It's a tree structered database.) Perhaps this is a limitation of the Mac plists and Google is enforcing it on both platforms for 'sameness'?

                                  Also.. XP Home has a Registry limit size (I'm constantly on the edge of it and often get the Registry Limit reached Warnings. If I'd known about it I would have paid the extra and bought XP Pro.)

                                  Anyhow.. an ini (or any settings file,) would give you complete freedom.
                                  It doesn't need to be an ini format. It can be simply a ruby script that decalres a Settings Hash wrapped in your plugin namespace. Did you know that Module and Class definitions can be split up into multiple files? They can. The main file can be the functional part of the class definition, and the other can be just a Hash declaration inside the same class namespace, that gets loaded by the first (if it exists,) and gets RE-written by the first file, when changes to the settings are made.

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    @dan rathbun said:

                                    Also.. XP Home has a Registry limit size (I'm constantly on the edge of it and often get the Registry Limit reached Warnings. If I'd known about it I would have paid the extra and bought XP Pro.)

                                    Never knew that.
                                    ...how much stuff you got installed? I've installed lots of random stuff on my old xp box through up the years - never had that warning.

                                    Thomas Thomassen — SketchUp Monkey & Coding addict
                                    List of my plugins and link to the CookieWare fund

                                    1 Reply Last reply Reply Quote 0
                                    • Dan RathbunD Offline
                                      Dan Rathbun
                                      last edited by

                                      @thomthom said:

                                      @dan rathbun said:

                                      Also.. XP Home has a Registry limit size (I'm constantly on the edge of it and often get the Registry Limit reached Warnings. If I'd known about it I would have paid the extra and bought XP Pro.)

                                      Never knew that.
                                      ...how much stuff you got installed? I've installed lots of random stuff on my old xp box through up the years - never had that warning.

                                      TOO much stuff! OpenOffice Suite, AutoCAD, Epson Scanner Software, Windows SDK, MS VisualStudio (w/ VB, C#, C++, SQL Server), Debugging Tools for Windows, Windows Support Tools, PowerToys for Windows XP, MS HTML Help Workshop, PCB123, AdobeReader, Paint.NET, NotePad++, SciTE, FamilyOrigins (Genealogy dBase), Google Sketchup 7.x, Google Earth. ..etc...
                                      (And at one time 2 full versions of MS Flight Simulator; which I uninstalled.)

                                      I believe I didn't get the errors until after I installed AutoCAD. I can't find the exact error message at a MS search, but did find an article dated 2007 that says the RSL (Registry Size Limit) "no longer applies" to Windows XP or Windows Server 2003. But I know I have got the error popup after that.

                                      @Alex: Thinking more about this. You don't need need to worry at all regarding this issue. The RSL applies only (if it even does anymore,) to the System Hive of the Registry. Sketchup settings are saved in the User Hive, which does not have (or never had) any size limit.

                                      I'm not here much anymore.

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by

                                        @dan rathbun said:

                                        Anyhow.. an ini (or any settings file,) would give you complete freedom.
                                        It doesn't need to be an ini format. It can be simply a ruby script that decalres a Settings Hash wrapped in your plugin namespace. ... that gets loaded by the first (if it exists,) and gets RE-written by the first file, when changes to the settings are made.

                                        If Js is more comfy for you, the same technique can be used but in Js. A ruby method can write out a Js script that is a JS object holding settings, or an Array of settings, or global varibales, whatever.
                                        The file just gets loaded with the webpage.. no complex parsing required.
                                        If the user makes settings changes.. you send them to a ruby callback that overwrites the settings .js file.

                                        Also if you wish to change CSS dynamically you'll need to assign ID attributes to the stylesheets (whether inline or loaded via LINK tag.)

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • alexschreyerA Offline
                                          alexschreyer Extension Creator
                                          last edited by

                                          Thanks for the recommendations, guys. I actually went with the easiest version (cookies) for now since I was able to implement this without hassle. I'll look at file storage later.

                                          Didn't know that the registry could actually become too large. I never ran into this problem. And I have several Autodesk products installed (those are huuuge - though mostly by filesize! When will ADSK finally cut down on bloating their own software).

                                          Cheers,
                                          Alex

                                          Author of "Architectural Design with SketchUp":
                                          http://sketchupfordesign.com/

                                          1 Reply Last reply Reply Quote 0
                                          • D Offline
                                            driven
                                            last edited by

                                            Hi,

                                            a possibly naive question from one your target end users,

                                            I'm teaching myself ruby on a mac

                                            I have a very Ruby for SU text book that is in PDF format on my hard-drive, I want to access it through the reference menu on your console, (which does throw up some code errors, but still mostly works)

                                            Is it possible, or could I put it on my website and read it from there (less preferable as the books not published yet)?

                                            john

                                            learn from the mistakes of others, you may not live long enough to make them all yourself...

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement