Hello everyone!
I am looking to make a small script to insert a dynamic component from the Sketchup library, it would be as follows:
- Choose Component (drop down list?)
- Get Insert point (click 0,0,0)
- Get Second point (click 400,200,0)
- Insert component (origin 0,0,0 scale Y to 400,200,0)
It should be noted that I am learning ruby and the Sketchup API in this forum and reviewing the Automatic Sketchup Creation of 3D models in Ruby book. I reviewed the example of the book on modeling a sphere sphere_tool.rb and it is fascinating, could you make some changes and adapt it to the tool I am looking to create??
class SphereTool
def activate
$ents = Sketchup.active_model.entities
# The points clicked by the user
@pt1 = Sketchup::InputPoint.new
@pt2 = Sketchup::InputPoint.new
# The initial state (user hasn't clicked yet)
@first_click = false
end
# If the user clicked, draw a line
def onMouseMove flags, x, y, view
if @first_click
@pt2.pick view, x, y, @pt1
view.invalidate
end
end
# Check the state, then draw a sphere or a point
def onLButtonDown flags, x, y, view
if @first_click
if (@pt1.position.distance @pt2.position) > 0
# Remove the construction point
$ents.erase_entities $c_point
draw_sphere
end
else
@pt1.pick view, x, y
$c_point = $ents.add_cpoint @pt1.position
@first_click = true
end
end
def draw view
if @first_click && @pt2.valid?
view.set_color_from_line @pt1.position, @pt2.position
view.line_width = 3
view.draw_line @pt1.position, @pt2.position
end
end
# Draw the sphere
def draw_sphere
# Draw the circles
rad = @pt1.position.distance @pt2.position
circle = $ents.add_circle @pt1.position, [1, 0, 0], rad
path = $ents.add_circle @pt1.position, [0, 1, 0], rad + 1
circle_face = $ents.add_face circle
# Extrude the sphere and erase the extrusion path
circle_face.followme path
$ents.erase_entities path
reset
end
# Return to original state
def reset
@pt1.clear
@pt2.clear
@first_click = false
end
# Respond when user presses Escape
def onCancel flags, view
reset
end
end
# Create the Command object
sphere_cmd = UI::Command.new("Sphere") {
Sketchup.active_model.select_tool SphereTool.new
}
# Configure the Command's appearance
sphere_cmd.small_icon = "sphere_small.gif"
sphere_cmd.large_icon = "sphere_large.gif"
sphere_cmd.tooltip = "Create a sphere"
# Create and configure the Toolbar
sphere_toolbar = UI::Toolbar.new "Sphere"
sphere_toolbar.add_item sphere_cmd
sphere_toolbar.show
when inserting the dynamic component it would be like the rectangle tool. Please if someone can give me an idea of how to modify the code of the sphere to adapt it to what I am looking for. Thank you very much!