module Reglue
extend self
# Menü öğesinin eklenme konumunu belirleyen değişken
SUPPORT_MENU_POSITION ||= Sketchup::Menu.instance_method(:add_item).arity != 1 ? 8 : nil
def glue_components_to_faces
model = Sketchup.active_model
selection = model.selection
if selection.empty?
UI.messagebox("Hata: Hiçbir seçim yapılmadı!")
return
end
faces = selection.grep(Sketchup::Face)
components = selection.grep(Sketchup::ComponentInstance).select { |comp| comp.glued_to.nil? }
if faces.empty? || components.empty?
UI.messagebox("Hata: Lütfen hem yüzeyleri hem de yapıştırılabilir bileşenleri seçin!")
return
end
model.start_operation("Bileşenleri Yüzeylere Yeniden Yapıştır", true)
components.each do |component|
closest_face = find_closest_aligned_face(component, faces)
component.glued_to = closest_face if closest_face
end
model.commit_operation
end
def find_closest_aligned_face(component, faces)
component_normal = component.transformation.zaxis
faces.min_by { |face| face.normal.angle_between(component_normal) }
end
unless @loaded
UI.add_context_menu_handler do |menu|
selection = Sketchup.active_model.selection
next if selection.empty?
has_faces = selection.any? { |entity| entity.is_a?(Sketchup::Face) }
has_glueable_components = selection.any? { |entity| entity.is_a?(Sketchup::ComponentInstance) && entity.glued_to.nil? }
if has_faces && has_glueable_components
position = SUPPORT_MENU_POSITION
menu.add_item("Reglue", position) { glue_components_to_faces }
end
end
@loaded = true
end
end