How to get the location of my click?
I’m trying it:
But each click, the component do a different movement, same when i click at the same local.
I’m trying to get the point on X Y and Z at the clicked face.
# Método para alinhar o componente ou grupo com a face clicada
def align_component_to_face(face, x, y)
model = Sketchup.active_model
selection = model.selection
return unless selection.length == 1 # Precisamos ter apenas um componente ou grupo selecionado
component_instance = selection.first
bounding_box = component_instance.bounds
bottom_center = Geom::Point3d.new((bounding_box.min.x + bounding_box.max.x) * 0.5, (bounding_box.min.y + bounding_box.max.y) * 0.5, bounding_box.min.z)
# Posição do centro da face clicada
face_center = face.bounds.center
# Calculando o vetor de movimentação para alinhar o componente com a face
transformation_vector = face_center - bottom_center
# Movendo o componente para a posição da face
transformation = Geom::Transformation.translation(transformation_vector)
component_instance.transform!(transformation)
# Exibindo mensagem com a posição do clique do mouse e da face
click_point = Geom::Point3d.new(x, y, 0)
message = "Posição do clique: X=#{click_point.x}, Y=#{click_point.y}, Z=#{click_point.z}\n" +
"Posição da face: X=#{face_center.x}, Y=#{face_center.y}, Z=#{face_center.z}"
UI.messagebox(message)
end
# Definindo o manipulador de eventos para o clique do mouse
class AlignComponentTool
def onLButtonDown(flags, x, y, view)
model = Sketchup.active_model
# Usando o view.pick_helper para obter a inferência de pontos no clique
ph = view.pick_helper
ph.do_pick(x, y)
# Usando o view.inputpoint para obter a inferência de pontos exata
inputpoint = view.inputpoint(x, y)
if inputpoint && inputpoint.valid?
face = inputpoint.face
return unless face && !face.deleted? # Verifica se selecionou uma face válida
model.start_operation("Align Component to Face", true)
align_component_to_face(face, inputpoint.position.x, inputpoint.position.y)
model.commit_operation
else
UI.messagebox("Nenhum ponto de inferência detectado. Por favor, clique em uma posição válida.")
end
end
end
# Adicionando o manipulador de eventos ao SketchUp
Sketchup.active_model.select_tool(AlignComponentTool.new)