Launching ruby script from Java
-
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
-
Have you tried also requiring the langhandler file?
require 'C:\product\SketchUp\SketchUp 2016\Tools\langhandler.rb'
-
@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.
-
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.
-
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, onlyrequire
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"
-
@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
Advertisement