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

    Launching ruby script from Java

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 4 Posters 1.0k Views 4 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.
    • ElagymE Offline
      Elagym
      last edited by Elagym

      Hi guys,

      I'm new to ruby and I wrote a script which automatize import of an .idf file in sketchup and exports it as a .dae file. WhenI load my script from the ruby code editor, there's no problem, but when I try to launch it from java, it seems like it cannot load the sketchup's dependencies I need.

      Here is my java code :

      
                  ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
                  jruby.eval(new BufferedReader(new FileReader("C;\\product\\SketchUp\\SketchUp 2016\\Tools\\api_dev.rb")));
      
      

      (I added my script in this folder, don't know if its a good way to do...)

      Here is the content of api_dev.rb :

      
      require 'C;\product\SketchUp\SketchUp 2016\Tools\sketchup.rb'
      
      model = Sketchup.active_model
      model.close_active;
      show_summary = true
      status = model.import "C;/path/myIdfFile.idf", show_summary
      
      # Or for a COLLADA (.dae) file, using the default options
      
      options_hash = { ;triangulated_faces => true,
      				;doublesided_faces => false,
      				;edges => true,
      				;author_attribution => false,
      				;texture_maps => false,
      				;selectionset_only => false,
      				;preserve_instancing => true}
      model.export 'C;/path/exportedDaeFile.dae',  options_hash
      
      
      

      And here is the stacktrace I get :

      
      LoadError; no such file to load -- langhandler
        require at org/jruby/RubyKernel.java;944
        require at uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55
          <top> at C;/product/SketchUp/SketchUp 2016/Tools/sketchup.rb;15
        require at org/jruby/RubyKernel.java;944
         (root) at uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;1
          <top> at uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55
      javax.script.ScriptException; org.jruby.embed.EvalFailedException; (LoadError) no such file to load -- langhandler
      	at org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java;104)
      	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;121)
      	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;146)
      	at ProxyTester.main(ProxyTester.java;60)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java;95)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java;56)
      	at java.lang.reflect.Method.invoke(Method.java;620)
      	at com.intellij.rt.execution.application.AppMain.main(AppMain.java;144)
      Caused by; org.jruby.embed.EvalFailedException; (LoadError) no such file to load -- langhandler
      	at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java;131)
      	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;118)
      	... 7 more
      Caused by; org.jruby.exceptions.RaiseException; (LoadError) no such file to load -- langhandler
      	at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java;944)
      	at RUBY.require(uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55)
      	at RUBY.<top>(C;/product/SketchUp/SketchUp 2016/Tools/sketchup.rb;15)
      	at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java;944)
      	at RUBY.(root)(uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;1)
      	at RUBY.<top>(uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55)
      
      

      I don't understand why it doesn't manage to find langhandler file because its located in the same folder than sketchup.rb ...
      Am I missing something when I try to retrieve the sketchup's ruby files ?

      Thanks for you attention,

      Elagym

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        Have you tried also requiring the langhandler file?
        require 'C:\product\SketchUp\SketchUp 2016\Tools\langhandler.rb'

        TIG

        1 Reply Last reply Reply Quote 0
        • ElagymE Offline
          Elagym
          last edited by

          @tig said:

          Have you tried also requiring the langhandler file?
          require 'C:\product\SketchUp\SketchUp 2016\Tools\langhandler.rb'

          Sorry I forgot to mention that I tryed to add this line too but I've the same error. I removed it because it is already required in sketchup.rb but I got an error in both cases.

          It looks like my script cannot access those ruby files if I run it from outside sketchup πŸ˜•

          I also tryed modifying the sketchup.rb file : I set the entire path in [require 'langhandler.rb'] and then I got this error :

          
          NameError; uninitialized constant LanguageHandler;;Sketchup
                const_missing at org/jruby/RubyModule.java;3309
            find_strings_file at C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;110
                        parse at C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;137
                   initialize at C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;36
                        <top> at C;/product/SketchUp/SketchUp 2016/Tools/sketchup.rb;16
                      require at org/jruby/RubyKernel.java;944
                       (root) at uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;1
                        <top> at uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55
          javax.script.ScriptException; org.jruby.embed.EvalFailedException; (NameError) uninitialized constant LanguageHandler;;Sketchup
          	at org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java;104)
          	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;121)
          	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;146)
          	at ProxyTester.main(ProxyTester.java;59)
          	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java;95)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java;56)
          	at java.lang.reflect.Method.invoke(Method.java;620)
          	at com.intellij.rt.execution.application.AppMain.main(AppMain.java;144)
          Caused by; org.jruby.embed.EvalFailedException; (NameError) uninitialized constant LanguageHandler;;Sketchup
          	at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java;131)
          	at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java;118)
          	... 7 more
          Caused by; org.jruby.exceptions.RaiseException; (NameError) uninitialized constant LanguageHandler;;Sketchup
          	at org.jruby.RubyModule.const_missing(org/jruby/RubyModule.java;3309)
          	at RUBY.find_strings_file(C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;110)
          	at RUBY.parse(C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;137)
          	at RUBY.initialize(C;/product/SketchUp/SketchUp 2016/Tools/langhandler.rb;36)
          	at RUBY.<top>(C;/product/SketchUp/SketchUp 2016/Tools/sketchup.rb;16)
          	at org.jruby.RubyKernel.require(org/jruby/RubyKernel.java;944)
          	at RUBY.(root)(uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;1)
          	at RUBY.<top>(uri;classloader;/META-INF/jruby.home/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb;55)
          
          

          I think there's like a snowball effect, I guess that modifying all the sketchup's files is not the good way to proceed.

          1 Reply Last reply Reply Quote 0
          • ElagymE Offline
            Elagym
            last edited by

            Ok I've found a workaround. It's not very clean but it works...

            I modified the AppObserver.rb located in my openstudio folder : in the function onOpenModel, I added a [load 'myScript.rb'] in order to inject my script in the sketchup scope.

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

              Just FYI, ...

              (1) your script does not use any of the features in the "sketchup.rb" file (which is trying to load "langhandler.rb",) and therefore it is not necessary (or desirable) for you to try to load "sketchup.rb" in your script.
              Ie, only require actual dependency files.

              (2) The Tools folder is protected in SketchUp version 2016 and higher. It is for Trimble use ONLY. Do not put your scripts in this folder.
              Put them in a sub-folder of the "plugins" subfolder, of the user %APPDATA% path. Ie,
              plugin_path = %USERPROFILE% + "\\AppData\\Roaming\\SketchUp\\SketchUp 2016\\Plugins\\" + "YourCompany_YourPluginName"

              (3) SketchUp loads it's OWN implementation of Ruby. It does NOT use JRuby !
              So, ...
              ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby"); jruby.eval(new BufferedReader(new FileReader("C:\\product\\SketchUp\\SketchUp 2016\\Tools\\api_dev.rb")));
              ... is worthless.

              You need to start the SketchUp application using a -RubyStartup parameter:
              "C:\\product\\SketchUp\\SketchUp 2016\\sketchup.exe" + " -RubyStartup " + plugin_path + "\\api_dev.rb"

              I'm not here much anymore.

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

                @elagym said:

                H
                Here is the content of api_dev.rb :

                The SketchUp Ruby API is only available from within SketchUp. It cannot be used in standalone builds of Ruby.

                If you need to read and write SKP files from your own application then there is the C API that can be used: https://extensions.sketchup.com/developer_center

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

                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