@designerbursa ```
module GlueComponentsToFaces
extend self
def glue_components_to_faces
model = Sketchup.active_model
selection = model.selection
if selection.empty?
return UI.messagebox("Error: No selection! Please select faces and components.")
end
faces = selection.grep(Sketchup::Face)
components = selection.grep(Sketchup::ComponentInstance)
if faces.empty? || components.empty?
return UI.messagebox("Error: Select faces and components!")
end
model.start_operation("Glue Components to Faces", true)
begin
components.each do |component|
closest_face = find_closest_aligned_face(component, faces)
component.glued_to = closest_face if closest_face
end
rescue => e
UI.messagebox("An error occurred: #{e.message}")
ensure
model.commit_operation
end
end
def find_closest_aligned_face(component, faces)
component_normal = component.transformation.zaxis
closest_face = faces.min_by { |face| face.normal.angle_between(component_normal) }
closest_face
end
end
# Adding a menu item for ease of use in SketchUp
if not file_loaded?(__FILE__)
UI.menu("Plugins").add_item("Glue Components to Faces") {
GlueComponentsToFaces.glue_components_to_faces
}
file_loaded(__FILE__)
end
### I haven't tested it on the 2024 version but you can try this, maybe it will work. It works smoothly on my 2021 version