How to toggle visibility of faces in nested groups?

As mentioned in the following post I’m trying to find a way to toggle visibility of faces in nested groups: Alternative method of moving/snapping objects - Point-to-Point - #45 by basbva

I found a script by TIG that, with some adjustment, toggles faces between the current visible state and hidden state. What I’ve been trying to achieve next was to first gather all faces in a group, for which I found and adjusted a method found here: Count the total number of faces in a model of a really complex model? - #20 by john_drivenupthewall

Both of these methods are given below.

This toggles faces of active_entities:

module SUForum
    module FaceVisibility
        extend self
        VERSION ||= "0.0.3"
        MENU ||= "View"
        
        #based on: https://community.sketchucation.com/post/1323194
        def tigs_facevisibility()
            model=Sketchup.active_model
            ants=model.active_entities

            unless @visible and @visible[0] ### hide and remember
                @visible=[]
                ants.each{|e|@visible << e if e.is_a? Sketchup::Face and e.visible?}
                @visible.each{|e|e.visible=false} if @visible[0]
                return 'invisible'
                else ### show and forget
                @visible.each{|e|e.visible=true if e.valid?}
                @visible=[]
                return 'visible'
            end
        end
        
        unless defined?(@ui_loaded)
            add_separator_to_menu(MENU)
            menu = UI.menu(MENU).add_item("Toggle Faces") { tigs_facevisibility() }
            @ui_loaded = true
        end
    end
end

And this gathers an array of ‘entity’s’:

              def a_a( entity )
                  a = []
                  entity.each { |e|
                      if e.is_a?( Sketchup::Face )
                          a << e
                      elsif e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group )
                          a.concat( a_a( e.definition.entities ))
                      end
                      }
                  return a
                  end
              
              puts a_a( Sketchup.active_model.selection )

As illustrated here:

The problem seems to me that the method of TIG doesn’t respond to this last method, because it responds to a collection of entities but does not to an array of entities such as the last method returns.

Are there any experienced coders who could give me a clue as to how I could gather this collection of entities within a set of nested groups, to which the first mentioned method would respond.

Any help would be greatly appreciated.

cca.


module SUForum
  module FaceVisibility
    extend self
    VERSION ||= "0.0.4"
    MENU ||= "View"
    
    #based on: https://community.sketchucation.com/post/1323194
    def tigs_facevisibility()
      model = Sketchup.active_model
      selection = model.selection
      faces = collect_faces( selection )

      model.start_operation("Toggle Faces", false)
      unless @visible and @visible[0] ### hide and remember
        @visible=[]
        faces.each{|e| @visible << e if e.visible?}
        @visible.each{ |e| e.visible=false } if @visible[0]
        state = @visible[0] ? 'invisible' : 'no previous selection'
      else ### show and forget
        @visible.each{ |e| e.visible=true if e.valid? }
        @visible = []
        state = 'visible'
      end
      model.commit_operation
      puts state
    end
    
    def collect_faces( ents )
      a = []
      ents.each { |e|
        if e.is_a?( Sketchup::Face )
          a << e
        elsif e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group )
          a.concat( collect_faces( e.definition.entities ))
        end
      }
      a
    end
    
    unless defined?(@ui_loaded)
        add_separator_to_menu(MENU)
        menu = UI.menu(MENU).add_item("Toggle Faces") { tigs_facevisibility() }
        @ui_loaded = true
    end
  end
end
3 Likes

That’s brilliant. You don’t want to know how long I’ve been struggling to sort this out by meself.

Are you just wanting to show wireframe rendering in those groups or what ?

1 Like

I think there are more opportunities for this simple ruby based on some simple code snippets that some may have forgotten.

Replacing ‘selection’ by ‘active_entities’ in Dezmo’s code, also offers interesting horizons to explore.

I had a look at your format for a plugin. It made me wonder, isn’t it possible to make a very simple format for open source freeware plugins?

I prefer to stay on the subject of face visibility in this topic. (Guide: Keep It Tidy)


Why did you not wish to simply hide the top level group ? (Everything nested would then be hidden.)

We so far managed to hide faces, creating a wire frame structure locally. So a difficult object of choice could be selected. Next we would like to hide all, until the object is placed in its new position, as the OP is asking here:

Sorry Dan, I need to study this.

Would we’ve been able to select the point we did here, thanks to TIG’s snippet?

Okay. Just making sure you have a good reason.

However, the Styles toolbar has a Wireframe render mode for the entire model.
With Ruby you can set it like:

Sketchup.active_model.rendering_options["RenderMode"]= 0

… or …

Sketchup.send_action('renderWireframe:')
1 Like