How to hide unselected groups?

I’m tring to create a code thats hide only unselected groups. But, when my selected groups is inside other groups, it’s been hidden too, this doesn’t work. i need to hidde all groups that’s unselected.

require 'sketchup'

module R3D_ViewToDetail
  def self.show_selected_object
    # Verifica se há algo selecionado
    selection = Sketchup.active_model.selection
    if selection.empty?
      UI.messagebox("Nenhum objeto selecionado.")
      return
    end

    # Obtém o ID do objeto selecionado
    selected_object = selection[0]
    selected_object_id = selected_object.entityID

    # Pergunta o nome do objeto selecionado
    input = UI.inputbox(["Digite o nome do objeto selecionado:"], ["Nome do Objeto"], [selected_object.name])
    if input
      selected_object.name = input[0]
    end

    # Oculta todos os objetos da cena
    model = Sketchup.active_model
    entities = model.entities
    entities.each do |entity|
      next if entity.is_a?(Sketchup::Edge) # Pula as arestas
      entity.hidden = true
    end

    # Torna o objeto selecionado visível
    selected_entity = entities.find { |entity| entity.entityID == selected_object_id }
    selected_entity.hidden = false if selected_entity
  end
end

# Adiciona uma entrada de menu para o plugin
menu = UI.menu("Plugins")
menu.add_item("R3D_ViewToDetail") { R3D_ViewToDetail.show_selected_object }

Your code assumes that a group will be selected and the object will respond to a #name call without verifying that the selected entity is indeed a group.

    # Obtém o ID do objeto selecionado
    selected_object = selection[0]
    unless selected_object.respond_to(:definition)
      UI.messagebox("O grupo deve ser selecionado.")
      return
    end
    selected_object_id = selected_object.entityID

Because you are brute force hiding everything at the top level of the model using model.entities().

You should be leveraging the model.active_entities() and model.active_path() methods.

Your loop (as quoted) will also hide faces, section planes, dimensions, etc.


If you wish to hide only groups, then grep them …

    edit_path = model.active_path
    model.entities.grep(Sketchup::Group) do |group|
      next if group == selected_object
      next if edit_path && edit_path.include?(group)
      group.hidden = true
    end
    if model.active_entities != model.entities
      edit_path.each do |object|
        entities = object.definition.entities
        entities.each do |object|
          next unless object.respond_to?(:definition)
          next if edit_path.include?(object)
          object.hidden = true
        end
      end
    end

my doubts is: Why when i try to hide a individual group its works as well… but, if my groups is inside other group, this doesn’t works

I think I gave you the how it should work in the 2 posts I posted above …

:thinking: