Hi All, I am trying to create a Leader Text tool that has the ability to snap start point and end points when I want to take notes. Here is the Ruby code I wrote but it can only snap the point but no Text yet, I hope to get some help from everyone. Thanks All,
The code is here,
require 'Sketchup.rb'
# Create a class for the tool
class Leadertext
# Create a method for handling points
def activate
# Initialize variables by clicking the mouse
@ip1 = Sketchup::InputPoint.new
@ip2 = Sketchup::InputPoint.new
@mouse_ip = Sketchup::InputPoint.new
@picked_first_ip = false
@picked_second_ip = false
end
# Create a method for marking elements that need to be redrawn when the tool is deactivated
def deactivate(view)
view.invalidate if view
end
# Create a method for marking elements that need to be redrawn when the tool is resumed after being paused
def resume(view)
view.invalidate if view
end
# Create a method for deactivating the tool when the user wants to exit the command, typically by pressing the Esc key
def onCancel(reason, view)
Sketchup.active_model.select_tool(nil)
end
# Create a method for updating the position of the mouse and displaying tooltip information when the user moves the mouse on the Sketchup interface
def onMouseMove(flags, x, y, view)
@mouse_ip.pick(view, x, y)
view.tooltip = @mouse_ip.tooltip if @mouse_ip.valid?
view.invalidate
end
# Create a method for determining the positions of the selected points when the user clicks the left mouse button to start the text creation method using the tool
def onLButtonDown(flags, x, y, view)
# Select the first point
if not @picked_first_ip
@ip1.pick(view, x, y)
@picked_first_ip = true
# Select the second point
elsif not @picked_second_ip
@ip2.pick(view, x, y)
@picked_second_ip = true
create_text
Sketchup.active_model.select_tool(nil)
end
view.invalidate
end
#
def draw(view)
# Draw the position of the mouse
if @mouse_ip.valid?
x, y = @mouse_ip.position
view.draw_points([x, y], 10, 1, "red")
end
# Draw a line connecting the two points
if @picked_first_ip
view.set_color_from_line(@ip1, @ip2)
view.draw(GL_LINE_STRIP, [@ip1.position, @ip2.position])
end
end
# Create a method for creating text
def create_text
# Create a variable for the starting point of the leader text
start_point = Geom::Point3d.new(@ip1)
# Create a vector to orient the leader text
vector = Geom::Vector3d.new(@ip2)
# Create a Text object to hold the text of the leader text
text = Sketchup.active_model.entities.add_text("Hello World", start_point, vector)
# Set properties for the leader text, such as line weight and color
text.arrow_type = 2 # 0 for none, 2 for dot, 3 for closed arrow, 4 for open arrow
text.line_weight = 1 # This method sets line weight in pixels
end
end
# Create a menu and tool
UI.menu("Plugin").add_item("Leader Text") {
Sketchup.active_model.select_tool(Leadertext.new)
}