sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Problems with script

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 234 Views 3 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.
    • P Offline
      Pout
      last edited by

      Can someone test this piece of code on the attached model?
      For me it does not erase the entities (all text)
      Any ideas?

      Sketchup.active_model.entities.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
      

      Untitled.skp

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

        Hi,
        It works, but to make it work you have to run the piece of code several times, until everything is erased.
        entities method returns a collection of entities. When you have to erase more than one entity, better use 'erase_entities', like this:

        def xxx()
        arrayents=[]
        Sketchup.active_model.entities.each{|e| arrayents.push(e) if (e.layer.name=="_4D_Finish_Text" or e.layer.name=="_4D_Start_Text") }
        Sketchup.active_model.entities.erase_entities(arrayents)
        end
        end
        

        Everything's gone in one go πŸ˜‰ Hope this helps,

        DB

        1 Reply Last reply Reply Quote 0
        • P Offline
          Pout
          last edited by

          Great! That works excellent.
          I wonder why it does not delete everything with the method i suggested.

          I think i will need to review some of my code. Is erase the only method that gives this 'problem'?

          Thx again Didier

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

            You should NOT change a collection of entities you are iterating through...
            NO-NO=

            Sketchup.active_model.entities.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
            

            YES-YES=

            Sketchup.active_model.entities.to_a.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' }
            

            Note how the entities is made into frozen array [.to_a] before processing...

            To remove such items from inside all definitions use

            Sketchup.active_model.definitions.each{|d|d.entities.to_a.each{|e| e.erase! if e.layer.name=='_4D_Finish_Text' || e.layer.name=='_4D_Start_Text' })
            

            πŸ€“

            EDIT: typo corrected '.to_a' added to final example 😳

            TIG

            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