Select And Extrude
We are creating a Web Dialogue instance called "my_thi_dialog". The title of the dialog is called "Selection Info". Next we define the dialog so that it has no scroll bar. Then, we define the size of the window, ie width and height, and that the size of the window can be changed. Next we are using add_action_callback methods so Ruby is waiting a contact from JavaScript. When the button is pressed, the program checks the ruby code for the corresponding function name. Next, the Ruby code executes the loop of the selected item where it checks for the surface. In the case of a surface, the surface area is added to the variable "area_a".The number and the area of the surfaces are reported. Next, the surface area is moved from Ruby to DHTML which displays them. Finally we show the WebDialog.
my_thi_dialog = UI::WebDialog.new("Selection Info", false, "Selection Info", 400, 300, 400, 200, true)
my_thi_dialog.add_action_callback("get_data") do |web_dialog,action_name|
if action_name=="face_selection_count"
selection = Sketchup.active_model.selection
model = Sketchup.active_model
entities = model.active_entities
selection = Sketchup.active_model.selection
face_count = 0
area_a = 0
# Look at all of the entities in the selection.
selection.each { |entity|
if entity.is_a? Sketchup::Face
face_count = face_count + 1
area_a = area_a + entity.area*0.00064513629
end
}
UI.messagebox "Number of faces: #{face_count}"
UI.messagebox "Face area : #{area_a} square meter"
js_command = "passFromRubyToJavascript(" + area_a.to_s + ")"
web_dialog.execute_script(js_command)
end
end
my_thi_dialog.add_action_callback("get_data_2") do |web_dialog,action_name|
if action_name=="entities_selection_count_2"
selection = Sketchup.active_model.selection
number = selection.length
UI.messagebox(" The number of entities in the drawing " + number.to_s)
end
js_command = "passFromRubyToJavascript("+ number.to_s + ")"
web_dialog.execute_script(js_command)
end
my_thi_dialog.add_action_callback("callRuby_3") do |web_dialog, message|
depth = message.to_s
value = depth.to_f*39.39
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
faces = selection.grep(Sketchup::Face)
faces.each do |face|
face.pushpull(value)
UI.messagebox "Added depth: #{value/39.39} meters"
end
end
my_thi_dialog.set_file("C:/Program Files/SketchUp/SketchUp 2017/Tools/select_4.html")
my_thi_dialog.show()
HTML FILE
!DOCTYPE html>
<html>
<head>
<title>Select And Extrude
</title>
</head>
<center><h1>Select And Extrude</h1>
<script>
function sendDataToSketchUp() {
var user_input1 = formid.ddlselect[formid.ddlselect.selectedIndex]
var user_input2 = document.getElementById('id2');
query = 'skp:getUserInput1@' + user_input1.value
window.location.href = query;
query = 'skp:getUserInput2@' + user_input2.value
window.location.href = query;
}
</script>
<body>
<script>
function dataFromSketchUp() {
}
</script>
</body>
<center><p>Extrude Model Face</p>
<form>
</form>
<form name="formid">
<select name="ddlselect" onchange="myFunction()">
<option value="50">Model Depth - 50</option>
<option value="-50">Model Height - 50</option>
<option value=40>Model Depth - 40</option>
<option value=-40>Model Height - 40</option>
<option value=30>Model Depth - 30</option>
<option value=-30>Model Height - 30</option>
<option value=20>Model Depth - 20</option>
<option value=-20>Model Height - 20</option>
<option value=10>Model Depth - 10</option>
<option value=-10>Model Height - 10</option></select>
</form>
<label id="input"></label>
<br>
<center> <button onclick='sendDataToSketchUp()'>Create Extruded Model</button>
</body>
</html>
RUBY FILE
my_thi_dialog = UI::WebDialog.new("Selection Info", false, "Selection Info", 400, 300, 400, 200, true)
my_thi_dialog.set_file("C:/Program Files/SketchUp/SketchUp 2017/Tools/Extrude.html")
my_thi_dialog.show()
my_thi_dialog.add_action_callback("getUserInput1"){|action_context, user_input1|
depth = user_input1.to_i
value = depth.to_f*39.39
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
faces = selection.grep(Sketchup::Face)
faces.each do |face|
face.pushpull(value)
UI.messagebox "Added depth: #{value/39.39} meters"
first_entity = entities[0]
end
}