• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Best way to delete entities

Scheduled Pinned Locked Moved Developers' Forum
19 Posts 9 Posters 7.1k Views
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.
  • X Offline
    xiobus
    last edited by 15 Dec 2009, 22:35

    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 }
    
    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 15 Dec 2009, 22:42

      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
      • D Offline
        daiku
        last edited by 16 Dec 2009, 14:54

        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
        • F Offline
          fredo6
          last edited by 17 Dec 2009, 21:51

          @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 19 Dec 2009, 15:50

            @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
            • T Offline
              TIG Moderator
              last edited by 19 Dec 2009, 21:13

              @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
              • D Offline
                daiku
                last edited by 21 Dec 2009, 14:38

                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 21 Dec 2009, 14:58

                  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
                  • D Offline
                    daiku
                    last edited by 22 Dec 2009, 14:48

                    Excellent. Thanks Tig.

                    Clark Bremer
                    http://www.northernlightstimberframing.com

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 22 Dec 2009, 15:14

                      @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 6 Oct 2013, 15:59

                        [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
                        • T Offline
                          TIG Moderator
                          last edited by 6 Oct 2013, 16:09

                          @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 6 Oct 2013, 17:06

                            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
                            • T Offline
                              TIG Moderator
                              last edited by 6 Oct 2013, 18:19

                              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 7 Oct 2013, 10:50

                                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
                                • T Offline
                                  TIG Moderator
                                  last edited by 7 Oct 2013, 11:04

                                  @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 8 Oct 2014, 23:05

                                    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.com/images/e/ef/Render_plus_colored30x30%29.PNG
                                    IRender nXt from Render Plus

                                    1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      TIG Moderator
                                      last edited by 13 Oct 2014, 15:58

                                      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 13 Oct 2014, 21:40

                                        great - that's what I was looking for anyway

                                        Al Hart

                                        http://wiki.renderplus.com/images/e/ef/Render_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