Drawingelement#visible? seems to return inconsistent results

I wrote a post for a similar problem but not in the right discussion

I also have a problem with whether an entity is visible or not in a scene (page)

to find out, it seems that there are two methods for this, Sketchup::active_model.pages.selected_page.hidden_entitie and Sketchup::active_model.pages.selected_page.get_drawingelement_visibility(entity)

The hidden_entities method only gives entities at the root, so the “planches toit” group is not in the table of hidden entities, and get_drawingelement_visibility for the component “planche toit terrasse” return “true”

So, we are forced to go through the entire tree of entities to find out if an element is visible or not.

but it’s very heavy to do this in a loop to test the visibility of each entity

One solution I found was to rebuild an array of all hidden entities in the active page (only group and component) and use a function that use this array to know if an entity is visible or not

here the code to rebuild $hiddenIdentities array

def getAllHiddenEntities
  $hiddenEntities = []
  listHiddenEntities(Sketchup.active_model.entities)  
end

def listHiddenEntities(entities, visible = true)
  entities.each { | ent | 
    if ent.is_a?(Sketchup::ComponentInstance) || ent.is_a?(Sketchup::Group)
      if !ent.visible? || !visible
        setHiddenEntity(ent)
      end
    end
    if ent.is_a?(Sketchup::Group)
      listHiddenEntities(ent.entities, ent.visible? & visible)
    end
  }  
end

def setHiddenEntity (entite)
  if !$hiddenEntities.include?(entite)
      $hiddenEntities.push(entite)
  end
end

and when you want to know if a group or a component is visible or not: (sorry is not very elegant, i’m beginner in ruby)

def entityActive?(ent) 
  if($hiddenEntities && $hiddenEntities.include?(ent)) 
    res = false
  else
    res = true
  end
  return(res);
end