sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [Code] layers.purge_unused

    Scheduled Pinned Locked Moved Developers' Forum
    18 Posts 6 Posters 2.3k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • chrisglasierC Offline
      chrisglasier
      last edited by

      @unknownuser said:

      It works with groups for me.

      It doesn't seem to discriminate. Layers generally comprise components and groups. Each entity and its layer is sent to a javascript array and that seems an OK mixture

      @unknownuser said:

      I suspect that the bug is in the way you deal with loose geometry.

      I think my comment about loose geometry is a bit of a red herring as I did not do anything with it before the purge. Anyway, any loose geometry is in layer0 as Gaieus' dictum.

      @unknownuser said:

      Do you change layer of all entities in component definitions and inside groups?

      No just top level as above. I did try active_entities but that did not make any difference (I never really understood the active bit). Perhaps you could expand a little on what you had in mind about this.

      Many thanks

      Chris

      With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        @chrisglasier said:

        No just top level as above. I did try active_entities but that did not make any difference (I never really understood the active bit). Perhaps you could expand a little on what you had in mind about this.

        model.entities - is always the top level - regardless.
        model.active_entities - is the entities of the open group/component.

        After you run your script, and some layers remain - do they disappear when you do a purge from SU?

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • chrisglasierC Offline
          chrisglasier
          last edited by

          @thomthom said:

          model.entities - is always the top level - regardless.
          model.active_entities - is the entities of the open group/component.

          OK goddit thanks

          @thomthom said:

          After you run your script, and some layers remain - do they disappear when you do a purge from SU?

          No. I also tried saving the file and opening it again (in case something got stuck!!).

          Perhaps ... ?

          Chris

          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

          1 Reply Last reply Reply Quote 0
          • Didier BurD Offline
            Didier Bur
            last edited by

            Hi,
            Just an πŸ’­ : as groups are stored like components, maybe that changing the layer of a group doesn't change it's internal definition ?
            Check for internal compos definitions and scan their entities to see if they are on their original layer. In such a case, SU doesn't allow to delete the layer.

            DB

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              Your code only modifies groups/components to be placed on layer 0. It assumes that all geometry is also placed on layer 0.

              Seeing how Tomasz reports it working for him I'm guessing there is some entity that's not located on layer 0. If you turn on Color By Layer (ensuring edges are coloured by material as well) - see if you see anything with a colour other than what layer 0 is set to.

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @didier bur said:

                Hi,
                Just an πŸ’­ : as groups are stored like components, maybe that changing the layer of a group doesn't change it's internal definition ?
                Check for internal compos definitions and scan their entities to see if they are on their original layer. In such a case, SU doesn't allow to delete the layer.

                Ah yea! iterating over the top level entities won't do it. In addition all model.definitions has to be iterated. that's it!

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  In fact, since you only want to modify groups components you don't have to check the top level at all.

                  Just scan the definition array for Group/Component definitions (ignoring Image definitions) and loop over the instances for each definition.

                  Thomas Thomassen β€” SketchUp Monkey & Coding addict
                  List of my plugins and link to the CookieWare fund

                  1 Reply Last reply Reply Quote 0
                  • chrisglasierC Offline
                    chrisglasier
                    last edited by

                    Thanks all ... some overnight sleuthing is required ...

                    With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      I think this will work:

                      
                      model = Sketchup.active_model
                      layers = model.layers
                      
                      model.definitions.each { |d|
                      
                      	# Ignore Images and definitions with no instances
                      	next if d.image? || d.count_instances == 0
                      	# Set Group/ComponentInstance to layer0
                      	d.instances.each { |i| i.layer = layers[0] }
                      
                      }
                      
                      layers.purge_unused
                      
                      UI.show_inspector("Layers") 
                      
                      

                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • chrisglasierC Offline
                        chrisglasier
                        last edited by

                        @thomthom said:

                        I think this will work: ...

                        Yes thanks it does reduce the layer count from 23 to 20, of which 12 are correctly retained for non real world replica dimensions and labels.

                        But I have to do more sleuthing ...

                        Chris

                        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                        1 Reply Last reply Reply Quote 0
                        • chrisglasierC Offline
                          chrisglasier
                          last edited by

                          @chrisglasier said:

                          But I have to do more sleuthing ...

                          Here are the results so far:

                          I deleted all loose geometry

                          The total number of groups and components matches the javascript array indicating all have been passed to layer0 using Thomas' definition loop (thanks again for that).

                          Each entity and any children in the expanded outliner view returns layer0 in entity info.

                          But see this:

                          Layer.png

                          The selected column shows in its original layer colour and can be turned on and off with the original layer checkbox - yet the entity info shows layer0. And if I make the columns layer active and hide layer0, nothing is displayed or indicated in th outliner. The same is true for the remaining seemingly "unpurgable" layers.

                          What think?

                          Chris

                          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                          1 Reply Last reply Reply Quote 0
                          • chrisglasierC Offline
                            chrisglasier
                            last edited by

                            I just noticed that there are 7 section planes -

                            Layer02.png

                            the same number as the unpurgable layers. I don't know much about these. Perhaps it is just coincidental?

                            Chris

                            With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                            1 Reply Last reply Reply Quote 0
                            • chrisglasierC Offline
                              chrisglasier
                              last edited by

                              @chrisglasier said:

                              I just noticed that there are 7 section planes - Perhaps it is just coincidental?

                              Yes it is - all section planes are on layer0 (obvious to me now).

                              Here is the current code:

                              
                              
                              @dlg.add_action_callback("sceneLayersPurge") {|d, p|
                              
                              model = Sketchup.active_model
                              layers = model.layers
                              entities = model.entities
                              
                              array = []
                              a = 0
                              
                              model.definitions.each { |d|
                              
                                 # Ignore Images and definitions with no instances
                                 next if d.image? || d.count_instances == 0
                                 # Set Group/ComponentInstance to layer0
                                 d.instances.each { |i| 
                              
                                 
                               if i.class == Sketchup;;Group 
                              
                              name = i.name
                              
                              end
                              
                              if i.class == Sketchup;;ComponentInstance 
                              
                              name = i.definition.name
                              
                              end
                              
                              original = i.layer.name
                              
                              i.layer = layers[0] 
                              
                              array[a] = [name,original,i.layer.name]
                              array[a] = array[a].join(",")
                              a = a + 1   }  }
                              
                              
                              layers.purge_unused
                              
                              UI.show_inspector("Layers") 
                              
                              
                              array = array.join(";")
                              
                              
                              cmd = "sceneLayersPurgeReceive('#{array}');"
                              @dlg.execute_script (cmd)     }
                              
                              
                              

                              and here are samples from the js report showing name, original layer, final layer

                              Layers034.png

                              I'm running out of sleuthing ideas!

                              Chris

                              With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                              1 Reply Last reply Reply Quote 0
                              • daikuD Offline
                                daiku
                                last edited by

                                Have you tried manually making the problem layers invisible? If so, does anything disappear? Also, try manually deleting the layer. If it's truly empty, SU will quietly remove it. If it's not empty, SU will warn you, and ask where you want to move the things. CB.

                                Clark Bremer
                                http://www.northernlightstimberframing.com

                                1 Reply Last reply Reply Quote 0
                                • Chris FullmerC Offline
                                  Chris Fullmer
                                  last edited by

                                  Can you supply the model you are working with? It might help figure it out faster.

                                  Chris

                                  Lately you've been tan, suspicious for the winter.
                                  All my Plugins I've written

                                  1 Reply Last reply Reply Quote 0
                                  • chrisglasierC Offline
                                    chrisglasier
                                    last edited by

                                    Thanks all. I need to rethink this before asking people to venture further down a blind alley.

                                    I can reduce a Sketchup up model to two plain text lists. One lists components and groups and the other lists scenes. Both lists are turned into javascript multi-level arrays.

                                    But I don't want to wipe out any established relationships between components and layers, layers and scenes, scenes and styles and scenes and property settings. I thought I could do that by manipulating the model but I am thinking it is probably better to capture the data and regenerate the whole thing.

                                    If you are interested in why I want to do this please see the discussion in the Modelur thread.

                                    Thanks and regards

                                    Chris

                                    With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                                    1 Reply Last reply Reply Quote 0
                                    • 1 / 1
                                    • First post
                                      Last post
                                    Buy SketchPlus
                                    Buy SUbD
                                    Buy WrapR
                                    Buy eBook
                                    Buy Modelur
                                    Buy Vertex Tools
                                    Buy SketchCuisine
                                    Buy FormFonts

                                    Advertisement