Change visibility from entities from LayersObserver

Hi,

I am trying to set the visibility for entities from a LayersObeserver.
So if the tags changes to invisible, is sets some entities, with some logic, to invisible.

If the code is run from the LayersObserver, the entities seem to flicker. It looks like the entities changed to invisible, and back to visible.
I reduced the code to it’s simplest form it still does produces the same effect.t

If I run the code from the onLayerChanged in the console it behaves as expected.

What am I doing wrong ?

module Mories
    module MTags

        class MyLayersObserver < Sketchup::LayersObserver

            def onLayerChanged(layers, layer)
                @model = Sketchup.active_model
                @entities = @model.entities
                
                @entities.each do |e|
                    next unless e.is_a?(Sketchup::Drawingelement)
                    e.visible = false
                end
            end

        end #MyLayersObserver

        @layer_observer = MyLayersObserver.new if not @layer_observer
        Sketchup.active_model.layers.add_observer(@layer_observer)

    end # module MTags
end # module Mories  

What do you mean by this? How else can it be done?

The code you posted seams to be more or less okay* (at least no logical problem) and should hide all Drawingelements, if one of the layer visibility changed. I do not see “flicker”, on Windows OS, whatever it is means.


*My version will togle dwge visibility according the changed layer visibility … just to be able to play with it more easy…
(and Use two spaces per indentation level…)

module Mories
  module MTags

    class MyLayersObserver < Sketchup::LayersObserver

      def onLayerChanged(layers, layer)
        model = Sketchup.active_model
        
        model.entities.grep(Sketchup::Drawingelement) do |e|
          e.visible = layer.visible?
        end
      end

    end #MyLayersObserver

    @layer_observer = MyLayersObserver.new if not @layer_observer
    Sketchup.active_model.layers.add_observer(@layer_observer)

  end # module MTags
end # module Mories  

I don’t think you can hide the active layer. So, if the entities are on the active layer, perhaps this is the flicker you see?

Also, why would you wish to set the individual entity to hidden if the layer it is using is hidden?

1 Like

If the code from the onLayersChangend function are run from the console they hide the entities fine.

Thanks for the grep example. I’have seen various methods, grep, select, each. This looks clean.

I have just tested and Layer0 is still the active layer.

I am trying to replicate behavior that I saw where the visibility of entities is based on component attributes.
It almost works, except for the fact that the entities don’t hide :wink:

Thanks for looking into this.

I found it. It was another extension messing with the visibility.
It now works as expected.