The code above tries to lock the inference based on the current direction. This method calls when user presses Shift key. The puts value would be true when the current inference line is red or green, otherwise would be fasle even if the inference line is pink, and can not restrict the line direction when user moves mouse.
So why doese view.inference_locked? return false after view.lock_inference(start_ip, end_ip)?
How can I lock the line direction when the inference line to be pink?
The #lock_inference method …“with two arguments, it locks inference along an axis.”
That means the method can lock only if the desired direction parallel to one of the axes.
Thy something like this:
def my_lock_inference(view, direction, start_ip, end_ip)
# record current axes
orig_origin, orig_xaxis, orig_yaxis, orig_zaxis = view.model.axes.to_a
# set the model axes to arbitrary, where one of the axes
# parallel to the desired lock direction
view.model.axes.set(ORIGIN, *direction.axes)
# now, you can lock the inference
view.lock_inference(start_ip, end_ip)
# set back the axes
view.model.axes.set(orig_origin, orig_xaxis, orig_yaxis, orig_zaxis)
# return the status
status = view.inference_locked?
end
That approach modifies the model axes, which changes the model state and may require wrapping in an operation. I think it’s unnecessary just to lock the direction.
I see, thanks. I just want to create a tool like the original Line tool in Sketchup, the Line tool could lock the direction when the inference line color is pink.
Yes, once you create instance variables like @ip1 or @ip2, you can access them anywhere in the class.
Take a look at this quick demo tool I made with GPT:
class DemoLockInferenceTool
def initialize
@model = Sketchup.active_model
@ip = Sketchup::InputPoint.new
@ip1 = Sketchup::InputPoint.new
@ip2 = Sketchup::InputPoint.new
@state = 0
end
def activate
@model.active_view.lock_inference
end
def onMouseMove(flags, x, y, view)
@ip.pick(view, x, y, @ip1)
if @state == 0
@ip1.copy!(@ip)
else
@ip2.copy!(@ip)
end
view.invalidate
end
def onLButtonDown(flags, x, y, view)
if @state == 0
@state = 1
else
Sketchup.active_model.active_entities.add_line(@ip1.position, @ip2.position)
reset(view)
end
view.invalidate
end
def onKeyDown(key, repeat, flags, view)
if @state == 1
point = @ip1.position
# Lock inference direction based on arrow key
case key
when VK_UP, VK_DOWN
end_point = point.offset(@model.axes.zaxis)
ip = Sketchup::InputPoint.new(end_point)
view.lock_inference(ip, @ip1)
when VK_RIGHT
end_point = point.offset(@model.axes.xaxis)
ip = Sketchup::InputPoint.new(end_point)
view.lock_inference(ip, @ip1)
when VK_LEFT
end_point = point.offset(@model.axes.yaxis)
ip = Sketchup::InputPoint.new(end_point)
view.lock_inference(ip, @ip1)
when CONSTRAIN_MODIFIER_KEY
# Toggle lock/unlock
if view.inference_locked?
view.lock_inference
elsif @ip2.valid?
view.lock_inference(@ip2, @ip1)
end
end
view.invalidate
elsif key == CONSTRAIN_MODIFIER_KEY && @state == 0 && @ip1.valid?
view.lock_inference(@ip1)
view.invalidate
end
end
def onKeyUp(key, repeat, flags, view)
if key == CONSTRAIN_MODIFIER_KEY
view.lock_inference # unlock
end
end
def reset(view)
@ip.clear
@ip1.clear
@ip2.clear
@state = 0
view.lock_inference # unlock
end
def draw(view)
@ip.draw(view) if @ip.display?
if @ip1.valid? && @ip.valid?
view.line_width = view.inference_locked? ? 3 : 1
view.set_color_from_line(@ip1, @ip)
view.draw(GL_LINES, [@ip1.position, @ip.position])
end
end
end
# Activate the tool
Sketchup.active_model.select_tool(DemoLockInferenceTool.new)
def onMouseMove
@ip2.pick(view, x, y, @ip1)
if @shift
pt1 = get_position(@ip2)
pt2 = pt1.project_to_line(@current_line)
@ip2 = Sketchup::InputPoint.new(pt2)
end
end
def onKeyDown
if (key == CONSTRAIN_MODIFIER_KEY && repeat == 1)
view.lock_inference
@shift = true
@current_line = [@ip1.position, @ip1.position.vector_to(@ip2.position)] if @ip1.valid? && @ip2.valid?
end
end
This solution could lock to any direction, but it also has a flaw: when Shift pressed, there’s no inference line color any more, it always black by default.
What I mean is that this method can not use set_color_from_line any more. And if want to set pink color when parallel or perpendicular some edges, maybe should write lots of codes.
This solution does not act like the original Line Tool. It only show parallel/perpendicular when the first point(@ip1) is on a face. Original Line Tool has no such restriction, it can detect if there is a line parallel or perdicular with the current vector at almost any time.