Is there a good choice to make tables in sketchup GUI?
I searched online and they are saying that TreeView would be a good choice. but how to import them in sketchup? I always have trouble trying to import file in sketchup plugin RubyCodeEditor. I’m not sure which directory should I use to save my imported file.
I installed gtk using gem install gtk3 and it is stored in a gem package. However, I installed CSV using gem install csv, it could be imported and works well in sketchup rubyCodeEditor. Quite confused now.
I tested a example GUI code in RubyMine Intellij, and it doesn’t work in rubycodeEditor.
require 'gtk3'
class RubyApp < Gtk::Window
def initialize
super
set_title "Center"
signal_connect "destroy" do
Gtk.main_quit
end
set_default_size 300, 200
set_window_position Gtk::WindowPosition
show
end
end
window = RubyApp.new
Gtk.main
"Nil result (no result returned or run failed)"
Error: #<LoadError: cannot load such file -- gtk3>
C:/Program Files/SketchUp/SketchUp 2019/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:59:in `require'
C:/Program Files/SketchUp/SketchUp 2019/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:59:in `require'
<main>:in `<main>'
C:/Users/cyg/AppData/Roaming/SketchUp/SketchUp 2019/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:324:in `eval'
C:/Users/cyg/AppData/Roaming/SketchUp/SketchUp 2019/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:324:in `block in initialize'
~
# the top bit is just for example content, I think it was posted by @Aerilius
exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls.ancestors.include? Exception
next if exceptions.include? cls
next if cls.superclass == SystemCallError # avoid dumping Errno's
exceptions << cls
cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end
# I added the formatting
formated = ''
indent = 0
tree_printer = Proc.new do |t|
t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
# and some html formatting
space = (' ' * indent); space ||= ''
formated << "<ul>#{space}#{k.to_s}</ul>"
indent += 2; tree_printer.call t[k]; indent -= 2
end
end
tree_printer.call tree
# use a standard dialog
dialog = UI::HtmlDialog.new(
{
:dialog_title => "Tree in Dialog",
:preferences_key => "tree.sample.ruby",
:scrollable => true,
:resizable => true,
:width => 600,
:height => 1000,
:left => 100,
:top => 100,
:min_width => 50,
:min_height => 50,
:max_width =>1000,
:max_height => 1000,
:style => UI::HtmlDialog::STYLE_DIALOG
})
# inject some styling
script = '<script>document.body.style="font:80% sans-serif; line-height:.4em;"</script>'
dialog.set_html(formated << script)
dialog.show
Did you try just importing the gems (require without a complete program code)? If csv can be imported and gtk3 not, check whether they are installed in the same Ruby installation (SketchUp’s Ruby, not system-wide Ruby installation).
This starts the “main loop” of an interactive program (graphical user interface). A normal function would do its processing and return (exit if it is a standalone program, or return control to the caller, here that is SketchUp). A main loop is an infinitely running function so that the user interface stays open.
It is blocking, so it does not return control to SketchUp. It will basically freeze SketchUp’s own main loop. The way to avoid that is either
to use separate threads (which don’t work in SketchUp)
or a separate process (write your GUI as a separate program and start it in a separate process)
or to use those GUI functionalities which are provided and supported by the SketchUp API
I don’t understand how sketchup ruby do when using require.
As When I use require_relative ‘file’ in RubyMine Intellij, the file under the same environment could be found, however, it could not be found in sketchup rubyEditor plugin.
puts "test_library included"
class TestClass
def initialize
puts "TestClass object created"
end
end
I created this two simple rb file to test it.
#!/usr/bin/env ruby
require_relative 'test_library.rb'
t = TestClass.new
it could be executed in RubyMine and could not be executed in sketchup rubyEditor:
"Nil result (no result returned or run failed)"
Error: #<LoadError: cannot infer basepath>
<main>:1:in `require_relative'
<main>:1:in `<main>'
C:/Users/cyg/AppData/Roaming/SketchUp/SketchUp 2019/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:324:in `eval'
C:/Users/cyg/AppData/Roaming/SketchUp/SketchUp 2019/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:324:in `block in initialize'
is not relative to SU ruby used by as_rubyeditor nor to as_rubyeditor.rb itself…
to require_relative it would need to be in the same folder as the __callee__ file…
to require it would need to be in the same folder as the SU ruby or __callee__ file…
BTW: @Aerilius has his own ‘RubyConsole+’ extension with more functionality then ae’s one…
Thank you john, I am trying to understand how to custumize it, but is it possible to change value on the interface? I mean it could definitely display values, but is it possible to insert a inputbox?
tree_printer = Proc.new do |t|
t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
# and some html formatting
space = (' ' * indent); space ||= ''
formated << "<ul>#{space}#{k.to_s}</ul>"
indent += 2; tree_printer.call t[k]; indent -= 2
end
end
is this the part to insert entities I might need? But how to draw a table using the HTMLDialog?
For this reason, GTK is not compatible for use (as is) in an embedded Ruby process.
Also, last I looked, GTK3 was not yet “ready for primetime” on MS Windows. (There was still work that needed to be done. I played with it after dealing with numerous issues getting the correct version of dependency libraries, and still had lock up and crash issues even within a system Ruby process.)
Hi, John. in the html you provided below. I believe this code is to replace each object by an mat object using <<, is there some function to save file from the html table? I searched for editable tables online and got a code like the second code attached below. there are also a button as well.
mat.each do |arrays|
html << %Q[
<tr>
<td>#{mat.name.gsub(/[<>]/, '')}</td>
<td>#{mat.display_name.gsub(/[<>]/, '')}</td>
</tr>
]
puts "#{arrays}"
end
combining the two allows you to receive data from the web dialog…
you need to step back and try to learn the basics…
html = %Q[
<!DOCTYPE html>
<html>
<body>
<h1>The onclick Event</h1>
<p>The onclick event is used to trigger a function when an element is clicked on.</p>
<p>Click the button to trigger a function that will output "Hello World" in a p element with id="demo".</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
sketchup.js2ruby('hi from javascript');
}
</script>
</body>
</html>
]
# use a standard dialog
dialog = UI::HtmlDialog.new(
{
:dialog_title => "Table in Dialog",
:preferences_key => "table.sample.ruby",
:scrollable => true,
:resizable => true,
:width => 600,
:height => 500,
:left => 100,
:top => 100,
:min_width => 50,
:min_height => 50,
:max_width =>1000,
:max_height => 1000,
:style => UI::HtmlDialog::STYLE_DIALOG
})
dialog.set_html(html)
dialog.add_action_callback("js2ruby") {|_, value|
UI.messagebox(value)
}
dialog.show