Just getting into Ruby for SketchUp, and the first tool I’m trying to develop is a mass view exporter, which would require user input to choose a list of scenes to export from, and a dynamic form reading the layer info inside the model. I got the scene exporting part figured out, but the UI is a bit confusing at first given it’s actually html+js, and it’s at its most confusing when I try to figure out how to push for example a list of model layer names as array into Javascript so I can dynamically generate a multi-select form in the html window.
The script below that I wrote kind of works, but it feels very hard coded and I’m posting here to see if I’m understanding HtmlDialog.add_action_callback
and HtmlDialog.execute_script
correctly, and if there are far more efficient and elegant ways to do this.
Thank you! (And also how do you post code in here? There is only blockquote and pre-formatted text Thnx I got it!) And if you have better htmldialog example scripts, please share!
module Example
model = Sketchup.active_model
model_layers = model.layers
model_scenes = model.pages
scene_names = []
model_scenes.each {|scene| scene_names.push(scene.name)}
html = <<-EOT
<p>Choose (multiple) layers</p>
<select id = "layer_select" name="ok" size="12" multiple>
</select>
<script>
sketchup.views_to_form();
</script>
EOT
options = {
:dialog_title => "Choose Views to Export",
:width => 300,
:height => 400,
:style => UI::HtmlDialog::STYLE_UTILITY
}
dialog = UI::HtmlDialog.new(options)
dialog.add_action_callback("views_to_form") {|action_context|
js_command = "var new_text = '';"
scene_names.each do |name|
js_command += "new_text += '<option>#{name}</option>';"
end
js_command += 'document.getElementById("layer_select").innerHTML = new_text;'
dialog.execute_script(js_command)
}
dialog.set_html(html)
dialog.center
dialog.show
end