sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🚨 Skimp | 25% Off until March 30 Buy Now

    Problems with script

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 316 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 Online
            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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better πŸ’—

            Register Login
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement