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

    Best way to delete entities

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 9 Posters 7.1k Views 9 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.
    • thomthomT Offline
      thomthom
      last edited by

      To delete one entity: use Entity.erase!

      entity = Sketchup.active_model.active_entities[0] entity.erase!

      To delete multiple:
      model = Sketchup.active_model entities = model.active_entities model.active_entities.erase_entities entities

      The reason you find, in your example, the only only delete some, is that you iterate the same collection of entities you erase from. So the indexes reshuffles without the .each method adjusting for it.
      To make it work, use the array of the Entities collection:
      model = Sketchup.active_model entities = model.active_entities entities.to_a.each { | entity| entity.erase! }
      this way you are working on a array copy of the entities collection which isn't affected when you erase entities. But when erasing multiple entities, use the previous example: Entities.erase_entities

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

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

        Thom is correct. It's a Ruby No-no to delete items from a container as you iterate through them.

        Clark Bremer
        http://www.northernlightstimberframing.com

        1 Reply Last reply Reply Quote 0
        • fredo6F Offline
          fredo6
          last edited by

          @xiobus said:

          Anyone know the best way to delete all the entities except for one layer? I also noticed when you do a foreach entity delete it does not remove them all here is the code

          model = Sketchup.active_model
          > entities = model.active_entities
          > entities.each { | entity| entities.erase_entities entity }
          

          You could take inspiration of the following code snippet

          
          model = Sketchup.active_model
          lst_del = model.entities.find_all { |e| e.layer != my_layer_to_keep }
          model.entities.erase_entities lst_del
          
          

          Be careful with that code, because the code is really 'destructive'.

          Note that it only check layer at first level. If there is a component or group at first level, which contains some geometry put on your leyer-to-keep, then it will be erased. To overcome this, you would have to loop on all components and groups in the model (there's no API method to get the list of entities on a given layer).

          Fredo

          1 Reply Last reply Reply Quote 0
          • M Offline
            MartinRinehart
            last edited by

            @unknownuser said:

            If there is a component or group at first level, which contains some geometry put on your leyer-to-keep, then it will be erased.

            As recursive code is fun, I was going to write something to solve this problem, but I thought better of it. Can anyone suggest an example of a sensible model that does not keep groups/components on a single layer with their entities?

            Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

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

              @martinrinehart said:

              @unknownuser said:

              If there is a component or group at first level, which contains some geometry put on your leyer-to-keep, then it will be erased.

              As recursive code is fun, I was going to write something to solve this problem, but I thought better of it. Can anyone suggest an example of a sensible model that does not keep groups/components on a single layer with their entities?

              Almost all 'properly' structured models have ALL base geometry on 'Layer0' and their containers - Groups or Component Instances - put on specific layers - so a group with its contents on different a layer id the NORM rather than the exception ?
              The only time you move base geometry onto a layer other than the Default [Layer0 et al] is if you are doing something complex like showing hiding bits in Scenes...

              TIG

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

                Almost all 'properly' structured models have ALL base geometry on 'Layer0'...[/quote]

                I agree. Is there a ruby out there that will "sanitize" you model to enforce this "rule"? It would ignore groups and comps, but any base geometry would be moced to layer0. CB.

                Clark Bremer
                http://www.northernlightstimberframing.com

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  Hi Clark,

                  There is such a plugin here:

                  http://forums.sketchucation.com/viewtopic.php?f=323&t=7638&hilit=default+layer

                  Hi

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

                    Excellent. Thanks Tig.

                    Clark Bremer
                    http://www.northernlightstimberframing.com

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

                      @daiku said:

                      Almost all 'properly' structured models have ALL base geometry on 'Layer0'...

                      I agree.
                      @unknownuser said:

                      Is there a ruby out there that will "sanitize" you model to enforce this "rule"? It would ignore groups and comps, but any base geometry would be moced to layer0. CB.

                      See this: http://forums.sketchucation.com/viewtopic.php?p=115749#p115749
                      copy/paste the code into a file called something like default_layer_geometry.rb in the Plugins folder...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • artmusicstudioA Offline
                        artmusicstudio
                        last edited by

                        [quote="thomthom"]To delete one entity: use Entity.erase!

                        entity = Sketchup.active_model.active_entities[0] entity.erase!

                        hi tig,

                        in my first ruby i have already managed it to copy one master-component (created with ruby at [0,0,0] and copying it, where ever i want.

                        after placing it in all places wanted, i would like

                        to DELETE this particular master-component.

                        i found your code:

                        entity = Sketchup.active_model.active_entities[0]
                        entity.erase!

                        but how can i define , which component is to be deleted? is there a name somewhere? or index, i could "memorize" while creating the master component? is the [0] maybe a place for a definition?

                        thanx a lot for helping!
                        stan

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

                          @Stan

                          How are you making the initial component instance?
                          How are you 'copying' it ?
                          To have done that you must have had a 'reference' to its definition - let's say it's called ' defn'...
                          To find that definition's first instance and then to delete it use:
                          defn.instances.first.erase!

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • artmusicstudioA Offline
                            artmusicstudio
                            last edited by

                            hi tig,
                            probably i have a mistake in the component-description.

                            i define a master component (two tubes).

                            #DEF PFOSTEN MASTER COMPONENT

                               model = Sketchup.active_model
                               entities = model.active_entities  
                            
                               pfm1 = [0, 0, 0+thickness]
                               pfm2 = [0, 0, 0+thickness+sl-((5/faktor)/100)]
                               pfm3 = [0, 0, 0+thickness+sl-((5/faktor)/100)]
                               pfm4 = [0, 0, 0+thickness+sl]        
                               
                               group = entities.add_group
                               
                               entities2 = group.entities
                                      
                               new_line = entities2.add_line pfm1, pfm2
                                            length = new_line.length
                                            centerpoint = pfm1
                                            vector = pfm2
                                            vector = vector.normalize!
                                             
                                            edges = entities2.add_circle centerpoint, vector, pradius
                                            kreis = entities2.add_face edges
                                            kreis.pushpull length
                                 
                                 
                               new_line2 = entities2.add_line pfm3, pfm4
                                            length2 = new_line2.length 
                                            centerpoint2 = pfm4
                               	            vector2 = pfm3
                                        vector2 = vector.normalize!
                            		                     
                            	    edges2 = entities2.add_circle centerpoint2, vector2, pradius/3
                            	    kreis2 = entities2.add_face edges2
                                            kreis2.pushpull -length2
                                            
                               masterpfostenl = group.to_component
                            

                            and then copy it to different places to ponits pf1,2,3:

                            componentinstance =
                            entities.add_instance(masterpfostenl.definition, pf1)
                            componentinstance =
                            entities.add_instance(masterpfostenr.definition, pf3)
                            componentinstance =
                            entities.add_instance(mittelpfostenm.definition, pf5)

                            after all components are placed, i will try the

                            defn.instances.first.erase! (have still to find out, how to point the right "instances-group"

                            thanx
                            stan

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

                              You have got it !

                              defn = masterpfostenl.definition

                              The masterpfostenl is a reference to the first 'instance' of the new 'definition'...

                              Then see my previous example...

                              BUT to delete it it use masterpfostenl.erase!
                              Because you already have a reference to it, which you have set up early on...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • artmusicstudioA Offline
                                artmusicstudio
                                last edited by

                                yes sir,
                                it works perfectly. so this deletes the master-component.
                                i just wonder, how then can be identified the ,daughter'-components.

                                per x,y,z ?

                                anyway, thank you very much for this solution.
                                stan

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

                                  @stan

                                  Assuming 'defn' is a reference to the component's definition then
                                  defn.instances
                                  gives you a list of all current instances, in the order of their creation.
                                  let's say you want to find the first one [remember that arrays start at 0 NOT 1 !]
                                  instance = defn.instances[0]
                                  To find out where it is in its current context use
                                  point = instance.transformation.origin
                                  There are also other transformation properties available to get the orientation of the objetc's x/y/zaxis too...
                                  There are also some methods available to get rotations, scaling etc from the instance.transformation search for them - here's an example... http://sketchucation.com/forums/viewtopic.php?p=411972#p411972

                                  TIG

                                  1 Reply Last reply Reply Quote 0
                                  • Al HartA Offline
                                    Al Hart
                                    last edited by

                                    I couldn't get this to work:

                                    
                                    model = Sketchup.active_model
                                    entities = model.active_entities
                                    model.active_entities.erase_entities entities
                                    
                                    

                                    I get thi error:

                                    Error: #<TypeError: wrong argument type (expected Sketchup::Entity)>
                                    <main>:in `erase_entities'

                                    But I was able to get this to work: (adding .to_a which probably serves to isolate the entities array)

                                    
                                    model = Sketchup.active_model
                                    entities = model.active_entities
                                    model.active_entities.erase_entities entities.to_a
                                    
                                    

                                    Al Hart

                                    http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
                                    IRender nXt from Render Plus

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

                                      The entities.erase_entities(array)
                                      Expects an array NOT an entities collection, so adding .to_a resolves that.
                                      BUT entites.clear! is even easier.

                                      TIG

                                      1 Reply Last reply Reply Quote 0
                                      • Al HartA Offline
                                        Al Hart
                                        last edited by

                                        great - that's what I was looking for anyway

                                        Al Hart

                                        http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
                                        IRender nXt from Render Plus

                                        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