• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

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 2 Aug 2010, 07:56

    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
    • D Offline
      Didier Bur
      last edited by 2 Aug 2010, 10:54

      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 2 Aug 2010, 12:44

        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
        • T Offline
          TIG Moderator
          last edited by 2 Aug 2010, 15:52

          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
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement