sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Html multiline, js to SketchUp

    Scheduled Pinned Locked Moved Developers' Forum
    16 Posts 5 Posters 969 Views 5 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.
    • Chris FullmerC Offline
      Chris Fullmer
      last edited by

      Sorry for the horrible title, I'm not really sure what I'm looking for here. But here's the description. I would like to have text in my html that is written out over multiple lines and then have those multiple lines passed into SketchUp. I'm close, but I keep getting nil in SketchUp when I try to pass the string. This is what I have, something like this:

      <p id="cool_stuff">This is my
        multiple lines 
        of cool
        text.
      </p>
      

      And then I have a simple js script that reads that text.

      function cool_text_to_su() {
        query = 'skp;get_data@' + document.getElementById('cool_stuff').innerHTML;
        window.location.href = query;
        alert(query);
      }
      

      The alert shows me that the query variable is getting all the words from inside the paragraph tags. But in SketchUp, I"m getting nil. When I fix it so its all on a single line, then it all comes through into SketchUp just fine. Any thoughts how to pass this more cleanly into SketchUp? I'm thinking maybe I could delete line breaks and replace them with a special character combo like " ^^" and then rebuild the lines in SketchUp - all_lines = query.split("^^") or something. Would that work? Can someone give me some hints on how I would do that in js? Or perhaps there is a more simple solution? Thanks!

      Chris

      Lately you've been tan, suspicious for the winter.
      All my Plugins I've written

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

        Not sure why you are getting nil. (Where are you getting nil btw?)

        Can you mock up a complete minimal test case? I have the feeling something is missing here.

        As for Ruby > JS > Ruby - I got a bridge in TT_Lib that let you pass most common data types between JS and Ruby. I made wrappers so I can call functions and methods with arguments in a more natural way between Ruby and WebDialogs - even forwards the return value back to the calling environment.

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

        1 Reply Last reply Reply Quote 0
        • A Offline
          Aerilius
          last edited by

          If the text can contain any user input, you need to use a sort of bridge (thomthom's bridge, WebDialogX or SketchUp's bridge.rb) to escape and urlencode the text.
          Especially if the url text contains a single quote (') SketchUp returns nil.

          1 Reply Last reply Reply Quote 0
          • Chris FullmerC Offline
            Chris Fullmer
            last edited by

            Ahh, I'm inclined to think it may be what Aerillius is suggesting. My text is fairly complex and I haven't looked closely, but it probably contains some single and/or double quotes.

            I'll look into that and report back. Then I'll try your bridge next Thom. Thanks guys!

            Chris

            Lately you've been tan, suspicious for the winter.
            All my Plugins I've written

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

              @chris fullmer said:

              I'm thinking maybe I could delete line breaks and replace them with a special character combo ...

              @unknownuser said:

              escape

              Returns the hexadecimal encoding of an argument in the ISO-Latin-1 character set.

              Core function

              Implemented in

              • JavaScript 1.0, NES 2.0
              • ECMA version ECMA-262 compatible, except for Unicode characters.
                see: escape()

              I'm not here much anymore.

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

                @chris fullmer said:

                Any thoughts how to pass this more cleanly into SketchUp? I'm thinking maybe I could delete line breaks and replace them with a special character combo like "^^" and then rebuild the lines in SketchUp - all_lines = query.split("^^") or something.

                On the Js side you would use replace
                see: http://docs.oracle.com/cd/E19957-01/816-6408-10/string.htm#1194258
                I think Jim had an example in his old WebConsole where he changed special characters " to "
                see:

                [url=http://msdn.microsoft.com/en-us/library/aa752007(v=vs.85).aspx:firgcgl8]ISO Latin-1 Character Set[/url:firgcgl8]

                for example, a single quote (apostrophe) is '
                also:

                • [url=http://msdn.microsoft.com/en-us/library/aa752009(v=vs.85).aspx:firgcgl8]Character Entities for Special Symbols[/url:firgcgl8]
                  [url=http://msdn.microsoft.com/en-us/library/aa752008(v=vs.85).aspx:firgcgl8]Additional Named Entities for HTML[/url:firgcgl8]

                On the Ruby-side you would use a inverse of the regular expression, but with the gsub!() method, or tr!() etc. There are quite a few Ruby methods you can use.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • Chris FullmerC Offline
                  Chris Fullmer
                  last edited by

                  Awesome, thanks for the tips Dan. I like the look of the escape() method in js. I'll probably post back eventually once I get around to trying to unescape it in ruby.

                  Thanks!

                  Lately you've been tan, suspicious for the winter.
                  All my Plugins I've written

                  1 Reply Last reply Reply Quote 0
                  • Chris FullmerC Offline
                    Chris Fullmer
                    last edited by

                    Great, thanks again! I got your PM. I do have ruby 1.8.6-p287 installed. but that sure made it easy. Thanks Dan,

                    Chris

                    Lately you've been tan, suspicious for the winter.
                    All my Plugins I've written

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

                      @aerilius said:

                      If the text can contain any user input, you need to ... escape and urlencode the text.

                      Here's another idea if you'll be using %hex uri-encoded strings:

                      On the Ruby-side:

                      1. cut and paste the "Ruby186/lib/ruby/1.8/uri/common.rb" INSIDE your plugin sub-module
                      2. delete the alias method for the Kernel module (at the bottom of the file.)
                      3. cut it down by removing any unwanted comments, or methods that you do not need for your plugin.
                      • If you do not have Ruby v1.8.6-p287 installed, I can PM you the "uri/common.rb" file.

                      • EDIT: Went ahead and PM'd ya' the file.

                      ADD: The online doc for URI::Escape

                      On the Javascript-side you might be able to get away with using the built-in %(#8000BF)[escape()] and %(#8000BF)[unescape()] functions.

                      I'm not here much anymore.

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

                        Your welcome ...

                        ADD: The online doc for URI::Escape

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • M Offline
                          Myhand
                          last edited by

                          @dan rathbun said:

                          [*]cut and paste the "Ruby186/lib/ruby/1.8/uri/common.rb" INSIDE your plugin sub-module

                          Thanks Dan, this works well. I know another basic question, but is it possible to have the "require" statement in my plugin specify a relative path to common.rb i.e. I currently have to use require 'MyPlugin/common.rb' from the MyPlugin.rb file, even though they are both in the same folder, i.e. the plugins/MyPlugin folder.

                          @dan rathbun said:

                          On the Javascript-side you might be able to get away with using the built-in %(#8000BF)[escape()] and %(#8000BF)[unescape()] functions.

                          Do you know if it is possible to port the 1.9.2 version of the common.rb library to work in Sketchup. This would allow you to use the new encode_www_form and encode_www_form_component methods which also have corresponding methods in JS.

                          http://www.keepingmyhandin.com/

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

                            (1) I said inside your module (not your directory!) Messing with require would not be good idea. But you can do whatever inside your own module. Override the method your module inherited. You can always call Kernel.require() to use the original, but this all sounds silly.

                            There is a code example here I wrote that shows using the SketchupExtension class, and dealing with paths. (Check the Code Snippets index.)

                            (2) Likely NOT. Ruby 1.9.2 has totally overhauled encoding. It's the reason SketchUp will not load it (I have tried.)

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • M Offline
                              Myhand
                              last edited by

                              @dan rathbun said:

                              (1) I said inside your module (not your directory!) Messing with require would not be good idea. But you can do whatever inside your own module. Override the method your module inherited. You can always call Kernel.require() to use the original, but this all sounds silly.

                              Sorry Dan, I am being really thick here πŸ˜•. Are you saying I should cut and paste the code in common.rb into MyPlugin.rb file...?

                              btw it appears that I can use the URI.escape() method without doing a require or even having the common.rb file in my plugin directory. Does Sketchup not maybe support it now out of the box?

                              @dan rathbun said:

                              There is a code example here I wrote that shows using the SketchupExtension class, and dealing with paths. (Check the Code Snippets index.)

                              Again being a bit slow... 😞 but where is the code example you refer to?

                              Thanks for the help.

                              http://www.keepingmyhandin.com/

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

                                @myhand said:

                                Again being a bit slow... 😞

                                If you weren't, ... then I wouldn't feel so smart! 🀣

                                @myhand said:

                                ... but where is the code example you refer to?

                                See all the topics at the top of the topic list that are shaded ? They have a special icon, because they are called "sticky topics". They always appear at the top of the list on page 1 (in any forum, but vary from forum to forum.)

                                For instance in the Plugins Forum, you will discover that Jim Foltz maintains a [ Plugins Index ] sticky topic.

                                In THIS forum, he has a [ Code Snippets ] index, to which I have added a manual categorical index.
                                Anyway... you can look under my name in the Author index.
                                .. and you'll find: "[Code] SketchupExtension and rbs rubies"

                                πŸ’­

                                I'm not here much anymore.

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

                                  @myhand said:

                                  btw it appears that I can use the URI.escape() method without doing a require or even having the "common.rb" file in my plugin directory.

                                  Does Sketchup not maybe support it now out of the box?

                                  No way. Either the Development Tools loaded it ... or you pushed the absolute path to your plugin sub-dir into the $LOAD_PATH array.

                                  ... or you used the SketchUp API's require_all() global method (defined in "Tools/sketchup.rb",) which will push the argument path into $LOAD_PATH. (But does not remove it afterward... which may not be the best implementation.)

                                  On the subject of autoloading files, and controlling them ... I have a example called !autoload.rb, see the snippets index, to find it.

                                  I'm not here much anymore.

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

                                    @myhand said:

                                    @dan rathbun said:

                                    (1) I said inside your module (not your directory!) ...

                                    Sorry Dan, I am being really thick here πŸ˜•. Are you saying I should cut and paste the code in common.rb into MyPlugin.rb file...?

                                    It is what I told Chris to do (in case he didn't wish to have users of his code do a full Ruby install.)

                                    %(#8000FF)[An aside rant to any reader who won't read a book, and learn the basics,
                                    before diving in and attempting to write complex projects:]

                                    If you don't know what I meant by "inside your module" you likely did not follow the advice in my Newbie Guide, and have skipped over learning the nitty-gritty basics of Ruby. Yes it can be boring, but your just running into frustrations because you don't know the simple things.

                                    Really .. print out a chapter a day from the ol' "Pick-Axe" book, and put it next to toilet. Don't waste the time when your sitting on the pot.

                                    In a nutshell... you need to be writing all your code within YOUR unique top-level namespace (which is a module in Ruby.)

                                    Unwrapped code runs within the TOPLEVEL_BINDING, which is the global instance of Object ... and everything is a descendant of Object, so everything inherits whatever is defined in Object.

                                    Other authors do not want your objects, variables etc, propagating into their plugins.
                                    OK ... back on subject, ... "uri/common.rb" ... for now just leave it in the Ruby lib directory, where it belongs, and use a script similar to the one posted to push paths for a full Ruby install.
                                    See: Ruby LOAD PATHs script (Win32)

                                    πŸ’­

                                    I'm not here much anymore.

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

                                    Advertisement