sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Selectionobserver

    Scheduled Pinned Locked Moved Developers' Forum
    13 Posts 4 Posters 697 Views 4 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.
    • K Offline
      kiesewetter
      last edited by

      Hallo!

      I tried to write a script that displays in the ruby window the name of the layer the entity is arranged at everytime it gets selected. After that the entity should be erased. I used the folling code an it didn't work...

      class Selbserver < Sketchup;;SelectionObserver
      
      
      def onSelectionAdded(sel)
      	
      	sel.each{|e|
      	           layer=e.layer.name
      			   puts layer
      			   e.erase!
      			 }
      			 
      end
      
      
      sel=Sketchup.active_model.selection
      sel.add_observer Selbserver.new
      
      #to load it
      #load "C;/Users/tim/Desktop/observertest.rb"
      
      
      
      end
      

      i have no idea, why nothing happens when i load the script an select an entity after that.

      thanks for your time.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        You can't change the entities in a selection as you refer to it [like erasing them]!
        You could try sel**.to_a**.each{|e|... as that would take a clone of it as an array...... but no promises it'll work though...

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @kiesewetter said:

          I tried to write a script that displays in the ruby window the name of the layer the entity is arranged at everytime it gets selected. After that the entity should be erased.

          Sketchup::SelectionObserver#onSelectionAdded() callback is NOT IMPLEMENTED. Use onSelectionBulkChange instead.

          I think it's best if the entity is removed from the selection set (not erased from the model.)

          module Kiesewetter
          
            @@sel_spy = nil
          
            class Selbserver < Sketchup;;SelectionObserver
          
              def onSelectionBulkChange(sel)
                sa = sel.to_a
                sa.each{|e|
                  puts("#{e.class.name};#{e.object_id} on layer; #{e.layer.name}")
                  sel.remove(e)
                }
              end # def callback
          
            end #class
          
            unless @@sel_spy
              @@sel_spy = Selbserver.new()
              Sketchup.active_model.selection.add_observer( @@sel_spy )
            end
          
            #to load it
            #load "C;/Users/tim/Desktop/observertest.rb"
          
          end # module
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            As Dan says, make a list of the selected items, remove them from the selection and then erase them ?

            I hadn't spotted the method you used - .onBulk... is the one to use - the others are foobar...

            TIG

            1 Reply Last reply Reply Quote 0
            • K Offline
              kiesewetter
              last edited by

              first of all, thank you guys.
              ok, the ...BulkChange observer responds.
              is it possible to delete instead of the entities in the selection every entity on a certain layer?

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                model=Sketchup.active_model layers=model.layers layername="Layer1"

                you decide which layer to use!

                model.active_entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layers[layername] }

                note how we make an array [.to_a] of the entities -

                you can't work on the entities directly AND change it - weird results will happen!

                the active_entities erases just things in that entities collection -

                that need not be model.entities, but could be group.entities or even definition.entities

                the e.valid? test avoids a crash -

                e.g. if you've erased an edge so its face no longer exists when we come to look at it!

                TIG

                1 Reply Last reply Reply Quote 0
                • K Offline
                  kiesewetter
                  last edited by

                  works with the faces, but all the lines aren't erased. any idea why?

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

                    @tig said:

                    model=Sketchup.active_model layers=model.layers layername="Layer1"

                    you decide which layer to use!

                    model.active_entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layers[layername] }

                    note how we make an array [.to_a] of the entities -

                    you can't work on the entities directly AND change it - weird results will happen!

                    the active_entities erases just things in that entities collection -

                    that need not be model.entities, but could be group.entities or even definition.entities

                    the e.valid? test avoids a crash -

                    e.g. if you've erased an edge so its face no longer exists when we come to look at it!

                    It'd be faster to fetch the reference to the layer you want to erase from before the loop. Retrieving a reference to a layer by looking up a string reference is slower.

                    <span class="syntaxdefault">model&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />layer&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'Layer1'</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">  e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!</span><span class="syntaxdefault"> if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layer&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxdefault">layer<br /></span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span>
                    

                    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

                      @kiesewetter said:

                      works with the faces, but all the lines aren't erased. any idea why?

                      The edges aren't on the same layer?

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

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        They are not on the layer ?
                        To find an object's layer Select it and open Entity Info...
                        The code snippet I offered erases everything on a particular layer - it is not careful about things like if the edge goes so does its face even if that face is NOT on that layer...
                        It was a simplistic example - you can add all sorts of checks too.
                        If you only want to layer faces but erase their edges then use something like...
                        Firstly change it to
                        layer=layers[layername] ### as tt recommends...

                        then inside the 'each' loop...

                        ...

                        if e.class==Sketchup;;Face and e.layer==layer
                          edges=e.edges
                          e.erase!
                          edges.each{|ed| ed.erase! if not ed.faces[0] }
                        end
                        

                        ...
                        which erases the face if it's on the specified layer... AND having made a list of that face's edges, it erases any of them that are now 'faceless' as a result...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • K Offline
                          kiesewetter
                          last edited by

                          the edges were on the same layer (tested it with test=e.layer; puts test)

                          could it be that lines only can be deleted, if they don't border a face?

                          i executed the script to delete the entities twice, in the first step only the faces were deleted. in the second step the lines were erased.

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

                            @kiesewetter said:

                            could it be that lines only can be deleted, if they don't border a face?

                            Erasing an edge will also erase any connected faces.

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

                            1 Reply Last reply Reply Quote 0
                            • K Offline
                              kiesewetter
                              last edited by

                              hmmm... ok. in this case i don't understand why the lines behaved like that. not that important.
                              ok, thanks to you once again. 😄

                              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