require 'sketchup.rb'
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face(pts)
area = face.area
reply = 'Face area is "' + area.to_s + '" square meters'
script = 'Face Area ( "' + reply + '" );'
UI.messagebox "#{reply}"
puts script
puts message.to_f
HTML DIALOG
Next we are creating a Web Dialogue instance called "wd". The title of the dialog is called "Face Drawing". Next we define the dialog so that it has a scroll bar. The register information is stored in the "Info" file Then, we define the size of the window, ie width and height, and that the size of the window can not be changed. Next we are using add_action_callback method so Ruby is waiting a contact from JavaScript. When button is pressed the "message" value is passed from WebDialog to Ruby. Then we convert the string value to integer and set the value to variables named "depth" and "width". Next, we define the geometric points according to the the given values. The Ruby code adds the surface to the generated points and calculates its field. Ruby transfers the information to the Web Dialog who reported it. Information is also displayed on the Ruby panel Finally we show the WebDialog.
RUBY FILE
require 'sketchup.rb'
wd=UI::WebDialog.new("Face Drawing", true, "Info",300, 300, 100, 200, false )
wd.add_action_callback("getUserInput1"){|action_context, user_input1|
wd.add_action_callback("getUserInput2"){|action_context, user_input2|
depth = user_input1.to_i
width = user_input2.to_i
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face(pts)
area = face.area
reply = 'Face area is "' + area.to_s + '" square meters'
script = 'Face Area ( "' + reply + '" );'
UI.messagebox "#{reply}"
puts script
puts message.to_f
js_wd.execute_script( script )
}}
wd.set_file( "C:/Program Files/SketchUp/SketchUp 2017/Tools/Area.html" )
wd.show()
HTML FILE
<!DOCTYPE html>
<html>
<head>
<title>Area Counting
</title>
</head>
<center><h1>Calculate Area</h1>
<script>
function sendDataToSketchUp() {
var user_input1 = document.getElementById('id1');
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>
<body>
</body>
<center><p>Draw A Plane And Calculate Area</p>
<form>
<center>Length: <input id='id1' type='number' name='length' value=50 required><br>
<br>
<center>Width: <input id='id2' type='number' name='breadth' value=75 required><br>
</form>
<br>
<center> <button onclick='sendDataToSketchUp()'>Draw Plane</button>
</body>
</html>