• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Sketchup_json gem for sketchupRuby API

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 2 Posters 1.1k Views 2 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.
  • P Offline
    penpendrum
    last edited by 14 Jun 2014, 17:23

    hello there everyone. I an just a novice self taught programmer and I am making a plugin that could pass data to my java code.

    my problem is that when I type( require 'sketchup_json') in the ruby console, it gives a lot of errors. ( I have typed require 'rubygems' which returns true )

    I've searched the web that json could be the key to pass data from ruby to java, and I further searched that there is this sketchup_json gem for sketchup . I do not know if this is the right gem for my objective . I have also transfered the msvcrt-ruby18.dll file to the googlesketchup folder to make the ruby version into 1.8.7 cuz sketchup has 1.8.6. and I cant download it.

    here is a part of my code:

    $LOAD_PATH << "C:/Ruby186/lib/ruby/1.8"
    $LOAD_PATH << "C:/Ruby186/lib/ruby/site_ruby/1.8"
    $LOAD_PATH << "C:/Ruby186/lib/ruby/1.8/i386-mingw32"
    $LOAD_PATH << "C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib"
    $LOAD_PATH << "C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib/sketchup_json"

    require 'rubygems'

    require 'sketchup_json'

    the error is:

    C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib/sketchup_json.rb:1: warning: already initialized constant APP_BASE_PATH
    C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib/sketchup_json/generator.rb:2: warning: already initialized constant JSONEncodeError
    Error: #<TypeError: C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib/sketchup_json/generator.rb:39: Array is not a class>
    C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36
    C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in gem_original_require' C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in require'
    C:/Ruby187/lib/ruby/gems/1.8/gems/sketchup_json-0.1.1/lib/sketchup_json.rb:5
    C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in gem_original_require' C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in require'
    (eval):63

    and I just do not know why it gives this errors when it works fine with interactive ruby.
    can anyone help me give a clue on how to fix this?cuz I do not know if this problem had already been discussed in this forum.

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 14 Jun 2014, 17:53

      (1) I have never heard of anyone successfully getting RubyGems to run inside SketchUp embedded Ruby in the 1.8.x trunk. That means ALL SketchUp versions before 2014. (It WILL work with SketchUp 2014 and higher, which uses Ruby 2.0, which comes with the standard library and RubyGems already loaded.)
      If you are still using SketchUp v 8 Make (Free,) there is no reason why you should not have upgraded to SketchUp 2014. (.. and even SketchUp 2013 before this!)

      (2) Why would you ever think you can mix together code from Ruby v 1.8.6 and Ruby v 1.8.7 within the same process, without errors ?
      (I am not surprised.)
      Upgrade to SketchUp 2014 if you wish to use gems. All the load paths are preset and you do not need to append them.
      Also there is a json extension that comes with the standard library. Use it if at all possible. (But beware if it changes base classes! Any plugin that changes Ruby Base classes or SketchUp API classes [or modules,] is likely to be blacklisted or quarantined by the community, and not allowed in the Extension Warehouse.)

      (3) Before you can require a gem, it usually must be installed.
      From a system shell, you'd use gem.bat:
      %(#004000)[gem install sketchup_json-0.1.1]
      .. or from within Ruby (after successfully getting RubyGems to load:
      Gem::install "sketchup_json"

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 14 Jun 2014, 18:33

        .

        FYI see topic: JSON in (SketchUp) Ruby

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 14 Jun 2014, 18:38

          .

          I had a look at the code in the sketchup_json-0.1.1 gem, and yes it modifies many Ruby base classes.
          Most of the mods are adding to_json() method wrappers around all these classes' to_s() method. This is rather clunky IMHO. (Ie, it is unnecessary.)

          Also it does not help the API classes that could benefit from json conversion.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 15 Jun 2014, 17:11

            I notice that under SketchUp 2014 and Ruby 2.0, that Object has been extended.

            At the console:
            Object.ancestors%(darkgreen)[>> [Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]]

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • P Offline
              penpendrum
              last edited by 19 Jun 2014, 17:39

              Hello again Dan. Thanks for the reply.. I have already download sketchup 14 and I took your advice. it is just too great..I did got the gems and json without and effort.

              but there is this little problem that I have encountered, I cant seem to parse a json file to sketchup.
              here is a sample code that I have written.

              I wrote this JSON file named input.json(I saved it inside the Tools folder):
              { "name":"Stephen"
              "age": 22 }

              and I wrote this simple ruby code in the Tools folder name testing.rb:

              require 'rubygems'
              require 'json'
              require 'pp'

              json = File.read('C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/input.json')
              obj = JSON.parse('json')

              PP obj

              and this is the error that I got in the ruby console:

              Error: #<JSON::ParserError: 757: unexpected token at 'json'>
              C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/json/common.rb:155:in parse' C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/json/common.rb:155:in parse'
              C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/tesing.rb:6:in <top (required)>' <main>:in load'
              <main>:in <main>' -e:1:in eval'
              nil

              do you have any Idea how to fix this . I mean how to let sketchup read seperate json files .

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 20 Jun 2014, 18:29

                @penpendrum said:

                (I saved it inside the Tools folder):

                The "Tools" folder is a OEM (Trimble) ONLY folder.
                You should stay out of that folder.

                Any "user" data files (or materials, styles, modles, etc.,) should be saved in your user path.
                Either under the %AppData% path, or under your documents path.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 20 Jun 2014, 18:38

                  @penpendrum said:

                  I wrote this JSON file named input.json(I saved it inside the Tools folder):

                  { "name";"Stephen"
                  >   "age";  22 }
                  

                  Each sub-object in JSON must be separated with a comma ( , ).
                  This is resulting in the unexpected token when parsing, because it sees the 2nd "age" token object while it is trying to isolate the 1st "name" token.

                  It should be like:

                  {
                    "name" ; "Stephen" , # <-- comma seapartor
                    "age"  ;  22
                  }
                  

                  @penpendrum said:

                  and I wrote this simple ruby code in the Tools folder name testing.rb:

                  require 'rubygems'

                  This is not needed (but harmless,) as the Ruby loader already loads RubyGems for Ruby 2.x

                  I'm not here much anymore.

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

                  Advertisement